-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
97 lines (74 loc) · 2.53 KB
/
config.py
File metadata and controls
97 lines (74 loc) · 2.53 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
import json
import time
import machine
class Config:
_config={}
_version={}
_name = None
_config_file = None
_dirty = False
def __init__(self, config_file="/config.json", version_file="/version.json", name="PicoSensor"):
self._name = name
self._config_file = config_file
try:
with open(config_file, "rt") as f:
self._config = json.load(f)
except:
self.reset()
try:
with open(version_file, "rt") as f:
self._version = json.load(f)
except:
self._version = {"version": 0}
def reset(self):
self._config = { 'name': self._name, 'sample_count': 4, 'sample_interval': 15, 'tvoc_wait': 3 }
self._dirty = True
self.save()
def save(self):
if self._dirty:
with open(self._config_file, "wt") as f:
json.dump(self._config, f)
self._dirty = False
def get_version(self):
return self._version['version']
def update(self, data):
for (k,v) in data.items():
if k == "utc":
date, time = v.split("T")
time, rubbish = time.split("Z")
yy, mon, dd = date.split("-")
hh, mm, ss = time.split(":")
print(f"date: {date}")
print(f"time: {time}")
dt_tuple = (int(yy), int(mon), int(dd), 0, int(hh), int(mm), int(ss), 0,)
print(f"Tuple: {dt_tuple}")
machine.RTC().datetime( dt_tuple )
else:
self.setVal(k,v)
self.save()
############################################
def getVal(self, key):
return self._config[key]
def setVal(self, key, value):
if key in self._config:
if self._config[key] == value:
return
self._config[key] = value
self._dirty = True
if __name__ == "__main__":
config = Config(name="PicoSensor", config_file="/config_test.json")
print(config._config)
config.reset()
print(config._config)
print(f"version: {config.get_version()}")
name = config.getVal('name')
print(f"name: {name}")
config.setVal('name', 'new name')
config.save()
print(f"config: {config._config}")
sample_interval = config.getVal('sample_interval')
print(f"sample_interval: {sample_interval}")
config.update({ "utc": "1959-12-11T22:11:05Z0"})
print(time.gmtime())
config.update({ "xxx": "xxx"})
print(f"_config: {config._config}")