Blame view

datalogger-gui.py 9.05 KB
bb3889092   mer0m   Add files via upload
1
2
3
4
5
6
7
8
9
10
11
12
  #!/usr/bin/env python
  
  # -*- coding: utf-8 -*-
  
  import time, os, instruments, inspect, sys, threading
  import PyQt4.QtGui as QtGui
  from PyQt4.QtCore import pyqtSlot
  
  #==============================================================================
  #==============================================================================
  
  class acq_routine():
b7ceca5e3   bmarechal   datalogger-gui.py...
13
      def __init__(self, instrument, channels, vtypes, address, samplingtime, path = os.getcwd(), fileduration = 24*3600):
9058343c5   bmarechal   some minor fixes
14
          exec('self.instrument = instruments.%s.%s(%s, %s, "%s")'%(instrument, instrument, channels, vtypes, address))
bb3889092   mer0m   Add files via upload
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
          self.path = path
          self.samplingtime = samplingtime
          self.fileduration = fileduration
  
      def makeTree(self):
          try:
              year = time.strftime("%Y", time.gmtime(self.t0))
              month = time.strftime("%Y-%m", time.gmtime(self.t0))
              os.chdir(self.path + '/' + year + '/' + month)
          except:
              try:
                  os.chdir(self.path + '/' + year)
                  os.mkdir(month)
                  os.chdir(self.path + '/' + year + '/' + month)
              except:
                  os.chdir(self.path)
                  os.mkdir(year)
                  os.chdir(self.path + '/' + year)
                  os.mkdir(month)
                  os.chdir(self.path + '/' + year + '/' + month)
  
      def connect(self):
          self.instrument.connect()
  
          self.t0 = time.time()
          self.filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(self.t0)) + '-' + self.instrument.model() + '.dat'
          self.makeTree()
          self.data_file = open(self.filename, 'wr', 0)
  
      def start(self):
          tic = time.time()
  
          if (time.time() - self.t0 >= self.fileduration) & (self.fileduration >0 ):
              self.data_file.close()
  
              self.t0 = time.time()
              self.filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(self.t0)) + '-' + self.instrument.model() + '.dat'
              self.makeTree()
              self.data_file = open(self.filename, 'wr', 0)
  
          #epoch time
          epoch = time.time()
          #MJD time
          mjd = epoch / 86400.0 + 40587
          # Meas values
          meas = self.instrument.getValue()
          meas = meas.replace(",", "\t")
          meas = meas.replace(";", "\t")
          meas = meas.replace("+", "")
050af2218   bmarechal   datalogger-gui: r...
64
          meas = meas.replace("E", "e")
bb3889092   mer0m   Add files via upload
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
  
          string = "%f\t%f\t%s" % (epoch, mjd, meas)
          self.data_file.write(string) # Write in a file
          print(string)
  
          self.thread = threading.Timer(self.samplingtime - (time.time() - tic), self.start)
          self.thread.start()
  
      def stop(self):
          self.thread.cancel()
          self.instrument.disconnect()
          self.data_file.close()
  
  #==============================================================================
  #==============================================================================
  
  class mainGui():
      def __init__(self):
          self.setWindow()
          self.setSignalsSlots()
          self.runApp()
  
      def setWindow(self):
          self.a = QtGui.QApplication(sys.argv)
          self.w = QtGui.QMainWindow()
          self.w.resize(640, 480)
          self.w.setWindowTitle('datalogger-gui')
  
          self.wid = QtGui.QWidget()
          self.w.setCentralWidget(self.wid)
          self.layout = QtGui.QGridLayout()
          self.wid.setLayout(self.layout)
  
          self.comboInst = QtGui.QComboBox()
          self.layout.addWidget(self.comboInst, 0, 0)
9058343c5   bmarechal   some minor fixes
100
          self.address = QtGui.QLineEdit()
c8a8ff0f6   bmarechal   modify gui
101
102
103
          self.address.setMinimumWidth(140)
          self.address.setMaximumWidth(140)
          self.layout.addWidget(self.address, 0, 1)
bb3889092   mer0m   Add files via upload
104

b7ceca5e3   bmarechal   datalogger-gui.py...
105
106
107
108
109
110
111
112
          self.samplingtime = QtGui.QDoubleSpinBox()
          #self.samplingtime.setMinimumWidth(60)
          #self.samplingtime.setMaximumWidth(60)
          self.samplingtime.setMinimum(0.1)
          self.samplingtime.setMaximum(1000)
          self.samplingtime.setSingleStep(0.1)
          self.samplingtime.setValue(1)
          self.layout.addWidget(self.samplingtime, 0, 2)
