import groovy.json.*
metadata {
definition (
name: "pp-Code Temp",
namespace: "pgitta",
author: "pgitta",
importUrl: ""
)
{
capability "TemperatureMeasurement"
capability "Sensor"
capability "Refresh" // refresh command
capability "Polling"
}
preferences {
input("ip", "string", title:"IP", description:"Sensor IP Address", defaultValue:"" , required: false, displayDuringSetup: true)
input "refreshEvery", "enum", title: "Enable auto refresh every XX Minutes", required: false, defaultValue: false, //RK
options: [5:"5 minutes",10:"10 minutes",15:"15 minutes",30:"30 minutes"] //RK
input "locale", "enum", title: "Choose refresh date format", required: true, defaultValue: true, //RK
options: [US:"US MM/DD/YYYY",UK:"UK DD/MM/YYYY"] //RK
input name: "debugOutput", type: "bool", title: "Enable debug logging?", defaultValue: true
input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true //RK
}
}
def installed() {
log.debug "Installed"
refresh()
}
// App Version *********************************************************************************
def setVersion(){
state.InternalName = "pp-CodeTempSensor"
sendEvent(name: "DriverAuthor", value: "pgitta")
}
def updated() {
log.debug "Updated"
log.info "Preferences updated..."
log.warn "Debug logging is: ${debugOutput == true}"
unschedule()
// RK start
if (refreshEvery != null) {
"runEvery${refreshEvery}Minutes"(autorefresh)
log.info "Refresh set for every ${refreshEvery} Minutes"
} else {
runEvery30Minutes (autorefresh)
log.info "Refresh set for every 30 Minutes"
}
if (debugOutput) runIn(1800,logsOff)
state.LastRefresh = new Date().format("YYYY/MM/dd \n HH:mm:ss", location.timeZone)
// RK stop
version()
refresh()
}
def parse(description) {
logDebug "Parsing result $description"
def msg = parseLanMessage(description)
def headersAsString = msg.header // => headers as a string
def headerMap = msg.headers // => headers as a Map
def body = msg.body // => request body as a string
def status = msg.status // => http status code of the response
def data = msg.data // => either JSON or XML in response body (whichever is specified by content-type header in response)
logDebug "Data"
logDebug data
def evt1 = null
if ( data.Stats.Temp != null ) {
logDebug "CreateEvent ison=true"
evt1 = createEvent(name: "Temperature", value: data.Stats.Temp, displayed: false)
} else {
logDebug "CreateEvent ison=false"
evt1 = createEvent(name: "Temperature", value: "null", displayed: false)
}
return evt1
}
def ping() {
logDebug "ping"
poll()
}
def initialize() {
log.info "initialize"
if (txtEnable) log.info "initialize" //RK
refresh()
}
def logsOff(){
log.warn "debug logging auto disabled..."
device.updateSetting("debugOutput",[value:"false",type:"bool"])
}
def autorefresh() {
if (locale == "UK") {
if (debugOutput) log.info "Get last UK Date DD/MM/YYYY"
state.LastRefresh = new Date().format("d/MM/YYYY \n HH:mm:ss", location.timeZone)
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
}
if (locale == "US") {
if (debugOutput) log.info "Get last US Date MM/DD/YYYY"
state.LastRefresh = new Date().format("MM/d/YYYY \n HH:mm:ss", location.timeZone)
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
}
if (txtEnable) log.info "Executing 'auto refresh'" //RK
refresh()
}
def refresh() {
logDebug "Refresh - Getting Status"
sendHubCommand(new hubitat.device.HubAction(
method: "GET",
path: "/b1247e052719&Stats/json",
headers: [
HOST: getSensorAddress(),
"Content-Type": "application/x-www-form-urlencoded"
]
))
}
private logDebug(msg) {
if (settings?.debugOutput || settings?.debugOutput == null) {
log.debug "$msg"
}
}
def sendSwitchCommand(action) {
if (txtEnable) log.info "Calling /relay/ with $action"
sendHubCommand(new hubitat.device.HubAction(
method: "POST",
path: "/b1247e052719&Stats/json",
body: action,
headers: [
HOST: getSensorAddress(),
"Content-Type": "application/x-www-form-urlencoded"
]
))
refresh()
}
// handle commands
//RK Updated to include last refreshed
def poll() {
if (locale == "UK") {
if (debugOutput) log.info "Get last UK Date DD/MM/YYYY"
state.LastRefresh = new Date().format("d/MM/YYYY \n HH:mm:ss", location.timeZone)
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
}
if (locale == "US") {
if (debugOutput) log.info "Get last US Date MM/DD/YYYY"
state.LastRefresh = new Date().format("MM/d/YYYY \n HH:mm:ss", location.timeZone)
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
}
if (txtEnable) log.info "Executing 'poll'" //RK
refresh()
}
private getSensorAddress() {
def port = 80
def iphex = ip.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join().toUpperCase()
def porthex = String.format('%04x', port.toInteger())
def sensorAddress = iphex + ":" + porthex
if (txtEnable) log.info "Using IP " + ip + ", PORT 80 and HEX ADDRESS " + sensorAddress + " for device: ${device.id}"
return sensorAddress
}
private dbCleanUp() {
unschedule()
state.remove("version")
state.remove("Version")
}
// Check Version ***** with great thanks and acknowlegment to Cobra (github CobraVmax) for his original code **************
def version(){
updatecheck()
schedule("0 0 18 1/1 * ? *", updatecheck) // Cron schedule
// schedule("0 0/1 * 1/1 * ? *", updatecheck) // Test Cron schedule
}