-
Notifications
You must be signed in to change notification settings - Fork 37
/
cameradbmod.py
executable file
·252 lines (191 loc) · 5.92 KB
/
cameradbmod.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
# -*- coding: utf-8 -*-
"""
camera setting storage utilities
"""
from __future__ import print_function
from builtins import str
import logging
import os
import os.path
import sys
import string
from datetime import datetime,date,timedelta
import time
import filestoragemod
# ///////////////// -- GLOBAL VARIABLES AND INIZIALIZATION --- //////////////////////////////////////////
DATAFILENAME="camdata.txt"
DEFDATAFILENAME="default/defcamdata.txt"
CAMERAPARAMETERS=["camname","resolution","position","servo","time","active","vflip"]
global data
data=[]
# read data -----
if not filestoragemod.readfiledata(DATAFILENAME,data): #read watering setting file
#read from default file
filestoragemod.readfiledata(DEFDATAFILENAME,data)
print("Watering writing default calibration data")
filestoragemod.savefiledata(DATAFILENAME,data)
# end read data -----
# ///////////////// --- END GLOBAL VARIABLES ------
#-- start filestorage utility--------////////////////////////////////////////////////////////////////////////////////////
# one row is called "default" and include the default data before setting
# {"resolution": "320x240", "fps": "20", "time": "10:30", "name": "default"}
# once the setting is saved, several lines will be produced with name="camera" and "camname" = video1 , video2 , etc
# {"resolution": "320x240", "fps": "20", "time": "10:30", "name": "camera", "camname" : "video1"}
def restoredefault():
filestoragemod.deletefile(DATAFILENAME)
filestoragemod.readfiledata(DEFDATAFILENAME,data)
savesetting()
def savesetting():
filestoragemod.savefiledata(DATAFILENAME,data)
def changecreatesetting(name,camname,parameter,value):
for line in data:
if line["name"]==name:
if line["camname"]==camname:
line[parameter]=value #this change the parameter or create one if not existing
return True
# need to create append new dictionary line
newline={}
newline["name"]=name
newline["camname"]=camname
newline[parameter]=value
data.append(newline)
return True
def getcameradata(videolist):
exportdata=[]
name="camera"
for video in videolist:
found=False
i=0
while (i<len(data))and(not found):
line=data[i]
i=i+1
if (line["name"]==name)and(line["camname"]==video):
newline={}
for param in CAMERAPARAMETERS:
if param in line:
newline[param]=line[param]
else:
newline[param]=""
exportdata.append(newline)
found=True
if (not found):
newline={}
for param in CAMERAPARAMETERS:
if param=="camname":
newline[param]=video
else:
newline[param]=searchdata("name","default",param)
exportdata.append(newline)
return exportdata
def getcameraname():
recordkey="name"
recordvalue="camera"
keytosearch="camname"
return searchdatalist(recordkey,recordvalue,keytosearch)
def getparamlist():
recordkey="name"
recordvalue="listparam"
datalist=[]
for ln in data:
if ln[recordkey]==recordvalue:
ind=0
for rw in ln:
if rw!=recordkey:
ind=ind+1
datalist.append(ln[str(ind)])
return datalist
def getrowdata(recordvalue,paramlist):
recordkey="name"
datalist=[]
for ln in data:
if ln[recordkey]==recordvalue:
for param in paramlist:
datalist.append((ln[param]))
return datalist
def gettable():
paramlist=getparamlist()
elementlist=getelementlist()
datalist=[]
for row in elementlist:
rowdatalist=getrowdata(row,paramlist)
datalist.append(rowdatalist)
return datalist
def replacerow(element,dicttemp):
searchfield="name"
searchvalue=element
for line in data:
if line[searchfield]==searchvalue:
for row in line:
line[row]=dicttemp[row]
filestoragemod.savefiledata(DATAFILENAME,data)
return True
return False
def changesavesetting(name,parameter,value):
# questo il possibile dizionario: { 'name':'', 'm':0.0, 'q':0.0, 'lastupdate':'' } #variabile tipo dizionario
for line in data:
if line["name"]==name:
line[parameter]=value
savesetting()
return True
return False
def searchdata(recordkey,recordvalue,keytosearch):
for ln in data:
if recordkey in ln:
if ln[recordkey]==recordvalue:
if keytosearch in ln:
return ln[keytosearch]
return ""
def isCameraActive(video):
param=searchdata("camname",video,"active")
if param=="True":
return True
else:
return False
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 data:
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 data:
valuelist.append(line[fielditem])
def getfieldinstringvalue(fielditem,stringtofind,valuelist):
del valuelist[:]
for line in data:
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