-
Notifications
You must be signed in to change notification settings - Fork 37
/
statusdataDBmod.py
executable file
·220 lines (173 loc) · 6.5 KB
/
statusdataDBmod.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from __future__ import print_function
from builtins import str
import logging
import filestoragemod
logger = logging.getLogger("hydrosys4."+__name__)
DATAFILENAME="statusdata.txt"
# ///////////////// --- UTILITY FOR STATUS VARIABLES ------
def read_status_data(data,element,variable,permanent=False, storeid=""):
output=""
if element in data:
#print " element present"
elementdata=data[element]
if variable in elementdata:
output=elementdata[variable]
else:
print(" element NOT present")
# element not present in the data use the default
data[element]=data["default"].copy()
elementdata=data[element]
#print data
if variable in elementdata:
output=elementdata[variable]
# check with persistent element
isok=False
if permanent:
if not storeid=="":
isok, persistenooutput=readstoredvariable(storeid,element,variable)
if isok:
if not (persistenooutput==output):
print("status variable output mismatch between current value and stored value")
logger.error('status variable output mismatch between current value =%s and stored value =%s', str(output) , str(persistenooutput))
output=persistenooutput
return output
def read_status_levels(data,element="",from_file=False, storeid=""):
output=""
if not from_file:
output=data
if (element)and(element in data):
#print " element present"
output=data[element]
else:
# check with persistent element
if not storeid=="":
isok, persistenooutput=readstoredvariable(storeid,element)
if isok:
output=persistenooutput
return output
def write_status_data(data,element,variable,value,permanent=False, storeid=""):
if element in data:
data[element][variable]=value
else:
if "default" in data:
data[element]=data["default"].copy()
data[element][variable]=value
else:
logger.error('ERROR: There is no default data for element %s ', element )
return
# in case of permanent option
if permanent:
if not storeid=="":
storevariable(storeid,element,variable,value)
def remove_element_status_data(data,element,permanent=False, storeid=""):
if element in data:
del data[element]
# in case of permanent option
if permanent:
if not storeid=="":
remove_stored_element(storeid,element)
# ///////////////// --- END STATUS VARIABLES ------
# the status variable is a dictionary of dictionary, the first is the element, for each element there is a dictionary of variables
# the same status procedures apply to several different variables. At this module level there is no identification possible of this variables
# the variable identification is needed in case the elements are the same. No unique identification possible.
# when the status is set to be permanent, then to have a unique copy saved on file, it is needed to specify the unique identifier then coincide with the variable name normally
#AUTO_data={} # dictionary of dictionary
#AUTO_data["default"]={"lasteventtime":datetime.utcnow()- timedelta(minutes=waitingtime),"lastactiontime":datetime.utcnow()- timedelta(minutes=waitingtime),"actionvalue":0, "alertcounter":0, "infocounter":0, "status":"ok" , "threadID":None , "blockingstate":False}
# model of data to store:
#{"storeid":"mystoreid", "element1":{"variable1":"xx", "variable2":"yy"}, "element2":{"variable1":"xx1", "variable2":"yy2"}}
def storevariable(storeid,element,variable,value):
filedata=[] # list of dictionaries
readok=filestoragemod.readfiledata(DATAFILENAME,filedata)
# even if readok is not True, the below procedure creates a new file
#search for the storeid and elemet
storeidfound=False
for thedict in filedata:
if "storeid" in thedict:
if thedict["storeid"]==storeid:
storeidfound=True
#search for elemet
if element in thedict:
thedict[element][variable]=value
else:
#create element
dicttemp={}
dicttemp[variable]=value
thedict[element]=dicttemp
if not storeidfound:
# add another row to the dictionary
dicttemp={}
dicttemp["storeid"]=storeid
dicttemp[element]={}
dicttemp[element][variable]=value
filedata.append(dicttemp)
filestoragemod.savefiledata(DATAFILENAME,filedata)
return True
def remove_stored_element(storeid,element):
filedata=[] # list of dictionaries
readok=filestoragemod.readfiledata(DATAFILENAME,filedata)
# even if readok is not True, the below procedure creates a new file
#search for the storeid and elemet
elementfound=False
for thedict in filedata:
if "storeid" in thedict:
if thedict["storeid"]==storeid:
#search for elemet
if element in thedict:
elementfound=True
del thedict[element]
else:
print(" no elelemt, nothing to delete ")
if elementfound:
filestoragemod.savefiledata(DATAFILENAME,filedata)
return True
def readstoredvariable(storeid,element="",variable=""):
filedata=[] # list of dictionaries
readok=filestoragemod.readfiledata(DATAFILENAME,filedata)
# even if readok is not True, the below procedure creates a new file
#search for the storeid and elemet
isok=False
value=""
for thedict in filedata:
if "storeid" in thedict:
if thedict["storeid"]==storeid:
value=thedict
isok=True
if element:
isok=False
#search for elemet
if element in thedict:
value=thedict[element]
isok=True
if variable:
isok=False
if variable in thedict[element]:
value=thedict[element][variable]
isok=True
return isok, value
if __name__ == '__main__':
# comment
a=10
PROVA_data={} # dictionary of dictionary
PROVA_data["default"]={"actionvalue":0, "alertcounter":0, "infocounter":0, "status":"ok" , "threadID":None , "blockingstate":False}
element="laprova"
variable="comefare"
value="adesso"
permanent=True
storeid="PROVA_data"
write_status_data(PROVA_data,element,variable,value,permanent, storeid)
element="laprova2"
variable="comefare"
value="adesso"
permanent=True
storeid="PROVA_data"
write_status_data(PROVA_data,element,variable,value,permanent, storeid)
element="laprova2"
variable="comefare"
value="adesso"
permanent=True
storeid="PROVA_data2"
write_status_data(PROVA_data,element,variable,value,permanent, storeid)
print(read_status_data(PROVA_data,element,variable,permanent, storeid))
value="nonadesso"
write_status_data(PROVA_data,element,variable,value)
print(read_status_data(PROVA_data,element,variable,permanent, storeid))