|
From: <ni...@us...> - 2010-11-09 09:48:17
|
Revision: 93
http://openautomation.svn.sourceforge.net/openautomation/?rev=93&view=rev
Author: nilss1
Date: 2010-11-09 09:48:10 +0000 (Tue, 09 Nov 2010)
Log Message:
-----------
OWFS Threads use mutex * get default configs for unknown datastore objects * except errors in knx_connector _setValue Function
Modified Paths:
--------------
PyWireGate/trunk/connector.py
PyWireGate/trunk/datastore.py
PyWireGate/trunk/knx_connector/KNX_Connector.py
PyWireGate/trunk/owfs_connector/OWFS_Connector.py
Modified: PyWireGate/trunk/connector.py
===================================================================
--- PyWireGate/trunk/connector.py 2010-11-07 09:51:00 UTC (rev 92)
+++ PyWireGate/trunk/connector.py 2010-11-09 09:48:10 UTC (rev 93)
@@ -52,6 +52,12 @@
self.log("unconfigured setValue in %r called for %s" % (self,dsobj.name) ,'warn','WireGate')
pass
+ def get_ds_defaults(self,id):
+ ## the defualt config for new Datasotre Items
+ config = {
+ }
+ return config
+
import SocketServer
import socket
class ConnectorServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer,Connector):
Modified: PyWireGate/trunk/datastore.py
===================================================================
--- PyWireGate/trunk/datastore.py 2010-11-07 09:51:00 UTC (rev 92)
+++ PyWireGate/trunk/datastore.py 2010-11-09 09:48:10 UTC (rev 93)
@@ -8,7 +8,11 @@
## use included json in > Python 2.6
import json
except ImportError:
- import simplejson as json
+ try:
+ import simplejson as json
+ except ImportError:
+ print >>sys.stderr, "apt-get install python-simplejson"
+ sys.exit(1)
class datastore:
@@ -35,7 +39,7 @@
self.load()
- def update(self,id,val):
+ def update(self,id,val,connector=False):
## Update the communication Object with value
####################################################
## Function: update
@@ -48,7 +52,7 @@
####################################################
##
## get the Datastore object
- obj = self.get(id)
+ obj = self.get(id,connector=connector)
self.debug("Updating %s (%s): %r" % (obj.name,id,val))
## Set the value of the object
@@ -68,7 +72,7 @@
## return the object for additional updates
return obj
- def get(self,id):
+ def get(self,id,connector=False):
####################################################
## Function: get
## Parameter:
@@ -84,6 +88,8 @@
except KeyError:
## create a new one if it don't exist
self.dataobjects[id] = dataObject(self,id)
+ if connector:
+ self.dataobjects[id].config = connector.get_ds_defaults(id)
## return it
self.locked.release()
return self.dataobjects[id]
@@ -133,7 +139,6 @@
dbfile = codecs.open(self.WG.config['WireGate']['datastore'],"wb",encoding='utf-8')
utfdb = json.dumps(savedict,dbfile,ensure_ascii=False,sort_keys=True,indent=3)
dbfile.write(utfdb)
- #json.dump(savedict,dbfile,sort_keys=True,indent=3)
dbfile.close()
@@ -199,8 +204,12 @@
## self override
## override with connector send function
if self.namespace:
- self._setValue = self.WG.connectors[self.namespace].setValue
- self.WG.connectors[self.namespace].setValue(refered_self)
+ try:
+ self.write_mutex.acquire()
+ self._setValue = self.WG.connectors[self.namespace].setValue
+ self.WG.connectors[self.namespace].setValue(refered_self)
+ finally:
+ self.write_mutex.release()
def setValue(self,val,send=False):
try:
Modified: PyWireGate/trunk/knx_connector/KNX_Connector.py
===================================================================
--- PyWireGate/trunk/knx_connector/KNX_Connector.py 2010-11-07 09:51:00 UTC (rev 92)
+++ PyWireGate/trunk/knx_connector/KNX_Connector.py 2010-11-09 09:48:10 UTC (rev 93)
@@ -150,8 +150,10 @@
self.errormsg("Failed send %r to %r" % (msg,dstaddr))
def setValue(self,dsobj,msg=False):
- if not msg:
- msg = dsobj.getValue()
- self.debug("SEND %r to %s (%s)" % (msg,dsobj.name,dsobj.id))
- self.send(self.dpt.encode(msg,dsobj=dsobj),dsobj.id)
-
\ No newline at end of file
+ try:
+ if not msg:
+ msg = dsobj.getValue()
+ self.debug("SEND %r to %s (%s)" % (msg,dsobj.name,dsobj.id))
+ self.send(self.dpt.encode(msg,dsobj=dsobj),dsobj.id)
+ except:
+ print "----------- ERROR IN KNX_CONNECTOR.setValue ----------------"
\ No newline at end of file
Modified: PyWireGate/trunk/owfs_connector/OWFS_Connector.py
===================================================================
--- PyWireGate/trunk/owfs_connector/OWFS_Connector.py 2010-11-07 09:51:00 UTC (rev 92)
+++ PyWireGate/trunk/owfs_connector/OWFS_Connector.py 2010-11-09 09:48:10 UTC (rev 93)
@@ -34,6 +34,8 @@
self.WG = False
self.instanceName = instanceName
+ self.mutex = threading.RLock()
+
defaultconfig = {
'cycletime' : 15,
'server' : '127.0.0.1',
@@ -69,6 +71,13 @@
self.sensors = {}
self.start()
+ def get_ds_defaults(self,id):
+ ## the defualt config for new Datasotre Items
+ config = {}
+ if id[-11:] == 'temperature':
+ config['resolution'] = 10
+ return config
+
def run(self):
cnt = 10
while self.isrunning:
@@ -113,15 +122,19 @@
if self.findbusmaster(bus):
## if this has no subbuses add it to the list
try:
- ## check if bus already in list and set time
- self.busmaster[bus]['lastseen'] = time.time()
- except KeyError:
- ## add to list
- self.busmaster[bus] = {
- 'sensors' : {},
- 'lastseen' : time.time(),
- 'readthread' : None
- }
+ self.mutex.acquire()
+ try:
+ ## check if bus already in list and set time
+ self.busmaster[bus]['lastseen'] = time.time()
+ except KeyError:
+ ## add to list
+ self.busmaster[bus] = {
+ 'sensors' : {},
+ 'lastseen' : time.time(),
+ 'readthread' : None
+ }
+ finally:
+ self.mutex.release()
self.findsensors(bus)
except:
## ignore all OWFS Errors
@@ -151,11 +164,15 @@
interfaces = self.supportedsensors[sensortype]
### add it to the list of active sensors
## FIXME: check for old sensor no longer active and remove
- self.busmaster[path]['sensors'][sensor] = {
- 'type':sensortype,
- 'interfaces':interfaces,
- 'resolution':'10' ## Resolution schould be read from Datastore
- }
+ try:
+ self.mutex.acquire()
+ self.busmaster[path]['sensors'][sensor] = {
+ 'type':sensortype,
+ 'interfaces':interfaces,
+ 'resolution':'10' ## Resolution schould be read from Datastore
+ }
+ finally:
+ self.mutex.release()
except KeyError:
self.debug("unsupported Type: %r" % sensortype)
@@ -181,12 +198,17 @@
## get the Datastore Object and look for config
obj = self.WG.DATASTORE.get(id)
if "resolution" in obj.config:
- resolution = obj.config['resolution']
+ resolution = str(obj.config['resolution'])
owfspath = "/uncached/%s/%s%s" % (sensor,get,resolution)
+ self.debug("Reading from path %s" % owfspath)
try:
## read uncached and put into local-list
- self.busmaster[busname]['sensors'][sensor][get] = self.owfs.read(owfspath)
+ try:
+ self.mutex.acquire()
+ self.busmaster[busname]['sensors'][sensor][get] = self.owfs.read(owfspath)
+ finally:
+ self.mutex.release()
except:
## ignore all OWFS Errors
self.WG.errorlog("Reading from path %s failed" % owfspath)
@@ -199,7 +221,7 @@
except:
self.WG.errorlog()
self.busmaster[busname]['readthread'] = None
- self.debug("Thread for %s finshed reading %d sensors in % f secs " % (busname,len(self.busmaster[busname]['sensors']), time.time() - readtime))
+ self.debug("Thread for %s finshed reading %d sensors in %f secs " % (busname,len(self.busmaster[busname]['sensors']), time.time() - readtime))
def read(self):
for busname in self.busmaster.keys():
@@ -207,5 +229,9 @@
if not self.busmaster[busname]['readthread']:
self.debug("Start read Thread for %s" % busname)
threadname = "OWFS-Reader_%s" % busname
- self.busmaster[busname]['readthread'] = threading.Thread(target=self._read,args=[busname],name=threadname)
- self.busmaster[busname]['readthread'].start()
+ try:
+ self.mutex.acquire()
+ self.busmaster[busname]['readthread'] = threading.Thread(target=self._read,args=[busname],name=threadname)
+ self.busmaster[busname]['readthread'].start()
+ finally:
+ self.mutex.release()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|