|
From: <ni...@us...> - 2010-11-04 08:02:04
|
Revision: 67
http://openautomation.svn.sourceforge.net/openautomation/?rev=67&view=rev
Author: nilss1
Date: 2010-11-04 08:01:58 +0000 (Thu, 04 Nov 2010)
Log Message:
-----------
cleanup, removed pickle and XML in favor of json
Modified Paths:
--------------
PyWireGate/trunk/datastore.py
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2010-11-04 07:49:34 UTC (rev 66)
+++ PyWireGate/trunk/datastore.py 2010-11-04 08:01:58 UTC (rev 67)
@@ -1,3 +1,4 @@
+import sys
import time
import threading
@@ -2,13 +3,7 @@
try:
- import cPickle as pickle
-except ImportError:
- import pickle
-
-try:
+ ## use included json in > Python 2.6
import json
except ImportError:
import simplejson as json
-import sys
-import xml.dom.minidom
@@ -32,8 +27,7 @@
self.dataobjects = {}
self.locked = threading.RLock()
self.locked.acquire()
- self.xmltag = lambda x,y,z='': len(z)>0 and "<%s %s>%s</%s>" % (x,z,y,x) or "<%s>%s</%s>" % (x,y,x)
- ## Load XML Database
+ ## Load JSON Database
self.load()
@@ -91,23 +85,10 @@
return self.dataobjects[id]
- ## FIXME: that should belong to the Connector
- def readgaconf(self):
- print "SHOULD NOT BE CALLED"
- ga = self.WG.readConfig("/etc/wiregate/eibga.conf")
- for key in ga.keys():
- obj = self.get("KNX:%s" % key)
- obj.config['dptid'] = ga[key]['dptsubid']
- obj.name = ga[key]['name']
- obj._send = self.WG.connectors['KNX'].send
-
-
def load(self):
- ## TODO:
self.debug("load DATASTORE")
try:
db = open(self.WG.config['WireGate']['datastore'],"rb")
- #loaddict = pickle.Unpickler(db).load()
loaddict = json.load(db)
db.close()
for name, obj in loaddict.items():
@@ -120,17 +101,14 @@
except IOError:
## no DB File
pass
- ## Fixme: should belong to conncetor
- self.locked.release()
- self.readgaconf()
except:
+ self.WG.errorlog()
## error
pass
def save(self):
- ## TODO:
self.debug("save DATASTORE")
self.locked.acquire()
savedict = {}
@@ -145,31 +123,11 @@
'connected' : obj.connected
}
dbfile = open(self.WG.config['WireGate']['datastore'],"wb")
- #db = pickle.Pickler(dbfile,-1)
- #db.dump(savedict)
json.dump(savedict,dbfile,sort_keys=True,indent=3)
dbfile.close()
- def savetoXML(self):
- objitemxml = ""
- for name,obj in self.dataobjects.items():
- configxml = ""
- for cname,cval in obj.config.items():
- configxml += self.xmltag(cname,cval)
- objitemxml += self.xmltag(
- "DSitem",
- self.xmltag("id",name) +
- self.xmltag("value",obj.getValue(),'type=%r' % type(obj.value).__name__) +
- self.xmltag("config",configxml)
- )
- self.locked.release()
- xmlout = xml.dom.minidom.parseString(self.xmltag("Datastore",objitemxml))
- #xmlout = xmlout.toprettyxml(indent=" ")
- xmlout = xmlout.toxml()
- ## write binary to preserve UTF8
- open(self.WG.config['WireGate']['datastore'],"wb").write(xmlout)
-
+
def debug(self,msg):
####################################################
## Function: debug
@@ -225,12 +183,10 @@
def _setValue(self,refered_self):
## self override
- print "Ovveride now"
+ ## override with connector send function
if self.namespace:
self._setValue = self.WG.connectors[self.namespace].setValue
self.WG.connectors[self.namespace].setValue(refered_self)
- ## override with connector send function
- pass
def setValue(self,val,send=False):
try:
@@ -259,40 +215,3 @@
self.read_mutex.release()
-
-#import json
-
-def JSON2DataStore(text):
- obj = {}
- return obj
-
-
-def DataStore2JSON(obj):
- text = ""
- return text
-
-class testtwo:
- def __init__(self):
- self.i=10
- def _send(self,val):
- """Hallo"""
- print str(dir(self)) +str(val+self.i)
-
-class testthree:
- def __init__(self):
- self.u=1
- def send(self,val):
- """its me"""
- print str(dir(self)) +str(self.u)
-
-
-
-if __name__ == "__main__":
- two = testtwo()
- two._send(20)
- three = testthree()
- two._send = three.send
- two._send(20)
- print two._send
- print dir(two)
-
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2010-11-04 10:54:43
|
Revision: 71
http://openautomation.svn.sourceforge.net/openautomation/?rev=71&view=rev
Author: nilss1
Date: 2010-11-04 10:54:37 +0000 (Thu, 04 Nov 2010)
Log Message:
-----------
don't write the db if the datastore.db is corrupt (missing commata....)
Modified Paths:
--------------
PyWireGate/trunk/datastore.py
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2010-11-04 08:12:36 UTC (rev 70)
+++ PyWireGate/trunk/datastore.py 2010-11-04 10:54:37 UTC (rev 71)
@@ -24,6 +24,7 @@
####################################################
self.WG = WireGateInstance
self.log("DATASTORE starting up")
+ self.DBLOADED = False
self.dataobjects = {}
self.locked = threading.RLock()
self.locked.acquire()
@@ -98,6 +99,7 @@
self.dataobjects[name].connected = obj['connected']
self.debug("%d entries loaded in DATASTORE" % len(self.dataobjects))
self.locked.release()
+ self.DBLOADED = True
except IOError:
## no DB File
pass
@@ -110,6 +112,9 @@
def save(self):
self.debug("save DATASTORE")
+ if not self.DBLOADED:
+ self.debug("No valid config, not saving")
+ return False
self.locked.acquire()
savedict = {}
## FIXME: user create a __reduce__ method for the Datastoreitem object
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2010-11-04 21:54:19
|
Revision: 78
http://openautomation.svn.sourceforge.net/openautomation/?rev=78&view=rev
Author: nilss1
Date: 2010-11-04 21:54:13 +0000 (Thu, 04 Nov 2010)
Log Message:
-----------
added Warning when non Unicode Data ist set by a connector
Modified Paths:
--------------
PyWireGate/trunk/datastore.py
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2010-11-04 21:50:46 UTC (rev 77)
+++ PyWireGate/trunk/datastore.py 2010-11-04 21:54:13 UTC (rev 78)
@@ -208,6 +208,9 @@
self.write_mutex.acquire()
## save the modified time
self.lastupdate = time.time()
+ if type(val) == str:
+ self.WG.log("Non Unicode Value received for %s" % self.id,'warning')
+ val = unicode(val,encoding='iso-8859-15',errors='ignore')
self.value = val
if send:
self._setValue(self)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2010-11-11 14:48:32
|
Revision: 107
http://openautomation.svn.sourceforge.net/openautomation/?rev=107&view=rev
Author: nilss1
Date: 2010-11-11 14:48:26 +0000 (Thu, 11 Nov 2010)
Log Message:
-----------
seperate sendConnected to be called from scheduler * get initial Value from datastore.db
Modified Paths:
--------------
PyWireGate/trunk/datastore.py
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2010-11-11 14:45:53 UTC (rev 106)
+++ PyWireGate/trunk/datastore.py 2010-11-11 14:48:26 UTC (rev 107)
@@ -64,14 +64,7 @@
##TODO: central subscriber function for other connectore or servers
- for attached in obj.connected:
- try:
- self.dataobjects[attached].setValue(val,True)
- except:
- print "FAILED %s" % attached
- __import__('traceback').print_exc(file=__import__('sys').stdout)
- pass
-
+ obj.sendConnected()
## return the object for additional updates
return obj
@@ -110,6 +103,7 @@
self.dataobjects[name].lastupdate = obj['lastupdate']
self.dataobjects[name].config = obj['config']
self.dataobjects[name].connected = obj['connected']
+ self.dataobjects[name].value = obj['value']
self.debug("%d entries loaded in DATASTORE" % len(self.dataobjects))
self.DBLOADED = True
except IOError:
@@ -220,6 +214,7 @@
else:
self.name = name
+ ## set Name to function for Scheduler
if type(self.name) <> unicode:
## guess that non unicode is iso8859
self.name = name.decode("iso-8859-15")
@@ -239,6 +234,8 @@
## connected Logics, communication objects ... goes here
self.connected = []
+ def __repr__(self):
+ return "%s - %s" % (self.id,self.name)
def _setValue(self,refered_self):
## self override
## override with connector send function
@@ -258,6 +255,7 @@
if cycletime < 0.0:
cycletime = 0
self.cyclestore.append(val)
+ ## FIXME: create global cycling Thread like in cycle.py
_cyclethread = self.WG.DATASTORE.attachThread(self,threading.Timer(cycletime,self._cycle))
#_cyclethread.setDaemon(1)
_cyclethread.start()
@@ -286,6 +284,18 @@
self.write_mutex.release()
self.read_mutex.release()
+ def sendConnected(self):
+ self.read_mutex.acquire()
+ val = self.getValue()
+ self.read_mutex.release()
+ for attached in self.connected:
+ try:
+ self.WG.DATASTORE.dataobjects[attached].setValue(val,True)
+ except:
+ print "FAILED %s" % attached
+ __import__('traceback').print_exc(file=__import__('sys').stdout)
+ pass
+
def getValue(self):
try:
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2010-11-14 09:52:26
|
Revision: 117
http://openautomation.svn.sourceforge.net/openautomation/?rev=117&view=rev
Author: nilss1
Date: 2010-11-14 09:52:13 +0000 (Sun, 14 Nov 2010)
Log Message:
-----------
Add some hooks for connectors
Modified Paths:
--------------
PyWireGate/trunk/datastore.py
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2010-11-12 23:53:55 UTC (rev 116)
+++ PyWireGate/trunk/datastore.py 2010-11-14 09:52:13 UTC (rev 117)
@@ -68,7 +68,7 @@
self.debug("Updating %s (%s): %r" % (obj.name,id,val))
## Set the value of the object
- obj.setValue(val)
+ obj.setValue(val,source=connector)
##TODO: central subscriber function for other connectore or servers
@@ -103,7 +103,7 @@
def load(self):
self.debug("load DATASTORE")
try:
- db = codecs.open(self.WG.config['WireGate']['datastore'],"rb",encoding='utf-8')
+ db = codecs.open(self.WG.config['WireGate']['datastore'],"r",encoding=self.WG.config['WireGate']['defaultencoding'])
loaddict = json.load(db)
db.close()
for name, obj in loaddict.items():
@@ -120,7 +120,7 @@
except ValueError:
## empty DB File
self.DBLOADED = True
- pass
+ raise
except:
self.WG.errorlog()
## error
@@ -145,7 +145,7 @@
'config' : obj.config,
'connected' : obj.connected
}
- dbfile = codecs.open(self.WG.config['WireGate']['datastore'],"wb",encoding='utf-8')
+ dbfile = codecs.open(self.WG.config['WireGate']['datastore'],"w",encoding=self.WG.config['WireGate']['defaultencoding'])
utfdb = json.dumps(savedict,dbfile,ensure_ascii=False,sort_keys=True,indent=3)
dbfile.write(utfdb)
dbfile.close()
@@ -197,16 +197,14 @@
## set Name to function for Scheduler
if type(self.name) <> unicode:
- ## guess that non unicode is iso8859
- self.name = name.decode("iso-8859-15")
+ ## guess that non unicode is default encoded
+ self.name = name.decode(self.WG.config['WireGate']['defaultencoding'])
## some defaults
self.value = None
self.lastupdate = 0
+ self.lastsource = None
self.id = id
- ## Fixme: dpt is only KNX specific , should be moved to self.config['KNX:dptid']
- self.dptid = -1
-
## connector specific vars
self.config = {}
@@ -217,7 +215,20 @@
self.connected = []
def __repr__(self):
+ return json.dumps({
+ 'name' : self.name,
+ 'id' : self.id,
+ 'value' : self.value,
+ 'lastupdate' : self.lastupdate,
+ 'config' : self.config,
+ 'connected' : self.connected
+ })
+
+
+ def __str__(self):
return "%s - %s" % (self.id,self.name)
+
+
def _setValue(self,refered_self):
## self override
## override with connector send function
@@ -229,7 +240,7 @@
finally:
self.write_mutex.release()
- def setValue(self,val,send=False):
+ def setValue(self,val,send=False,source=None):
if 'sendcycle' in self.config and self.value <> None:
if not self._cyclejob:
self._parent.debug("start Cycle ID: %s" % self.id)
@@ -251,9 +262,9 @@
self._parent.debug("ignore Cycle ID: %s" % self.id)
self.cyclestore.append(val)
else:
- self._real_setValue(val,send)
+ self._real_setValue(val,send,source=source)
- def _real_setValue(self,val,send):
+ def _real_setValue(self,val,send,source=None):
try:
## get read lock
self.read_mutex.acquire()
@@ -261,9 +272,10 @@
self.write_mutex.acquire()
## save the modified time
self.lastupdate = time.time()
+ self.lastsource = source
if type(val) == str:
self.WG.log("Non Unicode Value received for %s" % self.id,'warning')
- val = unicode(val,encoding='iso-8859-15',errors='ignore')
+ val = unicode(val,encoding=self.WG.config['WireGate']['defaultencoding'],errors='ignore')
self.value = val
if send:
self._setValue(self)
@@ -278,18 +290,21 @@
self.read_mutex.release()
for attached in self.connected:
try:
- self.WG.DATASTORE.dataobjects[attached].setValue(val,True)
+ self.WG.DATASTORE.dataobjects[attached].setValue(val,True,source=self)
except:
self.WG.log("sendconnected failed for %s" % attached,'error')
- #__import__('traceback').print_exc(file=__import__('sys').stdout)
+ __import__('traceback').print_exc(file=__import__('sys').stdout)
- def getValue(self):
+ def getValue(self,other=''):
try:
## get read lock
self.read_mutex.acquire()
- return self.value
+ ret = self.value
+ if other == 'lastupdate':
+ ret = [ret,self.lastupdate]
+ return ret
finally:
## release lock
self.read_mutex.release()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2010-11-23 14:00:58
|
Revision: 160
http://openautomation.svn.sourceforge.net/openautomation/?rev=160&view=rev
Author: nilss1
Date: 2010-11-23 14:00:52 +0000 (Tue, 23 Nov 2010)
Log Message:
-----------
show details on datastore.db errors
Modified Paths:
--------------
PyWireGate/trunk/datastore.py
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2010-11-23 12:25:11 UTC (rev 159)
+++ PyWireGate/trunk/datastore.py 2010-11-23 14:00:52 UTC (rev 160)
@@ -1,6 +1,7 @@
import sys
import time
import threading
+import re
import codecs
@@ -23,7 +24,7 @@
-class datastore:
+class datastore(object):
"""
Datastore Instance
"""
@@ -104,7 +105,8 @@
self.debug("load DATASTORE")
try:
db = codecs.open(self.WG.config['WireGate']['datastore'],"r",encoding='UTF-8')
- loaddict = json.load(db)
+ data = db.read()
+ loaddict = json.loads(data)
db.close()
for name, obj in loaddict.items():
self.dataobjects[name] = dataObject(self,obj['id'],obj['name'])
@@ -117,10 +119,22 @@
except IOError:
## no DB File
pass
- except ValueError:
+ except ValueError,e:
## empty DB File
self.DBLOADED = True
- raise
+ result = re.findall(r"line\s(\d+)\s", e.message)
+ if result:
+ ## only on json errors not when db is empty
+ lnum = int(result[0])
+ print "Can't load Datastore"
+ data = data.split("\n")
+ lines = data[lnum -10:lnum +10]
+ for line in range(lnum-10,lnum+10):
+ if line == lnum-1:
+ print "--" + data[line]
+ else:
+ print "##" + data[line]
+ sys.exit(1)
except:
self.WG.errorlog()
## error
@@ -172,7 +186,7 @@
self.WG.log(msg,severity,"datastore")
-class dataObject:
+class dataObject(object):
def __init__(self,parent,id,name=False):
self._parent = parent
self.WG = parent.WG
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2011-05-06 21:12:26
|
Revision: 320
http://openautomation.svn.sourceforge.net/openautomation/?rev=320&view=rev
Author: nilss1
Date: 2011-05-06 21:12:20 +0000 (Fri, 06 May 2011)
Log Message:
-----------
check if parent is True else dont assign WG
Modified Paths:
--------------
PyWireGate/trunk/datastore.py
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2011-04-27 16:17:31 UTC (rev 319)
+++ PyWireGate/trunk/datastore.py 2011-05-06 21:12:20 UTC (rev 320)
@@ -38,7 +38,8 @@
##
####################################################
self._parent = parent
- self.WG = parent.WG
+ if parent:
+ self.WG = parent.WG
self.log("DATASTORE starting up")
self.DBLOADED = False
self.dataobjects = {}
@@ -191,7 +192,8 @@
class dataObject(object):
def __init__(self,parent,id,name=False):
self._parent = parent
- self.WG = parent.WG
+ if parent:
+ self.WG = parent.WG
## Threadlocking
self.write_mutex = threading.RLock()
self.read_mutex = threading.RLock()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ni...@us...> - 2010-11-14 12:55:18
|
Revision: 121
http://openautomation.svn.sourceforge.net/openautomation/?rev=121&view=rev
Author: nilss1
Date: 2010-11-14 12:55:07 +0000 (Sun, 14 Nov 2010)
Log Message:
-----------
Fix KeyError when sending to unknown datastoreObject
Modified Paths:
--------------
PyWireGate/trunk/datastore.py
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2010-11-14 09:55:23 UTC (rev 120)
+++ PyWireGate/trunk/datastore.py 2010-11-14 12:55:07 UTC (rev 121)
@@ -290,7 +290,7 @@
self.read_mutex.release()
for attached in self.connected:
try:
- self.WG.DATASTORE.dataobjects[attached].setValue(val,True,source=self)
+ self.WG.DATASTORE.get(attached).setValue(val,True,source=self)
except:
self.WG.log("sendconnected failed for %s" % attached,'error')
__import__('traceback').print_exc(file=__import__('sys').stdout)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|