exemples/svghmi_foreach/py_ext_0@py_ext/pyfile.xml
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 18 Nov 2024 13:52:40 +0100
changeset 4039 9ff455817691
parent 4026 a3cf9f635952
permissions -rw-r--r--
IDE: refactor discovery panel / zeroconf

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