discovery.py
author 'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
Fri, 10 Jul 2009 11:06:30 +0200
changeset 358 855b6fd21416
parent 357 19db1076e93c
child 361 331d698e1118
child 374 8787fa8c6792
permissions -rwxr-xr-x
removed dead hidden files
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
357
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
    26
from Zeroconf import *
203
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")
357
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
    80
        self.Bind(wx.EVT_BUTTON, self.OnCancel, b)
221
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")
357
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
    84
        self.Bind(wx.EVT_BUTTON, self.OnOk, b)
221
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
357
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
    97
        self.Bind(wx.EVT_CLOSE, self.OnClose)
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
    98
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    99
    def RefreshList(self):
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   100
        type = "_PYRO._tcp.local."
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 226
diff changeset
   101
        self.browser = ServiceBrowser(self.zConfInstance, type, self)        
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   102
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   103
    def OnRefreshButton(self, event):
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   104
        self.list.DeleteAllItems()
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 226
diff changeset
   105
        self.browser.cancel()
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   106
        self.RefreshList()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   107
357
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   108
    def OnClose(self, event):
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   109
        self.zConfInstance.close()
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   110
        event.Skip()
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   111
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   112
    def OnCancel(self, event):
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   113
        self.zConfInstance.close()
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   114
        event.Skip()
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   115
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   116
    def OnOk(self, event):
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   117
        self.zConfInstance.close()
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   118
        event.Skip()
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   119
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   120
    # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   121
    def GetListCtrl(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   122
        return self.list
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   123
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   124
    def PopulateList(self):
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   125
        self.list.InsertColumn(0, 'NAME')
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   126
        self.list.InsertColumn(1, 'TYPE')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   127
        self.list.InsertColumn(2, 'IP')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   128
        self.list.InsertColumn(3, 'PORT')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   129
        self.list.SetColumnWidth(0, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   130
        self.list.SetColumnWidth(1, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   131
        self.list.SetColumnWidth(2, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   132
        self.list.SetColumnWidth(3, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   133
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   134
    def getColumnText(self, index, col):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   135
        item = self.list.GetItem(index, col)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   136
        return item.GetText()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   137
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   138
    def OnItemSelected(self, event):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   139
        self.currentItem = event.m_itemIndex
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   140
        self.setresult()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   141
        event.Skip()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   142
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   143
    def OnItemActivated(self, event):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   144
        self.currentItem = event.m_itemIndex
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   145
        self.setresult()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   146
        self.Close()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   147
        event.Skip()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   148
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   149
    def setresult(self):
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   150
        connect_type = self.getColumnText(self.currentItem, 1)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   151
        connect_address = self.getColumnText(self.currentItem, 2)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   152
        connect_port = self.getColumnText(self.currentItem, 3)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   153
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   154
        uri = self.CreateURI(connect_type, connect_address, connect_port)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   155
        self.my_result=uri
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   156
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   157
    def GetResult(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   158
        return self.my_result
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   159
        
357
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   160
    def removeService(self, zeroconf, type, name):
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   161
        pass
357
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   162
262
141a7145c099 add taskbaricon to beremiz_service.
greg
parents: 226
diff changeset
   163
    def addService(self, zeroconf, type, name):
357
19db1076e93c close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents: 277
diff changeset
   164
        info = self.zConfInstance.getServiceInfo(type, name)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   165
        typename = type.split(".")[0][1:]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   166
        num_items = self.list.GetItemCount()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   167
        self.list.InsertStringItem(num_items, name.split(".")[0])
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   168
        self.list.SetStringItem(num_items, 1, "%s"%typename)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   169
        self.list.SetStringItem(num_items, 2, "%s"%str(socket.inet_ntoa(info.getAddress())))
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   170
        self.list.SetStringItem(num_items, 3, "%s"%info.getPort())
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   171
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   172
    def CreateURI(self, connect_type, connect_address, connect_port):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   173
        uri = "%s://%s:%s"%(connect_type, connect_address, connect_port)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   174
        return uri
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   175
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   176
    def ChooseLocalID(self, event):
277
73890551f021 Local PYRO connection is designated by LOCAL://, not PYRO://localhost:3000 anymore
etisserant
parents: 262
diff changeset
   177
        self.my_result = "LOCAL://"
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   178
        self.Close()