bb3889092   mer0m   Add files via upload
113
114
          self.startButton = QtGui.QPushButton()
          self.startButton.setText('Start log')
c8a8ff0f6   bmarechal   modify gui
115
          self.layout.addWidget(self.startButton, 99, 0)
7bc4035fc   bmarechal   enable startButto...
116
          self.startButton.setEnabled(False)
bb3889092   mer0m   Add files via upload
117
118
119
  
          self.stopButton = QtGui.QPushButton()
          self.stopButton.setText('Stop log')
c8a8ff0f6   bmarechal   modify gui
120
          self.layout.addWidget(self.stopButton, 99, 1)
faa244fdd   bmarechal   Enable/disable st...
121
          self.stopButton.setEnabled(False)
bb3889092   mer0m   Add files via upload
122
123
124
  
          self.textDisplay = QtGui.QLabel()
          self.textDisplay.setText('>>')
c8a8ff0f6   bmarechal   modify gui
125
          self.layout.addWidget(self.textDisplay, 99, 2)
bb3889092   mer0m   Add files via upload
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  
          self.setComboInst()
          self.updateSignal()
  
      def setComboInst(self):
          for name, obj in inspect.getmembers(instruments):
              if inspect.ismodule(obj) and name.startswith('__') == False and name.startswith('abstract') == False:
                  self.comboInst.addItem(name)
  
      def setSignalsSlots(self):
          self.comboInst.currentIndexChanged.connect(self.updateSignal)
          self.startButton.clicked.connect(self.startLog)
          self.stopButton.clicked.connect(self.stopLog)
  
      def runApp(self):
          self.w.show()
587654767   bmarechal   datalogger-gui: s...
142
          self.a.aboutToQuit.connect(self.closeEvent)
bb3889092   mer0m   Add files via upload
143
          sys.exit(self.a.exec_())
587654767   bmarechal   datalogger-gui: s...
144
145
146
147
148
149
      def closeEvent(self):
          try:
              self.stopLog()
          except:
              pass
          print('Done')
bb3889092   mer0m   Add files via upload
150
151
152
153
      @pyqtSlot()
      def updateSignal(self):
          for i in reversed(range(5, self.layout.count())):
              self.layout.itemAt(i).widget().setParent(None)
9058343c5   bmarechal   some minor fixes
154
          defaultAddress = ''
bb3889092   mer0m   Add files via upload
155
156
157
158
159
          channelsAviables = []
          vtypesAviables = []
  
          exec('channelsAviables = instruments.%s.ALL_CHANNELS'%self.comboInst.currentText())
          exec('vtypesAviables = instruments.%s.ALL_VAL_TYPE'%self.comboInst.currentText())
9058343c5   bmarechal   some minor fixes
160
          exec('defaultAddress = instruments.%s.ADDRESS'%self.comboInst.currentText())
bb3889092   mer0m   Add files via upload
161

9058343c5   bmarechal   some minor fixes
162
          self.address.setText(defaultAddress)
bb3889092   mer0m   Add files via upload
163
164
165
166
167
168
169
170
171
172
173
174
  
          self.checkBoxChannels = [None]*len(channelsAviables)
          self.chListVtypes = [None]*len(self.checkBoxChannels)
  
          for i in range(len(self.checkBoxChannels)):
              self.checkBoxChannels[i] = QtGui.QCheckBox()
              self.checkBoxChannels[i].setText(channelsAviables[i])
              self.checkBoxChannels[i].setChecked(False)
              self.chListVtypes[i] = QtGui.QListWidget()
              for vtype in vtypesAviables:
                  self.chListVtypes[i].addItem(vtype)
              self.chListVtypes[i].setCurrentRow(0)
c8a8ff0f6   bmarechal   modify gui
175
176
              self.layout.addWidget(self.checkBoxChannels[i], i+3, 1)
              self.layout.addWidget(self.chListVtypes[i], i+3, 2)
bb3889092   mer0m   Add files via upload
177
178
              self.checkBoxChannels[i].stateChanged.connect(self.infoSignal)
              self.chListVtypes[i].currentItemChanged.connect(self.infoSignal)
