discovery.py
author greg
Wed, 26 Nov 2008 09:04:28 +0100
changeset 272 9cce83526487
parent 262 141a7145c099
child 277 73890551f021
permissions -rwxr-xr-x
Bug on Exception handling fixed
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     3
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     6
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     8
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    10
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    15
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    19
#General Public License for more details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    20
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    24
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    25
import wx
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    26
from Zeroconf import *
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    27
import socket
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    28
import  wx.lib.mixins.listctrl  as  listmix
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    29
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    30
class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    31
    def __init__(self, parent, ID, pos=wx.DefaultPosition,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    32
                 size=wx.DefaultSize, style=0):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    33
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    34
        listmix.ListCtrlAutoWidthMixin.__init__(self)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    35
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    36
class DiscoveryDialog(wx.Dialog, listmix.ColumnSorterMixin):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    37
    def __init__(self, parent, id=-1, title='Service Discovery'):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    38
        self.my_result=None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    39
        wx.Dialog.__init__(self, parent, id, title, size=(600,600), style=wx.DEFAULT_DIALOG_STYLE)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    40
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    41
        sizer = wx.FlexGridSizer(2, 1, 2, 2)  # rows, cols, vgap, hgap
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    42
        sizer.AddGrowableRow(0)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    43
        sizer.AddGrowableCol(0)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    44
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    45
        self.list = TestListCtrl(self, -1,
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    46
                                 #pos=(50,20), 
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    47
                                 #size=(500,300),
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    48
                                 style=wx.LC_REPORT 
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    49
                                | wx.LC_EDIT_LABELS
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    50
                                | wx.LC_SORT_ASCENDING
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    51
                                )
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    52
        sizer.Add(self.list, 1, wx.EXPAND)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    53
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    54
        btsizer = wx.FlexGridSizer(1, 6, 2, 2)  # rows, cols, vgap, hgap
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    55
        
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    56
        sizer.Add(btsizer, 1, wx.EXPAND)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    57
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    58
        self.PopulateList()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    59
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    60
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    61
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    62
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    63
        local_id = wx.NewId()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    64
        b = wx.Button(self, local_id, "Refresh")
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    65
        self.Bind(wx.EVT_BUTTON, self.OnRefreshButton, b)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    66
        btsizer.Add(b)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    67
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    68
        btsizer.AddSpacer(0)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    69
        btsizer.AddGrowableCol(1)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    70
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    71
        local_id = wx.NewId()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    72
        b = wx.Button(self, local_id, "Local")
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    73
        self.Bind(wx.EVT_BUTTON, self.ChooseLocalID, b)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    74
        btsizer.Add(b)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    75
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    76
        btsizer.AddSpacer(0)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    77
        btsizer.AddGrowableCol(3)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    78
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    79
        b = wx.Button(self, wx.ID_CANCEL, "Cancel")
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    80
        #self.Bind(wx.EVT_BUTTON, self.OnClose, b)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    81
        btsizer.Add(b)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    82
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    83
        b = wx.Button(self, wx.ID_OK, "OK")
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    84
        #self.Bind(wx.EVT_BUTTON, self.OnConnect, b)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    85
        b.SetDefault()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    86
        btsizer.Add(b)
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    87
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    88
        self.SetSizer(sizer)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    89
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    90
        listmix.ColumnSorterMixin.__init__(self, 4)
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    91
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    92
        #type = "_http._tcp.local."
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 226
diff changeset
    93
        self.browser = None
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    94
        self.zConfInstance = Zeroconf()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    95
        self.RefreshList()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    96
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    97
    def RefreshList(self):
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    98
        type = "_PYRO._tcp.local."
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 226
diff changeset
    99
        self.browser = ServiceBrowser(self.zConfInstance, type, self)        
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   100
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   101
    def OnRefreshButton(self, event):
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   102
        self.list.DeleteAllItems()
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 226
diff changeset
   103
        self.browser.cancel()
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   104
        self.RefreshList()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   105
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   106
    # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   107
    def GetListCtrl(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   108
        return self.list
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   109
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   110
    def PopulateList(self):
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   111
        self.list.InsertColumn(0, 'NAME')
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   112
        self.list.InsertColumn(1, 'TYPE')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   113
        self.list.InsertColumn(2, 'IP')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   114
        self.list.InsertColumn(3, 'PORT')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   115
        self.list.SetColumnWidth(0, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   116
        self.list.SetColumnWidth(1, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   117
        self.list.SetColumnWidth(2, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   118
        self.list.SetColumnWidth(3, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   119
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   120
    def getColumnText(self, index, col):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   121
        item = self.list.GetItem(index, col)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   122
        return item.GetText()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   123
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   124
    def OnItemSelected(self, event):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   125
        self.currentItem = event.m_itemIndex
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   126
        self.setresult()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   127
        event.Skip()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   128
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   129
    def OnItemActivated(self, event):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   130
        self.currentItem = event.m_itemIndex
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   131
        self.setresult()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   132
        self.Close()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   133
        event.Skip()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   134
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   135
    def setresult(self):
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   136
        connect_type = self.getColumnText(self.currentItem, 1)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   137
        connect_address = self.getColumnText(self.currentItem, 2)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   138
        connect_port = self.getColumnText(self.currentItem, 3)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   139
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   140
        uri = self.CreateURI(connect_type, connect_address, connect_port)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   141
        self.my_result=uri
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   142
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   143
    def GetResult(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   144
        return self.my_result
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   145
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   146
    def removeService(self, zeroconf, type, name):
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   147
        pass
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   148
	
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 226
diff changeset
   149
    def addService(self, zeroconf, type, name):
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   150
        info = self.zConfInstance.getServiceInfo(type, name)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   151
        typename = type.split(".")[0][1:]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   152
        num_items = self.list.GetItemCount()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   153
        self.list.InsertStringItem(num_items, name.split(".")[0])
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   154
        self.list.SetStringItem(num_items, 1, "%s"%typename)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   155
        self.list.SetStringItem(num_items, 2, "%s"%str(socket.inet_ntoa(info.getAddress())))
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   156
        self.list.SetStringItem(num_items, 3, "%s"%info.getPort())
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   157
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   158
    def CreateURI(self, connect_type, connect_address, connect_port):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   159
        uri = "%s://%s:%s"%(connect_type, connect_address, connect_port)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   160
        return uri
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   161
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   162
    def ChooseLocalID(self, event):
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   163
        self.my_result = "PYRO://localhost:3000"
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   164
        self.Close()