Code by Scott שאול בן ישוע
Verified Commit ae3e790b authored by Sha'ul ben Yeshua's avatar Sha'ul ben Yeshua 🎗
Browse files

code re-write and removed all ST code

parent ecad9b9f
Pipeline #225 passed with stage
in 5 seconds
/** /**
* *
* Raspberry Pi Stats * Raspberry Pi Stats
* *
...@@ -39,7 +39,8 @@ ...@@ -39,7 +39,8 @@
*------------------------------------------------------------------------------------------------------------------- *-------------------------------------------------------------------------------------------------------------------
* *
* Changes: * Changes:
* * 2.0.1 - Code re-write for json
* - Removed all ST code
* 2.0.0 - New License -- Personal Use Only * 2.0.0 - New License -- Personal Use Only
* 1.0.8 - changed behavour of refresh rate * 1.0.8 - changed behavour of refresh rate
* 1.0.7 - Added attributes ipAddresses and nameservers and code for future enhancements * 1.0.7 - Added attributes ipAddresses and nameservers and code for future enhancements
...@@ -75,37 +76,39 @@ metadata { ...@@ -75,37 +76,39 @@ metadata {
) )
{ {
capability "Polling" capability "Polling"
capability "Refresh" capability "Refresh"
capability "Temperature Measurement" capability "Temperature Measurement"
capability "Sensor" capability "Sensor"
attribute "cpuFrequency", "number" attribute "cpuFrequency", "number"
attribute "freeMemory", "number" attribute "freeMemory", "number"
attribute "freeMemoryPercent", "number" attribute "freeMemoryPercent", "number"
attribute "cpuCoreVoltage", "number" attribute "cpuCoreVoltage", "number"
attribute "modelName", "string" attribute "modelName", "string"
attribute "boardType", "string" attribute "boardType", "string"
attribute "javaVersion", "string" attribute "javaVersion", "string"
attribute "hostname", "string" attribute "hostname", "string"
attribute "ipAddresses", "string" attribute "ipAddresses", "string"
attribute "nameservers", "string" attribute "nameservers", "string"
attribute "serialNumber", "string" attribute "serialNumber", "string"
attribute "DriverVersion", "string" attribute "DriverVersion", "string"
attribute "DriverAuthor", "string" attribute "DriverAuthor", "string"
attribute "DriverStatus", "string" attribute "DriverStatus", "string"
attribute "LastRefresh", "string" attribute "LastRefresh", "string"
attribute "osName", "string"
attribute "online", "string"
} }
preferences { preferences {
input("ip", "string", title:"IP Address", description: "cpuTemperature", defaultValue: "" ,required: true, displayDuringSetup: true) input("ip", "string", title:"RPi IP Address", description: "", defaultValue: "" ,required: true, displayDuringSetup: true)
input("port", "string", title:"Port", description: "8080", defaultValue: "8080" , required: true, displayDuringSetup: true) input("port", "string", title:"Port", description: "8080", defaultValue: "8080" , required: true, displayDuringSetup: true)
input "refreshEvery", "enum", title: "Enable auto refresh every XX Minutes", required: false, defaultValue: false, //RK input "refreshEvery", "enum", title: "Enable auto refresh every XX Minutes", required: false, defaultValue: false,
options: [5:"5 minutes",10:"10 minutes",15:"15 minutes",30:"30 minutes"] //RK options: [5:"5 minutes",10:"10 minutes",15:"15 minutes",30:"30 minutes",Polling:"Polling"]
input "locale", "enum", title: "Choose refresh date format", required: true, defaultValue: true, //RK input "locale", "enum", title: "Choose refresh date format", required: true, defaultValue: true,
options: [US:"US MM/DD/YYYY",UK:"UK DD/MM/YYYY"] //RK options: [US:"US MM/DD/YYYY",UK:"UK DD/MM/YYYY"]
input name: "debugOutput", type: "bool", title: "Enable debug logging?", defaultValue: true input name: "debugOutput", type: "bool", title: "Enable debug logging?", defaultValue: true
input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true //RK input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
} }
} }
...@@ -116,12 +119,8 @@ def installed() { ...@@ -116,12 +119,8 @@ def installed() {
// App Version ********************************************************************************* // App Version *********************************************************************************
def setVersion(){ def setVersion(){
state.Version = "2.0.0" state.Version = "2.0.1"
state.InternalName = "RaspberryPiStats" state.InternalName = "RaspberryPiStats"
sendEvent(name: "DriverAuthor", value: "sgrayban")
sendEvent(name: "DriverVersion", value: state.Version)
sendEvent(name: "DriverStatus", value: state.Status)
} }
def updated() { def updated() {
...@@ -129,19 +128,20 @@ def updated() { ...@@ -129,19 +128,20 @@ def updated() {
log.info "Preferences updated..." log.info "Preferences updated..."
log.warn "Debug logging is: ${debugOutput == true}" log.warn "Debug logging is: ${debugOutput == true}"
unschedule() unschedule()
// RK start
if (refreshEvery != null) { if (refreshEvery != "Polling") {
"runEvery${refreshEvery}Minutes"(autorefresh) "runEvery${refreshEvery}Minutes"(autorefresh)
log.info "Refresh set for every ${refreshEvery} Minutes" log.info "Refresh set for every ${refreshEvery} Minutes"
} else { } else {
runEvery30Minutes (autorefresh) unschedule(autorefresh)
log.info "Refresh set for every 30 Minutes" log.info "Refresh set to polling only"
} }
if (debugOutput) runIn(1800,logsOff) if (debugOutput) runIn(1800,logsOff)
state.LastRefresh = new Date().format("YYYY/MM/dd \n HH:mm:ss", location.timeZone) state.LastRefresh = new Date().format("YYYY/MM/dd \n HH:mm:ss", location.timeZone)
// RK stop
version() version()
initialize(); updateDataValue("ip", ip)
updateDataValue("port", port)
} }
def ping() { def ping() {
...@@ -151,8 +151,7 @@ def ping() { ...@@ -151,8 +151,7 @@ def ping() {
def initialize() { def initialize() {
log.info "initialize" log.info "initialize"
if (txtEnable) log.info "initialize" //RK if (txtEnable) log.info "initialize"
refresh()
} }
def logsOff(){ def logsOff(){
...@@ -160,37 +159,24 @@ def logsOff(){ ...@@ -160,37 +159,24 @@ def logsOff(){
device.updateSetting("debugOutput",[value:"false",type:"bool"]) device.updateSetting("debugOutput",[value:"false",type:"bool"])
} }
// parse events into attributes // Get shelly device type
def parse(description) { def getStats(){
logDebug "Parsing '${description}'" log.info "Getting Pi Stats"
def params = [uri: "http://${ip}:${port}/api/pi"]
def msg = parseLanMessage(description)
def headerString = msg.header
def bodyString = msg.body
logDebug "received body:\n${bodyString}" try {
httpGet(params) {
if(bodyString.trim() == "ok") resp -> resp.headers.each {
return logDebug "Response: ${it.name} : ${it.value}"
}
def json = null; json = resp.data
try{
json = new groovy.json.JsonSlurper().parseText(bodyString)
logDebug "${json}"
if(json == null){
logDebug "body object not parsed"
return
}
}
catch(e){
log.error("Failed to parse json e = ${e}")
return
}
logDebug "JSON '${json}'" logDebug "params: ${params}"
logDebug "response contentType: ${resp.contentType}"
if (json.containsKey("cpuTemperature")) { logDebug "response data: ${resp.data}"
sendEvent(name: "online", value: "Online")
if (json.containsKey("cpuTemperature")) {
if (getTemperatureScale() == "C") { if (getTemperatureScale() == "C") {
sendEvent(name: "temperature", value: json.cpuTemperature) sendEvent(name: "temperature", value: json.cpuTemperature)
} else { } else {
...@@ -211,8 +197,11 @@ def parse(description) { ...@@ -211,8 +197,11 @@ def parse(description) {
sendEvent(name: "modelName", value: json.modelName) sendEvent(name: "modelName", value: json.modelName)
} }
if (json.containsKey("boardType")) { if (json.containsKey("boardType")) {
if (json.boardType != "UNKNOWN") {
sendEvent(name: "boardType", value: json.boardType) sendEvent(name: "boardType", value: json.boardType)
} } else
sendEvent(name: "boardType", value: "RaspberryPi_4B")
}
if (json.containsKey("javaVersion")) { if (json.containsKey("javaVersion")) {
sendEvent(name: "javaVersion", value: json.javaVersion) sendEvent(name: "javaVersion", value: json.javaVersion)
} }
...@@ -223,18 +212,24 @@ def parse(description) { ...@@ -223,18 +212,24 @@ def parse(description) {
sendEvent(name: "serialNumber", value: json.serialNumber) sendEvent(name: "serialNumber", value: json.serialNumber)
} }
if (json.containsKey("ipAddresses")) { if (json.containsKey("ipAddresses")) {
sendEvent(name: "ipAddresses", value: json.ipAddresses) sendEvent(name: "ipAddresses", value: json.ipAddresses[0])
} }
if (json.containsKey("nameservers")) { if (json.containsKey("nameservers")) {
sendEvent(name: "nameservers", value: json.nameservers) sendEvent(name: "nameservers", value: json.nameservers[0])
} }
if (json.containsKey("osName")) { if (json.containsKey("osName")) {
sendEvent(name: "osName", value: json.osName) sendEvent(name: "osName", value: json.osName)
} }
}
} // End try
} catch (e) {
sendEvent(name: "online", value: "Offline")
log.error "something went wrong: $e"
}
} // End Refresh Status
// handle commands // handle commands
//RK Updated to include last refreshed
def poll() { def poll() {
if (locale == "UK") { if (locale == "UK") {
if (debugOutput) log.info "Get last UK Date DD/MM/YYYY" if (debugOutput) log.info "Get last UK Date DD/MM/YYYY"
...@@ -247,7 +242,7 @@ def poll() { ...@@ -247,7 +242,7 @@ def poll() {
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed") sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
} }
if (txtEnable) log.info "Executing 'poll'" //RK if (txtEnable) log.info "Executing 'poll'" //RK
getPiInfo() getStats()
} }
def refresh() { def refresh() {
...@@ -261,8 +256,8 @@ def refresh() { ...@@ -261,8 +256,8 @@ def refresh() {
state.LastRefresh = new Date().format("MM/d/YYYY \n HH:mm:ss", location.timeZone) state.LastRefresh = new Date().format("MM/d/YYYY \n HH:mm:ss", location.timeZone)
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed") sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
} }
if (txtEnable) log.info "Executing 'manual refresh'" //RK if (txtEnable) log.info "Executing 'manual refresh'"
getPiInfo() getStats()
} }
def autorefresh() { def autorefresh() {
...@@ -277,65 +272,8 @@ def autorefresh() { ...@@ -277,65 +272,8 @@ def autorefresh() {
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed") sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
} }
if (txtEnable) log.info "Executing 'auto refresh'" //RK if (txtEnable) log.info "Executing 'auto refresh'" //RK
getPiInfo() getStats()
}
//RK Last Refresh End
private getPiInfo() {
def iphex = convertIPtoHex(ip)
def porthex = convertPortToHex(port)
def uri = "/api/pi"
def headers=[:]
headers.put("HOST", "${ip}:${port}")
headers.put("Accept", "application/json")
def hubAction = new hubitat.device.HubAction
(
method: "GET",
path: uri,
headers: headers,
"${iphex}:${porthex}",
[callback: parse]
)
logDebug "Getting Pi data ${hubAction}"
hubAction
}
private String convertIPtoHex(ipAddress) {
logDebug "convertIPtoHex ${ipAddress} to hex"
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
logDebug "convertPortToHex ${port} to hex"
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}
private Integer convertHexToInt(hex) {
return Integer.parseInt(hex,16)
}
private String convertHexToIP(hex) {
return [convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".")
}
def sync(ip, port) {
logDebug "sync ${ip} ${port}"
def existingIp = getDataValue("ip")
def existingPort = getDataValue("port")
if (ip && ip != existingIp) {
updateDataValue("ip", ip)
}
if (port && port != existingPort) {
updateDataValue("port", port)
}
def ipHex = convertIPToHex(ip)
def portHex = convertPortToHex(port)
device.deviceNetworkId = "${ipHex}:${portHex}"
} }
private logDebug(msg) { private logDebug(msg) {
...@@ -359,7 +297,7 @@ def version(){ ...@@ -359,7 +297,7 @@ def version(){
def updatecheck(){ def updatecheck(){
setVersion() setVersion()
def paramsUD = [uri: "https://sgrayban.github.io/Hubitat-Public/version.json"] def paramsUD = [uri: "http://sgrayban.borgnet.online:8081/scotts-projects/version.json"]
try { try {
httpGet(paramsUD) { respUD -> httpGet(paramsUD) { respUD ->
if (txtEnable) log.warn " Version Checking - Response Data: ${respUD.data}" // Troubleshooting Debug Code - Uncommenting this line should show the JSON response from your webserver if (txtEnable) log.warn " Version Checking - Response Data: ${respUD.data}" // Troubleshooting Debug Code - Uncommenting this line should show the JSON response from your webserver
...@@ -374,8 +312,12 @@ def updatecheck(){ ...@@ -374,8 +312,12 @@ def updatecheck(){
if(newVer == "NLS"){ if(newVer == "NLS"){
state.Status = "<b>** This driver is no longer supported by $state.author **</b>" state.Status = "<b>** This driver is no longer supported by $state.author **</b>"
log.warn "** This driver is no longer supported by $state.author **" log.warn "** This driver is no longer supported by $state.author **"
} }
else if(currentVer < newVer){ else if(newVer == "BETA"){
state.Status = "<b>** THIS IS BETA CODE **</b>"
log.warn "** BETA CODE **"
}
else if(currentVer < newVer){
state.Status = "<b>New Version Available (Version: $newVerRaw)</b>" state.Status = "<b>New Version Available (Version: $newVerRaw)</b>"
log.warn "** There is a newer version of this driver available (Version: $newVerRaw) **" log.warn "** There is a newer version of this driver available (Version: $newVerRaw) **"
log.warn "** $state.UpdateInfo **" log.warn "** $state.UpdateInfo **"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment