Runtime/PLCObject : move call to PreStartPLC right after call to the "start" method of all python extensions, and then just before the start of the python loop executing python_* blocks.
<?xml version='1.0' encoding='utf-8'?>
<PyFile xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<variables>
<variable name="AlarmNotify" type="HMI_INT"/>
<variable name="SendAlarm" type="HMI_INT" onchange="TriggerAlarm"/>
<variable name="AlarmText" type="HMI_STRING" initial="'POS'"/>
<variable name="AlarmStatus" type="HMI_STRING" initial="'alarm'"/>
</variables>
<globals>
<xhtml:p><![CDATA[
from twisted.web.resource import Resource
import json, time, random, collections
Alarms = []
AlarmIndex = {}
lastid = 0
def TriggerAlarm(changed_var_name):
global Alarms, lastid
new_entry = [time.time(), PLCGlobals.AlarmText, PLCGlobals.AlarmStatus, lastid]
Alarms.append(new_entry)
AlarmIndex[lastid] = new_entry
lastid = lastid + 1
PLCGlobals.AlarmNotify = random.randint(0, 4294967296)
class AlarmJsonResource(Resource):
def render_GET(self, request):
return ''
def render_POST(self, request):
newstr = request.content.getvalue()
newdata = json.loads(newstr)
vars = newdata[u'vars']
args = newdata[u'args']
visible = newdata[u'visible']
options = newdata[u'options']
if len(options) == 2 :
action, alarmid = options
if action == "onClick[acknowledge]":
AlarmIndex[int(alarmid)][2] = "ack"
svars = (vars + [0,0])[:3]
range_feedback = svars[1]
slider_position = svars[2]
answer = self.renderTable(range_feedback, slider_position, visible, *(args+svars[3:]))
janswer = json.dumps(answer)
return janswer
def renderTable(self, old_range, old_position, visible, *options):
new_range = len(Alarms)
delta = new_range - visible
new_position = 0 if delta <= 0 else delta if old_position > delta else old_position
new_visible = new_range if delta <= 0 else visible
visible_alarms = []
for ts, text, status, alarmid in Alarms[new_position:new_position + new_visible]:
visible_alarms.append({
"time": time.ctime(ts),
"text": text, # TODO translate text
"status": status,
"alarmid": alarmid
})
return new_range, new_position, visible_alarms
]]></xhtml:p>
</globals>
<init>
<xhtml:p><![CDATA[
]]></xhtml:p>
</init>
<cleanup>
<xhtml:p><![CDATA[
]]></xhtml:p>
</cleanup>
<start>
<xhtml:p><![CDATA[
svghmi_root.putChild("alarms", AlarmJsonResource())
]]></xhtml:p>
</start>
<stop>
<xhtml:p><![CDATA[
]]></xhtml:p>
</stop>
</PyFile>