|
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 class DiscoveryPanel(wx.Panel, listmix.ColumnSorterMixin): |
|
43 |
|
44 def _init_coll_MainSizer_Items(self, parent): |
|
45 parent.AddWindow(self.staticText1, 0, border=20, flag=wx.TOP | wx.LEFT | wx.RIGHT | wx.GROW) |
|
46 parent.AddWindow(self.ServicesList, 0, border=20, flag=wx.LEFT | wx.RIGHT | wx.GROW) |
|
47 parent.AddSizer(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.GROW) |
|
48 |
|
49 def _init_coll_MainSizer_Growables(self, parent): |
|
50 parent.AddGrowableCol(0) |
|
51 parent.AddGrowableRow(1) |
|
52 |
|
53 def _init_coll_ButtonGridSizer_Items(self, parent): |
|
54 parent.AddWindow(self.RefreshButton, 0, border=0, flag=0) |
|
55 parent.AddWindow(self.ByIPCheck, 0, border=0, flag=0) |
|
56 |
|
57 def _init_coll_ButtonGridSizer_Growables(self, parent): |
|
58 parent.AddGrowableCol(0) |
|
59 parent.AddGrowableRow(0) |
|
60 |
|
61 def _init_sizers(self): |
|
62 self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
|
63 self.ButtonGridSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=1, vgap=0) |
|
64 |
|
65 self._init_coll_MainSizer_Items(self.MainSizer) |
|
66 self._init_coll_MainSizer_Growables(self.MainSizer) |
|
67 self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer) |
|
68 self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer) |
|
69 |
|
70 self.SetSizer(self.MainSizer) |
|
71 |
|
72 def _init_list_ctrl(self): |
|
73 # Set up list control |
|
74 listID = wx.NewId() |
|
75 self.ServicesList = AutoWidthListCtrl( |
|
76 id=listID, |
|
77 name='ServicesList', parent=self, pos=wx.Point(0, 0), size=wx.Size(0, 0), |
|
78 style=wx.LC_REPORT | wx.LC_EDIT_LABELS | wx.LC_SORT_ASCENDING | wx.LC_SINGLE_SEL) |
|
79 self.ServicesList.InsertColumn(0, _('NAME')) |
|
80 self.ServicesList.InsertColumn(1, _('TYPE')) |
|
81 self.ServicesList.InsertColumn(2, _('IP')) |
|
82 self.ServicesList.InsertColumn(3, _('PORT')) |
|
83 self.ServicesList.SetColumnWidth(0, 150) |
|
84 self.ServicesList.SetColumnWidth(1, 150) |
|
85 self.ServicesList.SetColumnWidth(2, 150) |
|
86 self.ServicesList.SetColumnWidth(3, 150) |
|
87 self.ServicesList.SetInitialSize(wx.Size(-1, 300)) |
|
88 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, id=listID) |
|
89 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, id=listID) |
|
90 |
|
91 def _init_ctrls(self, prnt): |
|
92 self.staticText1 = wx.StaticText( |
|
93 label=_('Services available:'), name='staticText1', parent=self, |
|
94 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
95 |
|
96 refreshID = wx.NewId() |
|
97 self.RefreshButton = wx.Button( |
|
98 id=refreshID, |
|
99 label=_('Refresh'), name='RefreshButton', parent=self, |
|
100 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
101 self.Bind(wx.EVT_BUTTON, self.OnRefreshButton, id=refreshID) |
|
102 |
|
103 self.ByIPCheck = wx.CheckBox(self, label=_("Use IP instead of Service Name")) |
|
104 |
|
105 self._init_sizers() |
|
106 self.Fit() |
|
107 |
|
108 def __init__(self, parent): |
|
109 wx.Panel.__init__(self, parent) |
|
110 |
|
111 self._init_list_ctrl() |
|
112 listmix.ColumnSorterMixin.__init__(self, 4) |
|
113 |
|
114 self._init_ctrls(parent) |
|
115 |
|
116 self.itemDataMap = {} |
|
117 self.nextItemId = 0 |
|
118 |
|
119 self.URI = None |
|
120 self.Browser = None |
|
121 |
|
122 self.ZeroConfInstance = Zeroconf() |
|
123 self.RefreshList() |
|
124 self.LatestSelection = None |
|
125 |
|
126 def __del__(self): |
|
127 if self.Browser is not None: |
|
128 self.Browser.cancel() |
|
129 self.ZeroConfInstance.close() |
|
130 |
|
131 def RefreshList(self): |
|
132 if self.Browser is not None: |
|
133 self.Browser.cancel() |
|
134 self.Browser = ServiceBrowser(self.ZeroConfInstance, service_type, self) |
|
135 |
|
136 def OnRefreshButton(self, event): |
|
137 self.ServicesList.DeleteAllItems() |
|
138 self.RefreshList() |
|
139 |
|
140 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py |
|
141 def GetListCtrl(self): |
|
142 return self.ServicesList |
|
143 |
|
144 def getColumnText(self, index, col): |
|
145 item = self.ServicesList.GetItem(index, col) |
|
146 return item.GetText() |
|
147 |
|
148 def OnItemSelected(self, event): |
|
149 self.SetURI(event.m_itemIndex) |
|
150 event.Skip() |
|
151 |
|
152 def OnItemActivated(self, event): |
|
153 self.SetURI(event.m_itemIndex) |
|
154 self.EndModal(wx.ID_OK) |
|
155 event.Skip() |
|
156 |
|
157 # def SetURI(self, idx): |
|
158 # connect_type = self.getColumnText(idx, 1) |
|
159 # connect_address = self.getColumnText(idx, 2) |
|
160 # connect_port = self.getColumnText(idx, 3) |
|
161 # |
|
162 # self.URI = "%s://%s:%s"%(connect_type, connect_address, connect_port) |
|
163 |
|
164 def SetURI(self, idx): |
|
165 self.LatestSelection = idx |
|
166 svcname = self.getColumnText(idx, 0) |
|
167 connect_type = self.getColumnText(idx, 1) |
|
168 self.URI = "%s://%s" % (connect_type, svcname + '.' + service_type) |
|
169 |
|
170 def GetURI(self): |
|
171 if self.LatestSelection is not None: |
|
172 if self.ByIPCheck.IsChecked(): |
|
173 self.URI = "%s://%s:%s" % tuple( |
|
174 map(lambda col:self.getColumnText(self.LatestSelection, col), |
|
175 (1, 2, 3))) |
|
176 |
|
177 return self.URI |
|
178 return None |
|
179 |
|
180 def remove_service(self, zeroconf, _type, name): |
|
181 wx.CallAfter(self._removeService, name) |
|
182 |
|
183 def _removeService(self, name): |
|
184 ''' |
|
185 called when a service with the desired type goes offline. |
|
186 ''' |
|
187 |
|
188 # loop through the list items looking for the service that went offline |
|
189 for idx in xrange(self.ServicesList.GetItemCount()): |
|
190 # this is the unique identifier assigned to the item |
|
191 item_id = self.ServicesList.GetItemData(idx) |
|
192 |
|
193 # this is the full typename that was received by addService |
|
194 item_name = self.itemDataMap[item_id][4] |
|
195 |
|
196 if item_name == name: |
|
197 self.ServicesList.DeleteItem(idx) |
|
198 break |
|
199 |
|
200 def add_service(self, zeroconf, _type, name): |
|
201 wx.CallAfter(self._addService, _type, name) |
|
202 |
|
203 def _addService(self, _type, name): |
|
204 ''' |
|
205 called when a service with the desired type is discovered. |
|
206 ''' |
|
207 info = self.ZeroConfInstance.get_service_info(_type, name) |
|
208 svcname = name.split(".")[0] |
|
209 typename = _type.split(".")[0][1:] |
|
210 ip = str(socket.inet_ntoa(info.address)) |
|
211 port = info.port |
|
212 |
|
213 num_items = self.ServicesList.GetItemCount() |
|
214 |
|
215 # display the new data in the list |
|
216 new_item = self.ServicesList.InsertStringItem(num_items, svcname) |
|
217 self.ServicesList.SetStringItem(new_item, 1, "%s" % typename) |
|
218 self.ServicesList.SetStringItem(new_item, 2, "%s" % ip) |
|
219 self.ServicesList.SetStringItem(new_item, 3, "%s" % port) |
|
220 |
|
221 # record the new data for the ColumnSorterMixin |
|
222 # we assign every list item a unique id (that won't change when items |
|
223 # are added or removed) |
|
224 self.ServicesList.SetItemData(new_item, self.nextItemId) |
|
225 |
|
226 # the value of each column has to be stored in the itemDataMap |
|
227 # so that ColumnSorterMixin knows how to sort the column. |
|
228 |
|
229 # "name" is included at the end so that self.removeService |
|
230 # can access it. |
|
231 self.itemDataMap[self.nextItemId] = [svcname, typename, ip, port, name] |
|
232 |
|
233 self.nextItemId += 1 |