author | mjsousa |
Wed, 21 Oct 2015 15:00:32 +0100 | |
changeset 1475 | de4ee16f7c6c |
parent 728 | e0424e96e3fd |
child 1511 | 91538d0c242c |
permissions | -rw-r--r-- |
371 | 1 |
#!/usr/bin/python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
import os |
|
5 |
||
6 |
from nevow import rend, appserver, inevow, tags, loaders, athena |
|
7 |
import simplejson as json |
|
8 |
||
9 |
svgfile = '%(svgfile)s' |
|
10 |
||
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
11 |
svguiWidgets = {} |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
12 |
|
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
13 |
currentId = 0 |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
14 |
def getNewId(): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
15 |
global currentId |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
16 |
currentId += 1 |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
17 |
return currentId |
371 | 18 |
|
19 |
class SvguiWidget: |
|
20 |
||
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
21 |
def __init__(self, classname, id, **kwargs): |
371 | 22 |
self.classname = classname |
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
23 |
self.id = id |
371 | 24 |
self.attrs = kwargs.copy() |
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
25 |
self.inputs = {} |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
26 |
self.outputs = {} |
371 | 27 |
self.inhibit = False |
28 |
self.changed = False |
|
29 |
||
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
30 |
def setinput(self, attrname, value): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
31 |
self.inputs[attrname] = value |
371 | 32 |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
33 |
def getinput(self, attrname, default=None): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
34 |
if not self.inputs.has_key(attrname): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
35 |
self.inputs[attrname] = default |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
36 |
return self.inputs[attrname] |
371 | 37 |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
38 |
def setoutput(self, attrname, value): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
39 |
if self.outputs.get(attrname) != value: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
40 |
self.outputs[attrname] = value |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
41 |
self.changed = True |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
42 |
self.RefreshInterface() |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
43 |
|
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
44 |
def updateoutputs(self, **kwargs): |
371 | 45 |
for attrname, value in kwargs.iteritems(): |
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
46 |
if self.outputs.get(attrname) != value: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
47 |
self.outputs[attrname] = value |
371 | 48 |
self.changed = True |
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
49 |
self.RefreshInterface() |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
50 |
|
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
51 |
def RefreshInterface(self): |
371 | 52 |
interface = website.getHMI() |
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
53 |
if isinstance(interface, SVGUI_HMI) and self.changed and not self.inhibit: |
371 | 54 |
self.changed = False |
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
55 |
d = interface.sendData(self) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
56 |
if d is not None: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
57 |
self.inhibit = True |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
58 |
d.addCallback(self.InterfaceRefreshed) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
59 |
|
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
60 |
def InterfaceRefreshed(self, result): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
61 |
self.inhibit = False |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
62 |
if self.changed: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
63 |
self.RefreshInterface() |
371 | 64 |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
65 |
def get_object_init_state(obj): |
371 | 66 |
# Convert objects to a dictionary of their representation |
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
67 |
attrs = obj.attrs.copy() |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
68 |
attrs.update(obj.inputs) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
69 |
d = { '__class__': obj.classname, |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
70 |
'id': obj.id, |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
71 |
'kwargs': json.dumps(attrs), |
371 | 72 |
} |
73 |
return d |
|
74 |
||
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
75 |
def get_object_current_state(obj): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
76 |
# Convert objects to a dictionary of their representation |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
77 |
d = { '__class__': obj.classname, |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
78 |
'id': obj.id, |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
79 |
'kwargs': json.dumps(obj.outputs), |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
80 |
} |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
81 |
return d |
371 | 82 |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
83 |
class SVGUI_HMI(website.PLCHMI): |
371 | 84 |
jsClass = u"LiveSVGPage.LiveSVGWidget" |
85 |
||
86 |
docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
|
87 |
tags.xml(loaders.xmlfile(os.path.join(WorkingDir, svgfile))), |
|
88 |
]) |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
89 |
|
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
90 |
def HMIinitialisation(self): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
91 |
gadgets = [] |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
92 |
for gadget in svguiWidgets.values(): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
93 |
gadgets.append(unicode(json.dumps(gadget, default=get_object_init_state, indent=2), 'ascii')) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
94 |
d = self.callRemote('init', gadgets) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
95 |
d.addCallback(self.HMIinitialised) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
96 |
|
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
97 |
def sendData(self,data): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
98 |
if self.initialised: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
99 |
return self.callRemote('receiveData',unicode(json.dumps(data, default=get_object_current_state, indent=2), 'ascii')) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
100 |
return None |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
101 |
|
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
102 |
def setattr(self, id, attrname, value): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
103 |
svguiWidgets[id].setinput(attrname, value) |
371 | 104 |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
105 |
def createSVGUIControl(*args, **kwargs): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
106 |
id = getNewId() |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
107 |
gad = SvguiWidget(args[0], id, **kwargs) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
108 |
svguiWidgets[id] = gad |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
109 |
gadget = [unicode(json.dumps(gad, default=get_object_init_state, indent=2), 'ascii')] |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
110 |
interface = website.getHMI() |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
111 |
if isinstance(interface, SVGUI_HMI) and interface.initialised: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
112 |
interface.callRemote('init', gadget) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
113 |
return id |
371 | 114 |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
115 |
def setAttr(id, attrname, value): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
116 |
gad = svguiWidgets.get(id, None) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
117 |
if gad is not None: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
118 |
gad.setoutput(attrname, value) |
371 | 119 |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
120 |
def updateAttr(id, **kwargs): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
121 |
gad = svguiWidgets.get(id, None) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
122 |
if gad is not None: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
123 |
gad.updateoutput(**kwargs) |
371 | 124 |
|
381
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
125 |
def getAttr(id, attrname, default=None): |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
126 |
gad = svguiWidgets.get(id, None) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
127 |
if gad is not None: |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
128 |
return gad.getinput(attrname, default) |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
129 |
return default |
5c0f34a9ab00
Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents:
371
diff
changeset
|
130 |