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