homecontrol-dash: use f-strings

This commit is contained in:
Konstantin Koslowski 2020-02-10 16:05:18 +01:00
parent 9c0b659b5f
commit d021b20ada

View file

@ -13,11 +13,12 @@ URL_BASE="http://innocence:5000"
def get_sensors(): def get_sensors():
ret = {} ret = {}
try: try:
url = "%s/sensor/get" % URL_BASE url = f"{URL_BASE}/sensor/get"
res = requests.get(url) res = requests.get(url)
ret = res.json() ret = res.json()
except Exception as ex: except Exception as ex:
print('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args)) print(f"Exception Type: {type(ex).__name__}, args:\n{ex.args}")
return ret return ret
@ -25,15 +26,20 @@ def get_values(sensorId, min_ts, max_ts, limit):
ret = {} ret = {}
if sensorId: if sensorId:
try: try:
url = "%s/sensor/get_values/%s?min_ts=%s&max_ts=%s&limit=%s" % (URL_BASE, url = f"{URL_BASE}/sensor/get_values/{sensorId}?min_ts={min_ts}&max_ts={max_ts}&limit={limit}"
sensorId, min_ts, max_ts, limit)
# print(url) # print(url)
res = requests.get(url) res = requests.get(url)
ret = res.json()[sensorId] ret = res.json()[sensorId]
except Exception as ex: except Exception as ex:
print('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args)) print(f"Exception Type: {type(ex).__name__}, args:\n{ex.args}")
return ret return ret
def pTime(value, fmt="%Y/%m/%d %H:%M"):
if value > 0:
return time.strftime(fmt, time.localtime(float(value)))
else:
return 0
sensors = get_sensors() sensors = get_sensors()
sensor = next(iter(sensors), None) sensor = next(iter(sensors), None)
@ -108,7 +114,7 @@ def update_slider_min(sensorId):
min_ts = int(res["values"][0]["ts"]) min_ts = int(res["values"][0]["ts"])
sensorType = res["sensorType"] sensorType = res["sensorType"]
# print("min: [%f [%f] %f]" % (min_ts, min_ts, max_ts)) # print(f"min: [{min_ts} [{cur_ts}] {max_ts}]"
return min_ts, cur_ts, max_ts return min_ts, cur_ts, max_ts
@ -116,8 +122,7 @@ def update_slider_min(sensorId):
Output('slider-min-txt', 'children'), Output('slider-min-txt', 'children'),
[Input('slider-min', 'value')]) [Input('slider-min', 'value')])
def update_slider_min_txt(value): def update_slider_min_txt(value):
return "From: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))) if return f"From: {pTime(value)}"
value > 0 else 0)
@app.callback( @app.callback(
[Output('slider-max', 'min'), Output('slider-max', 'value'), [Output('slider-max', 'min'), Output('slider-max', 'value'),
@ -132,7 +137,7 @@ def update_slider_max(sensorId):
min_ts = int(res["values"][0]["ts"]) min_ts = int(res["values"][0]["ts"])
sensorType = res["sensorType"] sensorType = res["sensorType"]
# print("max: [%f [%f] %f]" % (min_ts, max_ts, max_ts)) # print(f"max: [{min_ts} [{max_ts}] {max_ts}]"
return min_ts, max_ts, max_ts return min_ts, max_ts, max_ts
@ -140,14 +145,14 @@ def update_slider_max(sensorId):
Output('slider-max-txt', 'children'), Output('slider-max-txt', 'children'),
[Input('slider-max', 'value')]) [Input('slider-max', 'value')])
def update_slider_max_txt(value): def update_slider_max_txt(value):
return "To: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value)))) return f"To: {pTime(value)}"
@app.callback( @app.callback(
Output('slider-limit-txt', 'children'), Output('slider-limit-txt', 'children'),
[Input('slider-limit', 'value')]) [Input('slider-limit', 'value')])
def update_slider_limit_txt(value): def update_slider_limit_txt(value):
return "Limit: %s" % (value if value > 0 else None) return f"Limit: {value if value > 0 else None}"
@app.callback( @app.callback(
@ -161,7 +166,7 @@ def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
if "values" in res: if "values" in res:
v = res["values"] v = res["values"]
s = res["sensorType"] s = res["sensorType"]
x = [time.strftime("%m%d-%H%M", time.localtime(float(v[i]["ts"]))) for i in range(len(v))] x = [pTime(v[i]["ts"], fmt="%m%d-%H%M") for i in range(len(v))]
if s == "luminance": if s == "luminance":
y = [log10(v[i]["value"]/5+1) for i in range(len(v))] y = [log10(v[i]["value"]/5+1) for i in range(len(v))]
else: else:
@ -171,8 +176,8 @@ def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
x = [0] x = [0]
y = [0] y = [0]
return { return {
'data': [ {'x': x, 'y': y, 'mode': 'line', 'name': "%s" % (s)} ], 'data': [ {'x': x, 'y': y, 'mode': 'line', 'name': f'{s}'} ],
'layout': { 'title': 'Data for sensor: %s (%d elements)' % (s, len(x)) } 'layout': { 'title': f'Data for sensor: {s} ({len(x)} elements)' }
} }
if __name__ == '__main__': if __name__ == '__main__':