forked from Hydrosys4/Master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fertilizerdbmod.py
271 lines (203 loc) · 6.58 KB
/
fertilizerdbmod.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
"""
fertilizer UI setting storage utilities
"""
import logging
import os
import os.path
import sys
import string
from datetime import datetime,date,timedelta
import time
import filestoragemod
import hardwaremod
# ///////////////// -- GLOBAL VARIABLES AND INIZIALIZATION --- //////////////////////////////////////////
global FTDATAFILENAME
FTDATAFILENAME="ftdata.txt"
global DEFFTDATAFILENAME
DEFFTDATAFILENAME="default/defftdata.txt"
global FTdata
FTdata=[]
# read FTdata -----
if not filestoragemod.readfiledata(FTDATAFILENAME,FTdata): #read setting file
#read from default file
filestoragemod.readfiledata(DEFFTDATAFILENAME,FTdata)
print "Fertilizer writing default calibration data"
filestoragemod.savefiledata(FTDATAFILENAME,FTdata)
# end read IOdata -----
# ///////////////// --- END GLOBAL VARIABLES ------
#-- start filestorage utility--------////////////////////////////////////////////////////////////////////////////////////
# filestoragemod.readfiledata(filename,filedata)
# filestoragemod.savefiledata(filename,filedata)
# filestoragemod.appendfiledata(filename,filedata)
# filestoragemod.savechange(filename,searchfield,searchvalue,fieldtochange,newvalue)
# filestoragemod.deletefile(filename)
def consitencycheck():
# this routine align the fertilizer table elements with the Hardware available elements "name" labelled with usedfor "fertilizing"
elementlist=getelementlist()
recordkey="element"
elementlistfile=[]
for ln in FTdata:
if recordkey in ln:
elementlistfile.append(ln[recordkey])
tabletoadd=[]
for tablename1 in elementlist:
found=False
for tablename2 in elementlistfile:
if tablename1==tablename2 :
found=True
if not(found) :
tabletoadd.append(tablename1)
tabletoremove=[]
for tablename1 in elementlistfile:
found=False
for tablename2 in elementlist:
if tablename1==tablename2 :
found=True
if not(found) :
tabletoremove.append(tablename1)
#print " ---------------------------------------------------------------------<----------------<-----------______________"
#print "to add ", tabletoadd
#print "to remove " , tabletoremove
#print "FTdata ", FTdata
# get the dictionary line with an element as reference (from default file)
defdata=[]
filestoragemod.readfiledata(DEFFTDATAFILENAME,defdata)
for ln in defdata:
if recordkey in ln:
referenceln=dict(ln)
break
for tablename in tabletoadd:
# add copying from element:"1"
#print "adding table ", tablename
ln=dict(referenceln)
ln[recordkey]=tablename
FTdata.append(ln)
for tablename in tabletoremove:
for ln in FTdata:
if recordkey in ln:
if ln[recordkey]==tablename:
FTdata.remove(ln)
saveFTsetting()
def replacewordandsave(oldword,newword):
filestoragemod.replacewordandsave(FTDATAFILENAME,oldword,newword)
filestoragemod.readfiledata(FTDATAFILENAME,FTdata)
def restoredefault():
filestoragemod.deletefile(FTDATAFILENAME)
filestoragemod.readfiledata(DEFFTDATAFILENAME,FTdata)
consitencycheck()
def saveFTsetting():
filestoragemod.savefiledata(FTDATAFILENAME,FTdata)
def getparamlist():
recordkey="name"
recordvalue="listparam"
datalist=[]
for ln in FTdata:
if recordkey in ln:
if ln[recordkey]==recordvalue:
ind=0
for rw in ln:
if rw!=recordkey:
ind=ind+1
datalist.append(ln[str(ind)])
return datalist
def getelementlist():
recordkey=hardwaremod.HW_FUNC_USEDFOR
recordvalue="fertilizercontrol"
keytosearch=hardwaremod.HW_INFO_NAME
datalist=hardwaremod.searchdatalist(recordkey,recordvalue,keytosearch)
print "elementlist= ",datalist
return datalist
def getrowdata(recordvalue,paramlist,index):
recordkey="element"
datalist=[]
for ln in FTdata:
if recordkey in ln:
if ln[recordkey]==recordvalue:
for param in paramlist:
datalist.append(int(ln[param][index]))
return datalist
def gettable(index):
paramlist=getparamlist()
#print "paramlist" , paramlist
elementlist=getelementlist()
datalist=[]
for row in elementlist:
rowdatalist=getrowdata(row,paramlist,index)
datalist.append(rowdatalist)
#print datalist
return datalist
def replacerow(element,dicttemp):
searchfield="element"
searchvalue=element
for line in FTdata:
if searchfield in line:
if line[searchfield]==searchvalue:
for row in line:
line[row]=dicttemp[row]
filestoragemod.savefiledata(FTDATAFILENAME,FTdata)
return True
return False
def changesaveFTsetting(FTname,FTparameter,FTvalue):
# questo il possibile dizionario: { 'name':'', 'm':0.0, 'q':0.0, 'lastupdate':'' } #variabile tipo dizionario
for line in FTdata:
if line["name"]==FTname:
line[FTparameter]=FTvalue
saveFTsetting()
return True
return False
def searchdata(recordkey,recordvalue,keytosearch):
for ln in FTdata:
if recordkey in ln:
if ln[recordkey]==recordvalue:
if keytosearch in ln:
return ln[keytosearch]
return ""
def gettimedata(name):
# return list with three integer values: hour , minute, second
timestr=searchdata("name",name,"time")
returntime=[]
if not timestr=="":
timelist=timestr.split(":")
for timeitem in timelist:
returntime.append(timeitem)
if len(timelist)<3:
returntime.append("00")
return returntime
else:
return ["00","00","00"]
def searchdatalist(recordkey,recordvalue,keytosearch):
datalist=[]
for ln in FTdata:
if recordkey in ln:
if ln[recordkey]==recordvalue:
if keytosearch in ln:
datalist.append(ln[keytosearch])
return datalist
def getfieldvaluelist(fielditem,valuelist):
del valuelist[:]
for line in FTdata:
valuelist.append(line[fielditem])
def getfieldinstringvalue(fielditem,stringtofind,valuelist):
del valuelist[:]
for line in FTdata:
name=line[fielditem]
if name.find(stringtofind)>-1:
valuelist.append(name)
def get_path():
'''Get the path to this script no matter how it's run.'''
#Determine if the application is a py/pyw or a frozen exe.
if hasattr(sys, 'frozen'):
# If run from exe
dir_path = os.path.dirname(sys.executable)
elif '__file__' in locals():
# If run from py
dir_path = os.path.dirname(__file__)
else:
# If run from command line
dir_path = sys.path[0]
return dir_path
#--end --------////////////////////////////////////////////////////////////////////////////////////
if __name__ == '__main__':
# comment
a=10