/
MiniYatt.py
executable file
·383 lines (284 loc) · 12.8 KB
/
MiniYatt.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env python
import sip
import sys
import serial
from serial import SerialException
from copy import deepcopy
import socket
from threading import Timer
sip.setapi('QVariant', 2)
from PyQt4.QtCore import QThread
from PyQt4.QtGui import QWidget, QTextEdit, QLineEdit, QShortcut, QKeySequence
from PyQt4.QtGui import QAction, QIcon, QActionGroup, QComboBox
from SerialCommunicationThread import *
from AddButtonWidget import *
from SequenceFactory import *
from SocketMock import *
class Mode:
MANUAL_MODE = 1
MANUAL_PLAYER_MODE = 2
PEGASUS_MODE = 3
class MainWindow(QtGui.QMainWindow):
def __init__(self, mock = None):
super(QtGui.QMainWindow, self).__init__()
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create("Plastique"));
self.setWindowTitle("Mini Yatt")
self.mock = mock
self.mode = Mode.MANUAL_PLAYER_MODE
self.currentCmd = 0
self.setupSocketThread()
self.createCentralWidget()
self.lineEdit.returnPressed.connect(self.sendData)
self.connect(self.thread, QtCore.SIGNAL("newData"), self.readNewData)
self.createShortcuts()
self.createActions()
self.createToolbar()
self.createActionGroup()
self.createMenus()
self.readSettings()
self.connect(self.scenarioComboBox,
QtCore.SIGNAL("currentIndexChanged( const QString)"), self.selectScenario)
for sequenceName in sorted(SequenceFactory().getAllSequenceNames()):
print(sequenceName)
self.scenarioComboBox.addItem(sequenceName)
def subscribeForFilename(self):
return "Log.miniyatt"
def saveFile(self):
filename = self.subscribeForFilename()
file = QtCore.QFile(filename)
if not file.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text):
QtGui.QMessageBox.warning(self, "Application",
"Cannot write file %s:\n%s." % (fileName, file.errorString()))
return False
outf = QtCore.QTextStream(file)
outf << self.textEdit.toPlainText()
file.close()
return True
def createToolbar(self):
self.fileToolBar = self.addToolBar("ATCommands Toolbar")
self.fileToolBar.addAction(self.addAct)
def createCentralWidget(self):
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.lineEdit = QtGui.QLineEdit()
self.textEdit = QtGui.QTextEdit()
self.scenarioComboBox = QtGui.QComboBox(self)
# currentIndexChanged = self.selectScenario )
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.lineEdit)
self.layout.addWidget(self.textEdit)
self.layout.addWidget(self.scenarioComboBox)
centralWidget.setLayout(self.layout)
def createAction(self, actionName, atCmd):
newActionObject = QAction(actionName, self)
newActionObject.setData(atCmd)
return newActionObject
def setupSocketThread (self):
self.socket = None
if self.mock == True:
QtCore.qDebug("MOCK MODE ON")
self.socket = SocketMock()
else:
try:
self.socket = serial.Serial(3,115200)
except SerialException as e:
QtCore.qDebug("Exception catched!")
QtGui.QMessageBox.about(self, "Error", "Socket in use")
self.thread = SerialCommunicationThread(self.socket)
self.thread.start()
def createMenus(self):
self.fileMenu = self.menuBar().addMenu("File")
self.fileMenu.addAction(self.quitAct)
self.fileMenu.addAction(self.saveAct)
self.modeMenu = self.menuBar().addMenu("Mode")
self.modeActionGroup = QActionGroup(self,triggered=self.modeActionTriggered)
self.modeActionGroup.setExclusive(True)
allModeList = []
allModeList.append(["Manual Mode", False, Mode.MANUAL_MODE])
allModeList.append(["Manual Player Mode",True,Mode.MANUAL_PLAYER_MODE])
allModeList.append(["Pegasus Mode", False,Mode.PEGASUS_MODE])
for modeData in allModeList:
modeActionObject = QAction(modeData[0],self)
modeActionObject.setCheckable(True)
modeActionObject.setChecked(modeData[1])
modeActionObject.setData(modeData[2])
self.modeActionGroup.addAction(modeActionObject)
self.modeMenu.addAction(modeActionObject)
def createShortcuts(self):
self.shortcut = QShortcut(QKeySequence("Ctrl+O"), self)
self.shortcut.activated.connect(self.openFile)
self.upArrow = QShortcut(QKeySequence("Up"), self)
self.upArrow.activated.connect(self.previousCmd)
self.downArrow = QShortcut(QKeySequence("Down"), self)
self.downArrow.activated.connect(self.nextCmd)
def previousCmd(self):
QtCore.qDebug("Hello From Up")
self.currentCmd -= 1
def nextCmd(self):
QtCore.qDebug("Hello From Down")
self.currentCmd += 1
def createActions(self):
self.addAct = QAction(QIcon("addIcon16.png"), "Add", self,
triggered=self.addButton)
#self.addButtonAct = QAction("Add Button",self,triggered=self.addButton)
self.quitAct = QAction("Quit", self, shortcut=QKeySequence.Quit,
triggered=QtGui.qApp.quit)
self.saveAct = QAction("Save", self, shortcut=QKeySequence.Save,
triggered=self.saveFile)
def modeActionTriggered(self, action):
QtCore.qDebug(str(action.data()))
###### PEGASUS MODE ########
if action.data() == Mode.PEGASUS_MODE:
QtCore.qDebug("PEGASUS MODE SELECTED - co powinienem dalej zrobic?")
self.mode = Mode.PEGASUS_MODE
#tutaj przyda sie jakis iterator
currentSequence = self.scenarioComboBox.currentText()
#self.commands = SequenceFactory().getNewSequence(currentSequence)
self.commands = SequenceFactory().getNewSequence("cmu900")
self.currentCmd = 0
#TODO
### Wczytaj pierwsza sekwencje
timeoutValue = self.commands[0].timeout
self.response = deepcopy(self.commands[0].response)
QtCore.qDebug("*****")
QtCore.qDebug(repr(self.response))
QtCore.qDebug("*****")
self.sendDataMsg(self.commands[0].cmd)
self.timer = Timer(timeoutValue, self.timeout)
self.timer.start()
def createActionGroup(self):
self.actionGroup=QActionGroup(self,triggered=self.actionGroupTriggered)
allActionsItemList = []
allActionsItemList.append(["Bands?", "at^scfg=radio/band"])
allActionsItemList.append(["at^moni", "at^moni"])
allActionsItemList.append(["PIN","at+cpin=9999"])
allActionsItemList.append(["PIN?","at+cpin?"])
allActionsItemList.append(["AT", "AT"])
allActionsItemList.append(["Model","at^siekret=1"])
allActionsItemList.append(["Shutdown","at^smso"])
for actionItem in allActionsItemList:
newActionObject = self.createAction(actionItem[0], actionItem[1])
self.actionGroup.addAction(newActionObject)
self.fileToolBar.addAction(newActionObject)
def selectScenario(self, sequenceName):
QtCore.qDebug("Hello From SelectScenario")
QtCore.qDebug(sequenceName)
self.commands = deepcopy(SequenceFactory().getSequence(sequenceName))
self.currentCmd = 0
self.lineEdit.setText(self.commands[0])
def closeEvent(self, event):
self.writeSettings()
def readSettings(self):
settings = QtCore.QSettings("REC", "MiniYatt")
pos = settings.value("pos", QtCore.QPoint(200, 200))
size = settings.value("size", QtCore.QSize(400, 400))
self.resize(size)
self.move(pos)
def writeSettings(self):
settings = QtCore.QSettings("REC", "MiniYatt")
settings.setValue("pos", self.pos())
settings.setValue("size", self.size())
def openFile(self):
QtCore.qDebug("Hello From Open FIle")
def addButton(self):
self.addButtonWidget = AddButtonWidget()
self.addButtonWidget.setWindowModality(QtCore.Qt.ApplicationModal)
self.addButtonWidget.resize(200,200)
self.addButtonWidget.show()
QtCore.qDebug("Hello From Add Button")
def readNewData(self, data):
data = data.strip()
self.textEdit.append(data)
self.textEdit.moveCursor(QtGui.QTextCursor.End)
if self.mode == Mode.PEGASUS_MODE:
self.sequencePlayer(data)
def timeout(self):
QtCore.qDebug("TIMEOUT!")
def sequencePlayer(self, data):
#TODO - poprawic indeksy
### Sprawdz, czy dostales oczekiwane dane
QtCore.qDebug("JESTEM w sequencePlayer")
QtCore.qDebug(repr(self.response))
QtCore.qDebug(data)
if data.find(self.response[0]) >= 0 :
if len(self.response) == 1:
self.timer.cancel()
waitBeforeNext = self.commands[self.currentCmd].waitBeforeNext
self.currentCmd += 1
#TRZEBA USTAWIC WSZYSTKIE PARAMETRY DLA NOWEJ KOMENDY
#wysylamy i zapominamy - nasluchujemy na odpowiedzi tutaj.
self.nextMsg = self.commands[self.currentCmd].cmd
self.timeoutValue = self.commands[self.currentCmd].timeout
self.response = self.commands[self.currentCmd].response
self.timerWaitBeforeNext = Timer(waitBeforeNext, self.sendNext)
self.timerWaitBeforeNext.start()
else:
self.response.pop(0)
else:
QtCore.qDebug("IGNORE")
def sendDataMsg(self, msg):
#msg = self.lineEdit.text()
QtCore.qDebug("JESTEM W SendDataMsg: " + msg)
self.socket.write(str.encode(msg) + b"\r\n")
if self.mode == Mode.MANUAL_PLAYER_MODE:
self.currentCmd += 1
self.lineEdit.setText(self.commands[self.currentCmd])
else:
#addHistory(msg)
self.lineEdit.setText("")
def sendNext(self):
self.sendDataMsg(self.nextMsg)
self.timer = Timer(self.timeoutValue, self.timeout)
self.timer.start()
def sendData(self):
QtCore.qDebug("sendData odpalone!")
self.sendDataMsg(self.lineEdit.text())
def actionGroupTriggered(self, action):
QtCore.qDebug("Hello from actionGroupTriggered")
QtCore.qDebug(action.data())
self.socket.write(str.encode(action.data()) + b"\r\n")
if __name__ == '__main__':
import sys
import platform
app = QtGui.QApplication(sys.argv)
mainWin = MainWindow(platform.node() == "vermont")
#mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
# senddata
#QtCore.qDebug(self.textEdit.text())
#self.socket.write(str.encode(self.textEdit.text()) + b"\r\n")
######## Dodawanie przycisku do toolbara akcji
#self.actionGroup.addAction("at^moni", "at^moni") #QKeySequence.New!
#self.pinAct = QAction("at+cpin=9999", self, triggered=self.sendPin)
#self.atAct = QAction("at", self, triggered=self.sendAt)
#self.modelAct = QAction("model", self, triggered=self.sendModel)
#self.smsoAct = QAction("Shutdown", self, triggered=self.sendShutdown)
#####################
# def playSequence(self):
# #here comes all the logic
# self.sequence.play();
# self.sequence = Sequence(init_band = "AT^SCFG=radio/band,4,4",
# first_band = "AT^SCFG=radio/band,8,8",
# second_band = "AT^SCFG=radio/band,4,12")
# self.playSequence();
#self.thread.newData.connect(self.textEdit2.setText)
#self.socket.write(b"AT\r\n")
#self.socket.write(b"AT" + b"\r\n")
#self.socket.write(str.encode("AT") + b"\r\n")
#NOT WORKING
# self.connect(self.textEdit, QtCore.SIGNAL("returnPressed()"), self, QtCore.SLOT("sendData()"))
#WORKING
# self.connect(self.textEdit, QtCore.SIGNAL("returnPressed()"), self.sendData)
#self.commands = copy.deepcopy(scenarios['cmu850'])
#self.commands = copy.deepcopy(scenarios['cmu850_AllowAll'])
#self.commands = copy.deepcopy(scenarios['cmu900'])
#self.commands = copy.deepcopy(scenarios['cmu900_AllowAll'])
#self.modeMenu.addActionGroup(self.manualModeAction)
#self.modeMenu.addAction(self.manualModeAction)
#self.modeMenu.addAction(self.manualPlayerModeAction)
#self.modeMenu.addAction(self.pegasusModeAction)
# def tryNextCommand(self, data):
# pass
# def actionDispatcher(self, data):
# pass