svghmi/svghmi_server.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 01 Aug 2023 21:04:00 +0200
changeset 3840 c2b6354f036f
parent 3750 f62625418bff
child 3864 3e434edfbfdf
permissions -rw-r--r--
Tests: Add minimal SVGHMI test
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
     3
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
     4
# This file is part of Beremiz
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
     5
# Copyright (C) 2019: Edouard TISSERANT
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
     6
# See COPYING file for copyrights details.
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
     7
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3661
diff changeset
     8
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
     9
import errno
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
    10
from threading import RLock, Timer
3647
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
    11
import os, time
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    12
2823
d631f8671c75 SVGHMI : add on Start, Stop and Watchdog command fields to configuration
Edouard Tisserant
parents: 2822
diff changeset
    13
try:
d631f8671c75 SVGHMI : add on Start, Stop and Watchdog command fields to configuration
Edouard Tisserant
parents: 2822
diff changeset
    14
    from runtime.spawn_subprocess import Popen
d631f8671c75 SVGHMI : add on Start, Stop and Watchdog command fields to configuration
Edouard Tisserant
parents: 2822
diff changeset
    15
except ImportError:
d631f8671c75 SVGHMI : add on Start, Stop and Watchdog command fields to configuration
Edouard Tisserant
parents: 2822
diff changeset
    16
    from subprocess import Popen
d631f8671c75 SVGHMI : add on Start, Stop and Watchdog command fields to configuration
Edouard Tisserant
parents: 2822
diff changeset
    17
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    18
from twisted.web.server import Site
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    19
from twisted.web.resource import Resource
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    20
from twisted.internet import reactor
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    21
from twisted.web.static import File
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    22
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    23
from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
    24
from autobahn.websocket.protocol import WebSocketProtocol
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    25
from autobahn.twisted.resource import  WebSocketResource
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    26
3647
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
    27
from runtime.loglevels import LogLevelsDict
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
    28
from runtime import GetPLCObjectSingleton
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
    29
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    30
max_svghmi_sessions = None
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
    31
svghmi_watchdog = None
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
    32
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    33
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    34
svghmi_wait = PLCBinary.svghmi_wait
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    35
svghmi_wait.restype = ctypes.c_int # error or 0
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    36
svghmi_wait.argtypes = []
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    37
3281
1fc4274de64e SVGHMI: Fixed halting problem when there is no session opened.
Edouard Tisserant
parents: 3278
diff changeset
    38
svghmi_continue_collect = ctypes.c_int.in_dll(PLCBinary, "svghmi_continue_collect")
1fc4274de64e SVGHMI: Fixed halting problem when there is no session opened.
Edouard Tisserant
parents: 3278
diff changeset
    39
2774
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
    40
svghmi_send_collect = PLCBinary.svghmi_send_collect
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
    41
svghmi_send_collect.restype = ctypes.c_int # error or 0
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
    42
svghmi_send_collect.argtypes = [
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    43
    ctypes.c_uint32,  # index
2774
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
    44
    ctypes.POINTER(ctypes.c_uint32),  # size
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
    45
    ctypes.POINTER(ctypes.c_void_p)]  # data ptr
2774
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
    46
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    47
svghmi_reset = PLCBinary.svghmi_reset
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    48
svghmi_reset.restype = ctypes.c_int # error or 0
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    49
svghmi_reset.argtypes = [
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    50
    ctypes.c_uint32]  # index
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
    51
2774
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
    52
svghmi_recv_dispatch = PLCBinary.svghmi_recv_dispatch
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
    53
svghmi_recv_dispatch.restype = ctypes.c_int # error or 0
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
    54
