improve set_instrument

- this still needs some checks
This commit is contained in:
2025-04-29 15:25:27 +02:00
parent 335d0a5078
commit e84fba1b49
3 changed files with 42 additions and 22 deletions

View File

@ -132,8 +132,10 @@ class SEHistory(InfluxDBWrapper):
for merge_key, (col_idx, curves) in col_info.items():
tags = summarize_tags(curves)
primary = tags.get(merge[0], '_value')
table = Table(tags, merge_key[0], ('_time', primary))
result[primary if single_merge else merge_key[1][:len(merge)]] = table
final_key = primary if single_merge else merge_key[1][:len(merge)]
table = result.get(final_key)
if table is None:
result[final_key] = table = Table(tags, merge_key[0], ('_time', primary))
by_idx[col_idx] = table
for row in rows:
by_idx[row[2]].append((row[0], row[1]))
@ -221,6 +223,7 @@ class SEHistory(InfluxDBWrapper):
for stream in sorted(all_entries):
entries = all_entries[stream]
current = None
last = None
instrument = None
for entry in sorted(entries, key=lambda e: e[0][0]):
(ts, flag), tags = entry
@ -232,10 +235,13 @@ class SEHistory(InfluxDBWrapper):
tags['instrument'] = instrument
current = [ts, tags]
elif current:
if ts < current[0] + 60:
# probably not containing real data
current = None
if current or include_finished:
if ts > current[0] + 60:
# else its probably not containing real data
last = current
current = None
if include_finished:
current = last
if current:
tags = current[1]
ins = tags.get('instrument')
if ins == '0':
@ -269,12 +275,15 @@ class SEHistory(InfluxDBWrapper):
start, end = abs_range(start, end)
inperiod = self.query(start, end, _measurement='_stream_', _field='on', interval=interval,
stream=stream, device=None, instrument=None, **tags)
# prepend previous to the items in period or create if not there
for key, rows in previous.items():
if key in inperiod:
inperiod[key].insert(0, rows[0])
else:
inperiod[key] = rows
# append items after the ones in period, ignoring keys not present in period
# in the same go, create by_stream dict, joining tables with common stream
by_stream = {} # dict <stream> of [<ts>, <flag>, <instrument>, <device>]
for key, table in inperiod.items():
nextrow = nextrows.get(key)
@ -284,12 +293,13 @@ class SEHistory(InfluxDBWrapper):
elist = by_stream.setdefault(stream, [])
for row in table:
elist.append([row[0], row[1], stream, device, instrument])
# combine now by either instrument or stream, if instrument is undefined (='0')
by_key = {}
for stream, rows in by_stream.items():
rows.sort()
instrument = '0'
for row in rows:
if row[-1] is None:
if not row[-1]:
row[-1] = instrument
else:
instrument = row[-1]
@ -364,7 +374,7 @@ class SEHistory(InfluxDBWrapper):
:param value: instrument, "0" to unassign the instrument or None when switching the stream off
:param ts: the time or None when now
"""
flag = bool(value)
flag = value is not None
try:
previns, prevts = self.get_instrument(stream, ts, **tags)
if prevts is not None and (previns is None or (ts or 1e10) < prevts):