Skip to content

Instantly share code, notes, and snippets.

@edalquist
Created February 2, 2020 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edalquist/1710a3dd13d477290341e1f345fc0af2 to your computer and use it in GitHub Desktop.
Save edalquist/1710a3dd13d477290341e1f345fc0af2 to your computer and use it in GitHub Desktop.
/**
* License:
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
**/
import groovy.transform.Field
@Field int VERSION = 1
@Field List<String> LOG_LEVELS = ["warn", "info", "debug", "trace"]
@Field String DEFAULT_LOG_LEVEL = LOG_LEVELS[1]
metadata {
definition(name: "Ambient Weather Device (WS)", namespace: "edalquist", author: "Eric Dalquist") {
command "refresh"
}
preferences {
section { // GENERAL:
input name: "logLevel", title: "Select log level",
displayDuringSetup: false, required: true,
type: "enum", defaultValue: DEFAULT_LOG_LEVEL,
options: LOG_LEVELS
}
section {
input name: "applicationKey", title: "Application Key",
type: "text", required: true
input name: "apiKey", title: "API Key",
type: "text", required: true
}
}
}
def refresh() {
logger("info", "refresh()")
}
def installed() {
logger("info", "installed()")
updated()
}
def updated() {
logger("info", "updated()")
//Create a 30 minute timer for debug logging
if (LOG_LEVELS.indexOf(logLevel) > 1) {
runIn(1800, logsOff)
}
//Connect the webSocket
initialize()
}
def initialize() {
logger("info", "initialize()")
// If applicationKey and apiKey are set
if (hasApplicationKey() && hasApiKey()) {
url = "https://api.ambientweather.net/?api=1&EIO=3&transport=websocket&applicationKey=" + applicationKey
logger("debug", "Creating WS connection to: " + url)
interfaces.webSocket.connect(url)
}
}
private hasApplicationKey() {
return applicationKey != null && applicationKey.length() >= 64;
}
private hasApiKey() {
return apiKey != null && apiKey.length() >= 64;
}
/**
* Websocket callback
*/
def parse(String description) {
//log.debug "parsed $description"
//state.description = []
def json = null;
try {
json = new groovy.json.JsonSlurper().parseText(description)
if (json == null) {
logger("warn", "Failed to parse response as JSON:\n" + description)
return
}
} catch(e) {
logger("warn", "Failed to parse response as JSON: " + e + "\n" + description)
return
}
logger("info", "Parsed Response!\nraw: " + description + "\njson: " + json)
}
def webSocketStatus(String message) {
logger("info", "webSocketStatus(): " + message)
}
def logsOff() {
logger("warn", "debug logging disabled...")
device.updateSetting("logLevel", [value:"info",type:"enum"])
}
/**
* @param level Level to log at, see LOG_LEVELS for options
* @param msg Message to log
*/
private logger(level, msg) {
if (level && msg) {
def levelIdx = LOG_LEVELS.indexOf(level)
def setLevelIdx = LOG_LEVELS.indexOf(logLevel)
if (setLevelIdx < 0) setLevelIdx = LOG_LEVELS.indexOf(DEFAULT_LOG_LEVEL)
if (levelIdx <= setLevelIdx) {
log."${level}" "${device.displayName}: ${msg}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment