exemples/svghmi_foreach/py_ext_0@py_ext/pyfile.xml
changeset 4026 a3cf9f635952
equal deleted inserted replaced
4025:92b3701fceed 4026:a3cf9f635952
       
     1 <?xml version='1.0' encoding='utf-8'?>
       
     2 <PyFile xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       
     3   <variables>
       
     4     <variable name="AlarmNotify" type="HMI_INT"/>
       
     5     <variable name="SendAlarm" type="HMI_INT" onchange="TriggerAlarm"/>
       
     6     <variable name="AlarmText" type="HMI_STRING" initial="'POS'"/>
       
     7     <variable name="AlarmStatus" type="HMI_STRING" initial="'alarm'"/>
       
     8   </variables>
       
     9   <globals>
       
    10     <xhtml:p><![CDATA[
       
    11 from twisted.web.resource import Resource
       
    12 import json, time, random, collections
       
    13 
       
    14 Alarms = []
       
    15 AlarmIndex = {}
       
    16 lastid = 0
       
    17 
       
    18 def TriggerAlarm(changed_var_name):
       
    19     global Alarms, lastid
       
    20     new_entry = [time.time(), PLCGlobals.AlarmText, PLCGlobals.AlarmStatus, lastid]
       
    21     Alarms.append(new_entry)
       
    22     AlarmIndex[lastid] = new_entry
       
    23     lastid = lastid + 1
       
    24     PLCGlobals.AlarmNotify = random.randint(0, 4294967296)
       
    25 
       
    26 class AlarmJsonResource(Resource):
       
    27     def render_GET(self, request):
       
    28         return ''
       
    29 
       
    30     def render_POST(self, request):
       
    31         newstr = request.content.getvalue()
       
    32         newdata = json.loads(newstr)
       
    33         args = newdata[u'args']
       
    34         range_feedback = newdata[u'range']
       
    35         slider_position = newdata[u'position']
       
    36         visible = newdata[u'visible']
       
    37         extra = newdata[u'extra']
       
    38         options = newdata[u'options']
       
    39 
       
    40         if len(options) == 1 :
       
    41             action, = options
       
    42             if action == "action_reset":
       
    43                 del Alarms[:]
       
    44                 AlarmIndex.clear()
       
    45         elif len(options) == 2 :
       
    46             action, alarmid = options
       
    47             if action == "onClick[acknowledge]":
       
    48                 AlarmIndex[int(alarmid)][2] = "ack"
       
    49 
       
    50         answer = self.renderTable(range_feedback, slider_position, visible, extra)
       
    51         janswer = json.dumps(answer)
       
    52         return janswer
       
    53 
       
    54     def renderTable(self, old_range, old_position, visible, extra):
       
    55         if len(extra) > 0 and extra[0] != "":
       
    56             fAlarms = [alrm for alrm in Alarms if alrm[1].find(extra[0])!=-1]
       
    57         else:
       
    58             fAlarms = Alarms[:]
       
    59         fAlarms.reverse()
       
    60         new_range = len(fAlarms)
       
    61         delta = new_range - visible
       
    62         new_position = 0 if delta <= 0 else delta if old_position > delta else old_position
       
    63         new_visible = new_range if delta <= 0 else visible
       
    64         
       
    65         visible_alarms = []
       
    66         for ts, text, status, alarmid in fAlarms[new_position:new_position + new_visible]:
       
    67             visible_alarms.append({
       
    68                 "time": time.ctime(ts),
       
    69                 "text": text, # TODO translate text
       
    70                 "status": status,
       
    71                 "alarmid": alarmid
       
    72             })
       
    73 
       
    74         return new_range, new_position, visible_alarms
       
    75 
       
    76 
       
    77 ]]></xhtml:p>
       
    78   </globals>
       
    79   <init>
       
    80     <xhtml:p><![CDATA[
       
    81 ]]></xhtml:p>
       
    82   </init>
       
    83   <cleanup>
       
    84     <xhtml:p><![CDATA[
       
    85 ]]></xhtml:p>
       
    86   </cleanup>
       
    87   <start>
       
    88     <xhtml:p><![CDATA[
       
    89 
       
    90 AddPathToSVGHMIServers("alarms", AlarmJsonResource)
       
    91 
       
    92 
       
    93 ]]></xhtml:p>
       
    94   </start>
       
    95   <stop>
       
    96     <xhtml:p><![CDATA[
       
    97 ]]></xhtml:p>
       
    98   </stop>
       
    99 </PyFile>