Hello,
I have a rule I wrote with Rule DSL and I want to rewrite it with JS.
But in my searches in the forum, I could not find an example of receiving data from the serial port or a relationship for a similar structure.
I want to take the information from the serial port into the Script and divide it into parts and process it.
runtimeInfo:
version: 4.1.2
buildString: Release Build
locale: default
systemInfo:
configFolder: /etc/openhab`Preformatted text`
userdataFolder: /var/lib/openhab
logFolder: /var/log/openhab
javaVersion: 17.0.10
javaVendor: Debian
osName: Linux
osVersion: 6.8.2-edge-rockchip64
osArchitecture: aarch64
availableProcessors: 4
freeMemory: 58068632
totalMemory: 304087040
uptime: 22125
startLevel: 100
addons:
- automation-jsscripting
- automation-jsscriptingnashorn
- automation-jythonscripting
- binding-exec
- binding-serial
- binding-systeminfo
- binding-telegram
- misc-homekit
- misc-openhabcloud
- persistence-mapdb
- persistence-rrd4j
- transformation-exec
- transformation-jsonpath
- transformation-map
- transformation-regex
- ui-basic
navigator:
cookieEnabled: true
deviceMemory: 4GB
hardwareConcurrency: 8
language: en-US
languages:
- en-US
- en
- tr
onLine: true
platform: Win32
Rule DSL is;
import org.openhab.core.model.script.ScriptServiceUtil
//import org.eclipse.smarthome.model.script.ScriptServiceUtil
//import org.eclipse.xtext.xbase.lib.Functions
import org.eclipse.smarthome.core.types.State
import org.eclipse.smarthome.core.types.Command
import org.eclipse.smarthome.core.types.TypeParser
var Number Log_Flag = 1
rule "INPUT"
when
Item CANBus received update // your condition here
then
var String CANBusData = CANBus.state.toString
if( CANBusData.startsWith("VID_T")){
val parts = CANBusData.split(";")
var dTYPE = Integer::parseInt(parts.get(0).split("=").get(1)) as Number
var dID = Integer::parseInt(parts.get(1).split("=").get(1)) as Number
var pID = Integer::parseInt(parts.get(2).split("=").get(1)) as Number
val String cID = (parts.get(3).split("=").get(1))
if ((cID == "S") || (cID == "L" )) {
val name = ScriptServiceUtil.getItemRegistry.getItem("I_"+ dID + "_" + pID + "_" + cID )
if (Log_Flag == 1) {
logInfo("CANBus", ">>>> String : {}", parts)
logInfo("CANBus", ">>>> CANBUS) Input.Device.Type : {}", dTYPE)
logInfo("CANBus", ">>>> CANBUS) Input.Device.ID : {}", dID)
logInfo("CANBus", ">>>> CANBUS) Input.Port.ID : {}", pID)
logInfo("CANBus", ">>>> CANBUS) Input.Command.ID : {}", cID)
logInfo("CANBus", ">>>> CANBUS) Item.Name : {}", name)
logInfo("CANBus", ">>>> CANBUS) Item.Type : {}", name.getType)
//logInfo("CANBus", ">>>> CANBUS) Item.AcceptedCommandTypes : '{}'", name.getAcceptedCommandTypes)
//logInfo("CANBus", ">>>> CANBUS) Item.getAcceptedDataTypes : '{}'", name.getAcceptedDataTypes)
logInfo("CANBus", ">>>> CANBUS) Item.State : {}", name.getState)
}
if ( cID == "S"){
if ((name.getType == "Switch" || name.getType == "Group" ) )
{
if (name.getState == OFF )
{
name.sendCommand(ON)
}
else
{
name.sendCommand(OFF)
}
}
else if (name.getType == "Rollershutter" )
{
if(RS_Direction.getState==ON )
{ // short STOP
name.sendCommand(STOP)
}
else
{ // short UP/DOWN
if (name.getState == 100 || name.getState == DOWN )
{
name.sendCommand(UP)
}
else
{
name.sendCommand(DOWN)
}
}
}
}
else if ( cID == "L" )
{
if (name.getType != "Rollershutter" ){
var name = ScriptServiceUtil.getItemRegistry.getItem("I_"+ dID + "_" + pID + "_" + cID)
}
if (name.getType == "Switch" || name.getType == "Group" )
{
if (name.getState == OFF )
{
name.sendCommand(ON)
}
else
{
name.sendCommand(OFF)
}
}
else if (name.getType == "Rollershutter" )
{
if(RS_Direction.getState==OFF )
{ // short STOP
name.sendCommand(STOP)
}
else
{ // short UP/DOWN
if (name.getState == 100 || name.getState == DOWN )
{
name.sendCommand(UP)
}
else
{
name.sendCommand(DOWN)
}
}
}
}
}
}
end
I need help on how to convert the following part. I have no idea for a starting point at the moment, unfortunately. Despite reading a lot of documents and messages, it has become more and more complicated.
when
Item CANBus received update // your condition here
then
var String CANBusData = CANBus.state.toString
if( CANBusData.startsWith("VID_T")){
val parts = CANBusData.split(";")
var dTYPE = Integer::parseInt(parts.get(0).split("=").get(1)) as Number
var dID = Integer::parseInt(parts.get(1).split("=").get(1)) as Number
var pID = Integer::parseInt(parts.get(2).split("=").get(1)) as Number
val String cID = (parts.get(3).split("=").get(1))
if ((cID == "S") || (cID == "L" )) {
val name = ScriptServiceUtil.getItemRegistry.getItem("I_"+ dID + "_" + pID + "_" + cID )
if (Log_Flag == 1) {
logInfo("CANBus", ">>>> String : {}", parts)
logInfo("CANBus", ">>>> CANBUS) Input.Device.Type : {}", dTYPE)
logInfo("CANBus", ">>>> CANBUS) Input.Device.ID : {}", dID)
logInfo("CANBus", ">>>> CANBUS) Input.Port.ID : {}", pID)
logInfo("CANBus", ">>>> CANBUS) Input.Command.ID : {}", cID)
logInfo("CANBus", ">>>> CANBUS) Item.Name : {}", name)
logInfo("CANBus", ">>>> CANBUS) Item.Type : {}", name.getType)
//logInfo("CANBus", ">>>> CANBUS) Item.AcceptedCommandTypes : '{}'", name.getAcceptedCommandTypes)
//logInfo("CANBus", ">>>> CANBUS) Item.getAcceptedDataTypes : '{}'", name.getAcceptedDataTypes)
logInfo("CANBus", ">>>> CANBUS) Item.State : {}", name.getState)
}
Thank you.