-
Notifications
You must be signed in to change notification settings - Fork 37
/
jsonFormUtils.py
150 lines (104 loc) · 5.07 KB
/
jsonFormUtils.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
import json
import logging
import os
from datetime import datetime,date,timedelta
class utils():
def __init__(self,itemname):
self.logger = logging.getLogger("hydrosys4."+__name__+"."+itemname)
self.templatepath="templates"
self.databasepath="database"
self.databasedefaultpath=os.path.join(self.databasepath, "default")
self.itemname=itemname
self.fileNameList=self.listtemplatefiles()
def listtemplatefiles(self):
fileNameList=[]
for file in os.listdir(self.templatepath):
if file.endswith(".json"):
fileNameList.append(file)
return fileNameList
def readJsonFormFilePlusValues(self):
jsonschema, itemslist = self.readJsonFormFile()
datadict=self.readDataFile()
jsonschema["value"]=datadict
return jsonschema, itemslist
def readJsonFormFile(self):
jsonschema={}
itemlist=[]
# create the path
filename=self.itemname + ".json"
# check if file exist
if filename not in self.fileNameList:
print("template file not found, abort")
self.logger.error("template file not found, abort")
return jsonschema, itemlist
fullpath=os.path.join(self.templatepath, filename)
# this form uses the template from JSONform
try:
# Opening JSON file
f = open(fullpath)
print("file open" , f)
# returns JSON object as
# a dictionary
jsonschema = json.load(f)
# Closing file
f.close()
except:
print("problem opening the form schema file")
self.logger.error("problem opening the form schema file")
if ("schema" in jsonschema):
for item in jsonschema["schema"]:
#print (item)
itemlist.append(item)
return jsonschema, itemlist
def readDataFile(self):
datadict={}
# create the path
filename=self.itemname + ".txt"
fullpath=os.path.join(self.databasepath, filename)
#if os.path.isfile(fullpath):
try:
with open(fullpath) as json_file:
datadict = json.load(json_file)
#print (datadict , "aaa")
except:
print("problem opening the Data file, try with the default setting")
self.logger.error("problem opening the Data file, try with the default setting")
# default path
fullpath=os.path.join(self.databasedefaultpath, "def"+filename)
try:
with open(fullpath) as json_file:
datadict = json.load(json_file)
# Save the file
#print (datadict , "bbb")
self.saveDataFile(datadict)
except:
print("problem opening the default Data file")
self.logger.error("problem opening the Default Data file")
#print(datadict , "*************************************************************************+")
return datadict
def saveDataFile(self,datadict):
# create the path
isok=False
if datadict:
filename=self.itemname + ".txt"
fullpath=os.path.join(self.databasepath, filename)
try:
with open(fullpath,'w') as outfile:
json.dump(datadict, outfile)
isok=True
except:
print("problem saving the data file")
self.logger.error("problem saving the Data file")
return isok
if __name__ == '__main__':
mycalss=utils('HC12form')
datadict=mycalss.readDataFile()
print (datadict)
#datadict=mycalss.saveDataFile(datadict)
"""
AT+Cxxx: Change wireless communication channel, selectable from 001 to 127 (for wireless channels exceeding 100, the communication distance cannot be guaranteed). The default value for the wireless channel is 001, with a working frequency of 433.4MHz. The channel stepping is 400KHz, and the working frequency of channel
AT+FUx: Change the serial port transparent transmission mode of the module. Four modes are available, namely FU1, FU2, FU3, and FU4. Only when the serial port speed, channel, and transparent transmission mode of two modules is set to be the same,can normal wireless communications occur. For more details, please see the abovesection “Wireless Serial Port Transparent Transmission”.
FU4 mode is useful for maximum range, up to 1.8km. Only a single baud rate of 1200bps is supported, with the in the air baud rate reduced to 500bps for improved communication distance. This mode can only be used for small amounts ofdata (each packet should be 60 bytes or less), and the time interval between sending packets must not be too short (preferably no less than 2 seconds) in order to prevent loss of data.
AT+Px: Set the transmitting power of the module, with x selectable from 1 to 8, default 8.
"""