Telnet Parse only works one device at a time

/**

 * 
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */



metadata {
	definition (name: "RFLC Dimmer", namespace: "Evan", author: "Samir_Genius") {
	capability "Initialize"
    capability "Telnet"
    capability "Switch"
    capability "Switch Level"
    capability "ChangeLevel"
      
      
    /*    
    command "sendMsg", ["String"], int
    command "sendLevel", ["Brightness 0-255"]
    command "sendDimLevel", ["Brightness 0-255"]
        
    */
        
    attribute "Telnet", ""
    attribute "Switch", ""
    attribute "level", ""
        
	}
    
    preferences {
        input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
        input name: "DevID", type: "string", title: "Legrand RFLC Device ID"
    }
    
    
}




// device commands
def on() {
    
    sendEvent(name:"switch", value:"on")
    initialize()
	log.debug "Send LNZL"
    /*
	sendEvent(name: "switch", value: "on")
    def msg = "shutdown -h now"
    log.debug "Sending msg = ${msg}"
    sendEvent(name: "switch", value: "off")
	sendHubCommand(new hubitat.device.HubAction("""$msg\r\n""", hubitat.device.Protocol.TELNET))
    */
    
    setLevel(100)

    
    
    
}


    

def off() {
    
    sendEvent(name:"switch", value:"off")
    initialize()
	log.debug "Send OFF"
    /*
	sendEvent(name: "switch", value: "on")
    def msg = "shutdown -h now"
    log.debug "Sending msg = ${msg}"
    sendEvent(name: "switch", value: "off")
	sendHubCommand(new hubitat.device.HubAction("""$msg\r\n""", hubitat.device.Protocol.TELNET))
    */
    
    setLevel(0)
    
}



    
// General App Events
def initialize(){
	telnetConnect('192.168.1.199', 23, null, null)
	log.debug "Opening telnet connection"

}

def parse(String message) {
    log.debug "parse: ${message}"
    
    log.debug DevID + " is running parse()"
    
    if(message.substring(0,9) == ">RG, " + DevID || message.substring(0,9) == ">CR, " + DevID){
        
        log.debug "level >> " + Math.round(message.substring(11,15).toInteger() / 2.55)
        
        sendEvent(name:"level", value: Math.round(message.substring(11,15).toInteger() / 2.55))
        
        if(message.substring(11,15).toInteger() == 0) {
            sendEvent(name:"switch", value:"off")   
        }
        else{
            sendEvent(name:"switch", value:"on")
        }
        
    }

        
        
    

}

def startLevelChange(direction) {
    
    initialize()
    
    if (direction == "up"){
        sendMsg("RAMPG "+(DevID)+" 255 40")
    }
    
    else {
        sendMsg("RAMPG "+(DevID)+" 0 40")
    }
}


def sendMsg(String msg) 
{
	log.info("Sending telnet msg: " + msg)
	return new hubitat.device.HubAction(msg, hubitat.device.Protocol.TELNET)
}

def setLevel(level, ramp){
    sendEvent(name:"level", value:level)
    if(level == 0) {
        sendEvent(name:"switch", value:"off")   
    }
    else{
        sendEvent(name:"switch", value:"on")
    }

    initialize()
    sendMsg("RAMPG " +(DevID)+" "+(int)(level*2.55) + " " + ramp)    
}

def setLevel(level){
    
    setLevel(level, 50)
}

def stopLevelChange() {
    sendMsg("CRAMP "+(DevID))
    
}

def telnetStatus(String status){
    log.warn "telnetStatus: error: " + status
	if (status != "receive error: Stream is closed")
	{
		log.error "Connection was dropped."
		initialize()
	}

}

I'm trying to create a bidirectional telnet driver for my Legrand lighting control system. I need to parse the incoming messages to update the light level variables for each device.

The parse(message) method is only running for 1 driver at a time. Every device that has the driver needs to run the parse() at the same time, so that the level state is kept updated with the actual light level.

Any suggestions to improve this driver is appreciated.

This will be difficult because there is only one port 23 to be shared. I suspect you need a parent/child set of drivers where the parent has the exclusive telnet connection/send/recieve. I know there are lots of community drivers that do parent/child but I haven't studied them enough to offer more advice.

Exactly. This is how the Lutron Integration works. There is one telnet devices that parses the messages and sends them to the children to fully parse to dimmer levels, switch state, etc. You won't be able to have 20 devices all maintaining a connection at the same time, that's not practical for the hub to do...and it's very slow.

Thanks for the help. Is there any driver/example that utilizes this form of control that I can look at to understand the idea?

Thanks again for the help.

There's the Hubduino project that uses a Parent Child relationship very well. It's also public so you can access all of it.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.