-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstore.py
61 lines (54 loc) · 1.2 KB
/
store.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
#-- OttDIY Python Project, 2020
#
# import json
# import os
#
# STOREDIR = '/eeprom'
#
#
# def save(key, value):
# try:
# stat = os.stat(STOREDIR)
# except:
# os.mkdir(STOREDIR)
#
# filename = STOREDIR + '/' + key + '.json'
# f = open(filename, 'w')
# json_str = json.dumps(value)
# f.write(json_str)
# f.close()
#
#
# def load(key, default = None):
# filename = STOREDIR + '/' + key + '.json'
#
# try:
# f = open(filename)
# json_str = f.read()
# value = json.loads(json_str)
# f.close()
# return value
# except:
# if default == None:
# raise ValueError()
# return default
import json
import os
from esp32 import NVS
def save(key, value, namespace='Eeprom'):
nvs = NVS(namespace)
json_str = json.dumps(value)
nvs.set_blob(key, json_str)
# don't forget to commit
nvs.commit()
def load(key, default=None, namespace='Eeprom'):
nvs = NVS(namespace)
buf = bytearray(1000)
try:
nvs.get_blob(key, buf)
value = json.loads(buf)
return value
except Exception as _:
if default is None:
raise ValueError()
return default