runtime/ServicePublisher.py
author Edouard Tisserant <edouard@beremiz.fr>
Tue, 23 Jul 2024 17:05:59 +0200
changeset 3999 1479acf750e2
parent 3750 f62625418bff
child 4037 4127c64df073
permissions -rw-r--r--
MQTT: WIP fix modified status not being set when adding and modifying topics or attributes.

Fix loading of CSV that was not applying model types, and that not checking conformance either.
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):
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    37
        # type: fully qualified service type name
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
    38
        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
    39
            '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
    40
            '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
    41
        }
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    42
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    43
        self.name = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    44
        self.ip_32b = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    45
        self.port = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    46
        self.server = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    47
        self.service_name = None
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    48
        self.retrytimer = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    49
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    50
    def RegisterService(self, name, ip, port):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    51
        try:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    52
            self._RegisterService(name, ip, port)
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
    53
        except Exception:
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):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    58
        # name: fully qualified service name
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
    59
        self.service_name = '%s.%s' % (name, service_type)
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    60
        self.name = name
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    61
        self.port = port
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    62
2320
dd959601e67a Force Zeroconf to listen and answer only on interface specified with '-i' command line argument.
Edouard Tisserant
parents: 1881
diff changeset
    63
        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
    64
            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
    65
            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
    66
            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
    67
        else:
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2477
diff changeset
    68
            interfaces = [ip]
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    69
2320
dd959601e67a Force Zeroconf to listen and answer only on interface specified with '-i' command line argument.
Edouard Tisserant
parents: 1881
diff changeset
    70
        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
    71
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
    72
        print("MDNS brodcasted service address :" + ip)
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    73
        self.ip_32b = socket.inet_aton(ip)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    74
1830
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    75
        self.server.register_service(
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
    76
            zeroconf.ServiceInfo(service_type,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
    77
                                 self.service_name,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
    78
                                 self.ip_32b,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
    79
                                 self.port,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
    80
                                 properties=self.serviceproperties))
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    81
        self.retrytimer = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    82
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    83
    def UnRegisterService(self):
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    84
        if self.retrytimer is not None:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    85
            self.retrytimer.cancel()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    86
1830
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    87
        if self.server is not None:
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    88
            self.server.unregister_service(
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    89
                zeroconf.ServiceInfo(service_type,
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    90
                                     self.service_name,
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    91
                                     self.ip_32b,
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    92
                                     self.port,
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    93
                                     properties=self.serviceproperties))
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    94
            self.server.close()
e598d1acf354 remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
    95
            self.server = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    96
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
    97
    def gethostaddr(self, dst='224.0.1.41'):
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    98
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
    99
        try:
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   100
            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
   101
            (host, _port) = s.getsockname()
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   102
            s.close()
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   103
            if host != '0.0.0.0':
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   104
                return host
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
   105
        except Exception:
647
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   106
            pass
edce87412f5a converted ServicePublisher.py to unix line ending
Edouard Tisserant
parents: 646
diff changeset
   107
        return socket.gethostbyname(socket.gethostname())