-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimperirazz.py
More file actions
187 lines (174 loc) · 6.07 KB
/
Copy pathimperirazz.py
File metadata and controls
187 lines (174 loc) · 6.07 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
import json
import web
import requests
import re
import base64
#Settings for razzberry connection
topLevelUrl = 'http://127.0.0.1:8083'
DevicesUrl= topLevelUrl +'/ZAutomation/api/v1/devices'
LocationsUrl= topLevelUrl +'/ZAutomation/api/v1/locations'
LoginUrl = topLevelUrl + '/ZAutomation/api/v1/login'
# username and password for login to razzberry interface
username = 'username'
password = 'password'
#Settings for imperirazz
#authentication for imperirazz interface
authEnabled = 0 #change to 1 to enable authorization
usernameImperirazz = "username"
passwordImperirazz = "password"
#Start script
LoginHeader = {'User-Agent': 'Mozilla/5.0', 'Content-Type': 'application/json'}
Formlogin = '{"form": true, "login": "'+username+'", "password": "'+password+'", "keepme": false, "default_ui": 1}'
urls = (
'/','index',
'/devices(.*)', 'devices',
'/system', 'system',
'/rooms', 'rooms',
'/login','login'
)
class devices:
def GET(self, action):
if authEnabled == 0 or web.ctx.env.get('HTTP_AUTHORIZATION') is not None:
session = requests.Session()
session.post(LoginUrl,headers=LoginHeader, data=Formlogin)
if not action:
response = session.get(DevicesUrl)
gettedData = response.json()
responseLocations = session.get(LocationsUrl)
gettedDataLocations = responseLocations.json()
output = []
for data in gettedData['data']['devices']:
typeData = ""
idData = ""
valueData = ""
levelData = ""
nameData = ""
roomData = ""
unitData = ""
if data['permanently_hidden'] == 0:
idData = data['id']
nameData = data['metrics']['title']
for data2 in gettedDataLocations['data']:
try:
for data3 in data2['namespaces']:
if data3['id'] == "devices_all":
for data4 in data3['params']:
if data4['deviceId'] == idData:
roomData = data2['title']
if roomData == "globalRoom":
roomData = ""
except:
pass
if data['deviceType'] == "switchMultilevel":
typeData = "DevDimmer"
levelData = data['metrics']['level']
if levelData == 0:
valueData = 0
else:
valueData = 1
output.append({"id": idData, "name": nameData, "type": typeData, "room": roomData, "params": [{ "key": "Status", "value": valueData }, { "key": "Level", "value": levelData}]})
if data['deviceType'] == "sensorMultilevel":
probeTypeData = data['probeType']
if not probeTypeData:
probeTitle = data['metrics']['probeTitle']
if probeTitle == "Distance":
typeData = "DevGenericSensor"
unitData = data['metrics']['scaleTitle']
else:
if probeTypeData == "temperature":
typeData = "DevTemperature"
if typeData:
valueData = data['metrics']['level']
output.append({"id": idData, "name": nameData, "type": typeData, "room": roomData, "params": [{ "key": "Value", "value": valueData, "unit": unitData, "graphable": "false" }]})
finalSend = json.dumps(output)
finalSend = finalSend[1:-1]
finalSend = '{"devices": [' + finalSend + ']}'
return finalSend
else:
print action
actionSplitted = action.split('/')
devAction = actionSplitted[2]
if devAction == "action":
devId = actionSplitted[1]
devActionName = actionSplitted[3]
devActionParam = actionSplitted[4]
if devActionName == "setStatus": #param = 0 or 1
if devActionParam == "1":
response = session.get(topLevelUrl + "/ZAutomation/api/v1/devices/" + devId + "/command/on")
gettedData = response.json()
if gettedData['code'] == 200:
return json.dumps({"success": "true"})
elif devActionParam == "0":
response = session.get(topLevelUrl + "/ZAutomation/api/v1/devices/" + devId + "/command/off")
gettedData = response.json()
if gettedData['code'] == 200:
return json.dumps({"success": "true"})
else:
pass
elif devActionName == "setLevel": #param = 0 - 100
response = session.get(topLevelUrl + "/ZAutomation/api/v1/devices/" + devId + "/command/exact?level=" + devActionParam)
gettedData = response.json()
if gettedData['code'] == 200:
return json.dumps({"success": "true"})
else:
pass
else:
raise web.seeother('/login')
class system:
def GET(self):
if authEnabled == 0 or web.ctx.env.get('HTTP_AUTHORIZATION') is not None:
return json.dumps({"id": "imperirazz", "apiversion": 1})
else:
raise web.seeother('/login')
class rooms:
def GET(self):
if authEnabled == 0 or web.ctx.env.get('HTTP_AUTHORIZATION') is not None:
roomData = ""
output = []
session = requests.Session()
session.post(LoginUrl,headers=LoginHeader, data=Formlogin)
responseLocations = session.get(LocationsUrl)
gettedDataLocations = responseLocations.json()
for data2 in gettedDataLocations['data']:
roomData = data2['title']
if roomData == "globalRoom":
pass
else:
output.append({"id": roomData, "name": roomData})
finalSend = json.dumps(output)
finalSend = finalSend[1:-1]
finalSend = '{"rooms": [' + finalSend + ']}'
return finalSend
else:
raise web.seeother('/login')
class login:
def GET(self):
auth = web.ctx.env.get('HTTP_AUTHORIZATION')
authreq = False
if auth is None:
authreq = True
else:
auth = re.sub('^Basic ','',auth)
username,password = base64.decodestring(auth).split(':')
if (username == usernameImperirazz) and (password == passwordImperirazz):
raise web.seeother('/')
else:
authreq = True
if authreq:
web.header('WWW-Authenticate','Basic realm="Auth example"')
web.ctx.status = '401 Unauthorized'
return
class index:
def GET(self):
if authEnabled == 0 or web.ctx.env.get('HTTP_AUTHORIZATION') is not None:
return 'Imperirazz ISS for Imperihome'
else:
raise web.seeother('/login')
def common_headers():
web.header('Content-type', "application/json")
web.header('Access-Control-Allow-Origin', "*")
if __name__ == "__main__":
app = web.application(urls, globals())
app.add_processor(web.loadhook(common_headers))
app.run()