|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 #This file is part of Beremiz, a Integrated Development Environment for |
|
4 #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
5 # |
|
6 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
7 # |
|
8 #See COPYING file for copyrights details. |
|
9 # |
|
10 #This library is free software; you can redistribute it and/or |
|
11 #modify it under the terms of the GNU General Public |
|
12 #License as published by the Free Software Foundation; either |
|
13 #version 2.1 of the License, or (at your option) any later version. |
|
14 # |
|
15 #This library is distributed in the hope that it will be useful, |
|
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 #General Public License for more details. |
|
19 # |
|
20 #You should have received a copy of the GNU General Public |
|
21 #License along with this library; if not, write to the Free Software |
|
22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
|
24 import socket |
|
25 import wx |
|
26 import wx.lib.mixins.listctrl as listmix |
|
27 from util.Zeroconf import * |
|
28 |
|
29 import connectors |
|
30 |
|
31 service_type = '_PYRO._tcp.local.' |
|
32 |
|
33 class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): |
|
34 def __init__(self, parent, id, name, pos=wx.DefaultPosition, |
|
35 size=wx.DefaultSize, style=0): |
|
36 wx.ListCtrl.__init__(self, parent, id, pos, size, style, name=name) |
|
37 listmix.ListCtrlAutoWidthMixin.__init__(self) |
|
38 |
|
39 [ID_DISCOVERYDIALOG, ID_DISCOVERYDIALOGSTATICTEXT1, |
|
40 ID_DISCOVERYDIALOGSERVICESLIST, ID_DISCOVERYDIALOGREFRESHBUTTON, |
|
41 ID_DISCOVERYDIALOGLOCALBUTTON, ID_DISCOVERYDIALOGIPBUTTON, |
|
42 ] = [wx.NewId() for _init_ctrls in range(6)] |
|
43 |
|
44 class DiscoveryDialog(wx.Dialog, listmix.ColumnSorterMixin): |
|
45 |
|
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) |
|
48 parent.AddWindow(self.ServicesList, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.GROW) |
|
49 parent.AddSizer(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW) |
|
50 |
|
51 def _init_coll_MainSizer_Growables(self, parent): |
|
52 parent.AddGrowableCol(0) |
|
53 parent.AddGrowableRow(1) |
|
54 |
|
55 def _init_coll_ButtonGridSizer_Items(self, parent): |
|
56 parent.AddWindow(self.RefreshButton, 0, border=0, flag=0) |
|
57 parent.AddWindow(self.LocalButton, 0, border=0, flag=0) |
|
58 parent.AddWindow(self.IpButton, 0, border=0, flag=0) |
|
59 parent.AddSizer(self.ButtonSizer, 0, border=0, flag=0) |
|
60 |
|
61 def _init_coll_ButtonGridSizer_Growables(self, parent): |
|
62 parent.AddGrowableCol(0) |
|
63 parent.AddGrowableCol(1) |
|
64 parent.AddGrowableRow(1) |
|
65 |
|
66 def _init_sizers(self): |
|
67 self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
|
68 self.ButtonGridSizer = wx.FlexGridSizer(cols=4, hgap=5, rows=1, vgap=0) |
|
69 |
|
70 self._init_coll_MainSizer_Items(self.MainSizer) |
|
71 self._init_coll_MainSizer_Growables(self.MainSizer) |
|
72 self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer) |
|
73 self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer) |
|
74 |
|
75 self.SetSizer(self.MainSizer) |
|
76 |
|
77 def _init_ctrls(self, prnt): |
|
78 wx.Dialog.__init__(self, id=ID_DISCOVERYDIALOG, |
|
79 name='DiscoveryDialog', parent=prnt, |
|
80 size=wx.Size(600, 600), style=wx.DEFAULT_DIALOG_STYLE, |
|
81 title='Service Discovery') |
|
82 |
|
83 self.staticText1 = wx.StaticText(id=ID_DISCOVERYDIALOGSTATICTEXT1, |
|
84 label=_('Services available:'), name='staticText1', parent=self, |
|
85 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
86 |
|
87 # Set up list control |
|
88 self.ServicesList = AutoWidthListCtrl(id=ID_DISCOVERYDIALOGSERVICESLIST, |
|
89 name='ServicesList', parent=self, pos=wx.Point(0, 0), size=wx.Size(0, 0), |
|
90 style=wx.LC_REPORT|wx.LC_EDIT_LABELS|wx.LC_SORT_ASCENDING|wx.LC_SINGLE_SEL) |
|
91 self.ServicesList.InsertColumn(0, 'NAME') |
|
92 self.ServicesList.InsertColumn(1, 'TYPE') |
|
93 self.ServicesList.InsertColumn(2, 'IP') |
|
94 self.ServicesList.InsertColumn(3, 'PORT') |
|
95 self.ServicesList.SetColumnWidth(0, 150) |
|
96 self.ServicesList.SetColumnWidth(1, 150) |
|
97 self.ServicesList.SetColumnWidth(2, 150) |
|
98 self.ServicesList.SetColumnWidth(3, 150) |
|
99 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, id=ID_DISCOVERYDIALOGSERVICESLIST) |
|
100 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, id=ID_DISCOVERYDIALOGSERVICESLIST) |
|
101 |
|
102 listmix.ColumnSorterMixin.__init__(self, 4) |
|
103 |
|
104 self.RefreshButton = wx.Button(id=ID_DISCOVERYDIALOGREFRESHBUTTON, |
|
105 label=_('Refresh'), name='RefreshButton', parent=self, |
|
106 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
107 self.Bind(wx.EVT_BUTTON, self.OnRefreshButton, id=ID_DISCOVERYDIALOGREFRESHBUTTON) |
|
108 |
|
109 self.LocalButton = wx.Button(id=ID_DISCOVERYDIALOGLOCALBUTTON, |
|
110 label=_('Local'), name='LocalButton', parent=self, |
|
111 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
112 self.Bind(wx.EVT_BUTTON, self.OnLocalButton, id=ID_DISCOVERYDIALOGLOCALBUTTON) |
|
113 |
|
114 self.IpButton = wx.Button(id=ID_DISCOVERYDIALOGIPBUTTON, |
|
115 label=_('Add IP'), name='IpButton', parent=self, |
|
116 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
117 self.Bind(wx.EVT_BUTTON, self.OnIpButton, id=ID_DISCOVERYDIALOGIPBUTTON) |
|
118 |
|
119 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTER) |
|
120 |
|
121 self._init_sizers() |
|
122 |
|
123 def __init__(self, parent): |
|
124 self._init_ctrls(parent) |
|
125 |
|
126 self.itemDataMap = {} |
|
127 self.nextItemId = 0 |
|
128 |
|
129 self.URI = None |
|
130 self.Browser = None |
|
131 |
|
132 self.ZeroConfInstance = Zeroconf() |
|
133 self.RefreshList() |
|
134 self.LatestSelection=None |
|
135 |
|
136 def __del__(self): |
|
137 if self.Browser is not None : self.Browser.cancel() |
|
138 self.ZeroConfInstance.close() |
|
139 |
|
140 def RefreshList(self): |
|
141 if self.Browser is not None : self.Browser.cancel() |
|
142 self.Browser = ServiceBrowser(self.ZeroConfInstance, service_type, self) |
|
143 |
|
144 def OnRefreshButton(self, event): |
|
145 self.ServicesList.DeleteAllItems() |
|
146 self.RefreshList() |
|
147 |
|
148 def OnLocalButton(self, event): |
|
149 self.URI = "LOCAL://" |
|
150 self.EndModal(wx.ID_OK) |
|
151 event.Skip() |
|
152 |
|
153 def OnIpButton(self, event): |
|
154 if self.LatestSelection is not None: |
|
155 l = lambda col : self.getColumnText(self.LatestSelection,col) |
|
156 self.URI = "%s://%s:%s"%tuple(map(l,(1,2,3))) |
|
157 self.EndModal(wx.ID_OK) |
|
158 event.Skip() |
|
159 |
|
160 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py |
|
161 def GetListCtrl(self): |
|
162 return self.ServicesList |
|
163 |
|
164 def getColumnText(self, index, col): |
|
165 item = self.ServicesList.GetItem(index, col) |
|
166 return item.GetText() |
|
167 |
|
168 def OnItemSelected(self, event): |
|
169 self.SetURI(event.m_itemIndex) |
|
170 event.Skip() |
|
171 |
|
172 def OnItemActivated(self, event): |
|
173 self.SetURI(event.m_itemIndex) |
|
174 self.EndModal(wx.ID_OK) |
|
175 event.Skip() |
|
176 |
|
177 # def SetURI(self, idx): |
|
178 # connect_type = self.getColumnText(idx, 1) |
|
179 # connect_address = self.getColumnText(idx, 2) |
|
180 # connect_port = self.getColumnText(idx, 3) |
|
181 # |
|
182 # self.URI = "%s://%s:%s"%(connect_type, connect_address, connect_port) |
|
183 |
|
184 def SetURI(self, idx): |
|
185 self.LatestSelection = idx |
|
186 svcname = self.getColumnText(idx, 0) |
|
187 connect_type = self.getColumnText(idx, 1) |
|
188 self.URI = "%s://%s"%(connect_type, svcname + '.' + service_type) |
|
189 |
|
190 def GetURI(self): |
|
191 return self.URI |
|
192 |
|
193 def removeService(self, zeroconf, _type, name): |
|
194 wx.CallAfter(self._removeService, name) |
|
195 |
|
196 |
|
197 def _removeService(self, name): |
|
198 ''' |
|
199 called when a service with the desired type goes offline. |
|
200 ''' |
|
201 |
|
202 # loop through the list items looking for the service that went offline |
|
203 for idx in xrange(self.ServicesList.GetItemCount()): |
|
204 # this is the unique identifier assigned to the item |
|
205 item_id = self.ServicesList.GetItemData(idx) |
|
206 |
|
207 # this is the full typename that was received by addService |
|
208 item_name = self.itemDataMap[item_id][4] |
|
209 |
|
210 if item_name == name: |
|
211 self.ServicesList.DeleteItem(idx) |
|
212 break |
|
213 |
|
214 def addService(self, zeroconf, _type, name): |
|
215 wx.CallAfter(self._addService, _type, name) |
|
216 |
|
217 def _addService(self, _type, name): |
|
218 ''' |
|
219 called when a service with the desired type is discovered. |
|
220 ''' |
|
221 info = self.ZeroConfInstance.getServiceInfo(_type, name) |
|
222 |
|
223 svcname = name.split(".")[0] |
|
224 typename = _type.split(".")[0][1:] |
|
225 ip = str(socket.inet_ntoa(info.getAddress())) |
|
226 port = info.getPort() |
|
227 |
|
228 num_items = self.ServicesList.GetItemCount() |
|
229 |
|
230 # display the new data in the list |
|
231 new_item = self.ServicesList.InsertStringItem(num_items, svcname) |
|
232 self.ServicesList.SetStringItem(new_item, 1, "%s" % typename) |
|
233 self.ServicesList.SetStringItem(new_item, 2, "%s" % ip) |
|
234 self.ServicesList.SetStringItem(new_item, 3, "%s" % port) |
|
235 |
|
236 # record the new data for the ColumnSorterMixin |
|
237 # we assign every list item a unique id (that won't change when items |
|
238 # are added or removed) |
|
239 self.ServicesList.SetItemData(new_item, self.nextItemId) |
|
240 |
|
241 # the value of each column has to be stored in the itemDataMap |
|
242 # so that ColumnSorterMixin knows how to sort the column. |
|
243 |
|
244 # "name" is included at the end so that self.removeService |
|
245 # can access it. |
|
246 self.itemDataMap[self.nextItemId] = [ svcname, typename, ip, port, name ] |
|
247 |
|
248 self.nextItemId += 1 |
|
249 |