-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
288 lines (253 loc) · 11.9 KB
/
main.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
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import os
import glob
import pyvisa
import sys
import configparser
import time
import numpy as np
import shutil, errno
import visa
import atexit
import threading
import inspect
from contextlib import contextmanager
from influxdb import InfluxDBClient
from collections import OrderedDict
from drivers import FS740
@contextmanager
def get_connection(*args, **kwargs):
connection = InfluxDBClient(*args, **kwargs)
try:
yield connection
finally:
connection.close()
class RecorderINFLUXDB(threading.Thread):
def __init__(self, host, port, database, table, user, password,
driver, dt, driver_kwargs):
# thread control
threading.Thread.__init__(self)
self.active = threading.Event()
# record operating parameters
self.host = host
self.port = port
self.database = database
self.table = table
self.user = user
self.password = password
self.driver = driver
self.dt = dt
if 'resource_manager' in driver_kwargs:
driver_kwargs['resource_manager'] = visa.ResourceManager()
self.driver_kwargs = driver_kwargs
with self.driver(**driver_kwargs) as device:
self.verify = device.VerifyOperation()
# main recording loop
def run(self):
while self.active.is_set():
with get_connection(host = self.host, port = int(self.port), username = self.user,
password = self.password) as con,\
self.driver(**self.driver_kwargs) as device:
con.switch_database(self.database)
device.WriteValueINFLUXDB(con, self.table)
time.sleep(self.dt)
class RecorderINFLUXDBGUI(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
########################################
# recording control and status
########################################
control_frame = tk.LabelFrame(self.parent)
control_frame.grid(row=0, padx=10, pady=10, sticky="nsew")
control_frame.grid_columnconfigure(index=2, weight=1)
record_button = tk.Button(control_frame,
text="\u26ab Start recording", command = self.start_recording)\
.grid(row=0, column=0)
stop_button = tk.Button(control_frame,
text="\u2b1b Stop recording", command = self.stop_recording)\
.grid(row=0, column=1)
self.status = "stopped"
self.status_message = tk.StringVar()
self.status_message.set("Ready to Record")
self.status_label = tk.Label(control_frame, textvariable=self.status_message,
font=("Helvetica", 16),anchor='e')\
.grid(row=0, column=2, sticky='nsew')
########################################
# influxdb
########################################
influxdb_frame = tk.LabelFrame(self.parent, text="INFLUXDB")
influxdb_frame.grid(row=1, padx=10, pady=10, sticky=tk.W)
tk.Label(influxdb_frame, text="host")\
.grid(row=0, column=0, sticky=tk.E)
influxdb_host = tk.Entry(influxdb_frame, width=25,
textvariable=self.parent.config["host"])\
.grid(row=0, column=1, sticky=tk.W)
tk.Label(influxdb_frame, text="port")\
.grid(row=0, column=2, sticky=tk.E)
influxdb_port = tk.Entry(influxdb_frame, width=5,
textvariable=self.parent.config["port"])\
.grid(row=0, column=3, sticky=tk.W)
tk.Label(influxdb_frame, text="user")\
.grid(row=0, column=4, sticky=tk.E)
influxdb_user = tk.Entry(influxdb_frame, width=10,
textvariable=self.parent.config["user"])\
.grid(row=0, column=5, sticky=tk.W)
tk.Label(influxdb_frame, text="pass")\
.grid(row=0, column=6, sticky=tk.E)
influxdb_pass = tk.Entry(influxdb_frame, width=10,
textvariable=self.parent.config["password"])\
.grid(row=0, column=7, sticky=tk.W)
tk.Label(influxdb_frame, text="db")\
.grid(row=1, column=0, sticky=tk.E)
influxdb_database = tk.Entry(influxdb_frame, width=25,
textvariable=self.parent.config["database"])\
.grid(row=1, column=1, sticky=tk.W)
########################################
# devices
########################################
devices_frame = tk.LabelFrame(self.parent, text="Devices")
devices_frame.grid(row=2, padx=10, pady=10, sticky='nsew')
# make the GUI elements and their variables for the list of devices
self.device_GUI_list = OrderedDict()
for d in self.parent.devices:
self.device_GUI_list[d] = OrderedDict([
("enable_b" , tk.Checkbutton(devices_frame, variable=self.parent.devices[d]["enabled"])),
("label" , tk.Label(devices_frame, text=self.parent.devices[d]["label"])),
("dt" , tk.Entry(devices_frame, textvariable=self.parent.devices[d]["dt"], width=5)),
("unit" , tk.Label(devices_frame, text="s", width = 5))
])
signature = inspect.getargspec(self.parent.devices[d]["driver"].__init__)
driver_args = signature.args[1:]
for arg in driver_args:
self.device_GUI_list[d][arg] =\
tk.Entry(devices_frame, textvariable=self.parent.devices[d][arg], width=15)
self.device_GUI_list[d][arg+'_label'] =\
tk.Label(devices_frame, text = arg)
# place the device list GUI elements in a grid
for i,d in enumerate(self.device_GUI_list):
for j,key in enumerate(self.device_GUI_list[d]):
self.device_GUI_list[d][key].grid(row=i,column=j,sticky=tk.W)
def start_recording(self):
# check we're not recording already
if self.status == "recording":
return
# check influxdb host
host = self.parent.config["host"].get()
port = self.parent.config["port"].get()
user = self.parent.config["user"].get()
password = self.parent.config["password"].get()
database = self.parent.config["database"].get()
with get_connection(host = host, port = int(port), username = user,
password = password) as con:
try:
con.ping()
except:
messagebox.showerror("Connection Error", "Error: cannot connect to INFLUXDB database")
self.status_message.set("Error: cannot connect to INFLUXDB database")
return
# connect to devices and check they respond correctly
for key in self.parent.devices:
signature = inspect.getargspec(self.parent.devices[key]["driver"].__init__)
driver_args = signature.args[1:]
d = self.parent.devices[key]
kwargs_recorder = OrderedDict({arg: d[arg].get() for arg in driver_args})
if d["enabled"].get():
d["recorder"] = RecorderINFLUXDB(host, port, database, d["table"], user,
password,
d["driver"], float(d['dt'].get()),
kwargs_recorder)
if d["recorder"].verify != d["correct_response"]:
messagebox.showerror("Device error",
"Error: " + d["label"] + " not responding correctly.")
self.status_message.set("Device configuration error")
return
d["recorder"].active.set()
# start all recorders
for key in self.parent.devices:
if self.parent.devices[key]["enabled"].get():
self.parent.devices[key]["recorder"].start()
# update status
self.status = "recording"
self.status_message.set("Recording")
def stop_recording(self):
if self.status == "stopped":
return
for key in self.parent.devices:
recorder = self.parent.devices[key].get("recorder")
if recorder:
recorder.active.clear()
self.status = "stopped"
self.status_message.set("Recording finished")
class CentrexClockGUI(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.winfo_toplevel().title("CeNTREX Clock Monitoring")
self.parent = parent
atexit.register(self.save_config)
# read program settings
self.config = {}
settings = configparser.ConfigParser()
settings.read("config/settings.ini")
for sect in settings.sections():
for key in settings[sect]:
self.config[key] = tk.StringVar()
self.config[key].set(settings[sect][key])
# read list of devices
self.devices = OrderedDict()
devices = configparser.ConfigParser()
devices.read("config/devices.ini")
for d in devices.sections():
signature = inspect.getargspec(eval(devices[d]["driver"]).__init__)
driver_args = signature.args[1:]
self.devices[d] = OrderedDict([
("label" , devices[d]["label"]),
("driver" , eval(devices[d]["driver"])),
("table" , devices[d]["table"]),
("dt" , tk.StringVar()),
("enabled" , tk.IntVar()),
("correct_response" , devices[d]["correct_response"]),
])
self.devices[d]["enabled"].set(devices[d].getboolean("enabled"),)
self.devices[d]["dt"].set(devices[d].getfloat("dt"),)
for arg in driver_args:
self.devices[d][arg] = tk.StringVar()
self.devices[d][arg].set(devices[d][arg])
# GUI elements
self.recordergui = RecorderINFLUXDBGUI(self, *args, **kwargs)
self.recordergui.grid(row=0, column=0)
def save_config(self):
# write program settings to disk
with open("config/settings.ini", 'w') as settings_f:
settings = configparser.ConfigParser()
settings['influxdb'] = OrderedDict([
('host' , self.config['host'].get()),
('port' , self.config['port'].get()),
('database' , self.config['database'].get()),
('user' , self.config['user'].get()),
('password' , self.config['password'].get()),
])
settings.write(settings_f)
# write device configuration to disk
with open("config/devices.ini", 'w') as dev_f:
dev = configparser.ConfigParser()
for d in self.devices:
signature = inspect.getargspec(self.devices[d]["driver"].__init__)
driver_args = signature.args[1:]
dev[d] = OrderedDict([
("label" , self.devices[d]["label"]),
("driver" , self.devices[d]["driver"].__name__),
("table" , self.devices[d]["table"]),
("dt" , self.devices[d]["dt"].get()),
("enabled" , self.devices[d]["enabled"].get()),
("correct_response" , self.devices[d]["correct_response"]),
])
for arg in driver_args:
dev[d][arg] = self.devices[d][arg].get()
dev.write(dev_f)
if __name__ == "__main__":
root = tk.Tk()
CentrexClockGUI(root).pack(side="top", fill="both", expand=True)
root.mainloop()