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