24 |
24 |
25 |
25 |
26 from __future__ import absolute_import |
26 from __future__ import absolute_import |
27 from __future__ import print_function |
27 from __future__ import print_function |
28 import os |
28 import os |
29 from nevow import appserver, inevow, tags, loaders, athena |
29 from zope.interface import implements |
|
30 from nevow import appserver, inevow, tags, loaders, athena, url, rend |
30 from nevow.page import renderer |
31 from nevow.page import renderer |
|
32 from formless import annotate |
|
33 from formless import webform |
|
34 |
31 from twisted.internet import reactor |
35 from twisted.internet import reactor |
32 import util.paths as paths |
36 import util.paths as paths |
|
37 |
|
38 PAGE_TITLE = 'Beremiz Runtime Web Interface' |
33 |
39 |
34 xhtml_header = '''<?xml version="1.0" encoding="utf-8"?> |
40 xhtml_header = '''<?xml version="1.0" encoding="utf-8"?> |
35 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
41 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
36 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
42 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
37 ''' |
43 ''' |
46 def HMIinitialised(self, result): |
52 def HMIinitialised(self, result): |
47 self.initialised = True |
53 self.initialised = True |
48 |
54 |
49 def HMIinitialisation(self): |
55 def HMIinitialisation(self): |
50 self.HMIinitialised(None) |
56 self.HMIinitialised(None) |
51 |
|
52 |
57 |
53 class DefaultPLCStartedHMI(PLCHMI): |
58 class DefaultPLCStartedHMI(PLCHMI): |
54 docFactory = loaders.stan( |
59 docFactory = loaders.stan( |
55 tags.div(render=tags.directive('liveElement'))[ |
60 tags.div(render=tags.directive('liveElement'))[ |
56 tags.h1["PLC IS NOW STARTED"], |
61 tags.h1["PLC IS NOW STARTED"], |
117 |
122 |
118 def detachFragmentChildren(self): |
123 def detachFragmentChildren(self): |
119 for child in self.liveFragmentChildren[:]: |
124 for child in self.liveFragmentChildren[:]: |
120 child.detach() |
125 child.detach() |
121 |
126 |
|
127 lastKnownConfig = { |
|
128 'net': { |
|
129 'mode': 'DHCP', |
|
130 'IP': '192.168.1.42', |
|
131 'gateway': '192.168.1.1', |
|
132 'mask': '255.255.255.0', |
|
133 'DNS': '8.8.8.8'}, |
|
134 'wamp': {} |
|
135 } |
|
136 |
|
137 def defaultVal(category): |
|
138 def _defaultVal(ctx,argument): |
|
139 return lastKnownConfig[category].get(argument.name, None) |
|
140 return _defaultVal |
|
141 |
|
142 |
|
143 class ISettings(annotate.TypedInterface): |
|
144 def networkConfig( |
|
145 ctx = annotate.Context(), |
|
146 mode = annotate.Choice(["DHCP", "Static"], |
|
147 required=True, |
|
148 label=_("Configuration type"), |
|
149 default=defaultVal('net')), |
|
150 IP = annotate.String(label=_("IP address"),default=defaultVal('net')), |
|
151 gateway = annotate.String(label=_("Gateway address"), |
|
152 default=defaultVal('net')), |
|
153 mask = annotate.String(label=_("Network mask"),default=defaultVal('net')), |
|
154 DNS = annotate.String(label=_("DNS address"),default=defaultVal('net'))): |
|
155 pass |
|
156 |
|
157 networkConfig = annotate.autocallable(networkConfig, label=_("Network settings"), action="Set", ) |
|
158 |
|
159 |
|
160 class SettingsPage(rend.Page): |
|
161 # We deserve a slash |
|
162 addSlash = True |
|
163 |
|
164 # This makes webform_css url answer some default CSS |
|
165 child_webform_css = webform.defaultCSS |
|
166 |
|
167 implements(ISettings) |
|
168 |
|
169 def networkConfig(*args, **kwargs): |
|
170 # TODO do the settings |
|
171 print(kwargs) |
|
172 lastKnownConfig['net'] = kwargs |
|
173 |
|
174 docFactory = loaders.stan([tags.html[ |
|
175 tags.head[ |
|
176 tags.title[_("Beremiz Runtime Settings")], |
|
177 tags.link(rel='stylesheet', |
|
178 type='text/css', |
|
179 href=url.here.child("webform_css")) |
|
180 ], |
|
181 tags.body[ |
|
182 webform.renderForms() |
|
183 ]]]) |
|
184 |
|
185 |
122 |
186 |
123 class WebInterface(athena.LivePage): |
187 class WebInterface(athena.LivePage): |
124 |
188 |
125 docFactory = loaders.stan([tags.raw(xhtml_header), |
189 docFactory = loaders.stan([tags.raw(xhtml_header), |
126 tags.html(xmlns="http://www.w3.org/1999/xhtml")[ |
190 tags.html(xmlns="http://www.w3.org/1999/xhtml")[ |
127 tags.head(render=tags.directive('liveglue')), |
191 tags.head(render=tags.directive('liveglue'))[ |
|
192 tags.title[PAGE_TITLE], |
|
193 tags.link(rel='stylesheet', |
|
194 type='text/css', |
|
195 href=url.here.child("webform_css")) |
|
196 ], |
128 tags.body[ |
197 tags.body[ |
129 tags.div[ |
198 tags.div[ |
130 tags.div(render=tags.directive("MainPage")) |
199 tags.div(render=tags.directive("MainPage")), |
131 ]]]]) |
200 ]]]]) |
132 MainPage = MainPage() |
201 MainPage = MainPage() |
133 PLCHMI = PLCHMI |
202 PLCHMI = PLCHMI |
|
203 |
|
204 def child_settings(self, context): |
|
205 return SettingsPage() |
134 |
206 |
135 def __init__(self, plcState=False, *a, **kw): |
207 def __init__(self, plcState=False, *a, **kw): |
136 super(WebInterface, self).__init__(*a, **kw) |
208 super(WebInterface, self).__init__(*a, **kw) |
137 self.jsModules.mapping[u'WebInterface'] = paths.AbsNeighbourFile(__file__, 'webinterface.js') |
209 self.jsModules.mapping[u'WebInterface'] = paths.AbsNeighbourFile(__file__, 'webinterface.js') |
138 self.plcState = plcState |
210 self.plcState = plcState |
182 self.MainPage.resetHMI() |
254 self.MainPage.resetHMI() |
183 # print reason |
255 # print reason |
184 # print "We will be called back when the client disconnects" |
256 # print "We will be called back when the client disconnects" |
185 |
257 |
186 |
258 |
|
259 |
187 def RegisterWebsite(port): |
260 def RegisterWebsite(port): |
188 website = WebInterface() |
261 website = WebInterface() |
189 site = appserver.NevowSite(website) |
262 site = appserver.NevowSite(website) |
190 |
263 |
191 reactor.listenTCP(port, site) |
264 reactor.listenTCP(port, site) |
192 print(_('HTTP interface port :'), port) |
265 print(_('HTTP interface port :'), port) |
193 return website |
266 return website |
194 |
|
195 |
267 |
196 class statuslistener(object): |
268 class statuslistener(object): |
197 def __init__(self, site): |
269 def __init__(self, site): |
198 self.oldstate = None |
270 self.oldstate = None |
199 self.site = site |
271 self.site = site |