svghmi_recv_dispatch.argtypes = [
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    55
    ctypes.c_uint32,  # index
2788
2ed9ff826d03 SVGHMI: Work in progress. C side mostly implemented, neither built nor tested.
Edouard Tisserant
parents: 2779
diff changeset
    56
    ctypes.c_uint32,  # size
2779
75c6a31caca6 SVGHMI: Work In Progress : fixed pointer types in ctypes interface, cleaned up server startup and cleanup code, changed document type to XHTML, cleaner JS script : encapsulated in a function and in CDATA.
Edouard Tisserant
parents: 2777
diff changeset
    57
    ctypes.c_char_p]  # data ptr
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    58
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    59
class HMISessionMgr(object):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    60
    def __init__(self):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    61
        self.multiclient_sessions = set()
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    62
        self.watchdog_session = None
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    63
        self.session_count = 0
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    64
        self.lock = RLock()
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    65
        self.indexes = set()
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    66
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    67
    def next_index(self):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    68
        if self.indexes:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    69
            greatest = max(self.indexes)
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    70
            holes = set(range(greatest)) - self.indexes
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    71
            index = min(holes) if holes else greatest+1
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    72
        else:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    73
            index = 0
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    74
        self.indexes.add(index)
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    75
        return index
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    76
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    77
    def free_index(self, index):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    78
        self.indexes.remove(index)
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    79
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    80
    def register(self, session):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    81
        global max_svghmi_sessions
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    82
        with self.lock:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    83
            if session.is_watchdog_session:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    84
                # Creating a new watchdog session closes pre-existing one
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    85
                if self.watchdog_session is not None:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    86
                    self.unregister(self.watchdog_session)
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    87
                else:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    88
                    assert(self.session_count < max_svghmi_sessions)
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    89
                    self.session_count += 1
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    90
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    91
                self.watchdog_session = session
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    92
            else:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    93
                assert(self.session_count < max_svghmi_sessions)
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    94
                self.multiclient_sessions.add(session)
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    95
                self.session_count += 1
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    96
            session.session_index = self.next_index()
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    97
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    98
    def unregister(self, session):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
    99
        with self.lock:
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   100
            if session.is_watchdog_session :
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   101
                if self.watchdog_session != session:
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   102
                    return
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   103
                self.watchdog_session = None
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   104
            else:
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   105
                try:
3278
2bcfbea6a2a8 SVGHMI: Fixed typo on session manager unregister, leading to wrong count of sessions and then exceptions when creating more session than allowed in protocol options. Also added more safety check in protocol in case session would be missing.
Edouard Tisserant
parents: 3273
diff changeset
   106
                    self.multiclient_sessions.remove(session)
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   107
                except KeyError:
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   108
                    return
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   109
            self.free_index(session.session_index)
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   110
            self.session_count -= 1
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   111
        session.kill()
3273
08a5a019bed2 SVGHMI: finished multiclient support. Still needs more testing.
Edouard Tisserant
parents: 3272
diff changeset
   112
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   113
    def close_all(self):
3272
c8182e066545 SVGHMI: Fixed iterator in session manager. Getting closer to working multiclient, to be continued.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3271
diff changeset
   114
        for session in self.iter_sessions():
c8182e066545 SVGHMI: Fixed iterator in session manager. Getting closer to working multiclient, to be continued.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3271
diff changeset
   115
            self.unregister(session)
3273
08a5a019bed2 SVGHMI: finished multiclient support. Still needs more testing.
Edouard Tisserant
parents: 3272
diff changeset
   116
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   117
    def iter_sessions(self):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   118
        with self.lock:
3272
c8182e066545 SVGHMI: Fixed iterator in session manager. Getting closer to working multiclient, to be continued.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3271
diff changeset
   119
            lst = list(self.multiclient_sessions)
c8182e066545 SVGHMI: Fixed iterator in session manager. Getting closer to working multiclient, to be continued.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3271
diff changeset
   120
            if self.watchdog_session is not None:
c8182e066545 SVGHMI: Fixed iterator in session manager. Getting closer to working multiclient, to be continued.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3271
diff changeset
   121
                lst = [self.watchdog_session]+lst
