-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtab1.py
More file actions
161 lines (137 loc) · 8.45 KB
/
tab1.py
File metadata and controls
161 lines (137 loc) · 8.45 KB
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
import dash
from dash import dcc, html
from dash.dependencies import Input, Output, State
import plotly.graph_objs as go
from server import app
import dash_daq as daq
import utilities as ut
import sqlite3 as sql
from datetime import datetime
avgs = {}
# Function to fetch preferences from the database
def fetch_preferences():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("SELECT * FROM preferences")
prefs = c.fetchone()
conn.close()
return prefs
# Function to fetch the latest run number and its end time
def fetch_run_number():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("SELECT * FROM run_number ORDER BY run_id DESC LIMIT 1")
run = c.fetchone()
conn.close()
return run
# Function to fetch slider positions
def fetch_slider_positions():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("SELECT * FROM sliderpos")
sliderpos = c.fetchone()
conn.close()
return sliderpos
def tab1():
prefs = fetch_preferences()
heading = prefs[1]
max_requests = prefs[2]
scan_frequency = prefs[3]
interval = prefs[4]
factors = prefs[5:12]
names = prefs[12:19]
max_values = prefs[20:27]
slider_positions = fetch_slider_positions()
pos1, pos2 = slider_positions[1], slider_positions[2]
run = fetch_run_number()
time_end = run[2]
state = True if time_end is None else False
tab1 = html.Div([
dcc.Interval(id='interval_gauges', interval=interval, n_intervals=0, disabled=False),
html.Div([html.P(str(heading))], style={'width': '80%', 'height': 80, 'marginTop': 0, 'textAlign': 'center', 'fontSize': 30, 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
html.Div(style={'width': '20%', 'height': 30, 'marginTop': 0, 'textAlign': 'center', 'fontSize': 30, 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
html.Div(style={'color': 'white', 'width': '20%', 'backgroundColor': 'black', 'float': 'right', 'height': '60px', 'textAlign': 'center'},
children=[daq.BooleanSwitch(id='start', on=state, style={'backgroundColor': 'black', 'width': '100px'}),
html.Div(id='output_status', style={'color': 'red', 'height': '15px', 'textAlign': 'left', 'padding': '5px'}),
html.Div(id='output_lastid', style={'color': 'white', 'height': '15px', 'textAlign': 'left', 'padding': '5px'})]),
html.Div(style={'width': '80%', 'textAlign': 'center', 'color': 'orange', 'backgroundColor': 'black', 'height': '50px', 'float': 'left'}),
html.Div(id='stop_output', style={'marginTop': 0, 'height': '40px', 'width': '20%', 'textAlign': 'center', 'color': 'orange', 'backgroundColor': 'black', 'float': 'left'}),
html.Div(style={'marginTop': 0, 'width': '80%', 'backgroundColor': 'black', 'float': 'left', 'height': '100px', 'fontWeight': 'bold'},
children=[dcc.Slider(id='s1', min=0, max=max_values[5], value=pos1, step=0.1, updatemode='drag',
marks={i * max_values[5] // 10: str(i * max_values[5] // 10) for i in range(11)}),
html.Div([html.Div(id='s1_output'), names[5]], style={'marginTop': 10, 'color': 'white', 'textAlign': 'center', 'fontSize': '16px'})]),
html.Div(id='dummy2', style={'width': '20%', 'backgroundColor': 'black', 'float': 'right', 'height': '50px'}),
html.Div(style={'marginTop': 0, 'height': '50px', 'width': '20%', 'textAlign': 'center', 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
html.Div(style={'width': '80%', 'backgroundColor': 'black', 'float': 'left', 'height': '100px', 'fontWeight': 'bold'},
children=[dcc.Slider(id='s2', min=0, max=max_values[6], step=0.1, value=pos2, updatemode='drag',
marks={i * max_values[6] // 10: str(i * max_values[6] // 10) for i in range(11)}),
html.Div([html.Div(id='s2_output'), names[6]], style={'marginTop': 10, 'color': 'white', 'textAlign': 'center', 'fontSize': '16px'})]),
html.Div([html.Div([html.P(id='clock')], style={'fontSize': 20, 'marginTop': 0, 'height': '100px', 'width': '20%', 'textAlign': 'center', 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
daq.Gauge(id='ain0', size=200, color='red', showCurrentValue=True, units=names[0], value=0, label=names[0], max=max_values[0], min=0, style={'width': '20%', 'height': 300, 'marginTop': 0, 'textAlign': 'center', 'fontSize': 30, 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
daq.Gauge(id='ain1', size=200, color='green', showCurrentValue=True, units=names[1], value=0, label=names[1], max=max_values[1], min=0, style={'width': '20%', 'height': 300, 'marginTop': 0, 'textAlign': 'center', 'fontSize': 30, 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
daq.Gauge(id='ain2', size=200, color='orange', showCurrentValue=True, units=names[2], value=0, label=names[2], max=max_values[2], min=0, style={'width': '20%', 'height': 300, 'marginTop': 0, 'textAlign': 'center', 'fontSize': 30, 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
daq.Gauge(id='ain3', size=200, color='blue', logarithmic=False, showCurrentValue=True, units=names[3], value=0, label=names[3], max=max_values[3], min=0, style={'width': '20%', 'height': 300, 'marginTop': 0, 'textAlign': 'center', 'fontSize': 30, 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
daq.Gauge(id='ain4', size=200, color={"gradient": False, "ranges": {"yellow": [0, 1.2], "green": [1.2, 4.8], "red": [4.8, 6]}}, logarithmic=True, showCurrentValue=True, units=names[4], value=0, label=names[4], max=max_values[4], min=0, style={'width': '20%', 'height': 300, 'marginTop': 0, 'textAlign': 'center', 'fontSize': 30, 'color': 'white', 'backgroundColor': 'black', 'float': 'left'})]),
html.Div(children='Bee Research Pty Ltd 2021', style={'width': '100%', 'height': 200, 'marginTop': 0, 'textAlign': 'center', 'fontSize': 12, 'color': 'white', 'backgroundColor': 'black', 'float': 'left'}),
html.Div(id='slipper')
])
return tab1
@app.callback(
Output('output_status', 'children'),
Output('ain0', 'value'),
Output('ain1', 'value'),
Output('ain2', 'value'),
Output('ain3', 'value'),
Output('ain4', 'value'),
Output('clock', 'children'),
Output('s1_output', 'children'),
Output('s2_output', 'children'),
Input('interval_gauges', 'n_intervals'),
State('start', 'on'),
State('s1', 'value'),
State('s2', 'value')
)
def record_status_text(n_intervals, on, s1, s2):
conn = sql.connect("labjackdb.db")
c = conn.cursor()
run = fetch_run_number()
lastid = run[0]
time_end = run[2]
table_name = "dac_readings" if on else "temp_readings"
state = ut.if_recording(c)
status = f'RECORDING {lastid}' if state else "NOT RECORDING"
if not state:
lastid = 1
c.execute(f"SELECT ain0, ain1, ain2, ain3 FROM {table_name} WHERE run_id = ? ORDER BY time DESC LIMIT 1", (lastid,))
readings = c.fetchone()
c.execute(f"""SELECT ((ain4 - LAG(ain4, 30) OVER (ORDER BY time ASC)) / (time - LAG(time, 30) OVER (ORDER BY time ASC))) * 1000000 AS cps
FROM {table_name} WHERE run_id = ? ORDER BY time DESC LIMIT 1""", (lastid,))
cps = c.fetchone()
cps = 0 if cps is None else cps[0]
if readings:
avgs.update({f'AIN{i}': readings[i] for i in range(4) if readings[i] is not None})
else:
avgs.update({f'AIN{i}': 0 for i in range(4)})
if on and time_end is not None:
now = int(datetime.now().timestamp() * 1000000)
c.execute("INSERT INTO run_number (time_start) VALUES (?)", (now,))
if not on and time_end is None:
now = int(datetime.now().timestamp() * 1000000)
c.execute("UPDATE run_number SET time_end = ? WHERE run_id = ?", (now, lastid))
run = fetch_run_number()
time_end = run[2]
conn.commit()
conn.close()
clock = datetime.now().strftime("%H:%M:%S")
return [status, avgs.get('AIN0', 0), avgs.get('AIN1', 0), avgs.get('AIN2', 0), avgs.get('AIN3', 0), cps, clock, s1, s2]
@app.callback(
Output('slipper', 'value'),
Input('s1', 'value'),
Input('s2', 'value')
)
def update_sliderpos(s1, s2):
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("UPDATE sliderpos SET s1 = ?, s2 = ?", (s1, s2))
conn.commit()
conn.close()