runtime/ServicePublisher.py
author Edouard Tisserant <edouard@beremiz.fr>
Thu, 05 Dec 2024 13:56:59 +0100
changeset 4060 d2f5eb3c7d6e
parent 4040 ea2db66b185f
permissions -rw-r--r--
py_ext: fix CSV Writer

fix POU logic :
- SAVE is a BOOL
- invocation of py_eval on rising edge of SAVE
- remove save python argument

fix python:
- use no encoding for file open (python2)
- re-use detected dialect if any
- use no "rt+" and truncate since no need to re-sniff dialect for output file
- return "OK" instead of "#SUCCESS", preventing POU logic to ACK result
- support creating new line if writing just after last line
- support appending data on short rows

fix example:
- use a HMI:Button to trigger CSV write instead of HMI:Input +1
- reload CSVs on on each new CSV opened in file browser
- add display of CSV write output
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     1
#!/usr/bin/env python
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     2
# -*- coding: utf-8 -*-
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     3
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
     4
# This file is part of Beremiz runtime.
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     5
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
     6
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     7
#
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
     8
# See COPYING.Runtime file for copyrights details.
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
     9
#
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    10
# This library is free software; you can redistribute it and/or
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    11
# modify it under the terms of the GNU Lesser General Public
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    12
# License as published by the Free Software Foundation; either
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    13
# version 2.1 of the License, or (at your option) any later version.
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    14
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    15
# This library is distributed in the hope that it will be useful,
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 763
diff changeset
    16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    18
# Lesser General Public License for more details.
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    19
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    20
# You should have received a copy of the GNU Lesser General Public
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    21
# License along with this library; if not, write to the Free Software
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    22
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    23
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
    24
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 2492
diff changeset
    25
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 2492
diff changeset
    26
1732
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    27
import socket
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    28
import threading
1830
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    29
import zeroconf
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    30
763
c1104099c151 Now, PYRO:// locations also accept MDNS service names
Edouard Tisserant
parents: 726
diff changeset
    31
2477
4866c01e7428 Added support to PYROS in Zeroconf discovery and publish. For now, PYRO://mdnsname._tcp.local. URI are broken, and discovery panel generate IP based URI by default.
Edouard Tisserant
parents: 2320
diff changeset
    32
service_type = '_Beremiz._tcp.local.'
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    33
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2477
diff changeset
    34
1831
56b48961cc68 fix (old-style-class) Old-style class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1830
diff changeset
    35
class ServicePublisher(object):
2477
4866c01e7428 Added support to PYROS in Zeroconf discovery and publish. For now, PYRO://mdnsname._tcp.local. URI are broken, and discovery panel generate IP based URI by default.
Edouard Tisserant
parents: 2320
diff changeset
    36
    def __init__(self, protocol):
4866c01e7428 Added support to PYROS in Zeroconf discovery and publish. For now, PYRO://mdnsname._tcp.local. URI are broken, and discovery panel generate IP based URI by default.
Edouard Tisserant
parents: 2320
diff changeset
    37
        self.serviceproperties = {
4866c01e7428 Added support to PYROS in Zeroconf discovery and publish. For now, PYRO://mdnsname._tcp.local. URI are broken, and discovery panel generate IP based URI by default.
Edouard Tisserant
parents: 2320
diff changeset
    38
            'description': 'Beremiz remote PLC',
4866c01e7428 Added support to PYROS in Zeroconf discovery and publish. For now, PYRO://mdnsname._tcp.local. URI are broken, and discovery panel generate IP based URI by default.
Edouard Tisserant
parents: 2320
diff changeset
    39
            'protocol': protocol
4866c01e7428 Added support to PYROS in Zeroconf discovery and publish. For now, PYRO://mdnsname._tcp.local. URI are broken, and discovery panel generate IP based URI by default.
Edouard Tisserant
parents: 2320
diff changeset
    40
        }
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    41
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    42
        self.name = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    43
        self.ip_32b = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    44
        self.port = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    45
        self.server = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    46
        self.service_name = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    47
        self.retrytimer = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    48
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    49
    def RegisterService(self, name, ip, port):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    50
        try:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    51
            self._RegisterService(name, ip, port)
4037
4127c64df073 python runtime: update ServicePublisher to current Zeroconf API
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    52
        except Exception as e:
4127c64df073 python runtime: update ServicePublisher to current Zeroconf API
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    53
            print(f"Failed to register service ({str(e)}), retrying in 2 seconds")
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    54
            self.retrytimer = threading.Timer(2, self.RegisterService, [name, ip, port])
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    55
            self.retrytimer.start()
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    56
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    57
    def _RegisterService(self, name, ip, port):
2477
4866c01e7428 Added support to PYROS in Zeroconf discovery and publish. For now, PYRO://mdnsname._tcp.local. URI are broken, and discovery panel generate IP based URI by default.
Edouard Tisserant
parents: 2320
diff changeset
    58
        self.service_name = '%s.%s' % (name, service_type)
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    59
        self.name = name
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    60
        self.port = port
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    61
2320
dd959601e67a Force Zeroconf to listen and answer only on interface specified with '-i' command line argument.
Edouard Tisserant
parents: 1881
diff changeset
    62
        if ip == "0.0.0.0":
dd959601e67a Force Zeroconf to listen and answer only on interface specified with '-i' command line argument.
Edouard Tisserant
parents: 1881
diff changeset
    63
            print("MDNS brodcasted on all interfaces")
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2477
diff changeset
    64
            interfaces = zeroconf.InterfaceChoice.All
2320
dd959601e67a Force Zeroconf to listen and answer only on interface specified with '-i' command line argument.
Edouard Tisserant
parents: 1881
diff changeset
    65
            ip = self.gethostaddr()
dd959601e67a Force Zeroconf to listen and answer only on interface specified with '-i' command line argument.
Edouard Tisserant
parents: 1881
diff changeset
    66
        else:
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2477
diff changeset
    67
            interfaces = [ip]
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    68
2320
dd959601e67a Force Zeroconf to listen and answer only on interface specified with '-i' command line argument.
Edouard Tisserant
parents: 1881
diff changeset
    69
        self.server = zeroconf.Zeroconf(interfaces=interfaces)
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2477
diff changeset
    70
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
    71
        print("MDNS brodcasted service address :" + ip)
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    72
        self.ip_32b = socket.inet_aton(ip)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    73
4040
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    74
        self.service_info = zeroconf.ServiceInfo(
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    75
            service_type,
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    76
            self.service_name,
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    77
            self.port,
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    78
            addresses=[self.ip_32b],
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    79
            properties=self.serviceproperties)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    80
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    81
        self.server.register_service(self.service_info)
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    82
        self.retrytimer = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    83
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    84
    def UnRegisterService(self):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    85
        if self.retrytimer is not None:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    86
            self.retrytimer.cancel()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    87
1830
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    88
        if self.server is not None:
4040
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4037
diff changeset
    89
            self.server.unregister_service(self.service_info)
1830
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    90
            self.server.close()
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    91
            self.server = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    92
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
    93
    def gethostaddr(self, dst='224.0.1.41'):
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    94
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    95
        try:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    96
            s.connect((dst, 7))
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
    97
            (host, _port) = s.getsockname()
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    98
            s.close()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    99
            if host != '0.0.0.0':
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   100
                return host
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
   101
        except Exception:
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   102
            pass
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   103
        return socket.gethostbyname(socket.gethostname())