Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSupdate.py
More file actions
Latest commit
152 lines (131 loc) · 4.96 KB
/
Copy pathDSupdate.py
File metadata and controls
152 lines (131 loc) · 4.96 KB
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
importgetopt
importConfigParser
importsys
importcodecs
importdatastore
try:
## try Python 2.6 json lib
importjson
exceptImportError:
importsimplejsonasjson
classdbloader:
def__init__(self,config,fname):
self.config=config
self.dataobjects= {}
## load datastore
self.load()
ifconfig['type'].upper() =="KNX":
self.KNXloader(fname)
elifconfig['type'].upper() =="OWFS":
self.OWFSloader(fname)
self.save()
defreadConfig(self,configfile):
cfile=codecs.open(configfile,"r")
## fix for missingsectionheaders
whileTrue:
pos=cfile.tell()
ifcfile.readline().startswith("["):
break
cfile.seek(pos)
config= {}
configparse=ConfigParser.SafeConfigParser()
configparse.readfp(cfile)
forsectioninconfigparse.sections():
options=configparse.options(section)
config[section] = {}
foroptinoptions:
try:
config[section][opt] =configparse.getint(section,opt)
exceptValueError:
try:
config[section][opt] =configparse.getfloat(section,opt)
exceptValueError:
config[section][opt] =configparse.get(section,opt)
returnconfig
defKNXloader(self,fname):
ga=self.readConfig(fname)
forkeyinga.keys():
id="%s:%s"% (self.config['namespace'],key)
self.dataobjects[id] =datastore.dataObject(False,id,ga[key]['name'].decode('iso-8859-15'))
self.dataobjects[id].config['dptid'] =ga[key]['dptsubid']
defOWFSloader(self,fname):
ow=self.readConfig(fname)
forkeyinow.keys():
id="%s:%s_temperature"% (self.config['namespace'],key)
## Fixme: Humidity ... not included
print"add %s "%id
self.dataobjects[id] =datastore.dataObject(False,id,ow[key]['name'].decode('iso-8859-15'))
if'resolution'inow[key]:
self.dataobjects[id].config['resolution'] =ow[key]['resolution']
if'eib_ga_temp'inow[key]:
iflen(ow[key]['eib_ga_temp']) >0:
knxid="KNX:%s"%ow[key]['eib_ga_temp']
print"Try to attach to %s "%knxid
self.dataobjects[id].connected.append(knxid)
print"attached"
defdebug(self,msg=''):
printmsg
defload(self):
self.debug("load DATASTORE")
try:
db=codecs.open(self.config['datastore'],"rb",encoding='utf-8')
loaddict=json.load(db)
db.close()
forname, objinloaddict.items():
self.dataobjects[name] =datastore.dataObject(False,obj['id'],obj['name'])
self.dataobjects[name].lastupdate=obj['lastupdate']
self.dataobjects[name].config=obj['config']
self.dataobjects[name].connected=obj['connected']
self.debug("%d entries loaded in DATASTORE"%len(self.dataobjects))
except:
## no DB File
print"DB not found"
defsave(self):
self.debug("save DATASTORE")
savedict= {}
## FIXME: user create a __reduce__ method for the Datastoreitem object
forname,objinself.dataobjects.items():
savedict[name] = {
'name' : obj.name,
'id' : obj.id,
'value' : obj.value,
'lastupdate' : obj.lastupdate,
'config' : obj.config,
'connected' : obj.connected
}
dbfile=codecs.open(self.config['datastore'],"wb",encoding='utf-8')
json.dump(savedict,dbfile,sort_keys=True,indent=3)
dbfile.close()
foriinsavedict.keys():
iflen(savedict[i]['connected'])>0:
printsavedict[i]
if__name__=="__main__":
importos
importsys
importgetopt
try:
opts, args=getopt.getopt(sys.argv[1:], "f:d:n:t:", ["file=","datastore=","namespace=","type="])
exceptgetopt.GetoptError:
print"Fehler"
sys.exit(2)
config= {
'datastore' : 'datastore.db',
'namespace' : 'KNX',
'type' : False
}
fname=False
foropt, arginopts:
ifoptin ("-d","--datastore"):
config['datastore'] =arg
ifoptin ("-n","--namespace"):
config['namespace'] =arg
ifoptin ("-t","--type"):
config['type'] =arg
ifoptin ("-f","--file"):
fname=arg
ifnotfname:
print"no configfilename"
sys.exit(1)
ifnotconfig['type']:
config['type'] =config['namespace']
dbloader(config,fname)