forked from sailoog/openplotter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
214 lines (177 loc) · 8.17 KB
/
connection.py
File metadata and controls
214 lines (177 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
# This file is part of Openplotter.
# Copyright (C) 2015 by sailoog <https://github.com/sailoog/openplotter>
#
# Openplotter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
# Openplotter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Openplotter. If not, see <http://www.gnu.org/licenses/>.
import wx, os, sys, re, pyudev
from classes.paths import Paths
from classes.conf import Conf
from classes.language import Language
paths=Paths()
currentpath=paths.currentpath
class MainFrame(wx.Frame):
def __init__(self):
self.conf=Conf()
Language(self.conf.get('GENERAL','lang'))
wx.Frame.__init__(self, None, title=_('Connection'), size=(550,385))
self.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
self.icon = wx.Icon(currentpath+'/openplotter.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
self.in_out=sys.argv[1]
self.con_type=sys.argv[2]
wx.StaticText(self, label=_('Name'), pos=(25, 35))
self.name = wx.TextCtrl(self, -1, size=(120, 32), pos=(20, 55))
if self.con_type=='serial':
if self.in_out=='in': wx.StaticBox(self, label=_(' Serial input '), size=(530, 90), pos=(10, 10))
if self.in_out=='out': wx.StaticBox(self, label=_(' Serial output '), size=(530, 90), pos=(10, 10))
self.SerialCheck()
wx.StaticText(self, label=_('Port'), pos=(155, 35))
self.deviceComboBox = wx.ComboBox(self, choices=self.SerDevLs, style=wx.CB_DROPDOWN, size=(155, 32), pos=(150, 55))
if self.SerDevLs : self.deviceComboBox.SetValue(self.SerDevLs[0])
self.bauds = ['4800', '9600', '19200', '38400', '57600', '115200']
wx.StaticText(self, label=_('Bauds'), pos=(320, 35))
self.baudComboBox = wx.ComboBox(self, choices=self.bauds, style=wx.CB_READONLY, size=(90, 32), pos=(315, 55))
self.baudComboBox.SetValue('4800')
if self.con_type=='network':
if self.in_out=='in':
wx.StaticBox(self, label=_(' Network input '), size=(530, 90), pos=(10, 10))
self.gpsd =wx.Button(self, label=_('GPSD'), pos=(440, 55))
self.Bind(wx.EVT_BUTTON, self.create_gpsd, self.gpsd)
if self.in_out=='out': wx.StaticBox(self, label=_(' Network output '), size=(530, 90), pos=(10, 10))
self.type = ['TCP', 'UDP']
wx.StaticText(self, label=_('Type'), pos=(155, 35))
self.typeComboBox = wx.ComboBox(self, choices=self.type, style=wx.CB_READONLY, size=(70, 32), pos=(150, 55))
self.typeComboBox.SetValue('TCP')
wx.StaticText(self, label=_('Address'), pos=(235, 35))
self.address = wx.TextCtrl(self, -1, size=(120, 32), pos=(230, 55))
wx.StaticText(self, label=_('Port'), pos=(365, 35))
self.port = wx.TextCtrl(self, -1, size=(55, 32), pos=(360, 55))
wx.StaticBox(self, label=_(' Filter '), size=(530, 195), pos=(10, 105))
self.mode_filter = [_('none'), _('Accept only sentences:'), _('Ignore sentences:')]
self.filter = wx.ComboBox(self, choices=self.mode_filter, style=wx.CB_READONLY, size=(220, 32), pos=(20, 130))
self.filter.SetValue(self.mode_filter[0])
wx.StaticText(self, label=_('Filtering'), pos=(25, 175))
self.sentences = wx.TextCtrl(self, -1, style=wx.CB_READONLY, size=(400, 32), pos=(20, 195))
self.sentences.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_INACTIVECAPTION))
self.delete =wx.Button(self, label=_('Delete'), pos=(440, 195))
self.Bind(wx.EVT_BUTTON, self.delete_sentences, self.delete)
wx.StaticText(self, label=_('Talker - Sentence'), pos=(25, 240))
self.talker = wx.TextCtrl(self, -1, size=(35, 32), pos=(20, 260))
wx.StaticText(self, label=_('-'), pos=(60, 265))
self.sent = wx.TextCtrl(self, -1, size=(45, 32), pos=(70, 260))
self.add_sent =wx.Button(self, label=_('Add sentence'), pos=(160, 260))
self.Bind(wx.EVT_BUTTON, self.add_sentence, self.add_sent)
self.sentences.SetValue(_('nothing'))
self.talker.SetValue('**')
self.sent.SetValue('***')
self.ok =wx.Button(self, label=_('OK'), pos=(440, 310))
self.Bind(wx.EVT_BUTTON, self.ok_conn, self.ok)
self.cancel =wx.Button(self, label=_('Cancel'), pos=(330, 310))
self.Bind(wx.EVT_BUTTON, self.cancel_conn, self.cancel)
self.Centre()
def ShowMessage(self, w_msg):
wx.MessageBox(w_msg, 'Info', wx.OK | wx.ICON_INFORMATION)
def SerialCheck(self):
self.SerDevLs = [_('none')]
context = pyudev.Context()
for device in context.list_devices(subsystem='tty'):
i=device['DEVNAME']
if '/dev/ttyU' in i or '/dev/ttyA' in i or '/dev/ttyS' in i or '/dev/ttyO' in i or '/dev/r' in i or '/dev/i' in i:
self.SerDevLs.append(i)
try:
ii=device['DEVLINKS']
value= ii[ii.rfind('/dev/ttyOP_'):]
if value.find('/dev/ttyOP_') >=0:
self.SerDevLs.append(value)
except: pass
def delete_sentences(self,event):
self.sentences.SetValue(_('nothing'))
def add_sentence(self,event):
talker=self.talker.GetValue()
sent=self.sent.GetValue()
if not re.match('^[*A-Z]{2}$', talker):
self.ShowMessage(_('Talker must have 2 uppercase characters. The symbol * matches any character.'))
return
if not re.match('^[*A-Z]{3}$', sent):
self.ShowMessage(_('Sentence must have 3 uppercase characters. The symbol * matches any character.'))
return
r_sentence=talker+sent
if r_sentence=='*****':
self.ShowMessage(_('You must enter 2 uppercase characters for talker or 3 uppercase characters for sentence. The symbol * matches any character.'))
return
if r_sentence in self.sentences.GetValue():
self.ShowMessage(_('This sentence already exists.'))
return
if self.sentences.GetValue()==_('nothing'):
self.sentences.SetValue(r_sentence)
else:
self.sentences.SetValue(self.sentences.GetValue()+','+r_sentence)
def create_gpsd(self,event):
self.name.SetValue('gpsd')
self.typeComboBox.SetValue('TCP')
self.address.SetValue('127.0.0.1')
self.port.SetValue('2947')
def ok_conn(self,event):
name= self.name.GetValue()
name=name.replace(' ', '_')
if not re.match('^[_0-9a-z]{1,13}$', name):
self.ShowMessage(_('"Name" must be a string between 1 and 13 lowercase letters and/or numbers which is not used as name for another input/output.'))
return
if self.con_type=='serial':
type_conn='Serial'
if self.deviceComboBox.GetValue(): port_address=self.deviceComboBox.GetValue()
else:
self.ShowMessage(_('You must select a Port.'))
return
bauds_port=self.baudComboBox.GetValue()
if self.con_type=='network':
type_conn=self.typeComboBox.GetValue()
if self.address.GetValue(): port_address=self.address.GetValue()
else:
self.ShowMessage(_('You must enter an Address.'))
return
if self.port.GetValue(): bauds_port=self.port.GetValue()
else:
self.ShowMessage(_('You must enter a Port.'))
return
if self.filter.GetValue()==_('none') and self.sentences.GetValue()!=_('nothing'):
self.ShowMessage(_('You must select a Filter type.'))
return
filter_type='none'
filtering='nothing'
if self.filter.GetValue()==_('Accept only sentences:') and self.sentences.GetValue()!=_('nothing'):
filter_type='accept'
filtering=''
r=self.sentences.GetValue()
l=r.split(',')
for index,item in enumerate(l):
if index!=0: filtering+=':'
filtering+='+'+item
filtering+=':-all'
if self.filter.GetValue()==_('Ignore sentences:') and self.sentences.GetValue()!=_('nothing'):
filter_type='ignore'
filtering=''
r=self.sentences.GetValue()
l=r.split(',')
for index,item in enumerate(l):
if index!=0: filtering+=':'
filtering+='-'+item
print self.in_out+','+name+','+type_conn+','+port_address+','+bauds_port+','+filter_type+','+filtering+',1'
self.Destroy()
def cancel_conn(self,event):
self.Destroy()
#Main#############################
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()