-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathonOffDevice.groovy
More file actions
98 lines (78 loc) · 2.85 KB
/
onOffDevice.groovy
File metadata and controls
98 lines (78 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.security.MessageDigest
preferences {
input("email", "password", title: "Email Address", description: "Your email address")
input("pin", "password", title: "pin code", description: "Your pin code")
input("roomID", "text", title: "Room ID", description: "The room id")
input("deviceID", "text", title: "Device ID", description: "The device id")
}
metadata {
definition (name: "Lightwave Lights", namespace: "smartthings-users", author: "Adam Clark") {
capability "Switch"
}
simulator {}
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "on", label:'on', action:"on", icon:"st.switches.switch.on", backgroundColor:"#79b821", nextState:"off"
state "off", label:'off', action:"off", icon:"st.switches.switch.off", backgroundColor:"#ffffff", nextState:"on"
}
main "switch"
details (["switch"])
}
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
// TODO: handle 'switch' attribute
}
// handle commands
def on() {
sendEvent(name: "switch", value: 'off')
log.debug "Executing 'off'"
sendCommand(settings.roomID, settings.deviceID, 0)
}
def off() {
sendEvent(name: "switch", value: 'on')
log.debug "Executing 'on'"
sendCommand(settings.roomID, settings.deviceID, 1)
}
def MD5(s) {
def digest = MessageDigest.getInstance("MD5")
new BigInteger(1,digest.digest(s.getBytes())).toString(16).padLeft(32,"0")
}
def timestamp() {
Calendar.getInstance(TimeZone.getTimeZone('GMT')).getTimeInMillis().toString().substring(0,10)
}
def sendCommand(room, device, status) {
def params = [
uri: 'http://lightwaverfhost.co.uk/mobile/getsessionkey.php?email=' + settings.email + '&pin=' + settings.pin,
headers: [
'Origin' : 'lightwaverfhost.co.uk',
'Host' : 'lightwaverfhost.co.uk'
]
]
httpGet(params) {response ->
def loginResponse = response.data.toString().tokenize('~')
def secret = loginResponse[0]
def mac = loginResponse[1]
def currentTimestamp = timestamp()
def commandParams = [
uri: 'http://lightwaverfhost.co.uk/mobile/writerecord.php',
headers: [
'Origin' : 'lightwaverfhost.co.uk',
'Host' : 'lightwaverfhost.co.uk'
],
body : [
'signature' : 'JSMobile',
'secret' : MD5(secret.trim() + currentTimestamp),
'timestamp' : timestamp(),
'action' : 'I',
'email' : settings.email,
'name' : mac,
'commandstring' : '!R' + room + 'D' + device + 'F' + status + '|SMARTTHINGS|SMARTTHINGS'
]
]
httpPost(commandParams) {commandResponse ->
log.debug commandResponse.data
}
}
}