c8182e066545 SVGHMI: Fixed iterator in session manager. Getting closer to working multiclient, to be continued.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3271
diff changeset
   122
            for nxt_session in lst:
c8182e066545 SVGHMI: Fixed iterator in session manager. Getting closer to working multiclient, to be continued.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3271
diff changeset
   123
                yield nxt_session
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   124
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   125
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   126
svghmi_session_manager = HMISessionMgr()
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   127
2774
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
   128
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   129
class HMISession(object):
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   130
    def __init__(self, protocol_instance):
2773
6e5e752372c8 SVGHMI: single session for now
Edouard Tisserant
parents: 2772
diff changeset
   131
        self.protocol_instance = protocol_instance
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   132
        self._session_index = None
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   133
        self.closed = False
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   134
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   135
    @property
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   136
    def is_watchdog_session(self):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   137
        return self.protocol_instance.has_watchdog
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   138
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   139
    @property
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   140
    def session_index(self):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   141
        return self._session_index
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   142
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   143
    @session_index.setter
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   144
    def session_index(self, value):
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   145
        self._session_index = value
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   146
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   147
    def reset(self):
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   148
        return svghmi_reset(self.session_index)
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   149
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   150
    def close(self):
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   151
        if self.closed: return
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   152
        self.protocol_instance.sendClose(WebSocketProtocol.CLOSE_STATUS_CODE_NORMAL)
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   153
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   154
    def notify_closed(self):
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   155
        self.closed = True
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   156
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   157
    def kill(self):
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   158
        self.close()
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   159
        self.reset()
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   160
2772
3f1dd8312710 SVGHMI: few fixes on serving
Edouard Tisserant
parents: 2771
diff changeset
   161
    def onMessage(self, msg):
2779
75c6a31caca6 SVGHMI: Work In Progress : fixed pointer types in ctypes interface, cleaned up server startup and cleanup code, changed document type to XHTML, cleaner JS script : encapsulated in a function and in CDATA.
Edouard Tisserant
parents: 2777
diff changeset
   162
        # pass message to the C side recieve_message()
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   163
        if self.closed: return
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   164
        return svghmi_recv_dispatch(self.session_index, len(msg), msg)
2777
cdf6584953a0 SVGHMI: WIP for python<->C data exchange : message from browser hit the C side.
Edouard Tisserant
parents: 2776
diff changeset
   165
2774
9857b4b0d979 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2773
diff changeset
   166
    def sendMessage(self, msg):
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   167
        if self.closed: return
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   168
        self.protocol_instance.sendMessage(msg, True)
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   169
        return 0
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   170
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   171
class Watchdog(object):
2831
6c9cfdbe94dc SVGHMI : watchdog is now taking an initial and interval duration as CTN fields.
Edouard Tisserant
parents: 2830
diff changeset
   172
    def __init__(self, initial_timeout, interval, callback):
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   173
        self._callback = callback
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   174
        self.lock = RLock()
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   175
        self.initial_timeout = initial_timeout
2831
6c9cfdbe94dc SVGHMI : watchdog is now taking an initial and interval duration as CTN fields.
Edouard Tisserant
parents: 2830
diff changeset
   176
        self.interval = interval
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   177
        self.callback = callback
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   178
        with self.lock:
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   179
            self._start()
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   180
2831
6c9cfdbe94dc SVGHMI : watchdog is now taking an initial and interval duration as CTN fields.
Edouard Tisserant
parents: 2830
diff changeset
   181
    def _start(self, rearm=False):
6c9cfdbe94dc SVGHMI : watchdog is now taking an initial and interval duration as CTN fields.
Edouard Tisserant
parents: 2830
diff changeset
   182
        duration = self.interval if rearm else self.initial_timeout
6c9cfdbe94dc SVGHMI : watchdog is now taking an initial and interval duration as CTN fields.
Edouard Tisserant
parents: 2830
diff changeset
   183
        if duration:
