I'm trying to modify a driver code for a simple relay switcher (iTach IP2CC) to simulate a holding/releasing button effect.
The device controls a projector screen. When only Port1 is grounded (relay closed), the screen goes up. When only Port2 is grounded, the screen goes down. When both ports are grounded, the screen stops.
NOTE: After the relay is closed and then reopened, the screen will continue to move until both ports are closed together.
What I am trying to simulate is hold release button effect for UP and DOWN.
When UP button is pressed: {execute code1} (screen moved up)
When UP button is released: {execute code2} (screen stops)
code1: def ScreenUP()
code2: def ScreenSTOP()
I have the basic controls working. Here is the driver code:
/*
* iTach iP2CC Driver for Projector Screen Control
* Screen uses Port 1 for UP trigger and Port 2 for DOWN Trigger
* To stop the screen both ports must be triggered
*/
@SuppressWarnings('unused')
static String version() {return "0.0.1"}
metadata {
definition (
name: "iTach iP2CC",
namespace: "uw",
author: "U W",
importUrl:"na"
) {
capability "Telnet"
capability "Initialize"
capability "Switch"
capability "HoldableButton"
capability "ReleasableButton"
attribute "Port1", "ENUM", ["on", "off"]
attribute "Port2", "ENUM", ["on", "off"]
attribute "lastMessage", "STRING"
command "connectTelnet"
command "disconnectTelnet"
command "p1On"
command "p1Off"
command "p2On"
command "p2Off"
command "ScreenUP"
command "ScreenDOWN"
command "ScreenSTOP"
}
}
preferences {
input(name: "ipAddr", type: "string", title:"IP Address", required: true)
input(name: "portNum", type: "number", title: "Port Number", required: true)
}
@SuppressWarnings('unused')
def installed() {
}
def initialize(){
connectTelnet()
log.debug "----- INITIALIZING TELNET -----"
}
void updateAttr(String aKey, aValue, String aUnit = ""){
sendEvent(name:aKey, value:aValue, unit:aUnit)
}
def connectTelnet(){
try{
telnetConnect([termChars:[10]], ipAddr, (int)portNum, null, null)
} catch (ex) {
updateAttr("lastMessage", ex)
}
updateAttr("switch", "on")
}
def disconnectTelnet() {
telnetClose()
updateAttr("switch", "off")
}
def sendMsg(message) {
sendHubCommand(new hubitat.device.HubAction("""$message\r\n""", hubitat.device.Protocol.TELNET))
}
def p1On(){
sendMsg("setstate,1:1,1")
updateAttr("Port1", "On")
}
def p1Off(){
sendMsg("setstate,1:1,0")
updateAttr("Port1", "Off")
}
def ScreenUP(){
p1On()
runInMillis(100,p1Off)
}
def p2On(){
sendMsg("setstate,1:2,1")
updateAttr("Port2", "On")
}
def p2Off(){
sendMsg("setstate,1:2,0")
updateAttr("Port2", "Off")
}
def ScreenDOWN(){
p2On()
runInMillis(100,p2Off)
}
def ScreenSTOP(){
p1On()
p2On()
runInMillis(500,p1Off)
runInMillis(500,p2Off)
}
def parse(message) {
updateAttr("parsedMessage", message)
}
def telnetStatus(message){
updateAttr("statusMessage", message)
}
However, I need some guidance on how to implement the holdable and releasable buttons so they can then be picked up from the Dashboard for simple UP/DOWN control.