82 lines
2.1 KiB
Python
82 lines
2.1 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.config import Config
|
|
|
|
#import Module
|
|
import database
|
|
import modbus
|
|
|
|
MYSQL = 0
|
|
MODBUS = 1
|
|
|
|
### LOAD CONFIG ###
|
|
c = Config()
|
|
conf = c.readConf()
|
|
|
|
'''
|
|
TODO: comment :)
|
|
'''
|
|
|
|
class Switch(Thread):
|
|
|
|
def __init__(self, source = MYSQL):
|
|
Thread.__init__(self)
|
|
self.source = source
|
|
|
|
def initialisiere(self):
|
|
'''
|
|
initialize the swich with the given source and creates a queue and a query
|
|
it calls the setup method from the source module
|
|
|
|
@return: queue and query
|
|
'''
|
|
if self.source == MYSQL:
|
|
self.cursor = database.setup(conf)
|
|
elif self.source == MODBUS:
|
|
self.cursor = modbus.setup(conf)
|
|
|
|
### init Query ###
|
|
self.query = Queue()
|
|
if conf['config_debug']:
|
|
print '\tSWITCH-QUERY:\t', self.query
|
|
|
|
### init Queue ###
|
|
self.queue = Queue()
|
|
if conf['config_debug']:
|
|
print '\tSWITCH-QUEUE:\t', self.queue
|
|
|
|
return self.queue, self.query
|
|
|
|
def run(self):
|
|
'''
|
|
The loop method of the source module is called with a parameter from the query.
|
|
Its output is written to the queue.
|
|
'''
|
|
while True:
|
|
# Queue implementaion
|
|
# Queue contains the tablename for query
|
|
# item = query.get() #block = True
|
|
# query should be included in the result
|
|
|
|
item = self.query.get(block = True)
|
|
|
|
if self.source == MYSQL:
|
|
result = database.loop(self.cursor, item)
|
|
elif self.source == MODBUS:
|
|
result = modbus.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)
|