Commit bb3889092e0d7ebbd1f8b26342429def41df0a56
Committed by
GitHub
1 parent
97c7affa24
Exists in
master
Add files via upload
Showing 1 changed file with 194 additions and 0 deletions Side-by-side Diff
datalogger-gui.py
1 | +#!/usr/bin/env python | |
2 | + | |
3 | +# -*- coding: utf-8 -*- | |
4 | + | |
5 | +import time, os, instruments, inspect, sys, threading | |
6 | +import PyQt4.QtGui as QtGui | |
7 | +from PyQt4.QtCore import pyqtSlot | |
8 | + | |
9 | +#============================================================================== | |
10 | +#============================================================================== | |
11 | + | |
12 | +class acq_routine(): | |
13 | + def __init__(self, instrument, channels, vtypes, adress, path = os.getcwd(), samplingtime = 1, fileduration = 24*3600): | |
14 | + exec('self.instrument = instruments.%s.%s(%s, %s, "%s")'%(instrument, instrument, channels, vtypes, adress)) | |
15 | + self.path = path | |
16 | + self.samplingtime = samplingtime | |
17 | + self.fileduration = fileduration | |
18 | + | |
19 | + def makeTree(self): | |
20 | + try: | |
21 | + year = time.strftime("%Y", time.gmtime(self.t0)) | |
22 | + month = time.strftime("%Y-%m", time.gmtime(self.t0)) | |
23 | + os.chdir(self.path + '/' + year + '/' + month) | |
24 | + except: | |
25 | + try: | |
26 | + os.chdir(self.path + '/' + year) | |
27 | + os.mkdir(month) | |
28 | + os.chdir(self.path + '/' + year + '/' + month) | |
29 | + except: | |
30 | + os.chdir(self.path) | |
31 | + os.mkdir(year) | |
32 | + os.chdir(self.path + '/' + year) | |
33 | + os.mkdir(month) | |
34 | + os.chdir(self.path + '/' + year + '/' + month) | |
35 | + | |
36 | + def connect(self): | |
37 | + self.instrument.connect() | |
38 | + | |
39 | + self.t0 = time.time() | |
40 | + self.filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(self.t0)) + '-' + self.instrument.model() + '.dat' | |
41 | + self.makeTree() | |
42 | + self.data_file = open(self.filename, 'wr', 0) | |
43 | + | |
44 | + def start(self): | |
45 | + tic = time.time() | |
46 | + | |
47 | + if (time.time() - self.t0 >= self.fileduration) & (self.fileduration >0 ): | |
48 | + self.data_file.close() | |
49 | + | |
50 | + self.t0 = time.time() | |
51 | + self.filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(self.t0)) + '-' + self.instrument.model() + '.dat' | |
52 | + self.makeTree() | |
53 | + self.data_file = open(self.filename, 'wr', 0) | |
54 | + | |
55 | + #epoch time | |
56 | + epoch = time.time() | |
57 | + #MJD time | |
58 | + mjd = epoch / 86400.0 + 40587 | |
59 | + # Meas values | |
60 | + meas = self.instrument.getValue() | |
61 | + meas = meas.replace(",", "\t") | |
62 | + meas = meas.replace(";", "\t") | |
63 | + meas = meas.replace("+", "") | |
64 | + | |
65 | + string = "%f\t%f\t%s" % (epoch, mjd, meas) | |
66 | + self.data_file.write(string) # Write in a file | |
67 | + print(string) | |
68 | + | |
69 | + self.thread = threading.Timer(self.samplingtime - (time.time() - tic), self.start) | |
70 | + self.thread.start() | |
71 | + | |
72 | + def stop(self): | |
73 | + self.thread.cancel() | |
74 | + self.instrument.disconnect() | |
75 | + self.data_file.close() | |
76 | + | |
77 | +#============================================================================== | |
78 | +#============================================================================== | |
79 | + | |
80 | +class mainGui(): | |
81 | + def __init__(self): | |
82 | + self.setWindow() | |
83 | + self.setSignalsSlots() | |
84 | + self.runApp() | |
85 | + | |
86 | + def setWindow(self): | |
87 | + self.a = QtGui.QApplication(sys.argv) | |
88 | + self.w = QtGui.QMainWindow() | |
89 | + self.w.resize(640, 480) | |
90 | + self.w.setWindowTitle('datalogger-gui') | |
91 | + | |
92 | + self.wid = QtGui.QWidget() | |
93 | + self.w.setCentralWidget(self.wid) | |
94 | + self.layout = QtGui.QGridLayout() | |
95 | + self.wid.setLayout(self.layout) | |
96 | + | |
97 | + self.comboInst = QtGui.QComboBox() | |
98 | + self.layout.addWidget(self.comboInst, 0, 0) | |
99 | + | |
100 | + self.adress = QtGui.QLineEdit() | |
101 | + self.adress.setMaximumWidth(120) | |
102 | + self.layout.addWidget(self.adress, 99, 0) | |
103 | + | |
104 | + self.startButton = QtGui.QPushButton() | |
105 | + self.startButton.setText('Start log') | |
106 | + self.layout.addWidget(self.startButton, 99, 1) | |
107 | + | |
108 | + self.stopButton = QtGui.QPushButton() | |
109 | + self.stopButton.setText('Stop log') | |
110 | + self.layout.addWidget(self.stopButton, 99, 2) | |
111 | + | |
112 | + self.textDisplay = QtGui.QLabel() | |
113 | + self.textDisplay.setText('>>') | |
114 | + self.layout.addWidget(self.textDisplay, 100, 2) | |
115 | + | |
116 | + self.setComboInst() | |
117 | + self.updateSignal() | |
118 | + | |
119 | + def setComboInst(self): | |
120 | + for name, obj in inspect.getmembers(instruments): | |
121 | + if inspect.ismodule(obj) and name.startswith('__') == False and name.startswith('abstract') == False: | |
122 | + self.comboInst.addItem(name) | |
123 | + | |
124 | + def setSignalsSlots(self): | |
125 | + self.comboInst.currentIndexChanged.connect(self.updateSignal) | |
126 | + self.startButton.clicked.connect(self.startLog) | |
127 | + self.stopButton.clicked.connect(self.stopLog) | |
128 | + | |
129 | + def runApp(self): | |
130 | + self.w.show() | |
131 | + sys.exit(self.a.exec_()) | |
132 | + | |
133 | + @pyqtSlot() | |
134 | + def updateSignal(self): | |
135 | + for i in reversed(range(5, self.layout.count())): | |
136 | + self.layout.itemAt(i).widget().setParent(None) | |
137 | + | |
138 | + defaultAdress = '' | |
139 | + channelsAviables = [] | |
140 | + vtypesAviables = [] | |
141 | + | |
142 | + exec('channelsAviables = instruments.%s.ALL_CHANNELS'%self.comboInst.currentText()) | |
143 | + exec('vtypesAviables = instruments.%s.ALL_VAL_TYPE'%self.comboInst.currentText()) | |
144 | + exec('defaultAdress = instruments.%s.ADRESS'%self.comboInst.currentText()) | |
145 | + | |
146 | + self.adress.setText(defaultAdress) | |
147 | + | |
148 | + self.checkBoxChannels = [None]*len(channelsAviables) | |
149 | + self.chListVtypes = [None]*len(self.checkBoxChannels) | |
150 | + | |
151 | + for i in range(len(self.checkBoxChannels)): | |
152 | + self.checkBoxChannels[i] = QtGui.QCheckBox() | |
153 | + self.checkBoxChannels[i].setText(channelsAviables[i]) | |
154 | + self.checkBoxChannels[i].setChecked(False) | |
155 | + self.chListVtypes[i] = QtGui.QListWidget() | |
156 | + for vtype in vtypesAviables: | |
157 | + self.chListVtypes[i].addItem(vtype) | |
158 | + self.chListVtypes[i].setCurrentRow(0) | |
159 | + self.layout.addWidget(self.checkBoxChannels[i], i, 1) | |
160 | + self.layout.addWidget(self.chListVtypes[i], i, 2) | |
161 | + self.checkBoxChannels[i].stateChanged.connect(self.infoSignal) | |
162 | + self.chListVtypes[i].currentItemChanged.connect(self.infoSignal) | |
163 | + | |
164 | + self.infoSignal() | |
165 | + | |
166 | + @pyqtSlot() | |
167 | + def infoSignal(self): | |
168 | + self.instToLog = self.comboInst.currentText() | |
169 | + self.adressToLog = self.adress.text() | |
170 | + self.chToLog = [] | |
171 | + self.vTypeToLog = [] | |
172 | + | |
173 | + for i in range(len(self.checkBoxChannels)): | |
174 | + if self.checkBoxChannels[i].isChecked(): | |
175 | + self.chToLog.append(str(self.checkBoxChannels[i].text())) | |
176 | + self.vTypeToLog.append(str(self.chListVtypes[i].currentItem().text())) | |
177 | + self.textDisplay.setText('>> %s@%s - %s - %s'%(self.instToLog, self.adressToLog, self.chToLog, self.vTypeToLog)) | |
178 | + | |
179 | + self.myLog = acq_routine(self.instToLog, self.chToLog, self.vTypeToLog, self.adressToLog) | |
180 | + | |
181 | + @pyqtSlot() | |
182 | + def startLog(self): | |
183 | + self.myLog.connect() | |
184 | + self.myLog.start() | |
185 | + | |
186 | + @pyqtSlot() | |
187 | + def stopLog(self): | |
188 | + self.myLog.stop() | |
189 | + | |
190 | +#============================================================================== | |
191 | +#============================================================================== | |
192 | + | |
193 | +if __name__ == "__main__": | |
194 | + mainGui() |