6c9cfdbe94dc SVGHMI : watchdog is now taking an initial and interval duration as CTN fields.
Edouard Tisserant
parents: 2830
diff changeset
   184
            self.timer = Timer(duration, self.trigger)
6c9cfdbe94dc SVGHMI : watchdog is now taking an initial and interval duration as CTN fields.
Edouard Tisserant
parents: 2830
diff changeset
   185
            self.timer.start()
2835
bc666f020ab3 SVGHMI : fix watchdog exception when timeout null
Edouard Tisserant
parents: 2832
diff changeset
   186
        else:
bc666f020ab3 SVGHMI : fix watchdog exception when timeout null
Edouard Tisserant
parents: 2832
diff changeset
   187
            self.timer = None
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   188
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   189
    def _stop(self):
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   190
        if self.timer is not None:
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   191
            self.timer.cancel()
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   192
            self.timer = None
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   193
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   194
    def cancel(self):
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   195
        with self.lock:
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   196
            self._stop()
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   197
2832
e9ba4dee6ffb SVGHMI : wait for initial timeout after watchdog is triggered, since it generaly induces HMI restart
Edouard Tisserant
parents: 2831
diff changeset
   198
    def feed(self, rearm=True):
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   199
        with self.lock:
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   200
            self._stop()
2832
e9ba4dee6ffb SVGHMI : wait for initial timeout after watchdog is triggered, since it generaly induces HMI restart
Edouard Tisserant
parents: 2831
diff changeset
   201
            self._start(rearm)
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   202
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   203
    def trigger(self):
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   204
        self._callback()
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   205
        # Don't repeat trigger periodically
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   206
        # # wait for initial timeout on re-start
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   207
        # self.feed(rearm=False)
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   208
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   209
class HMIProtocol(WebSocketServerProtocol):
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   210
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   211
    def __init__(self, *args, **kwargs):
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   212
        self._hmi_session = None
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   213
        self.has_watchdog = False
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   214
        WebSocketServerProtocol.__init__(self, *args, **kwargs)
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   215
3268
d22782b9591f SVGHMI: Added a way to distinguish watchdog-enabled HMI from multi-client HMI in URL.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3200
diff changeset
   216
    def onConnect(self, request):
d22782b9591f SVGHMI: Added a way to distinguish watchdog-enabled HMI from multi-client HMI in URL.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3200
diff changeset
   217
        self.has_watchdog = request.params.get("mode", [None])[0] == "watchdog"
d22782b9591f SVGHMI: Added a way to distinguish watchdog-enabled HMI from multi-client HMI in URL.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3200
diff changeset
   218
        return WebSocketServerProtocol.onConnect(self, request)
d22782b9591f SVGHMI: Added a way to distinguish watchdog-enabled HMI from multi-client HMI in URL.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3200
diff changeset
   219
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   220
    def onOpen(self):
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   221
        global svghmi_session_manager
2799
f5da343b9b63 SVGHMI: Many fixes. Subscriptions to HMItree seems to be working, and dispatch function is called in JS with good data. Bidirectional communication now really working.
Edouard Tisserant
parents: 2798
diff changeset
   222
        assert(self._hmi_session is None)
3278
2bcfbea6a2a8 SVGHMI: Fixed typo on session manager unregister, leading to wrong count of sessions and then exceptions when creating more session than allowed in protocol options. Also added more safety check in protocol in case session would be missing.
Edouard Tisserant
parents: 3273
diff changeset
   223
        _hmi_session = HMISession(self)
2bcfbea6a2a8 SVGHMI: Fixed typo on session manager unregister, leading to wrong count of sessions and then exceptions when creating more session than allowed in protocol options. Also added more safety check in protocol in case session would be missing.
Edouard Tisserant
parents: 3273
diff changeset
   224
        registered = svghmi_session_manager.register(_hmi_session)
