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