48 self.inhibit = False |
51 self.inhibit = False |
49 self.changed = False |
52 self.changed = False |
50 |
53 |
51 def setinput(self, attrname, value): |
54 def setinput(self, attrname, value): |
52 self.inputs[attrname] = value |
55 self.inputs[attrname] = value |
53 |
56 |
54 def getinput(self, attrname, default=None): |
57 def getinput(self, attrname, default=None): |
55 if not self.inputs.has_key(attrname): |
58 if attrname not in self.inputs: |
56 self.inputs[attrname] = default |
59 self.inputs[attrname] = default |
57 return self.inputs[attrname] |
60 return self.inputs[attrname] |
58 |
61 |
59 def setoutput(self, attrname, value): |
62 def setoutput(self, attrname, value): |
60 if self.outputs.get(attrname) != value: |
63 if self.outputs.get(attrname) != value: |
61 self.outputs[attrname] = value |
64 self.outputs[attrname] = value |
62 self.changed = True |
65 self.changed = True |
63 self.RefreshInterface() |
66 self.RefreshInterface() |
64 |
67 |
65 def updateoutputs(self, **kwargs): |
68 def updateoutputs(self, **kwargs): |
66 for attrname, value in kwargs.iteritems(): |
69 for attrname, value in kwargs.iteritems(): |
67 if self.outputs.get(attrname) != value: |
70 if self.outputs.get(attrname) != value: |
68 self.outputs[attrname] = value |
71 self.outputs[attrname] = value |
69 self.changed = True |
72 self.changed = True |
70 self.RefreshInterface() |
73 self.RefreshInterface() |
71 |
74 |
72 def RefreshInterface(self): |
75 def RefreshInterface(self): |
73 interface = website.getHMI() |
76 interface = website.getHMI() |
74 if isinstance(interface, SVGUI_HMI) and self.changed and not self.inhibit: |
77 if isinstance(interface, SVGUI_HMI) and self.changed and not self.inhibit: |
75 self.changed = False |
78 self.changed = False |
76 d = interface.sendData(self) |
79 d = interface.sendData(self) |
77 if d is not None: |
80 if d is not None: |
78 self.inhibit = True |
81 self.inhibit = True |
79 d.addCallback(self.InterfaceRefreshed) |
82 d.addCallback(self.InterfaceRefreshed) |
80 |
83 |
81 def InterfaceRefreshed(self, result): |
84 def InterfaceRefreshed(self, result): |
82 self.inhibit = False |
85 self.inhibit = False |
83 if self.changed: |
86 if self.changed: |
84 self.RefreshInterface() |
87 self.RefreshInterface() |
85 |
88 |
|
89 |
86 def get_object_init_state(obj): |
90 def get_object_init_state(obj): |
87 # Convert objects to a dictionary of their representation |
91 # Convert objects to a dictionary of their representation |
88 attrs = obj.attrs.copy() |
92 attrs = obj.attrs.copy() |
89 attrs.update(obj.inputs) |
93 attrs.update(obj.inputs) |
90 d = { '__class__': obj.classname, |
94 d = { |
91 'id': obj.id, |
95 '__class__': obj.classname, |
92 'kwargs': json.dumps(attrs), |
96 'id': obj.id, |
93 } |
97 'kwargs': json.dumps(attrs), |
|
98 } |
94 return d |
99 return d |
|
100 |
95 |
101 |
96 def get_object_current_state(obj): |
102 def get_object_current_state(obj): |
97 # Convert objects to a dictionary of their representation |
103 # Convert objects to a dictionary of their representation |
98 d = { '__class__': obj.classname, |
104 d = { |
99 'id': obj.id, |
105 '__class__': obj.classname, |
100 'kwargs': json.dumps(obj.outputs), |
106 'id': obj.id, |
101 } |
107 'kwargs': json.dumps(obj.outputs), |
|
108 } |
102 return d |
109 return d |
|
110 |
103 |
111 |
104 class SVGUI_HMI(website.PLCHMI): |
112 class SVGUI_HMI(website.PLCHMI): |
105 jsClass = u"LiveSVGPage.LiveSVGWidget" |
113 jsClass = u"LiveSVGPage.LiveSVGWidget" |
106 |
114 |
107 docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
115 docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[ |
108 tags.xml(loaders.xmlfile(os.path.join(WorkingDir, svgfile))), |
116 tags.xml(loaders.xmlfile(os.path.join(WorkingDir, svgfile))), |
109 ]) |
117 ]) |
110 |
118 |
111 def HMIinitialisation(self): |
119 def HMIinitialisation(self): |
112 gadgets = [] |
120 gadgets = [] |
113 for gadget in svguiWidgets.values(): |
121 for gadget in svguiWidgets.values(): |
114 gadgets.append(unicode(json.dumps(gadget, default=get_object_init_state, indent=2), 'ascii')) |
122 gadgets.append(unicode(json.dumps(gadget, default=get_object_init_state, indent=2), 'ascii')) |
115 d = self.callRemote('init', gadgets) |
123 d = self.callRemote('init', gadgets) |
116 d.addCallback(self.HMIinitialised) |
124 d.addCallback(self.HMIinitialised) |
117 |
125 |
118 def sendData(self,data): |
126 def sendData(self, data): |
119 if self.initialised: |
127 if self.initialised: |
120 return self.callRemote('receiveData',unicode(json.dumps(data, default=get_object_current_state, indent=2), 'ascii')) |
128 return self.callRemote('receiveData', unicode(json.dumps(data, default=get_object_current_state, indent=2), 'ascii')) |
121 return None |
129 return None |
122 |
130 |
123 def setattr(self, id, attrname, value): |
131 def setattr(self, id, attrname, value): |
124 svguiWidgets[id].setinput(attrname, value) |
132 svguiWidgets[id].setinput(attrname, value) |
|
133 |
125 |
134 |
126 def createSVGUIControl(*args, **kwargs): |
135 def createSVGUIControl(*args, **kwargs): |
127 id = getNewId() |
136 id = getNewId() |
128 gad = SvguiWidget(args[0], id, **kwargs) |
137 gad = SvguiWidget(args[0], id, **kwargs) |
129 svguiWidgets[id] = gad |
138 svguiWidgets[id] = gad |
131 interface = website.getHMI() |
140 interface = website.getHMI() |
132 if isinstance(interface, SVGUI_HMI) and interface.initialised: |
141 if isinstance(interface, SVGUI_HMI) and interface.initialised: |
133 interface.callRemote('init', gadget) |
142 interface.callRemote('init', gadget) |
134 return id |
143 return id |
135 |
144 |
|
145 |
136 def setAttr(id, attrname, value): |
146 def setAttr(id, attrname, value): |
137 gad = svguiWidgets.get(id, None) |
147 gad = svguiWidgets.get(id, None) |
138 if gad is not None: |
148 if gad is not None: |
139 gad.setoutput(attrname, value) |
149 gad.setoutput(attrname, value) |
|
150 |
140 |
151 |
141 def updateAttr(id, **kwargs): |
152 def updateAttr(id, **kwargs): |
142 gad = svguiWidgets.get(id, None) |
153 gad = svguiWidgets.get(id, None) |
143 if gad is not None: |
154 if gad is not None: |
144 gad.updateoutput(**kwargs) |
155 gad.updateoutput(**kwargs) |
145 |
156 |
|
157 |
146 def getAttr(id, attrname, default=None): |
158 def getAttr(id, attrname, default=None): |
147 gad = svguiWidgets.get(id, None) |
159 gad = svguiWidgets.get(id, None) |
148 if gad is not None: |
160 if gad is not None: |
149 return gad.getinput(attrname, default) |
161 return gad.getinput(attrname, default) |
150 return default |
162 return default |
151 |
|