ems/src/ems.py
2014-02-19 19:03:21 +01:00

96 lines
2.5 KiB
Python

'''
Created on 15.11.2013
@author: Philipp Rauch
@version: 0.02
'''
from threading import Thread
from Connector.switch import Switch, MYSQL, MODBUS
from Config.parser import config
#from API.service import Request
### LOAD CONFIG ###
c = config()
conf = c.readConf()
def startSwitch(modul):
'''
Create a thread of the switch with the given Module
@param param modul: modul to load
@return switch: Obejct of the switch
@return sql_queue: Queue object for results
@retunn sql_query: Queue object for querys
'''
switch = Switch(modul)
switch.initialisiere()
if conf['config_debug']:
print 'SWITCH-Thread:\t', switch
print '\tEMS-QUERY:\t', switch.query
print '\tEMS-QUEUE:\t', switch.queue
switch.start()
return switch
def update_device(query, queue, buf):
'''
update all devices witch have tables in the DB
@param sql_query: queue for querys
@param queue: queue for results
@param buf: Buffer object witch is updated
'''
keys = buf.system['device'].keys()
for key in keys:
if key.startswith("000"):
continue
query.put('/device/%s' % key)
get = buf.update_buffer(queue.get())
#print 'GET:\t', get
#sleep(0.1)
class ems(Thread):
def __init__(self, buf):
Thread.__init__(self)
self.buffer = buf
self.mysql = startSwitch(MYSQL)
# self.modbus = startSwitch(MODBUS)
if conf['config_debug']:
print '\tEMS-BUFFER:\t', buf
self.mysql.query.put('/device')
get = self.buffer.update_buffer(self.mysql.queue.get())
#print 'GET:\t', get
def __del__(self):
self.update.stop()
self.switch.stop = True
def run(self):
while True:
update_device(self.mysql.query, self.mysql.queue, self.buffer)
''' YOUR CODE HERE'''
def getNewMsg(self, old):
'''
A blocking Method to get a different Message from Queue
@param old: the old message
'''
tmp = self.mysql.queue.get()
while tmp == old:
self.mysql.queue.task_done()
tmp = self.mysql.queue.get()
return tmp
def getRequest(self):
'''
'''
try:
req = self.buffer.system['request'].pop()
self.mysql.query.put(req.content)
self.mysql.query.join()
return self.mysql.queue.get()
except IndexError:
return False