-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
419 lines (365 loc) · 15.9 KB
/
main.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
__author__ = 'Jan Bogaerts'
__copyright__ = "Copyright 2016, AllThingsTalk"
__credits__ = []
__maintainer__ = "Jan Bogaerts"
__email__ = "jb@allthingstalk.com"
__status__ = "Prototype" # "Development", or "Production"
# todo: monitor 'event' topics so that we can see when an asset has been added/removed so the UI can be updated.
# todo: edit label in popup menu.
# todo:
import sys, os
if sys.executable.endswith("pythonw.exe"):
sys.stdout = open(os.devnull, "w");
sys.stderr = open(os.path.join(os.getenv("TEMP"), "stderr-"+os.path.basename(sys.argv[0])), "w")
import kivy
kivy.require('1.9.1') # replace with your current kivy version !
import logging
logging.getLogger().setLevel(logging.INFO)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import Screen
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty
from kivy.uix.actionbar import ActionButton
from kivy.core.window import Window
from errors import *
from inputItem import InputItem, inputWidgets
import outputItem
import iotUserClient.attiotuserclient as IOT
from iotUserClient.popups.credentials import Credentials, CredentialsDialog
import data
import controllino
usedOutputsId = '94'
usedRelaysId = '97'
pinTypesId = '98'
ioMapId = '99'
class OutputsScreen(Screen):
relays = ObjectProperty()
outputs = ObjectProperty()
def __init__(self, **kwargs):
super(OutputsScreen, self).__init__(**kwargs)
outputItem.relays = [] # always reset before we fill the lists.
outputItem.outputs = []
def clearOutputs(self):
if self.relays:
self.relays.clear_widgets()
outputItem.relays = []
if self.outputs:
self.outputs.clear_widgets()
outputItem.outputs = []
def on_relays(self, instance, value):
if self.outputs and self.relays and _Main and _Main.currentDevice:
_Main.dataToWidgets()
def on_outputs(self, instance, value):
if self.outputs and self.relays and _Main and _Main.currentDevice:
_Main.dataToWidgets()
def loadOutputs(self):
'''load the widgets for the relay grid'''
for i in range(1, 17):
item = outputItem.OutputItem(i, controllino.relaysPins[i - 1])
item.bind(isActive=_Main.dataChanged)
item.bind(assetLabel=_Main.dataChanged)
self.relays.add_widget(item)
outputItem.relays.append(item)
for i in range(1, 17):
item = outputItem.OutputItem(i, controllino.outputPins[i - 1])
item.bind(isActive=_Main.dataChanged)
item.bind(assetLabel=_Main.dataChanged)
self.outputs.add_widget(item)
outputItem.outputs.append(item)
class InputsScreen(Screen):
def __init__(self, **kwargs):
super(InputsScreen, self).__init__(**kwargs)
scroll = ScrollView()
scroll.size_hint = (1, 1)
self.add_widget(scroll)
layout = GridLayout()
layout.cols = 3
layout.padding = '8dp'
layout.spacing = '8dp'
layout.size_hint_y = None
layout.bind(minimum_height=layout.setter('height'))
scroll.add_widget(layout)
self.layout = layout
def clearInputs(self):
global inputWidgets
if self.layout:
self.layout.clear_widgets()
inputWidgets = []
def loadInputs(self):
for i in range(1, 22):
item = InputItem(i, controllino.inputPins[i - 1])
item.bind(mode=_Main.dataChanged)
item.bind(label=_Main.dataChanged)
item.bind(outputLabel=_Main.dataChanged)
self.layout.add_widget(item)
inputWidgets.append(item)
class MainWindow(Widget):
editbar = ObjectProperty()
outputsPage = ObjectProperty()
inputsPage = ObjectProperty()
def __init__(self, **kwargs):
#self.data = None
global _Main
_Main = self
self.isChanged = False
self.isLoading = False # so we don't show the save/undo buttons after loading the config.
self.currentDevice = None
self.editButtons = [] # the X and V buttons
Window.softinput_mode = 'below_target' # so the screen resizes when the keybaord is shown, otherwise it hides editing.
super(MainWindow, self).__init__(**kwargs)
if len(data.devices) > 0:
self.loadDevice(data.devices[0])
def on_outputsPage(self, instance, value):
if self.inputsPage and self.outputsPage and self.currentDevice:
self.dataToWidgets()
def on_inputsPage(self, instance, value):
if self.inputsPage and self.outputsPage and self.currentDevice:
self.dataToWidgets()
def showCredentials(self):
dlg = CredentialsDialog(data.credentials, self.credentialsChanged)
dlg.open()
def credentialsChanged(self, value):
"""called when the user closed the credentials dialog"""
IOT.disconnect(False)
data.credentials = value
popup = Popup(title='connecting', content=Label(text='searching for devices,\nand syncing...'),
size_hint=(None, None), size=(400, 250), auto_dismiss=False)
popup.bind(on_open=self._syncWithCloud)
popup.open()
def _syncWithCloud(self, instance):
try:
Application.connect()
self.scanDevices()
data.saveSettings()
if len(data.devices) > 0:
self.loadDevice(data.devices[0])
finally:
instance.dismiss()
def scanDevices(self):
"""scan for controllino devices and add them to the list."""
data.devices = []
grounds = IOT.getGrounds(False)
for ground in grounds:
devices = IOT.getDevices(ground['id'])
for device in devices:
try:
ver = IOT.getAssetByName(device['id'], '90') # 90 = application
if ver and 'state' in ver:
ver = ver['state']['value']
except:
ver = None
if ver and str(ver) == 'Controllino mega - light control': # found a controllino
data.devices.append(device['id'])
def loadDevice(self, device):
"""loads the configuration data of the controllino and displays it."""
self.isLoading = True
try:
self.currentDevice = device
data.ioMap = self.getAssetValue(device, ioMapId)
data.pinTypes = self.getAssetValue(device, pinTypesId)
data.usedRelays = self.getAssetValue(device, usedRelaysId)
if not data.usedRelays:
data.usedRelays = 0
data.usedOutputs = self.getAssetValue(device, usedOutputsId)
if not data.usedOutputs:
data.usedOutputs = 0
self.dataToWidgets()
finally:
self.isLoading = False
def getAssetValue(self, device, asset):
result = IOT.getAssetByName(device, asset)
if result:
result = result['state']
if result:
result = result['value']
return result
def dataToWidgets(self):
if self.inputsPage and self.outputsPage:
self.inputsPage.clearInputs()
self.outputsPage.clearOutputs()
self.inputsPage.loadInputs()
self.outputsPage.loadOutputs()
self.outputsToWidgets(outputItem.outputs, data.usedOutputs)
self.outputsToWidgets(outputItem.relays, data.usedRelays)
self.inputsToWidgets()
def inputsToWidgets(self):
for index in range(0, len(inputWidgets)):
input = inputWidgets[index]
sensor = IOT.getAssetByName(self.currentDevice, input.assetName)
input.assetId = sensor['id']
input.label = sensor['title']
input.labelChanged = False
if sensor['state']:
input.on_valueChanged(sensor['state'])
IOT.subscribe(input.assetId, input.on_valueChanged)
input.mode = "Disabled"
if data.pinTypes:
if data.pinTypes[index] == 'T':
input.mode = "Toggle"
elif data.pinTypes[index] == 'B':
input.mode = "Button"
elif data.pinTypes[index] == 'A':
input.mode = "Analog"
if data.ioMap and data.ioMap[index] != 0xFF:
try:
outIndex = controllino.relaysPins.index(data.ioMap[index])
input.OutputSelected(None, outputItem.relays[outIndex])
except:
outIndex = controllino.outputPins.index(data.ioMap[index])
input.OutputSelected(None, outputItem.outputs[outIndex])
else:
input.OutputSelected(self, None)
def outputsToWidgets(self, widgets, outputPins):
bitIndex = 1
for index in range(0, len(widgets)):
output = widgets[index]
if outputPins & bitIndex > 0:
output.isActive = True
try:
asset = IOT.getAssetByName(self.currentDevice, str(output.assetName))
except:
asset = None
if asset:
if asset['state']: # do before storing the id, so that we don't send a command upon storing the current state.
output.value = asset['state']['value']
output.cloudValue = output.value
output.assetId = asset['id']
output.assetLabel = asset['title']
output.assetLabelChanged = False
IOT.subscribe(output.assetId, output.on_valueChanged)
else:
output.assetLabel = ''
if output.assetId:
IOT.unsubscribe(output.assetId)
output.assetId = None
bitIndex = bitIndex << 1
def sendSettingsToDevice(self, instance):
popup = Popup(title='updating', content=Label(text='sending settings to device,\n please wait...'),
size_hint=(None, None), size=(400, 250), auto_dismiss=False)
popup.bind(on_open=self.widgetsToData)
popup.open()
def widgetsToData(self, instance):
"""store the data in the widgets in the cloud (and in the data module"""
try:
data.usedOutputs = self.widgetsToOutputs(outputItem.outputs, usedOutputsId, data.usedOutputs)
data.usedRelays = self.widgetsToOutputs(outputItem.relays, usedRelaysId, data.usedRelays)
newPinTypes = list(data.pinTypes)
ioMapChanged = False
for index in range(0, len(inputWidgets)):
try:
input = inputWidgets[index]
newPinTypes[index] = input.mode[0]
ioMapChanged |= self.setIoMap(input, index)
if input.labelChanged:
IOT.updateAsset(_Main.currentDevice, input.assetName, {'title': input.label, 'is': 'sensor', 'id': input.assetId})
input.labelChanged = False
if input.mode[0] == 'D' and data.pinTypes[index] != 'D': # the input got disabled, so remove the asset (not done by the device, it only creates asssets)
IOT.deleteAssetbyName(self.currentDevice, input.assetName)
except Exception as e:
showError(e)
newPinTypes = "".join(newPinTypes)
if newPinTypes != data.pinTypes:
IOT.sendByName(self.currentDevice, pinTypesId, newPinTypes)
data.pinTypes = newPinTypes
if ioMapChanged:
IOT.sendByName(self.currentDevice, ioMapId, data.ioMap)
except Exception as e:
showError(e)
finally:
instance.dismiss()
self.removeEditButtons()
self.isChanged = False
def setIoMap(self, input, index):
if input.output:
result = not data.ioMap[index] == input.output.assetName
data.ioMap[index] = input.output.assetName
else:
result = not data.ioMap[index] == 0xFF
data.ioMap[index] = 0xFF
return result
def widgetsToOutputs(self, list, configAssetId, curVal):
result = 0
bitIndex = 1
for item in list:
try:
if item.isActive:
result += bitIndex
if item.assetLabelChanged:
IOT.updateAsset(_Main.currentDevice, item.assetName, {'title': item.assetLabel, 'is': 'actuator', 'id': item.assetId})
item.assetLabelChanged = False
elif curVal & bitIndex > 0:
IOT.deleteAssetbyName(self.currentDevice, item.assetName)
except Exception as e:
showError(e)
bitIndex = bitIndex << 1
if result != curVal: #only send if there was a change.
IOT.sendByName(self.currentDevice, configAssetId, result)
return result
def dataChanged(self, instance, value):
""""called when some part of the data is changed. So we can show the edit buttons"""
if not self.isChanged and not self.isLoading:
btn = ActionButton(text = 'V', on_press=self.sendSettingsToDevice)
self.editbar.add_widget(btn, 1)
self.editButtons.append(btn)
btn = ActionButton(text = 'X', on_press=self.undoEdit)
self.editbar.add_widget(btn, 1)
self.editButtons.append(btn)
self.isChanged = True
def undoEdit(self, instance):
popup = Popup(title='Test popup', content=Label(text='retrieving settings from device,\nplease wait...'), size_hint=(None, None), size=(400, 250), auto_dismiss=False)
popup.bind(on_open=self.on_undo_opened)
popup.open()
def on_undo_opened(self, instance):
self.isLoading = True
try:
self.dataToWidgets()
self.removeEditButtons()
finally:
instance.dismiss()
self.isLoading = False
self.isChanged = False
def removeEditButtons(self):
if len(self.editButtons) > 0:
map(self.editbar.remove_widget, self.editButtons)
self.editButtons = []
class CIConfigApp(App):
def build(self):
data.loadSettings()
self.connect()
self._main = MainWindow()
return self._main
def on_pause(self):
self.saveState(True)
return True
def on_resume(self):
try:
if data.credentials:
IOT.reconnect(data.credentials.server, data.credentials.broker)
logging.info("reconnected after resume")
except Exception as e:
showError(e)
def connect(self):
try:
if data.credentials and data.credentials.isDefined():
IOT.connect(data.credentials.userName, data.credentials.password, data.credentials.server, data.credentials.broker)
logging.info("reconnected after resume")
except Exception as e:
showError(e)
def on_stop(self):
self.saveState(False)
def saveState(self, recoverable):
'''close the connection, save the settings.'''
try:
#data.saveSettings() not required, already done after changing the settings.
IOT.disconnect(recoverable) # close network connection, for cleanup
except:
logging.exception('failed to save application state')
Application = CIConfigApp()
_Main = None # so that the inputs can access the main window and bind to it upon creation.
if __name__ == '__main__':
try:
Application.run()
except Exception as e:
showError(e, "fatal error")