2bcfbea6a2a8 SVGHMI: Fixed typo on session manager unregister, leading to wrong count of sessions and then exceptions when creating more session than allowed in protocol options. Also added more safety check in protocol in case session would be missing.
Edouard Tisserant
parents: 3273
diff changeset
   225
        self._hmi_session = _hmi_session
3661
62860665fa94 SVGHMI: server systematically resets session on connect, rather than waiting for client to ask for it
Edouard Tisserant
parents: 3647
diff changeset
   226
        self._hmi_session.reset()
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   227
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   228
    def onClose(self, wasClean, code, reason):
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   229
        global svghmi_session_manager
3278
2bcfbea6a2a8 SVGHMI: Fixed typo on session manager unregister, leading to wrong count of sessions and then exceptions when creating more session than allowed in protocol options. Also added more safety check in protocol in case session would be missing.
Edouard Tisserant
parents: 3273
diff changeset
   230
        if self._hmi_session is None : return
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   231
        self._hmi_session.notify_closed()
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   232
        svghmi_session_manager.unregister(self._hmi_session)
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   233
        self._hmi_session = None
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   234
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   235
    def onMessage(self, msg, isBinary):
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   236
        global svghmi_watchdog
3278
2bcfbea6a2a8 SVGHMI: Fixed typo on session manager unregister, leading to wrong count of sessions and then exceptions when creating more session than allowed in protocol options. Also added more safety check in protocol in case session would be missing.
Edouard Tisserant
parents: 3273
diff changeset
   237
        if self._hmi_session is None : return
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   238
        result = self._hmi_session.onMessage(msg)
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   239
        if result == 1 and self.has_watchdog:  # was heartbeat
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   240
            if svghmi_watchdog is not None:
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   241
                svghmi_watchdog.feed()
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   242
2779
75c6a31caca6 SVGHMI: Work In Progress : fixed pointer types in ctypes interface, cleaned up server startup and cleanup code, changed document type to XHTML, cleaner JS script : encapsulated in a function and in CDATA.
Edouard Tisserant
parents: 2777
diff changeset
   243
class HMIWebSocketServerFactory(WebSocketServerFactory):
75c6a31caca6 SVGHMI: Work In Progress : fixed pointer types in ctypes interface, cleaned up server startup and cleanup code, changed document type to XHTML, cleaner JS script : encapsulated in a function and in CDATA.
Edouard Tisserant
parents: 2777
diff changeset
   244
    protocol = HMIProtocol
75c6a31caca6 SVGHMI: Work In Progress : fixed pointer types in ctypes interface, cleaned up server startup and cleanup code, changed document type to XHTML, cleaner JS script : encapsulated in a function and in CDATA.
Edouard Tisserant
parents: 2777
diff changeset
   245
3269
5d174cdf4d98 SVGHMI: More configuration parameters : network interface, TCP port, URL path and watchdog enabling.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3268
diff changeset
   246
svghmi_servers = {}
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   247
svghmi_send_thread = None
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   248
3310
6a6d3c318a6b Fix SVGHMI exception when running on Windows, missing ENODATA in errno.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3284
diff changeset
   249
# python's errno on windows seems to have no ENODATA
6a6d3c318a6b Fix SVGHMI exception when running on Windows, missing ENODATA in errno.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3284
diff changeset
   250
ENODATA = errno.ENODATA if hasattr(errno,"ENODATA") else None
6a6d3c318a6b Fix SVGHMI exception when running on Windows, missing ENODATA in errno.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3284
diff changeset
   251
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   252
def SendThreadProc():
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   253
    global svghmi_session_manager
2819
3b99c908f43b SVGHMI: change collect/send thread looping condition to fix infinite loop in some cases
Edouard Tisserant
parents: 2799
diff changeset
   254
    size = ctypes.c_uint32()
