discovery.py
author greg
Sun, 07 Sep 2008 10:03:23 +0200
changeset 244 85e92d9e34a8
parent 226 f301f4ed4717
child 262 141a7145c099
permissions -rwxr-xr-x
fixed : bug in debugthreadproc (plugger.py)
changes in plc_win32_main.c and plc_debug
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."
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    93
        self.zConfInstance = Zeroconf()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    94
        self.RefreshList()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    95
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    96
    def RefreshList(self):
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    97
        type = "_PYRO._tcp.local."
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    98
        browser = ServiceBrowser(self.zConfInstance, type, self)        
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
    99
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   100
    def OnRefreshButton(self, event):
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   101
        self.list.DeleteAllItems()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   102
        self.RefreshList()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   103
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   104
    # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   105
    def GetListCtrl(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   106
        return self.list
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   107
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   108
    def PopulateList(self):
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   109
        self.list.InsertColumn(0, 'NAME')
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   110
        self.list.InsertColumn(1, 'TYPE')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   111
        self.list.InsertColumn(2, 'IP')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   112
        self.list.InsertColumn(3, 'PORT')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   113
        self.list.SetColumnWidth(0, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   114
        self.list.SetColumnWidth(1, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   115
        self.list.SetColumnWidth(2, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   116
        self.list.SetColumnWidth(3, 150)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   117
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   118
    def getColumnText(self, index, col):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   119
        item = self.list.GetItem(index, col)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   120
        return item.GetText()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   121
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   122
    def OnItemSelected(self, event):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   123
        self.currentItem = event.m_itemIndex
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   124
        self.setresult()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   125
        event.Skip()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   126
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   127
    def OnItemActivated(self, event):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   128
        self.currentItem = event.m_itemIndex
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   129
        self.setresult()
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   130
        self.Close()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   131
        event.Skip()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   132
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   133
    def setresult(self):
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   134
        connect_type = self.getColumnText(self.currentItem, 1)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   135
        connect_address = self.getColumnText(self.currentItem, 2)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   136
        connect_port = self.getColumnText(self.currentItem, 3)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   137
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   138
        uri = self.CreateURI(connect_type, connect_address, connect_port)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   139
        self.my_result=uri
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   140
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   141
    def GetResult(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   142
        return self.my_result
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   143
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   144
    def removeService(self, zeroconf, type, name):
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   145
        pass
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   146
	
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   147
    def addService(self, zeroconf, type, name):
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   148
        info = self.zConfInstance.getServiceInfo(type, name)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   149
        typename = type.split(".")[0][1:]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   150
        num_items = self.list.GetItemCount()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   151
        self.list.InsertStringItem(num_items, name.split(".")[0])
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   152
        self.list.SetStringItem(num_items, 1, "%s"%typename)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   153
        self.list.SetStringItem(num_items, 2, "%s"%str(socket.inet_ntoa(info.getAddress())))
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   154
        self.list.SetStringItem(num_items, 3, "%s"%info.getPort())
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   155
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   156
    def CreateURI(self, connect_type, connect_address, connect_port):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   157
        uri = "%s://%s:%s"%(connect_type, connect_address, connect_port)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   158
        return uri
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   159
221
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   160
    def ChooseLocalID(self, event):
451bb2c1d157 Rewritten horrible discovery.py.
etisserant
parents: 203
diff changeset
   161
        self.my_result = "PYRO://localhost:3000"
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   162
        self.Close()