forked from philippoo66/optolink-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviessdata_util.py
124 lines (98 loc) · 4.16 KB
/
viessdata_util.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
'''
Copyright 2024 philippoo66
Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import datetime
import os
import settings_ini
import utils
def get_headline() -> str:
now = datetime.datetime.now()
dt = "{2:04d}-{1:02d}-{0:02d}".format(now.day, now.month, now.year)
cols = []
for itm in settings_ini.poll_items:
cols.append(itm[1])
capts = ';'.join([format(addr, '04X') for addr in cols])
return f";{dt};{capts};"
def get_filename() -> str:
now = datetime.datetime.now()
yr, cw, _ = now.isocalendar()
return "{0:04d}_KW{1:02d}_data.csv".format(yr, cw)
def minutes_since_monday_midnight() -> int:
# Aktuelles Datum und Uhrzeit abrufen
now = datetime.datetime.now()
# Montag 0 Uhr 0 Minuten berechnen
monday_midnight = now - datetime.timedelta(days=now.weekday(), hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond)
# Differenz zwischen dem aktuellen Zeitpunkt und Montag 0 Uhr 0 Minuten in Minuten berechnen
return int((now - monday_midnight).total_seconds() // 60)
def formatted_timestamp() -> str:
# Aktuellen Zeitstempel abrufen
now = datetime.datetime.now()
# Wochentag abrufen und in das entsprechende Kürzel umwandeln
weekday = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"][now.weekday()]
# Zeitstempel im gewünschten Format erstellen
return "{0}-{1:02d}:{2:02d}:{3:02d}".format(weekday, now.hour, now.minute, now.second)
wrbuffer = []
mins_old = 0
recent_filename = get_filename()
def buffer_csv_line(data, force_write=False):
global wrbuffer
global mins_old
global recent_filename
sline = None
mins_new = minutes_since_monday_midnight()
new_week = (mins_new < mins_old) # new week
mins_old = mins_new
buffer_full = (len(wrbuffer) >= settings_ini.buffer_to_write)
if(data):
sline = str(mins_new) + ";"
sline += formatted_timestamp() + ";"
# decimal separator
if(settings_ini.dec_separator == ","):
tbreplaced = "."
else:
tbreplaced = ","
for i in range(0, len(settings_ini.poll_items)):
sval = str(data[i])
if(utils.to_number(data[i]) != None):
# format number, anything else left like it is
sval = sval.replace(tbreplaced, settings_ini.dec_separator)
sline += sval + ";"
if(force_write and not new_week):
wrbuffer.append(sline)
sline = None
if(force_write or new_week or buffer_full):
try:
csvfile = os.path.join(settings_ini.viessdata_csv_path, recent_filename)
writehd = (not os.path.exists(csvfile))
with open(csvfile, 'a') as f:
if(writehd):
hl = get_headline()
f.write(hl + '\n')
for ln in wrbuffer:
f.write(ln + '\n')
f.flush()
wrbuffer = []
recent_filename = get_filename()
except Exception as e:
print("ERROR write csv: ", e)
if(sline is not None):
wrbuffer.append(sline)
# main for test only
if __name__ == "__main__":
print(get_headline())
print(get_filename())
# Minuten seit Montag 0 Uhr 0 Minuten berechnen und ausgeben
print("Minuten seit Montag 0 Uhr 0 Minuten:", minutes_since_monday_midnight())
# Formatierter Zeitstempel ausgeben
print("Formatierter Zeitstempel:", formatted_timestamp())
# Formatierter Zeitstempel ausgeben
# print("Formatierter Zeitstempel2:", formatted_timestamp2())