Compare commits

..

1 Commits

Author SHA1 Message Date
Kiril Maslenkov
c09c530f81 MSA-1182: Control your Air Conditioner remotely. 2016-04-14 03:11:54 -05:00
2 changed files with 351 additions and 268 deletions

View File

@@ -0,0 +1,351 @@
/**
* Melissa Climate
*
* Copyright 2016 Kiril Maslenkov
*
* 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.
*
*/
preferences {
input "email", "text", title: "Email", description: "Your Email", required: true
input "password", "password", title: "Password", description: "Your Melissa Password", required: true
input "mac", "text", title: "Melissa MAC Address", description: "Melissa Mac Address", required: true
}
metadata {
definition (name: "Melissa Climate", namespace: "Melissa", author: "Melissa Climate") {
capability "Thermostat"
command temperatureUp
command temperatureDown
command sendCommand
command switchMode
command switchFanMode
command switchingState
command refreshApp
}
simulator { }
tiles(scale: 2) {
multiAttributeTile(name:"status", type: "thermostat", width: 6, height: 4){
tileAttribute("device.temperature", key:"PRIMARY_CONTROL"){
attributeState("default", label:'${currentValue}°', unit: "df", backgroundColor: '#4b8df8')
}
tileAttribute("device.humidity", key: "SECONDARY_CONTROL") {
attributeState("default", label:'${currentValue}%', unit:"%")
}
tileAttribute("device.temperature", key: "VALUE_CONTROL") {
attributeState("VALUE_UP", action: "temperatureUp")
attributeState("VALUE_DOWN", action: "temperatureDown")
}
}
standardTile("mode", "device.mode", decoration: "flat", width: 3, height: 2) {
state "auto", action:"switchMode", label: '${name}', nextState: "cool", icon: "http://server.seemelissa.com/smartthings/icons/modes/auto-2.png"
state "cool", action:"switchMode", label: '${name}', nextState: "heat", icon: "http://server.seemelissa.com/smartthings/icons/modes/cool.png"
state "heat", action:"switchMode", label: '${name}', nextState: "dry", icon: "http://server.seemelissa.com/smartthings/icons/modes/heat.png"
state "dry", action:"switchMode", label: '${name}', nextState: "auto", icon: "http://server.seemelissa.com/smartthings/icons/modes/dry.png"
}
standardTile("fan", "device.fan", decoration: "flat", width: 3, height: 2, canChangeIcon: true, canChangeBackground: true) {
state "auto", action:"switchFanMode", label: '${name}', nextState: "high", icon: "http://server.seemelissa.com/smartthings/icons/modes/auto-2.png"
state "high", action:"switchFanMode", label: '${name}', nextState: "medium", icon: "http://server.seemelissa.com/smartthings/icons/fan/fan1.png"
state "medium", action:"switchFanMode", label: '${name}', nextState: "low", icon: "http://server.seemelissa.com/smartthings/icons/fan/fan2.png"
state "low", action:"switchFanMode", label: '${name}', nextState: "auto", icon: "http://server.seemelissa.com/smartthings/icons/fan/fan3.png"
}
standardTile("switchState", "device.switchState", width: 3, height: 2, decoration: "flat", canChangeIcon: true, canChangeBackground: true) {
state "on", label: '${name}', action: "switchingState", nextState: "off", icon: "http://server.seemelissa.com/smartthings/icons/on_off/turn-on-off-white.png", backgroundColor: "#79b821"
state "off", label: '${name}', action: "switchingState", nextState: "on", icon: "http://server.seemelissa.com/smartthings/icons/on_off/turn-on-off.png", backgroundColor: "#ffffff"
//state "on", label: '${name}', action: "switchState.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
//state "off", label: '${name}', action: "switchState.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
}
standardTile("send", "device.send", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", action:"sendCommand", label: 'Send Command'
}
standardTile("refresh", "device.send2", inactiveLabel: false, decoration: "flat", width: 3, height: 2) {
state "default", action:"refreshApp", icon: "st.secondary.refresh"
}
standardTile("username", "device.username", inactiveLabel: false, decoration: "flat", width: 6, height: 2) {
state "default", label:'${currentValue}'
}
main(["status", "mode", "fan", "send"])
//details(["status", "mode", "fan", "refresh", "switchState", "username"])
details(["status", "mode", "fan", "refresh", "switchState"])
}
}
def switchingState() {
if (state.ac_state == "on") {
state.ac_state = "off"
} else {
state.ac_state = "on"
}
sendEvent(name: "switchState", value: state.ac_state);
sendCommand()
}
/* SWITCHING MODES Auto, Cool, Heat, Dry*/
def switchMode() {
switch (state.mode) {
case "auto":
state.mode = "cool";
break;
case "cool":
state.mode = "heat";
break;
case "heat":
state.mode = "dry";
break;
case "dry":
state.mode = "auto";
break;
}
//sendEvent(name: "username", value: state.mode)
sendCommand()
// log.debug state.mode
}
/* SWITCHING FAN MODES*/
def switchFanMode() {
switch (state.fan) {
case "auto":
state.fan = "high";
break;
case "high":
state.fan = "medium";
break;
case "medium":
state.fan = "low";
break;
case "low":
state.fan = "auto";
break;
}
sendEvent(name: "username", value: state.fan)
sendCommand()
//log.debug state.fan
}
def refreshApp() {
def params = [
uri: 'http://api2.seemelissa.com/user/login',
body: [
email: "k.maslenkov@sabev.at",
password: "xxxxx"
//email: settings.email,
//password: settings.password
]
]
try {
httpPost(params) {resp ->
/*
log.debug "resp data: ${resp.data}"
log.debug resp.data
log.debug resp
*/
def _try = resp.data
def slurper = new groovy.json.JsonSlurper()
def results = slurper.parseText("${resp.data}")
state.token = results.Data
settings.mac = "OIEQ321TGR6"
def getParams = [
uri: "http://api2.seemelissa.com",
path: "/testusers/getMelissaData/${settings.mac}"
//path: "/testusers/getMelissaData/OIEQ321TGR6"
]
try {
httpGet(getParams) {response->
def getResult = slurper.parseText("${response.data}")
state.temp = getResult.temp as int
state.humidity = getResult.humidity
state.codeset = getResult.codeset_id as int
switch (getResult.mode) {
case "0":
state.mode = "auto";
break;
case "1":
state.mode = "fan";
break;
case "2":
state.mode = "heat";
break;
case "3":
state.mode = "cool";
break;
case "4":
state.mode = "dry";
break;
}
switch (getResult.fan) {
case "0":
state.fan = "auto";
break;
case "1":
state.fan = "low";
break;
case "2":
state.fan = "medium";
break;
case "3":
state.fan = "high";
break;
}
if (getResult.state == "0") {
state.ac_state = "off"
} else {
state.ac_state = "on"
}
sendEvent(name: "mode", value: state.mode)
sendEvent(name: "fan", value: state.fan)
sendEvent(name: "temperature", value: state.temp)
sendEvent(name: "humidity", value: state.humidity)
sendEvent(name: "switchState", value: state.ac_state)
}
}
catch (e) {
log.error "error: $e"
}
}
} catch (e) {
log.error "error: $e"
}
}
def sendCommand () {
log.error state
def params = [
uri: 'http://api2.seemelissa.com/testusers/sendCommand',
body: [
mac: "OIEQ321TGR6",
//mac: settings.mac,
temp: state.temp,
token: state.token,
fan: state.fan,
mode: state.mode,
ac_state: state.ac_state,
codeset: state.codeset
]
]
try {
httpPost(params) {resp ->
log.debug "Send command done"
log.debug "resp data: ${resp.data}"
}
} catch (e) {
log.error "error: $e"
}
}
def updated() {
//log.debug "Updated !!!"
login()
}
def temperatureDown() {
if (state.temp != 18) {
state.temp = state.temp - 1
}
sendEvent(name: "temperature", value: state.temp)
sendCommand()
}
def temperatureUp() {
if (state.temp != 30) {
state.temp = state.temp + 1
}
sendEvent(name: "temperature", value: state.temp)
sendCommand()
}
def login() {
refreshApp()
/*
def params = [
uri: 'http://api2.seemelissa.com/user/login',
body: [
email: settings.email,
password: settings.password
]
]
try {
httpPost(params) {resp ->
log.debug "resp data: ${resp.data}"
log.debug "Response Received: Status [$resp.status]"
def _try = resp.data
def slurper = new groovy.json.JsonSlurper()
def results = slurper.parseText("${resp.data}")
state.token = results.Data
state.temp = 18
state.humidity = 40
sendEvent(name: "temperature", value: state.temp)
sendEvent(name: "humidity", value: state.humidity)
sendEvent(name: "username", value: state.token)
}
} catch (e) {
log.error "error: $e"
}
*/
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
}

