controls/DiscoveryPanel.py
changeset 2492 7dd551ac2fa0
parent 2482 cb53d3bf3380
child 2521 48ebcbe7f19b
equal deleted inserted replaced
2491:362039519454 2492:7dd551ac2fa0
    28 import socket
    28 import socket
    29 from six.moves import xrange
    29 from six.moves import xrange
    30 import wx
    30 import wx
    31 import wx.lib.mixins.listctrl as listmix
    31 import wx.lib.mixins.listctrl as listmix
    32 from zeroconf import ServiceBrowser, Zeroconf, get_all_addresses
    32 from zeroconf import ServiceBrowser, Zeroconf, get_all_addresses
    33 import netifaces
       
    34 
    33 
    35 service_type = '_Beremiz._tcp.local.'
    34 service_type = '_Beremiz._tcp.local.'
    36 
    35 
    37 
    36 
    38 class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    37 class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    39     def __init__(self, parent, id, name, pos=wx.DefaultPosition,
    38     def __init__(self, parent, id, name, pos=wx.DefaultPosition,
    40                  size=wx.DefaultSize, style=0):
    39                  size=wx.DefaultSize, style=0):
    41         wx.ListCtrl.__init__(self, parent, id, pos, size, style, name=name)
    40         wx.ListCtrl.__init__(self, parent, id, pos, size, style, name=name)
    42         listmix.ListCtrlAutoWidthMixin.__init__(self)
    41         listmix.ListCtrlAutoWidthMixin.__init__(self)
       
    42 
    43 
    43 
    44 class DiscoveryPanel(wx.Panel, listmix.ColumnSorterMixin):
    44 class DiscoveryPanel(wx.Panel, listmix.ColumnSorterMixin):
    45 
    45 
    46     def _init_coll_MainSizer_Items(self, parent):
    46     def _init_coll_MainSizer_Items(self, parent):
    47         parent.AddWindow(self.staticText1,    0, border=20, flag=wx.TOP | wx.LEFT | wx.RIGHT | wx.GROW)
    47         parent.AddWindow(self.staticText1,    0, border=20, flag=wx.TOP | wx.LEFT | wx.RIGHT | wx.GROW)
   127         self.LatestSelection = None
   127         self.LatestSelection = None
   128 
   128 
   129         self.IfacesMonitorState = None
   129         self.IfacesMonitorState = None
   130         self.IfacesMonitorTimer = wx.Timer(self)
   130         self.IfacesMonitorTimer = wx.Timer(self)
   131         self.IfacesMonitorTimer.Start(2000)
   131         self.IfacesMonitorTimer.Start(2000)
   132         self.Bind(wx.EVT_TIMER, self.IfacesMonitor , self.IfacesMonitorTimer)
   132         self.Bind(wx.EVT_TIMER, self.IfacesMonitor, self.IfacesMonitorTimer)
   133 
   133 
   134     def __del__(self):
   134     def __del__(self):
   135         self.IfacesMonitorTimer.Stop()
   135         self.IfacesMonitorTimer.Stop()
   136         self.Browser.cancel()
   136         self.Browser.cancel()
   137         self.ZeroConfInstance.close()
   137         self.ZeroConfInstance.close()
   138 
   138 
   139     def IfacesMonitor(self, event):
   139     def IfacesMonitor(self, event):
   140         NewState = get_all_addresses(socket.AF_INET)
   140         NewState = get_all_addresses(socket.AF_INET)
   141 
   141 
   142         if self.IfacesMonitorState != NewState: 
   142         if self.IfacesMonitorState != NewState:
   143             if self.IfacesMonitorState is not None:
   143             if self.IfacesMonitorState is not None:
   144                 # refresh only if a new address appeared
   144                 # refresh only if a new address appeared
   145                 for addr in NewState:
   145                 for addr in NewState:
   146                     if addr not in self.IfacesMonitorState:
   146                     if addr not in self.IfacesMonitorState:
   147                         self.RefreshList()
   147                         self.RefreshList()
   190 
   190 
   191     def GetURI(self):
   191     def GetURI(self):
   192         if self.LatestSelection is not None:
   192         if self.LatestSelection is not None:
   193             # if self.ByIPCheck.IsChecked():
   193             # if self.ByIPCheck.IsChecked():
   194             svcname, scheme, host, port = \
   194             svcname, scheme, host, port = \
   195                 map(lambda col:self.getColumnText(self.LatestSelection, col),
   195                 map(lambda col: self.getColumnText(self.LatestSelection, col),
   196                     range(4))
   196                     range(4))
   197             return ("%s://%s:%s#%s" % (scheme, host, port, svcname)) \
   197             return ("%s://%s:%s#%s" % (scheme, host, port, svcname)) \
   198                 if scheme[-1] == "S" \
   198                 if scheme[-1] == "S" \
   199                 else ("%s://%s:%s" % (scheme, host, port))
   199                 else ("%s://%s:%s" % (scheme, host, port))
   200             # else:
   200             # else: