forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P212_Dash.py
379 lines (362 loc) · 15 KB
/
_P212_Dash.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
#!/usr/bin/env python3
#############################################################################
#################### Dashboard plugin for RPIEasy ###########################
#############################################################################
#
# Copyright (C) 2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import time
import webserver
import Settings
import os_os as OS
class Plugin(plugin.PluginProto):
PLUGIN_ID = 212
PLUGIN_NAME = "GUI - Dashboard"
PLUGIN_VALUENAME1 = "State"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_DUMMY
self.vtype = rpieGlobals.SENSOR_TYPE_NONE
self.readinprogress = 0
self.valuecount = 0
self.senddataoption = False
self.timeroption = True
self.timeroptional = False
self.formulaoption = False
self._nextdataservetime = 0
self.celldata = []
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.initialized = False
def webform_load(self): # create html page for settings
try:
webserver.addFormCheckBox("Use standard HTML head","p212_head",self.taskdevicepluginconfig[2])
try:
sp = Settings.AdvSettings["startpage"]
except:
sp = "/"
webserver.addFormCheckBox("Set as startpage","p212_start",(sp=="/dash"))
webserver.addHtml("<tr><td>Columns:<td>")
webserver.addSelector_Head("p212_cols",False)
for o in range(7):
webserver.addSelector_Item(str(o),o,(str(o)==str(self.taskdevicepluginconfig[0])),False)
webserver.addSelector_Foot()
webserver.addHtml("<tr><td>Rows:<td>")
webserver.addSelector_Head("p212_rows",False)
for o in range(16):
webserver.addSelector_Item(str(o),o,(str(o)==str(self.taskdevicepluginconfig[1])),False)
webserver.addSelector_Foot()
if int(self.taskdevicepluginconfig[0])>0 and int(self.taskdevicepluginconfig[1])>0:
if self.enabled:
webserver.addHtml("<tr><td>Dashboard address:</td><td><a href='dash'>/dash</a></td></tr>")
options1 = ["None","Text","Binary input","Switch output","Meter","Gauge","Slider output","Select output"]
optionvalues1 = [-1,0,1,2,3,4,5,6]
options2 = ["None"]
optionvalues2 = ["_"]
for t in range(0,len(Settings.Tasks)):
if (Settings.Tasks[t] and (type(Settings.Tasks[t]) is not bool)):
for v in range(0,Settings.Tasks[t].valuecount):
options2.append("T"+str(t+1)+"-"+str(v+1)+" / "+str(Settings.Tasks[t].taskname)+"-"+str(Settings.Tasks[t].valuenames[v]))
optionvalues2.append(str(t)+"_"+str(v))
for r in range(int(self.taskdevicepluginconfig[1])):
for c in range(int(self.taskdevicepluginconfig[0])):
offs = (r * int(self.taskdevicepluginconfig[0])) + c
try:
adata = self.celldata[offs]
except:
adata = {}
dtype = -1
if "type" in adata:
dtype = int(adata["type"])
webserver.addHtml("<tr><td><b>Cell"+str(offs)+" (y"+str(r)+"x"+str(c)+")</b><td>")
dname = ""
if "name" in adata:
dname = str(adata["name"])
webserver.addFormTextBox("Name overwrite","p212_names_"+str(offs),dname,64)
webserver.addFormSelector("Type","p212_type_"+str(offs),len(options1),options1,optionvalues1,None,dtype)
webserver.addHtml("<tr><td>Data source:<td>")
ddata = "_"
if "data" in adata:
ddata = str(adata["data"])
webserver.addSelector_Head("p212_data_"+str(offs),False)
for o in range(len(options2)):
webserver.addSelector_Item(options2[o],optionvalues2[o],(str(optionvalues2[o])==str(ddata)),False)
webserver.addSelector_Foot()
if dtype in (0,4):
try:
udata = str(adata["unit"])
except:
udata = ""
webserver.addFormTextBox("Unit","p212_unit_"+str(offs),udata,16)
if dtype in (3,4,5):
try:
umin = float(adata["min"])
except:
umin = 0
try:
umax = float(adata["max"])
except:
umax = 100
webserver.addFormFloatNumberBox("Min value","p212_min_"+str(offs),umin,-65535.0,65535.0)
webserver.addFormFloatNumberBox("Max value","p212_max_"+str(offs),umax,-65535.0,65535.0)
elif dtype == 6:
try:
uon = str(adata["optionnames"])
except:
uon = ""
try:
uopt = str(adata["options"])
except:
uopt = ""
webserver.addFormTextBox("Option name list","p212_optionnames_"+str(offs),uon,1024)
webserver.addFormTextBox("Option value list","p212_options_"+str(offs),uopt,1024)
webserver.addFormNote("Input comma separated values for selector boxes!")
except Exception as e:
print(e)#debug
return False
return True
def webform_save(self,params): # process settings post reply
try:
sp = Settings.AdvSettings["startpage"]
except:
sp = "/"
spold = sp
if (webserver.arg("p212_start",params)=="on"):
try:
if sp != "/dash":
Settings.AdvSettings["startpage"] = "/dash"
except:
pass
else:
try:
if sp == "/dash":
Settings.AdvSettings["startpage"] = "/"
except:
pass
try:
if spold != Settings.AdvSettings["startpage"]:
Settings.saveadvsettings()
except:
pass
if (webserver.arg("p212_head",params)=="on"):
self.taskdevicepluginconfig[2] = True
else:
self.taskdevicepluginconfig[2] = False
par = webserver.arg("p212_cols",params)
try:
self.taskdevicepluginconfig[0] = int(par)
except:
self.taskdevicepluginconfig[0] = 1
par = webserver.arg("p212_rows",params)
try:
self.taskdevicepluginconfig[1] = int(par)
except:
self.taskdevicepluginconfig[1] = 1
try:
for c in range(int(self.taskdevicepluginconfig[0])):
for r in range(int(self.taskdevicepluginconfig[1])):
offs = (r * int(self.taskdevicepluginconfig[0])) + c
mknew = True
try:
self.celldata[offs]["type"] = int(webserver.arg("p212_type_"+str(offs),params))
self.celldata[offs]["data"] = str(webserver.arg("p212_data_"+str(offs),params))
mknew = False
except:
pass
if mknew:
try:
adata = {"type":int(webserver.arg("p212_type_"+str(offs),params)), "data":str(webserver.arg("p212_data_"+str(offs),params))}
except:
adata = {"type":-1, "data":"_"}
self.celldata.append(adata)
try:
self.celldata[offs]["unit"] = str(webserver.arg("p212_unit_"+str(offs),params))
except:
pass
try:
self.celldata[offs]["min"] = float(webserver.arg("p212_min_"+str(offs),params))
self.celldata[offs]["max"] = float(webserver.arg("p212_max_"+str(offs),params))
except:
pass
try:
self.celldata[offs]["optionnames"] = str(webserver.arg("p212_optionnames_"+str(offs),params))
self.celldata[offs]["options"] = str(webserver.arg("p212_options_"+str(offs),params))
self.celldata[offs]["name"] = str(webserver.arg("p212_names_"+str(offs),params))
except:
pass
except:
pass
return True
def plugin_read(self): # deal with data processing at specified time interval
result = False
if self.enabled:
self._lastdataservetime = rpieTime.millis()
return result
@webserver.WebServer.route('/dash')
def handle_dash(self):
try:
if (not webserver.isLoggedIn(self.get,self.cookie)):
return self.redirect('/login')
webserver.navMenuIndex=7
dashtask = None
for t in range(0,len(Settings.Tasks)):
if (Settings.Tasks[t] and (type(Settings.Tasks[t]) is not bool)):
try:
if Settings.Tasks[t].enabled:
if Settings.Tasks[t].pluginid==212:
dashtask = Settings.Tasks[t]
break
except:
pass
webserver.TXBuffer =""
if (dashtask is not None) and (dashtask.taskdevicepluginconfig[2]):
webserver.sendHeadandTail("TmplStd",webserver._HEAD)
webserver.TXBuffer += "<link rel='stylesheet' href='/img/dash.css'>"
else:
webserver.TXBuffer += "<!DOCTYPE html><html lang='en'><head><meta charset='utf-8'/><link rel='stylesheet' href='/img/dash.css'></head><body>"
if (dashtask is not None) and (len(dashtask.celldata)>0):
astr = ""
estr = ""
vstr = ""
if int(dashtask.taskdevicepluginconfig[0])>1 and int(dashtask.taskdevicepluginconfig[0])<7:
astr = " tab"+str(dashtask.taskdevicepluginconfig[0])
webserver.addHtml("<table width='100%' class='tab "+astr+"'>")
offs = 0
for r in range(int(dashtask.taskdevicepluginconfig[1])):
if offs>=len(dashtask.celldata):
break
webserver.addHtml("<tr>")
for c in range(int(dashtask.taskdevicepluginconfig[0])):
if offs>=len(dashtask.celldata):
break
webserver.addHtml("<td>")
offs = (r * int(dashtask.taskdevicepluginconfig[0])) + c
try:
dtype = int(dashtask.celldata[offs]["type"])
tid = str(dashtask.celldata[offs]["data"])
estr += '"'+tid+'",'
ti = tid.split("_")
tasknum = int(ti[0])
valnum = int(ti[1])
vstr += '"'+ str(Settings.Tasks[tasknum].uservar[valnum]) +'",'
try:
namestr = str(dashtask.celldata[offs]["name"])
except:
namestr = ""
if namestr.strip() == "":
namestr = str(Settings.Tasks[tasknum].gettaskname())+"#"+str(Settings.Tasks[tasknum].valuenames[valnum])
webserver.addHtml("<div class='div_d' id='valuename_"+ str(tid) + "'>"+ namestr + "</div>")
except:
dtype = -1
try:
udata = str(dashtask.celldata[offs]["unit"])
except:
udata = ""
if dtype == 0:
webserver.addHtml("<div class='textval' id='value_"+str(tid)+ "'>"+ str(Settings.Tasks[tasknum].uservar[valnum]) +"</div>")
elif dtype == 1:
webserver.addHtml("<div class='centered'><input type='checkbox' id='value_"+str(tid)+ "' class='state' disabled='disabled'/><label for='value_"+str(tid)+ "' class='toggleWrapper'><div class='toggle'></div></label></div>")
elif dtype == 2:
webserver.addHtml("<div class='switch'><input id='value_"+str(tid)+ "' class='cmn-toggle cmn-toggle-round' type='checkbox' onchange='cboxchanged(this)'><label for='value_"+str(tid)+ "'></label></div>")
elif dtype == 3:
try:
umin = float(dashtask.celldata[offs]["min"])
except:
umin = 0
try:
umax = float(dashtask.celldata[offs]["max"])
except:
umax = 100
webserver.addHtml("<div style='width:100%'><meter id='value_"+str(tid)+ "' min='"+str(umin)+"' max='"+str(umax)+"' value='" + str(Settings.Tasks[tasknum].uservar[valnum]) + "' class='meter'></meter>")
sval = umin
stepval = (umax-umin)/5
webserver.addHtml("<ul id='scale'><li style='width: 10%'><span></span></li>")
while sval < (umax-stepval):
sval = sval + stepval
webserver.addHtml("<li><span id='scale'>"+str(sval)+"</span></li>")
webserver.addHtml("<li style='width: 10%'><span id='scale'></span></li></ul></div>")
elif dtype == 4:
webserver.addHtml("<div class='gauge' id='value_"+str(tid)+ "'><div class='gauge__body'><div class='gauge__fill'></div><div class='gauge__cover'></div></div></div>")
elif dtype == 5:
try:
umin = float(dashtask.celldata[offs]["min"])
except:
umin = 0
try:
umax = float(dashtask.celldata[offs]["max"])
except:
umax = 100
webserver.addHtml("<input type='range' min='" +str(umin) +"' max='"+ str(umax) +"' value='" + str(Settings.Tasks[tasknum].uservar[valnum]) + "' class='slider' id='value_"+str(tid)+ "' onchange='sselchanged(this)' oninput='this.nextElementSibling.value = this.value'>")
webserver.addHtml("<output>"+ str(Settings.Tasks[tasknum].uservar[valnum]) +"</output>")
elif dtype == 6:
webserver.addHtml("<div class='box'><select name='value_"+str(tid)+ "' id='value_"+str(tid)+ "' onchange='sselchanged(this)'>")
optionnames = []
if "," in dashtask.celldata[offs]["optionnames"]:
optionnames = dashtask.celldata[offs]["optionnames"].split(",")
elif ";" in dashtask.celldata[offs]["optionnames"]:
optionnames = dashtask.celldata[offs]["optionnames"].split(",")
options = []
if "," in dashtask.celldata[offs]["options"]:
options = dashtask.celldata[offs]["options"].split(",")
elif ";" in dashtask.celldata[offs]["options"]:
options = dashtask.celldata[offs]["options"].split(",")
ol = len(optionnames)
if ol> len(options):
ol = len(options)
for o in range(ol):
webserver.addHtml("<option value='"+ str(options[o]) +"'>"+ str(optionnames[o]) +"</option>")
webserver.addHtml("</select></div>")
else:
webserver.addHtml(" ")
if udata.strip() != "":
webserver.addHtml(textparse(udata))
webserver.addHtml("</td>")
webserver.addHtml("</tr>")
webserver.addHtml("</table>")
webserver.addHtml('<script type="text/javascript" src="/img/dash.js"></script>')
webserver.addHtml("<script>var elements=["+estr+"]; var values=["+vstr+"];")
offs = (dashtask.taskdevicepluginconfig[0] * dashtask.taskdevicepluginconfig[1])
pstr = ""
for o in range(offs):
if "min" in dashtask.celldata[o]:
pstr += "[" + str(dashtask.celldata[o]["min"]) + "," + str(dashtask.celldata[o]["max"]) + "],"
else:
pstr += '[0,100],'
webserver.addHtml('var props=['+str(pstr)+'];')
webserver.addHtml("var ownurl='http://" + str(OS.get_ip())+":"+str(Settings.WebUIPort)+"';")
webserver.addHtml("refreshDatas();setInterval(function(){ getDatas(); }, "+ str(dashtask.interval*1000) +");</script>")
else:
webserver.TXBuffer += "<p>Setup dashboard first!"
if dashtask.taskdevicepluginconfig[2]:
webserver.sendHeadandTail("TmplStd",webserver._TAIL)
else:
webserver.TXBuffer += "</body></html>"
except Exception as e:
print("Config error, try to delete and recreate task!")
return webserver.TXBuffer
def textparse(ostr):
resstr=str(ostr)
if "{" in resstr or "&" in resstr:
resstr = resstr.replace("{D}","˚").replace("°","˚")
resstr = resstr.replace("{<<}","«").replace("«","«")
resstr = resstr.replace("{>>} ","»").replace("»","»")
resstr = resstr.replace("{u} ","µ").replace("µ ","µ")
resstr = resstr.replace("{E}","€").replace("€","€")
resstr = resstr.replace("{Y}","¥").replace("¥","¥")
resstr = resstr.replace("{P}","£").replace("£","£")
resstr = resstr.replace("{c}","¢").replace("¢","¢")
resstr = resstr.replace("{^1}","¹").replace("¹","¹")
resstr = resstr.replace("{^2}","²").replace("²","²")
resstr = resstr.replace("{^3}","³").replace("³","³")
resstr = resstr.replace("{1_4}","¼").replace("¼","¼")
resstr = resstr.replace("{1_2}","½").replace("&frac24;","½")
resstr = resstr.replace("{3_4}","¾").replace("¾","¾")
resstr = resstr.replace("{+-}","±").replace("±","±")
resstr = resstr.replace("{x}","×").replace("×","×")
resstr = resstr.replace("{..}","÷").replace("÷","÷")
return resstr