andrej@1571: #!/usr/bin/env python etisserant@203: # -*- coding: utf-8 -*- etisserant@203: andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1571: # andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1696: # Copyright (C) 2017: Andrey Skvortsov andrej@1571: # andrej@1571: # See COPYING file for copyrights details. andrej@1571: # andrej@1571: # This program is free software; you can redistribute it and/or andrej@1571: # modify it under the terms of the GNU General Public License andrej@1571: # as published by the Free Software Foundation; either version 2 andrej@1571: # of the License, or (at your option) any later version. andrej@1571: # andrej@1571: # This program is distributed in the hope that it will be useful, andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1571: # GNU General Public License for more details. andrej@1571: # andrej@1571: # You should have received a copy of the GNU General Public License andrej@1571: # along with this program; if not, write to the Free Software andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. etisserant@203: andrej@1881: andrej@1881: from __future__ import absolute_import laurent@399: import socket andrej@2432: from six.moves import xrange etisserant@203: import wx andrej@1738: import wx.lib.mixins.listctrl as listmix Edouard@2477: from zeroconf import ServiceBrowser, Zeroconf, get_all_addresses Edouard@2477: Edouard@2477: service_type = '_Beremiz._tcp.local.' Edouard@763: andrej@1736: b@375: class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): Edouard@2737: def __init__(self, parent, name, pos=wx.DefaultPosition, etisserant@203: size=wx.DefaultSize, style=0): Edouard@2737: wx.ListCtrl.__init__(self, parent, wx.ID_ANY, pos, size, style, name=name) etisserant@203: listmix.ListCtrlAutoWidthMixin.__init__(self) etisserant@203: edouard@2492: Edouard@2332: class DiscoveryPanel(wx.Panel, listmix.ColumnSorterMixin): andrej@1730: laurent@392: def _init_coll_MainSizer_Items(self, parent): edouard@3303: parent.Add(self.staticText1, 0, border=20, flag=wx.TOP | wx.LEFT | wx.RIGHT | wx.GROW) edouard@3303: parent.Add(self.ServicesList, 0, border=20, flag=wx.LEFT | wx.RIGHT | wx.GROW) edouard@3303: parent.Add(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.GROW) andrej@1730: laurent@392: def _init_coll_MainSizer_Growables(self, parent): laurent@392: parent.AddGrowableCol(0) laurent@392: parent.AddGrowableRow(1) andrej@1730: laurent@392: def _init_coll_ButtonGridSizer_Items(self, parent): edouard@3303: parent.Add(self.RefreshButton, 0, border=0, flag=0) edouard@3303: # parent.Add(self.ByIPCheck, 0, border=0, flag=0) andrej@1730: laurent@392: def _init_coll_ButtonGridSizer_Growables(self, parent): laurent@392: parent.AddGrowableCol(0) andrej@1494: parent.AddGrowableRow(0) andrej@1730: laurent@392: def _init_sizers(self): laurent@392: self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) Edouard@2332: self.ButtonGridSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=1, vgap=0) andrej@1730: laurent@392: self._init_coll_MainSizer_Items(self.MainSizer) laurent@392: self._init_coll_MainSizer_Growables(self.MainSizer) laurent@392: self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer) laurent@392: self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer) andrej@1730: laurent@392: self.SetSizer(self.MainSizer) andrej@1730: andrej@1836: def _init_list_ctrl(self): laurent@392: # Set up list control andrej@1768: self.ServicesList = AutoWidthListCtrl( andrej@1768: name='ServicesList', parent=self, pos=wx.Point(0, 0), size=wx.Size(0, 0), andrej@1768: style=wx.LC_REPORT | wx.LC_EDIT_LABELS | wx.LC_SORT_ASCENDING | wx.LC_SINGLE_SEL) andrej@1495: self.ServicesList.InsertColumn(0, _('NAME')) andrej@1495: self.ServicesList.InsertColumn(1, _('TYPE')) andrej@1495: self.ServicesList.InsertColumn(2, _('IP')) andrej@1495: self.ServicesList.InsertColumn(3, _('PORT')) laurent@392: self.ServicesList.SetColumnWidth(0, 150) laurent@392: self.ServicesList.SetColumnWidth(1, 150) laurent@392: self.ServicesList.SetColumnWidth(2, 150) laurent@392: self.ServicesList.SetColumnWidth(3, 150) andrej@1696: self.ServicesList.SetInitialSize(wx.Size(-1, 300)) Edouard@2737: self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.ServicesList) Edouard@2737: self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.ServicesList) andrej@1730: andrej@1836: def _init_ctrls(self, prnt): andrej@1836: self.staticText1 = wx.StaticText( andrej@1836: label=_('Services available:'), name='staticText1', parent=self, andrej@1836: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) andrej@1730: andrej@1768: self.RefreshButton = wx.Button( andrej@1768: label=_('Refresh'), name='RefreshButton', parent=self, andrej@1768: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) Edouard@2477: self.RefreshButton.Bind(wx.EVT_BUTTON, self.OnRefreshButton) Edouard@2332: Edouard@2478: # self.ByIPCheck = wx.CheckBox(self, label=_("Use IP instead of Service Name")) Edouard@2478: # self.ByIPCheck.SetValue(True) andrej@1730: laurent@392: self._init_sizers() andrej@1696: self.Fit() andrej@1730: laurent@392: def __init__(self, parent): Edouard@2332: wx.Panel.__init__(self, parent) andrej@1836: Edouard@2466: self.parent = parent Edouard@2466: andrej@1836: self._init_list_ctrl() andrej@1836: listmix.ColumnSorterMixin.__init__(self, 4) andrej@1836: laurent@392: self._init_ctrls(parent) andrej@1730: b@375: self.itemDataMap = {} b@375: self.nextItemId = 0 andrej@1730: laurent@392: self.URI = None Edouard@763: self.Browser = None Edouard@2477: self.ZeroConfInstance = None Edouard@2477: etisserant@221: self.RefreshList() andrej@1742: self.LatestSelection = None andrej@1730: Edouard@2477: self.IfacesMonitorState = None Edouard@2477: self.IfacesMonitorTimer = wx.Timer(self) Edouard@2477: self.IfacesMonitorTimer.Start(2000) edouard@2492: self.Bind(wx.EVT_TIMER, self.IfacesMonitor, self.IfacesMonitorTimer) Edouard@2477: laurent@392: def __del__(self): Edouard@2477: self.IfacesMonitorTimer.Stop() Edouard@2477: self.Browser.cancel() Edouard@2477: self.ZeroConfInstance.close() Edouard@2477: Edouard@2477: def IfacesMonitor(self, event): Edouard@2477: NewState = get_all_addresses(socket.AF_INET) Edouard@2477: edouard@2492: if self.IfacesMonitorState != NewState: Edouard@2477: if self.IfacesMonitorState is not None: Edouard@2477: # refresh only if a new address appeared Edouard@2477: for addr in NewState: Edouard@2477: if addr not in self.IfacesMonitorState: Edouard@2477: self.RefreshList() Edouard@2477: break Edouard@2477: self.IfacesMonitorState = NewState Edouard@2477: event.Skip() Edouard@2477: Edouard@2477: def RefreshList(self): Edouard@2477: self.ServicesList.DeleteAllItems() andrej@1756: if self.Browser is not None: andrej@1756: self.Browser.cancel() Edouard@2477: if self.ZeroConfInstance is not None: Edouard@2477: self.ZeroConfInstance.close() Edouard@2477: self.ZeroConfInstance = Zeroconf() Edouard@763: self.Browser = ServiceBrowser(self.ZeroConfInstance, service_type, self) etisserant@221: etisserant@221: def OnRefreshButton(self, event): etisserant@221: self.RefreshList() etisserant@221: etisserant@203: # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py etisserant@203: def GetListCtrl(self): laurent@392: return self.ServicesList etisserant@203: etisserant@203: def getColumnText(self, index, col): laurent@392: item = self.ServicesList.GetItem(index, col) etisserant@203: return item.GetText() etisserant@203: etisserant@203: def OnItemSelected(self, event): laurent@392: self.SetURI(event.m_itemIndex) etisserant@203: event.Skip() etisserant@203: etisserant@203: def OnItemActivated(self, event): laurent@392: self.SetURI(event.m_itemIndex) Edouard@2466: self.parent.EndModal(wx.ID_OK) etisserant@203: event.Skip() etisserant@203: Edouard@763: # def SetURI(self, idx): Edouard@763: # connect_type = self.getColumnText(idx, 1) Edouard@763: # connect_address = self.getColumnText(idx, 2) Edouard@763: # connect_port = self.getColumnText(idx, 3) andrej@1730: # Edouard@763: # self.URI = "%s://%s:%s"%(connect_type, connect_address, connect_port) Edouard@763: laurent@392: def SetURI(self, idx): Edouard@763: self.LatestSelection = idx andrej@1730: laurent@392: def GetURI(self): Edouard@2332: if self.LatestSelection is not None: Edouard@2478: # if self.ByIPCheck.IsChecked(): Edouard@2478: svcname, scheme, host, port = \ edouard@2492: map(lambda col: self.getColumnText(self.LatestSelection, col), Edouard@2478: range(4)) Edouard@2478: return ("%s://%s:%s#%s" % (scheme, host, port, svcname)) \ Edouard@2478: if scheme[-1] == "S" \ Edouard@2478: else ("%s://%s:%s" % (scheme, host, port)) Edouard@2478: # else: Edouard@2478: # svcname = self.getColumnText(self.LatestSelection, 0) Edouard@2478: # connect_type = self.getColumnText(self.LatestSelection, 1) Edouard@2478: # return str("MDNS://%s" % svcname) Edouard@2332: return None andrej@1730: andrej@1830: def remove_service(self, zeroconf, _type, name): Edouard@644: wx.CallAfter(self._removeService, name) Edouard@644: Edouard@644: def _removeService(self, name): b@375: ''' b@375: called when a service with the desired type goes offline. b@375: ''' andrej@1730: b@375: # loop through the list items looking for the service that went offline laurent@392: for idx in xrange(self.ServicesList.GetItemCount()): b@375: # this is the unique identifier assigned to the item laurent@392: item_id = self.ServicesList.GetItemData(idx) b@375: b@375: # this is the full typename that was received by addService b@375: item_name = self.itemDataMap[item_id][4] b@375: b@375: if item_name == name: laurent@392: self.ServicesList.DeleteItem(idx) b@375: break andrej@1730: andrej@1830: def add_service(self, zeroconf, _type, name): Edouard@763: wx.CallAfter(self._addService, _type, name) Edouard@763: Edouard@763: def _addService(self, _type, name): b@375: ''' b@375: called when a service with the desired type is discovered. b@375: ''' andrej@1830: info = self.ZeroConfInstance.get_service_info(_type, name) Edouard@2482: if info is None: Edouard@2482: return andrej@1758: svcname = name.split(".")[0] Edouard@2477: typename = info.properties.get("protocol", None) andrej@1830: ip = str(socket.inet_ntoa(info.address)) andrej@1830: port = info.port b@374: laurent@392: num_items = self.ServicesList.GetItemCount() b@375: b@375: # display the new data in the list laurent@392: new_item = self.ServicesList.InsertStringItem(num_items, svcname) laurent@392: self.ServicesList.SetStringItem(new_item, 1, "%s" % typename) laurent@392: self.ServicesList.SetStringItem(new_item, 2, "%s" % ip) laurent@392: self.ServicesList.SetStringItem(new_item, 3, "%s" % port) b@375: b@375: # record the new data for the ColumnSorterMixin b@375: # we assign every list item a unique id (that won't change when items b@375: # are added or removed) laurent@392: self.ServicesList.SetItemData(new_item, self.nextItemId) andrej@1730: b@375: # the value of each column has to be stored in the itemDataMap b@375: # so that ColumnSorterMixin knows how to sort the column. b@375: b@375: # "name" is included at the end so that self.removeService b@375: # can access it. andrej@1746: self.itemDataMap[self.nextItemId] = [svcname, typename, ip, port, name] b@375: b@375: self.nextItemId += 1