Edouard@2983: <?xml version='1.0' encoding='utf-8'?>
Edouard@2983: <PyFile xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Edouard@2983:   <variables>
Edouard@3034:     <variable name="AlarmNotify" type="HMI_INT"/>
Edouard@3046:     <variable name="SendAlarm" type="HMI_INT" onchange="TriggerAlarm"/>
Edouard@3036:     <variable name="AlarmText" type="HMI_STRING" initial="'POS'"/>
Edouard@3036:     <variable name="AlarmStatus" type="HMI_STRING" initial="'alarm'"/>
Edouard@2983:   </variables>
Edouard@2983:   <globals>
Edouard@2983:     <xhtml:p><![CDATA[
edouard@2994: from twisted.web.resource import Resource
Edouard@3048: import json, time, random, collections
Edouard@3036: 
Edouard@3036: Alarms = []
Edouard@3048: AlarmIndex = {}
Edouard@3048: lastid = 0
Edouard@3036: 
Edouard@3036: def TriggerAlarm(changed_var_name):
Edouard@3048:     global Alarms, lastid
Edouard@3048:     new_entry = [time.time(), PLCGlobals.AlarmText, PLCGlobals.AlarmStatus, lastid]
Edouard@3048:     Alarms.append(new_entry)
Edouard@3048:     AlarmIndex[lastid] = new_entry
Edouard@3048:     lastid = lastid + 1
Edouard@3038:     PLCGlobals.AlarmNotify = random.randint(0, 4294967296)
edouard@2994: 
edouard@2994: class AlarmJsonResource(Resource):
edouard@2994:     def render_GET(self, request):
edouard@2994:         return ''
edouard@2994: 
edouard@2994:     def render_POST(self, request):
Edouard@3036:         newstr = request.content.getvalue()
Edouard@3036:         newdata = json.loads(newstr)
Edouard@3036:         args = newdata[u'args']
Edouard@3065:         range_feedback = newdata[u'range']
Edouard@3065:         slider_position = newdata[u'position']
Edouard@3036:         visible = newdata[u'visible']
edouard@3069:         extra = newdata[u'extra']
Edouard@3048:         options = newdata[u'options']
Edouard@3048: 
Edouard@3048:         if len(options) == 2 :
Edouard@3048:             action, alarmid = options
Edouard@3048:             if action == "onClick[acknowledge]":
Edouard@3048:                 AlarmIndex[int(alarmid)][2] = "ack"
Edouard@3048: 
edouard@3069:         answer = self.renderTable(range_feedback, slider_position, visible, extra)
Edouard@3036:         janswer = json.dumps(answer)
Edouard@3036:         return janswer
edouard@2994: 
edouard@3069:     def renderTable(self, old_range, old_position, visible, extra):
edouard@3069:         if len(extra) > 0 and extra[0] != "":
edouard@3069:             fAlarms = [alrm for alrm in Alarms if alrm[1].find(extra[0])!=-1]
edouard@3069:         else:
edouard@3069:             fAlarms = Alarms
edouard@3069:         new_range = len(fAlarms)
Edouard@3036:         delta = new_range - visible
Edouard@3036:         new_position = 0 if delta <= 0 else delta if old_position > delta else old_position
Edouard@3036:         new_visible = new_range if delta <= 0 else visible
Edouard@3036:         
Edouard@3036:         visible_alarms = []
edouard@3069:         for ts, text, status, alarmid in fAlarms[new_position:new_position + new_visible]:
Edouard@3036:             visible_alarms.append({
Edouard@3036:                 "time": time.ctime(ts),
Edouard@3036:                 "text": text, # TODO translate text
Edouard@3048:                 "status": status,
Edouard@3048:                 "alarmid": alarmid
Edouard@3036:             })
Edouard@2983: 
Edouard@3036:         return new_range, new_position, visible_alarms
Edouard@3036: 
Edouard@2983: 
Edouard@2983: ]]></xhtml:p>
Edouard@2983:   </globals>
Edouard@2983:   <init>
Edouard@2983:     <xhtml:p><![CDATA[
Edouard@2983: ]]></xhtml:p>
Edouard@2983:   </init>
Edouard@2983:   <cleanup>
Edouard@2983:     <xhtml:p><![CDATA[
Edouard@2983: ]]></xhtml:p>
Edouard@2983:   </cleanup>
Edouard@2983:   <start>
Edouard@2983:     <xhtml:p><![CDATA[
edouard@2994: 
edouard@2994: svghmi_root.putChild("alarms", AlarmJsonResource())
edouard@2994: 
edouard@2994: 
Edouard@2983: ]]></xhtml:p>
Edouard@2983:   </start>
Edouard@2983:   <stop>
Edouard@2983:     <xhtml:p><![CDATA[
Edouard@2983: ]]></xhtml:p>
Edouard@2983:   </stop>
Edouard@2983: </PyFile>