/* * UPB Connector for Scenes * * Assembles UPB commands and transmits via GlobalCache Flex Link to PIM-R via HTTP or to the PulseWorx Gateway * * Intial release: 11-Sep-22 */ metadata { definition(name: "UPB Scene", namespace: "UPB", author: "UPB Voice Bridge", importUrl: "") { capability "Switch" } } preferences { section("UPB Network Connection") { input name: "UPBId", type: "text", title: "UPB Scene Id", required: true input name: "SourceId", type: "text", title: "Use this device id as the source of the scene command", required: true input name: "ConnectedAt", type: "text", title: "Last configuration update" input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: false } } // //----------------------------------------------------------------------------- // Standard stuff //----------------------------------------------------------------------------- // void installed() { if (logEnable) log.debug ("In Scene driver Installed"); } def logsOff() { log.warn ("debug logging disabled..."); device.updateSetting("logEnable", [value: "false", type: "bool"]); } def updated() { log.info ("updated..."); log.warn ("debug logging is: ${logEnable == true}"); if (logEnable) runIn(1800, logsOff); } def parse(String description) { // Shouldn't be used log.debug(description); } // //----------------------------------------------------------------------------- // Used by the app //----------------------------------------------------------------------------- // def UpdateUPBId(Integer id) { if (logEnable) log.debug ("In UpdateUPBId " + id); device.updateSetting("UPBId", [value: id, type: "Integer"]); } def UpdateSourceId(Integer id) { if (logEnable) log.debug ("In UpdateSourceId " + id); device.updateSetting("SourceId", [value: id, type: "Integer"]); } def UpdateConnectedAt(String text) { if (logEnable) log.debug ("In UpdateConnectedAt " + text); device.updateSetting("ConnectedAt", [value: text, type: "text"]); } def UpdateDeviceLabel(String text) { device.setLabel(text) } def GetConnectedAt() { if (logEnable) log.debug ("In GetConnectedAt"); return settings.ConnectedAt; } // //----------------------------------------------------------------------------- // Operations //----------------------------------------------------------------------------- // def on() { if (logEnable) log.debug ("Sending ON to scene [${settings.UPBId}]"); try { worksWith = parent.GetWorksWith(); if (worksWith == 0) { PIMIP = parent.GetPIMIP(); String url = "http://" + PIMIP + "/api/v1/serialports/1/sendserial"; String cmd = parent.BuildSceneActivateCommand(Integer.parseInt(settings.UPBId), Integer.parseInt(settings.SourceId)); if (logEnable) log.debug ("UPB Command activate [${cmd}]"); def postMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(postMap) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: true); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } } else { gatewayIP = parent.GetGatewayIP(); String s = "http://" + gatewayIP + "/api/v1/ActivateLink?id=" + settings.UPBId; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: true); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } } } catch (Exception e) { log.warn ("Call to scene on failed: ${e.message}"); } } def off() { if (logEnable) log.debug ("Sending OFF to scene [${settings.UPBId}]"); try { worksWith = parent.GetWorksWith(); if (worksWith == 0) { PIMIP = parent.GetPIMIP(); String url = "http://" + PIMIP + "/api/v1/serialports/1/sendserial"; String cmd = parent.BuildSceneDeactivateCommand(Integer.parseInt(settings.UPBId), Integer.parseInt(settings.SourceId)); if (logEnable) log.debug ("UPB Command deactivate [${cmd}]"); def postMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(postMap) { resp -> if (resp.success) { sendEvent(name: "switch", value: "off", isStateChange: true); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } } else { gatewayIP = parent.GetGatewayIP(); String s = "http://" + gatewayIP + "/api/v1/DeactivateLink?id=" + settings.UPBId; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { sendEvent(name: "switch", value: "off", isStateChange: true); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } } } catch (Exception e) { log.warn ("Call to scene off failed: ${e.message}"); } }