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
*
......@@ -39,7 +39,8 @@
*-------------------------------------------------------------------------------------------------------------------
*
* Changes:
*
* 2.0.1 - Code re-write for json
* - Removed all ST code
* 2.0.0 - New License -- Personal Use Only
* 1.0.8 - changed behavour of refresh rate
* 1.0.7 - Added attributes ipAddresses and nameservers and code for future enhancements
......@@ -95,17 +96,19 @@ metadata {
attribute "DriverAuthor", "string"
attribute "DriverStatus", "string"
attribute "LastRefresh", "string"
attribute "osName", "string"
attribute "online", "string"
}
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 "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 "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",Polling:"Polling"]
input "locale", "enum", title: "Choose refresh date format", required: true, defaultValue: true,
options: [US:"US MM/DD/YYYY",UK:"UK DD/MM/YYYY"]
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() {
// App Version *********************************************************************************
def setVersion(){
state.Version = "2.0.0"
state.Version = "2.0.1"
state.InternalName = "RaspberryPiStats"
sendEvent(name: "DriverAuthor", value: "sgrayban")
sendEvent(name: "DriverVersion", value: state.Version)
sendEvent(name: "DriverStatus", value: state.Status)
}
def updated() {
......@@ -129,19 +128,20 @@ def updated() {
log.info "Preferences updated..."
log.warn "Debug logging is: ${debugOutput == true}"
unschedule()
// RK start
if (refreshEvery != null) {
if (refreshEvery != "Polling") {
"runEvery${refreshEvery}Minutes"(autorefresh)
log.info "Refresh set for every ${refreshEvery} Minutes"
} else {
runEvery30Minutes (autorefresh)
log.info "Refresh set for every 30 Minutes"
unschedule(autorefresh)
log.info "Refresh set to polling only"
}
if (debugOutput) runIn(1800,logsOff)
state.LastRefresh = new Date().format("YYYY/MM/dd \n HH:mm:ss", location.timeZone)
// RK stop
version()
initialize();
updateDataValue("ip", ip)
updateDataValue("port", port)
}
def ping() {
......@@ -151,8 +151,7 @@ def ping() {
def initialize() {
log.info "initialize"
if (txtEnable) log.info "initialize" //RK
refresh()
if (txtEnable) log.info "initialize"
}
def logsOff(){
......@@ -160,36 +159,23 @@ def logsOff(){
device.updateSetting("debugOutput",[value:"false",type:"bool"])
}
// parse events into attributes
def parse(description) {
logDebug "Parsing '${description}'"
// Get shelly device type
def getStats(){
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}"
if(bodyString.trim() == "ok")
return
def json = null;
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
try {
httpGet(params) {
resp -> resp.headers.each {
logDebug "Response: ${it.name} : ${it.value}"
}
json = resp.data
logDebug "JSON '${json}'"
logDebug "params: ${params}"
logDebug "response contentType: ${resp.contentType}"
logDebug "response data: ${resp.data}"
sendEvent(name: "online", value: "Online")
if (json.containsKey("cpuTemperature")) {
if (getTemperatureScale() == "C") {
sendEvent(name: "temperature", value: json.cpuTemperature)
......@@ -211,7 +197,10 @@ def parse(description) {
sendEvent(name: "modelName", value: json.modelName)
}
if (json.containsKey("boardType")) {
if (json.boardType != "UNKNOWN") {
sendEvent(name: "boardType", value: json.boardType)
} else
sendEvent(name: "boardType", value: "RaspberryPi_4B")
}
if (json.containsKey("javaVersion")) {
sendEvent(name: "javaVersion", value: json.javaVersion)
......@@ -223,18 +212,24 @@ def parse(description) {
sendEvent(name: "serialNumber", value: json.serialNumber)
}
if (json.containsKey("ipAddresses")) {
sendEvent(name: "ipAddresses", value: json.ipAddresses)
sendEvent(name: "ipAddresses", value: json.ipAddresses[0])
}
if (json.containsKey("nameservers")) {
sendEvent(name: "nameservers", value: json.nameservers)
sendEvent(name: "nameservers", value: json.nameservers[0])
}
if (json.containsKey("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
//RK Updated to include last refreshed
def poll() {
if (locale == "UK") {
if (debugOutput) log.info "Get last UK Date DD/MM/YYYY"
......@@ -247,7 +242,7 @@ def poll() {
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
}
if (txtEnable) log.info "Executing 'poll'" //RK
getPiInfo()
getStats()
}
def refresh() {
......@@ -261,8 +256,8 @@ def refresh() {
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 'manual refresh'" //RK
getPiInfo()
if (txtEnable) log.info "Executing 'manual refresh'"
getStats()
}
def autorefresh() {
......@@ -277,66 +272,9 @@ def autorefresh() {
sendEvent(name: "LastRefresh", value: state.LastRefresh, descriptionText: "Last refresh performed")
}
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) {
if (settings?.debugOutput || settings?.debugOutput == null) {
......@@ -359,7 +297,7 @@ def version(){
def updatecheck(){
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 {
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
......@@ -375,6 +313,10 @@ def updatecheck(){
state.Status = "<b>** This driver is no longer supported by $state.author **</b>"
log.warn "** This driver is no longer supported by $state.author **"
}
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>"
log.warn "** There is a newer version of this driver available (Version: $newVerRaw) **"
......
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