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