79 lines
2 KiB
Python
79 lines
2 KiB
Python
'''
|
|
Created on 15.11.2013
|
|
|
|
@author: Philipp Rauch
|
|
@version: 0.1
|
|
'''
|
|
from threading import Thread
|
|
from Queue import Queue
|
|
from time import sleep
|
|
from Config.parser import config
|
|
|
|
#import Module
|
|
import database
|
|
import modbus
|
|
|
|
#set source modules
|
|
MYSQL = database
|
|
MODBUS = modbus
|
|
|
|
### LOAD CONFIG ###
|
|
c = config()
|
|
conf = c.readConf()
|
|
|
|
'''
|
|
TODO: comment :)
|
|
'''
|
|
|
|
class Switch(Thread):
|
|
|
|
def __init__(self, source = MYSQL):
|
|
Thread.__init__(self)
|
|
self.source = source
|
|
self.query = None
|
|
self.queue = None
|
|
|
|
def initialisiere(self, query = None, queue = None):
|
|
'''
|
|
initialize the swich with the given source and creates a sql_queue and a sql_query
|
|
it calls the setup method from the submodule of the source
|
|
'''
|
|
try:
|
|
self.cursor = self.source.setup(conf)
|
|
except:
|
|
raise NotImplementedError
|
|
|
|
### init Query ###
|
|
self.query = Queue() if query is None else query
|
|
if conf['config_debug']:
|
|
print '\tSWITCH-QUERY:\t', self.query
|
|
|
|
### init Queue ###
|
|
self.queue = Queue() if queue is None else queue
|
|
if conf['config_debug']:
|
|
print '\tSWITCH-QUEUE:\t', self.queue
|
|
|
|
|
|
def run(self):
|
|
'''
|
|
The loop method from the submodule is called with a parameter from the sql_query.
|
|
Its output is written to the queue.
|
|
'''
|
|
while True:
|
|
# Queue implementaion
|
|
# Queue contains the tablename for sql_query
|
|
# item = sql_query.get() #block = True
|
|
# sql_query should be included in the result
|
|
|
|
item = self.query.get(block = True)
|
|
|
|
result = self.source.loop(self.cursor, item)
|
|
|
|
#print 'PUT:\t%s' % result
|
|
|
|
self.queue.put(result)
|
|
self.query.task_done()
|
|
if self.source == MYSQL:
|
|
sleep(float(conf['mySQL_speed']))
|
|
elif self.source == MODBUS:
|
|
sleep(0.1)
|