3b99c908f43b SVGHMI: change collect/send thread looping condition to fix infinite loop in some cases
Edouard Tisserant
parents: 2799
diff changeset
   255
    ptr = ctypes.c_void_p()
3b99c908f43b SVGHMI: change collect/send thread looping condition to fix infinite loop in some cases
Edouard Tisserant
parents: 2799
diff changeset
   256
    res = 0
3281
1fc4274de64e SVGHMI: Fixed halting problem when there is no session opened.
Edouard Tisserant
parents: 3278
diff changeset
   257
    while svghmi_continue_collect:
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   258
        svghmi_wait()
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   259
        for svghmi_session in svghmi_session_manager.iter_sessions():
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   260
            res = svghmi_send_collect(
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   261
                svghmi_session.session_index,
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   262
                ctypes.byref(size), ctypes.byref(ptr))
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   263
            if res == 0:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   264
                svghmi_session.sendMessage(
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   265
                    ctypes.string_at(ptr.value,size.value))
3310
6a6d3c318a6b Fix SVGHMI exception when running on Windows, missing ENODATA in errno.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3284
diff changeset
   266
            elif res == ENODATA:
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   267
                # this happens when there is no data after wakeup
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   268
                # because of hmi data refresh period longer than
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   269
                # PLC common ticktime
3271
561dbd1e3e04 SVGHMI: Fixing last commit's multiclient implementation, in case of watchdog. To be continued, since multiclient still fail...
Edouard Tisserant
parents: 3270
diff changeset
   270
                pass
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   271
            else:
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   272
                # this happens when finishing
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   273
                break
2776
246ae685ab65 SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2775
diff changeset
   274
3284
55e69df85fe1 SVGHMI: add arguments to pass to ressource factory when attaching rerssource to svghmi root
Edouard Tisserant
parents: 3281
diff changeset
   275
def AddPathToSVGHMIServers(path, factory, *args, **kwargs):
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3661
diff changeset
   276
    for k,v in svghmi_servers.items():
3269
5d174cdf4d98 SVGHMI: More configuration parameters : network interface, TCP port, URL path and watchdog enabling.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3268
diff changeset
   277
        svghmi_root, svghmi_listener, path_list = v
3284
55e69df85fe1 SVGHMI: add arguments to pass to ressource factory when attaching rerssource to svghmi root
Edouard Tisserant
parents: 3281
diff changeset
   278
        svghmi_root.putChild(path, factory(*args, **kwargs))
2779
75c6a31caca6 SVGHMI: Work In Progress : fixed pointer types in ctypes interface, cleaned up server startup and cleanup code, changed document type to XHTML, cleaner JS script : encapsulated in a function and in CDATA.
Edouard Tisserant
parents: 2777
diff changeset
   279
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   280
# Called by PLCObject at start
2993
b76f303ffce6 Python Runtime: order of execution of extension's init() and cleanup() now reflects order of appearance of extensions in configuration tree.
Edouard Tisserant
parents: 2835
diff changeset
   281
def _runtime_00_svghmi_start():
3269
5d174cdf4d98 SVGHMI: More configuration parameters : network interface, TCP port, URL path and watchdog enabling.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3268
diff changeset
   282
    global svghmi_send_thread
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   283
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   284
    # start a thread that call the C part of SVGHMI
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   285
    svghmi_send_thread = Thread(target=SendThreadProc, name="SVGHMI Send")
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   286
    svghmi_send_thread.start()
2771
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   287
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   288
361366b891ca WIP on svghmi, now builds and runs. HTTP serving + WS transport ready, missing actual data to transmit and thread to collect it.
Edouard Tisserant
parents:
diff changeset
   289
# Called by PLCObject at stop
2993
b76f303ffce6 Python Runtime: order of execution of extension's init() and cleanup() now reflects order of appearance of extensions in configuration tree.
Edouard Tisserant
parents: 2835
diff changeset
   290