View File

@@ -1,268 +0,0 @@
/**
* swarmx2
*
* Copyright 2016 Badrinarayanan Rangarajan
*
* 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.
*
*/
metadata {
definition (name: "swarmx2", namespace: "swarmx", author: "Badrinarayanan Rangarajan") {
capability "Actuator"
capability "Sensor"
capability "Image Capture"
command "setAuthToken"
command "removeAuthToken"
}
preferences {
input("CameraIP", "string", title:"Camera IP Address", description: "Please enter your camera's IP Address", required: true, displayDuringSetup: true)
input("CameraPort", "string", title:"Camera Port", description: "Please enter your camera's Port", defaultValue: 80 , required: true, displayDuringSetup: true)
input("CameraPath", "string", title:"Camera Path to Image", description: "Please enter the path to the image (default: /cgi-bin/video.cgi?msubmenu=jpg&resolution=2)", defaultValue: "/cgi-bin/video.cgi?msubmenu=jpg&resolution=2", required: true, displayDuringSetup: true)
input("CameraPostGet", "string", title:"Does Camera use a Post or Get, normally Get?", description: "Please choose if the camera uses a POST or a GET command to retreive the image", defaultValue: "GET", displayDuringSetup: true)
input("CameraUser", "string", title:"Camera User", description: "Please enter your camera's username (default: admin)", defaultValue: "admin", required: false, displayDuringSetup: true)
input("CameraPassword", "string", title:"Camera Password", description: "Please enter your camera's password", required: false, displayDuringSetup: true)
}
simulator {
}
tiles {
standardTile("camera", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: true) {
state "default", label: "", action: "", icon: "st.camera.dropcam-centered", backgroundColor: "#FFFFFF"
}
carouselTile("cameraDetails", "device.image", width: 3, height: 2) { }
standardTile("take", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
state "take", label: "Take", action: "Image Capture.take", icon: "st.camera.camera", backgroundColor: "#FFFFFF", nextState:"taking"
state "taking", label:'Taking', action: "", icon: "st.camera.take-photo", backgroundColor: "#53a7c0"
state "image", label: "Take", action: "Image Capture.take", icon: "st.camera.camera", backgroundColor: "#FFFFFF", nextState:"taking"
}
standardTile("authenticate", "device.button", width: 1, height: 1, canChangeIcon: true) {
state "DeAuth", label: '${name}', action: "removeAuthToken", icon: "st.switches.light.on", backgroundColor: "#79b821"
state "Auth", label: '${name}', action: "setAuthToken", icon: "st.switches.light.off", backgroundColor: "#ffffff"
}
main "camera"
details(["cameraDetails", "take", "error", "authenticate"])
}
}
// method to set digest token
def setAuthToken() {
trace("setAuth")
state.auth = "empty"
take()
}
// method to set remove token (a.k.a. logout)
def removeAuthToken() {
trace("removeAuth")
sendEvent(name: "authenticate", value: "Auth")
state.auth = "empty"
}
// method called after touching the take button
def take() {
def porthex = convertPortToHex(CameraPort)
def hosthex = convertIPtoHex(CameraIP)
def path = CameraPath.trim()
def request = ""
// set a proper network Id of the device
device.deviceNetworkId = "$hosthex:$porthex"
trace("The device id configured is: $device.deviceNetworkId")
trace("state: " + state)
if (!state.auth || state.auth == "empty") {
// empty request to get nonce token
request = """GET ${path} HTTP/1.1\r\nAccept: */*\r\nHost: ${getHostAddress()}\r\n\r\n"""
} else {
// got nonce token, parsing headers and calculating digest header
def auth_headers = calcDigestAuth(state.auth)
request = """GET ${path} HTTP/1.1\r\nAccept: */*\r\nHost: ${getHostAddress()}\r\nAuthorization: ${auth_headers}\r\n\r\n"""
}
try {
def hubAction = new physicalgraph.device.HubAction(request, physicalgraph.device.Protocol.LAN, "${device.deviceNetworkId}")
if (state.auth && state.auth != "empty") {
// upload image/jpg output to S3
hubAction.options = [outputMsgToS3: true]
}
return hubAction
} catch (Exception e) {
trace("Hit Exception $e on $hubAction")
}
}
// method to parse output from the camera
def parse(String output) {
trace("Parsing output: '${output}'")
def headers = ""
def parsedHeaders = ""
def map = stringToMap(output)
if (map.headers) {
headers = new String(map.headers.decodeBase64())
parsedHeaders = parseHttpHeaders(headers)
if (parsedHeaders.auth) {
// set required tokens in the special state variable (see description above)
state.auth = parsedHeaders.auth
trace("Got 401, send request again (click on 'take' one more time): " + state.auth)
sendEvent(name: "authenticate", value: "DeAuth")
return result
}
}
if (map.body != null) {
def bodyString = new String(map.body.decodeBase64())
trace(bodyString)
}
if (map.bucket && map.key) {
trace("Uploading the picture to amazon S3")
putImageInS3(map)
}
return result
}
// parse headers that are returned from the camera
private parseHttpHeaders(String headers) {
def lines = headers.readLines()
def status = lines[0].split()
def result = [
protocol: status[0],
status: status[1].toInteger(),
reason: status[2]
]
if (result.status == 401) {
result.auth = stringToMap(lines[1].replaceAll("WWW-Authenticate: Digest ", "").replaceAll("=", ":").replaceAll("\"", ""))
trace("It's ok. Press take again" + result.auth)
}
if (result.status == 200) {
trace("Authentication successful! :" + result)
}
return result
}
// calculate digest token, more details: http://en.wikipedia.org/wiki/Digest_access_authentication#Overview
private String calcDigestAuth(headers) {
def HA1 = new String("${CameraUser}:" + headers.realm.trim() + ":${CameraPassword}").trim().encodeAsMD5()
def HA2 = new String("${CameraPostGet}:${CameraPath}").trim().encodeAsMD5()
// increase nc every request by one
if (!state.nc) {
state.nc = 1
} else {
state.nc = state.nc + 1
}
def cnonce = java.util.UUID.randomUUID().toString().replaceAll('-', '').substring(0, 8)
def response = new String("${HA1}:" + headers.nonce.trim() + ":" + state.nc + ":" + cnonce + ":" + "auth" + ":${HA2}")
def response_enc = response.encodeAsMD5()
trace("HA1: " + HA1 + " ===== org:" + "${CameraUser}:" + headers.realm.trim() + ":${CameraPassword}")
trace("HA2: " + HA2 + " ===== org:" + "${CameraPostGet}:${CameraPath}")
trace("Response: " + response_enc + " ===== org:" + response)
def eol = " "
return 'Digest username="' + CameraUser.trim() + '",' + eol +
'realm="' + headers.realm.trim() + '",' + eol +
'qop="' + headers.qop.trim() + '",' + eol +
'algorithm="MD5",' + eol +
'uri="'+ CameraPath.trim() + '",' + eol +
'nonce="' + headers.nonce.trim() + '",' + eol +
'cnonce="' + cnonce.trim() + '",'.trim() + eol +
'opaque="",' + eol +
'nc=' + state.nc + ',' + eol +
'response="' + response_enc.trim() + '"'
}
private getPictureName() {
def pictureUuid = java.util.UUID.randomUUID().toString().replaceAll('-', '')
return device.deviceNetworkId + "_$pictureUuid" + ".jpg"
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
trace("IP address entered is $ipAddress and the converted hex code is $hex")
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}
private Integer convertHexToInt(hex) {
Integer.parseInt(hex, 16)
}
private String convertHexToIP(hex) {
[convertHexToInt(hex[0..1]), convertHexToInt(hex[2..3]), convertHexToInt(hex[4..5]), convertHexToInt(hex[6..7])].join(".")
}
private getHostAddress() {
def parts = device.deviceNetworkId.split(":")
def ip = convertHexToIP(parts[0])
def port = convertHexToInt(parts[1])
return ip + ":" + port
}
private hashMD5(String somethingToHash) {
java.security.MessageDigest.getInstance("MD5").digest(somethingToHash.getBytes("UTF-8")).encodeHex().toString()
}
// store image on S3. Hint: if you use your bucket and key maybe you can upload it to your cloud? Never tested, but possible it will work.
def putImageInS3(map) {
def s3ObjectContent
try {
def imageBytes = getS3Object(map.bucket, map.key + ".jpg")
if (imageBytes) {
s3ObjectContent = imageBytes.getObjectContent()
def bytes = new ByteArrayInputStream(s3ObjectContent.bytes)
storeImage(getPictureName(), bytes)
}
} catch (Exception e) {
log.error e
} finally {
if (s3ObjectContent) {
s3ObjectContent.close()
}
}
}
private def delayHubAction(ms) {
return new physicalgraph.device.HubAction("delay ${ms}")
}
private getCallBackAddress() {
device.hub.getDataValue("localIP") + ":" + device.hub.getDataValue("localSrvPortTCP")
}
private trace(message) {
log.debug message
}