# This file is part of Beremiz runtime.
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
# See COPYING.Runtime file for copyrights details.
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
service_type = '_Beremiz._tcp.local.'
class ServicePublisher(object):
def __init__(self, protocol):
self.serviceproperties = {
'description': 'Beremiz remote PLC',
def RegisterService(self, name, ip, port):
self._RegisterService(name, ip, port)
print(f"Failed to register service ({str(e)}), retrying in 2 seconds")
self.retrytimer = threading.Timer(2, self.RegisterService, [name, ip, port])
def _RegisterService(self, name, ip, port):
self.service_name = '%s.%s' % (name, service_type)
print("MDNS brodcasted on all interfaces")
interfaces = zeroconf.InterfaceChoice.All
self.server = zeroconf.Zeroconf(interfaces=interfaces)
print("MDNS brodcasted service address :" + ip)
self.ip_32b = socket.inet_aton(ip)
self.service_info = zeroconf.ServiceInfo(
properties=self.serviceproperties)
self.server.register_service(self.service_info)
def UnRegisterService(self):
if self.retrytimer is not None:
if self.server is not None:
self.server.unregister_service(self.service_info)
def gethostaddr(self, dst='224.0.1.41'):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
(host, _port) = s.getsockname()
return socket.gethostbyname(socket.gethostname())