-
Notifications
You must be signed in to change notification settings - Fork 15
/
Domoticz.py
250 lines (188 loc) · 5.95 KB
/
Domoticz.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import requests
import json
import chardet
Parameters = {"Mode5": "Debug"}
Devices = {}
Images = {}
debug_Level = 0
def Debug(textStr):
if debug_Level >= 62:
print(u'Debug : {}'.format(textStr))
def Error(textStr):
print(u'Error : {}'.format(textStr))
def Status(textStr):
print(u'Status : {}'.format(textStr))
def Log(textStr):
print(u'Log : {}'.format(textStr))
def Debugging(value):
global debug_Level
debug_Level = int(value)
Log(u'debug_Level: {}'.format(debug_Level))
def Heartbeat(value):
pass
def UpdateDevice(num, Image='', nValue=0, sValue=''):
pass
class Connection:
@property
def Name(self):
return self._name
@property
def data(self):
return self._data
@property
def bp(self):
return self._bp
@bp.setter
def bp(self, value):
self._bp = value
def __init__(self, Name="", Transport="", Protocol="", Address="", Port=""):
self._name = Name
self._transport = Transport
self._ptrotocol = Protocol.lower()
self._address = Address
self._port = Port
self._requestUrl = u'{}://{}:{}'.format(self._ptrotocol, self._address, self._port)
self._data = None
self._bp = None
def Connect(self):
Debug(u'requestUrl: {}'.format(self._requestUrl))
self._bp.onConnect('Connection', 0, 'Description')
return None
def Connecting(self):
return True
def Connected(self):
return True
def Send(self, params):
params['Headers']['accept'] = 'application/json'
if params['Verb'] == 'POST':
url = u'{}/{}'.format(self._requestUrl, params['URL'])
r = requests.post(url, data=params['Data'], headers=params['Headers'])
# build onMessage params
data = {}
data["Status"] = r.status_code
data["Data"] = bytes(json.dumps(r.json()), 'utf-8')
r.encoding = 'utf-8'
self._data = {}
self._data["Status"] = r.status_code
self._data["Data"] = bytes(json.dumps(r.json()), 'utf-8')
self.bp.onMessage(self, data)
return
elif params['Verb'] == 'GET':
url = u'{}/{}'.format(self._requestUrl, params['URL'])
r = requests.get(url, data=params['Data'], headers=params['Headers'])
# build onMessage params and onMessage call
data = {}
data["Status"] = r.status_code
data["Data"] = bytes(json.dumps(r.json()), 'utf-8')
self.bp.onMessage(self, data)
r.encoding = 'utf-8'
self._data = {}
self._data["Status"] = r.status_code
self._data["Data"] = bytes(json.dumps(r.json()), 'utf-8')
return True
class Device:
global Devices
@property
def nValue(self):
return self._nValue
@nValue.setter
def nValue(self, value):
self._nValue = value
@property
def sValue(self):
return self._sValue
@sValue.setter
def nValue(self, value):
self._sValue = value
@property
def ID(self):
return self._id
@ID.setter
def ID(self, value):
self._id = value
@property
def DeviceID(self):
return self._device_id
@DeviceID.setter
def DeviceID(self, value):
self._device_id = value
@property
def Typename(self):
return self._typeName
@Typename.setter
def Typename(self, value):
self._typeName = value
@property
def Name(self):
return self._name
@Name.setter
def Name(self, value):
self._name = value
@property
def LastLevel(self):
return 0
@property
def Image(self):
return self._image
@Image.setter
def Image(self, value):
self._image = value
def __init__(self, Name="", Unit=0, TypeName="", Used=0, Type=0, Subtype=0, Image="", Options=""):
self._nValue = 0
self._sValue = ''
self._name = Name
self._unit = Unit
self._typeName = TypeName
self._used = Used
self._type = Type
self._subtype = Subtype
self._image = Image
self._options = Options
self._id = len(Devices.keys()) + 101
self._device_id = len(Devices.keys()) + 4001
Debug(u'ID: {}'.format(self._id))
def Update(self, nValue=0, sValue='', Options='', Image=None):
self._nvalue = nValue
self._svalue = sValue
self._image = Image
txt_log = self.__str__()
Log(txt_log)
def __str__(self):
txt_log = u'Info - Update device Name : {} nValue : {} sValue : {} Options : {} Image: {}\n'
txt_log = txt_log.format(self._name, self._nvalue, self._svalue, self._options, self._image)
return txt_log
def Create(self):
self._id = len(Devices.keys()) + 101
self._device_id = len(Devices.keys()) + 4001
txt_log = u'Info - Create device : \n\tName : {}\n\tUnit : {}\n\tTypeName : {}\n\tUsed : {}\n\tType : {}\n\tSubtype : {}'
txt_log += u'\n\tImage : {}\n\tOptions : {}'
txt_log = txt_log.format(self._name, self._unit, self._typeName,
self._used, self._type, self._subtype, self._image, self._options)
Log(txt_log)
Devices[len(Devices.keys())] = self
Debug(u'ID: {}'.format(self._id))
class Image:
@property
def Name(self):
return self._name
@Name.setter
def ID(self, value):
self._name = value
@property
def Base(self):
return self._base
@Base.setter
def ID(self, value):
self._base = value
@property
def ID(self):
return self._filename
@ID.setter
def ID(self, value):
self._filename = value
def __init__(self, Filename=""):
self._filename = Filename
self._name = Filename
self._base = Filename
def Create(self):
Images[self._filename.split(u' ')[0]] = self