def _runtime_00_svghmi_stop():
3269
5d174cdf4d98 SVGHMI: More configuration parameters : network interface, TCP port, URL path and watchdog enabling.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3268
diff changeset
   291
    global svghmi_send_thread, svghmi_session
2822
9101a72a1da0 SVGHMI: added a watchdog. To ensure that the whole chain is checked, watchdog use a periodic echo of a hearteat variable. JS client code systematically register /HEARTBEAT at 1s update freq, and reacts on updates of /HEARTBEAT by systematically incrementing it. C code catch /HEARTBEAT update and feeds python-implemented watchdog. For now, watchdog does nothing when tiggered
Edouard Tisserant
parents: 2819
diff changeset
   292
3270
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   293
    svghmi_session_manager.close_all()
38f7122ccbf9 SVGHMI: Implemented multiserver+multiclient, but only tested with single client and single server for now. To be continued...
Edouard Tisserant
parents: 3269
diff changeset
   294
2775
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   295
    # plc cleanup calls svghmi_(locstring)_cleanup and unlocks send thread
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   296
    svghmi_send_thread.join()
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   297
    svghmi_send_thread = None
3b93409ba22c SVGHMI: WIP for python<->C data exchange
Edouard Tisserant
parents: 2774
diff changeset
   298
2830
15d7bd79d9e8 SVGHMI : avoid caching of xhtml file by forcing cache-control header in http response.
Edouard Tisserant
parents: 2823
diff changeset
   299
15d7bd79d9e8 SVGHMI : avoid caching of xhtml file by forcing cache-control header in http response.
Edouard Tisserant
parents: 2823
diff changeset
   300
class NoCacheFile(File):
15d7bd79d9e8 SVGHMI : avoid caching of xhtml file by forcing cache-control header in http response.
Edouard Tisserant
parents: 2823
diff changeset
   301
    def render_GET(self, request):
15d7bd79d9e8 SVGHMI : avoid caching of xhtml file by forcing cache-control header in http response.
Edouard Tisserant
parents: 2823
diff changeset
   302
        request.setHeader(b"Cache-Control", b"no-cache, no-store")
15d7bd79d9e8 SVGHMI : avoid caching of xhtml file by forcing cache-control header in http response.
Edouard Tisserant
parents: 2823
diff changeset
   303
        return File.render_GET(self, request)
15d7bd79d9e8 SVGHMI : avoid caching of xhtml file by forcing cache-control header in http response.
Edouard Tisserant
parents: 2823
diff changeset
   304
    render_HEAD = render_GET
15d7bd79d9e8 SVGHMI : avoid caching of xhtml file by forcing cache-control header in http response.
Edouard Tisserant
parents: 2823
diff changeset
   305
15d7bd79d9e8 SVGHMI : avoid caching of xhtml file by forcing cache-control header in http response.
Edouard Tisserant
parents: 2823
diff changeset
   306
3647
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   307
def waitpid_timeout(proc, helpstr="", timeout = 3):
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   308
    if proc is None:
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   309
        return
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   310
    def waitpid_timeout_loop(pid=proc.pid, timeout = timeout):
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   311
        try:
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   312
            while os.waitpid(pid,os.WNOHANG) == (0,0):
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   313
                time.sleep(1)
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   314
                timeout = timeout - 1
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   315
                if not timeout:
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   316
                    GetPLCObjectSingleton().LogMessage(
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   317
                        LogLevelsDict["WARNING"], 
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   318
                        "Timeout waiting for {} PID: {}".format(helpstr, str(pid)))
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   319
                    break
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   320
        except OSError:
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   321
            # workaround exception "OSError: [Errno 10] No child processes"
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   322
            pass
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   323
    Thread(target=waitpid_timeout_loop, name="Zombie hunter").start()
7c427418396f SVGHMI: prevent browser and watchdog commands to become zombie once finished.
Edouard Tisserant
parents: 3310
diff changeset
   324