9058343c5   bmarechal   some minor fixes
179
          self.address.textChanged.connect(self.infoSignal)
b7ceca5e3   bmarechal   datalogger-gui.py...
180
          self.samplingtime.valueChanged.connect(self.infoSignal)
faa244fdd   bmarechal   Enable/disable st...
181

bb3889092   mer0m   Add files via upload
182
183
184
185
186
          self.infoSignal()
  
      @pyqtSlot()
      def infoSignal(self):
          self.instToLog = self.comboInst.currentText()
9058343c5   bmarechal   some minor fixes
187
          self.addressToLog = self.address.text()
bb3889092   mer0m   Add files via upload
188
189
          self.chToLog = []
          self.vTypeToLog = []
b7ceca5e3   bmarechal   datalogger-gui.py...
190
          self.ts = self.samplingtime.value()
bb3889092   mer0m   Add files via upload
191
192
193
  
          for i in range(len(self.checkBoxChannels)):
              if self.checkBoxChannels[i].isChecked():
8b41ae5b5   bmarechal   enable/disable vt...
194
                  self.chListVtypes[i].setEnabled(True)
bb3889092   mer0m   Add files via upload
195
196
                  self.chToLog.append(str(self.checkBoxChannels[i].text()))
                  self.vTypeToLog.append(str(self.chListVtypes[i].currentItem().text()))
8b41ae5b5   bmarechal   enable/disable vt...
197
198
              else:
                  self.chListVtypes[i].setEnabled(False)
7bc4035fc   bmarechal   enable startButto...
199
200
201
202
203
204
205
          allChannelsUnchecked = False
          for i in self.checkBoxChannels:
              allChannelsUnchecked = allChannelsUnchecked or i.isChecked()
          if allChannelsUnchecked == False:
              self.startButton.setEnabled(False)
          else:
              self.startButton.setEnabled(True)
b7ceca5e3   bmarechal   datalogger-gui.py...
206
          self.textDisplay.setText('>> %s@%s - %s - %s - %d'%(self.instToLog, self.addressToLog, self.chToLog, self.vTypeToLog, self.ts))
bb3889092   mer0m   Add files via upload
207

b7ceca5e3   bmarechal   datalogger-gui.py...
208
          self.myLog = acq_routine(self.instToLog, self.chToLog, self.vTypeToLog, self.addressToLog, self.ts)
bb3889092   mer0m   Add files via upload
209
210
211
  
      @pyqtSlot()
      def startLog(self):
faa244fdd   bmarechal   Enable/disable st...
212
213
          self.startButton.setEnabled(False)
          self.stopButton.setEnabled(True)
9058343c5   bmarechal   some minor fixes
214
          self.address.setEnabled(False)
b7ceca5e3   bmarechal   datalogger-gui.py...
215
          self.samplingtime.setReadOnly(True)
120da9952   bmarechal   enable/disable co...
216
          self.comboInst.setEnabled(False)
f5cf6b2b6   bmarechal   disable address a...
217
218
          for i in self.checkBoxChannels:
              i.setEnabled(False)
56312b9ab   bmarechal   improve GUI
219
220
          for i in self.chListVtypes:
              i.setEnabled(False)
bb3889092   mer0m   Add files via upload
221
222
223
224
225
          self.myLog.connect()
          self.myLog.start()
  
      @pyqtSlot()
      def stopLog(self):
faa244fdd   bmarechal   Enable/disable st...
226
227
          self.startButton.setEnabled(True)
          self.stopButton.setEnabled(False)
9058343c5   bmarechal   some minor fixes
228
          self.address.setEnabled(True)
b7ceca5e3   bmarechal   datalogger-gui.py...
229
          self.samplingtime.setReadOnly(False)
120da9952   bmarechal   enable/disable co...
230
          self.comboInst.setEnabled(True)
56312b9ab   bmarechal   improve GUI
231
232
233
234
235
236
237
          for i in range(len(self.checkBoxChannels)):
              if self.checkBoxChannels[i].isChecked():
                  self.checkBoxChannels[i].setEnabled(True)
                  self.chListVtypes[i].setEnabled(True)
              else:
                  self.checkBoxChannels[i].setEnabled(True)
                  self.chListVtypes[i].setEnabled(False)
bb3889092   mer0m   Add files via upload
238
239
240
241
242
243
244
          self.myLog.stop()
  
  #==============================================================================
  #==============================================================================
  
  if __name__ == "__main__":
      mainGui()