config Pfad geändert, modbus angepasst, database modul angepasst
This commit is contained in:
parent
6c07fec275
commit
3533b5164c
11 changed files with 310 additions and 311 deletions
|
|
@ -5,7 +5,7 @@ Created on 29.01.2014
|
|||
'''
|
||||
from sys import stderr
|
||||
from socket import gethostname
|
||||
from Config.config import Config
|
||||
from Config.parser import config
|
||||
from datetime import datetime
|
||||
from flask import abort
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ from decimal import Decimal
|
|||
from math import isnan
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = Config()
|
||||
c = config()
|
||||
conf = c.readConf()
|
||||
|
||||
api_host = gethostname() if conf['flask_server'] == '0.0.0.0' else conf['flask_server']
|
||||
|
|
|
|||
|
|
@ -8,12 +8,12 @@ Created on 24.10.2013
|
|||
from datetime import datetime
|
||||
from flask import Flask, jsonify, make_response, request
|
||||
from socket import gethostname
|
||||
from Config.config import Config
|
||||
from Config.parser import config
|
||||
from ems import ems
|
||||
from buffer import Buffer
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = Config()
|
||||
c = config()
|
||||
conf = c.readConf()
|
||||
|
||||
api_host = gethostname() if conf['flask_server'] == '0.0.0.0' else conf['flask_server']
|
||||
|
|
|
|||
|
|
@ -1,119 +1,121 @@
|
|||
'''
|
||||
Created on 21.11.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from sys import stderr, exit
|
||||
|
||||
config = "config\ems.conf"
|
||||
|
||||
class Config():
|
||||
_instance = None
|
||||
|
||||
_confDic = {
|
||||
'mySQL_server': '',
|
||||
'mySQL_port': '3306',
|
||||
'mySQL_user': '',
|
||||
'mySQL_pass': '',
|
||||
'mySQL_database': '',
|
||||
'mySQL_speed' : '0.02',
|
||||
'flask_server': '0.0.0.0',
|
||||
'flask_port': '5000',
|
||||
'config_debug' : False,
|
||||
'config_read' : False,
|
||||
'config_dictionary' : 'config',
|
||||
'can_baudrate' : '500k',
|
||||
'can_symfile' : ''
|
||||
}
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
# http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern
|
||||
if not cls._instance:
|
||||
cls._instance = super(Config, cls).__new__(
|
||||
cls, *args, **kwargs)
|
||||
return cls._instance
|
||||
|
||||
def printConf(self):
|
||||
'''
|
||||
Print the config only if debug is enable.
|
||||
'''
|
||||
if self._confDic['config_debug']:
|
||||
mes = 'config:\t{'
|
||||
for x in self._confDic:
|
||||
mes = '%s\r %s \t: %s' % (mes, x, self._confDic[x])
|
||||
mes = '%s\r }\n' % mes
|
||||
print mes
|
||||
|
||||
def makeList(self, arg):
|
||||
for a in range(len(arg)):
|
||||
arg[a] = arg[a].strip()
|
||||
arg[a] = True if arg[a] == 'True' else arg[a]
|
||||
arg[a] = False if arg[a] == 'False' else arg[a]
|
||||
return arg
|
||||
|
||||
def getElement(self, line):
|
||||
tmp = line.split("=")
|
||||
key = tmp[0].strip()
|
||||
val = tmp[1].split("#") #cut off comments, comment is on val[1]
|
||||
val = str(val[0].strip()) #overwrite val with string
|
||||
|
||||
if key == '':
|
||||
stderr.write('config_error: there is an empty key\r')
|
||||
return None, None
|
||||
if val == '':
|
||||
stderr.write('config_error: %s has no value\r' % key)
|
||||
return None, None
|
||||
|
||||
arg = val.split(',')
|
||||
while arg.count('') > 0:
|
||||
arg.remove('')
|
||||
if len(arg) == 0:
|
||||
stderr.write('config_error: %s has an empty argument\r' % key)
|
||||
return None, None
|
||||
return key, arg
|
||||
|
||||
def readConf(self):
|
||||
if self._confDic['config_read']: #prevent re-read
|
||||
return self._confDic
|
||||
|
||||
try:
|
||||
confFile = open(config, "r")
|
||||
except IOError:
|
||||
self._confDic.update({'error' : '%s not found' % config})
|
||||
stderr.write('config_error %s not found\r' % config)
|
||||
return self._confDic
|
||||
|
||||
_linenumber = 0
|
||||
|
||||
for line in confFile:
|
||||
_linenumber += 1
|
||||
line = line.strip()
|
||||
|
||||
if len(line) == 0 or line[0] == "#": #remove empty lines and comments
|
||||
continue
|
||||
|
||||
if line.find("=") == -1:
|
||||
exit('config_error: an error has occurred in line %s\r' % _linenumber)
|
||||
|
||||
key, arg = self.getElement(line)
|
||||
if key == None:
|
||||
continue #skip errors
|
||||
self.makeList(arg)
|
||||
arg = arg if len(arg) > 1 else arg[0]
|
||||
|
||||
self._confDic[key] = arg
|
||||
|
||||
confFile.close()
|
||||
self._confDic['config_read'] = True
|
||||
|
||||
for key in self._confDic:
|
||||
if self._confDic[key] == '':
|
||||
exit('please select a value for {0}'.format(key))
|
||||
|
||||
if self._confDic['config_debug']:
|
||||
self.printConf()
|
||||
|
||||
return self._confDic
|
||||
|
||||
c = Config()
|
||||
conf = c.readConf()
|
||||
'''
|
||||
Created on 21.11.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from sys import stderr, exit
|
||||
|
||||
_config_dir = 'config'
|
||||
_config_file = 'ems.conf'
|
||||
_config_path = _config_dir + '/' + _config_file
|
||||
|
||||
class config():
|
||||
_instance = None
|
||||
|
||||
_confDic = {
|
||||
'mySQL_server': '',
|
||||
'mySQL_port': '3306',
|
||||
'mySQL_user': '',
|
||||
'mySQL_pass': '',
|
||||
'mySQL_database': '',
|
||||
'mySQL_speed' : '0.02',
|
||||
'flask_server': '0.0.0.0',
|
||||
'flask_port': '5000',
|
||||
'config_debug' : False,
|
||||
'config_read' : False,
|
||||
'config_dictionary' : _config_dir,
|
||||
'can_baudrate' : '500k',
|
||||
'can_symfile' : ''
|
||||
}
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
# http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern
|
||||
if not cls._instance:
|
||||
cls._instance = super(config, cls).__new__(
|
||||
cls, *args, **kwargs)
|
||||
return cls._instance
|
||||
|
||||
def printConf(self):
|
||||
'''
|
||||
Print the config only if debug is enable.
|
||||
'''
|
||||
if self._confDic['config_debug']:
|
||||
mes = 'config:\t{'
|
||||
for x in self._confDic:
|
||||
mes = '%s\r %s \t: %s' % (mes, x, self._confDic[x])
|
||||
mes = '%s\r }\n' % mes
|
||||
print mes
|
||||
|
||||
def makeList(self, arg):
|
||||
for a in range(len(arg)):
|
||||
arg[a] = arg[a].strip()
|
||||
arg[a] = True if arg[a] == 'True' else arg[a]
|
||||
arg[a] = False if arg[a] == 'False' else arg[a]
|
||||
return arg
|
||||
|
||||
def getElement(self, line):
|
||||
tmp = line.split("=")
|
||||
key = tmp[0].strip()
|
||||
val = tmp[1].split("#") #cut off comments, comment is on val[1]
|
||||
val = str(val[0].strip()) #overwrite val with string
|
||||
|
||||
if key == '':
|
||||
stderr.write('config_error: there is an empty key\r')
|
||||
return None, None
|
||||
if val == '':
|
||||
stderr.write('config_error: %s has no value\r' % key)
|
||||
return None, None
|
||||
|
||||
arg = val.split(',')
|
||||
while arg.count('') > 0:
|
||||
arg.remove('')
|
||||
if len(arg) == 0:
|
||||
stderr.write('config_error: %s has an empty argument\r' % key)
|
||||
return None, None
|
||||
return key, arg
|
||||
|
||||
def readConf(self):
|
||||
if self._confDic['config_read']: #prevent re-read
|
||||
return self._confDic
|
||||
|
||||
try:
|
||||
confFile = open(_config_path, "r")
|
||||
except IOError:
|
||||
self._confDic.update({'error' : '%s not found' % _config_path})
|
||||
stderr.write('config_error %s not found\r' % _config_path)
|
||||
return self._confDic
|
||||
|
||||
_linenumber = 0
|
||||
|
||||
for line in confFile:
|
||||
_linenumber += 1
|
||||
line = line.strip()
|
||||
|
||||
if len(line) == 0 or line[0] == "#": #remove empty lines and comments
|
||||
continue
|
||||
|
||||
if line.find("=") == -1:
|
||||
exit('config_error: an error has occurred in line %s\r' % _linenumber)
|
||||
|
||||
key, arg = self.getElement(line)
|
||||
if key == None:
|
||||
continue #skip errors
|
||||
self.makeList(arg)
|
||||
arg = arg if len(arg) > 1 else arg[0]
|
||||
|
||||
self._confDic[key] = arg
|
||||
|
||||
confFile.close()
|
||||
self._confDic['config_read'] = True
|
||||
|
||||
for key in self._confDic:
|
||||
if self._confDic[key] == '':
|
||||
exit('please select a value for {0}'.format(key))
|
||||
|
||||
if self._confDic['config_debug']:
|
||||
self.printConf()
|
||||
|
||||
return self._confDic
|
||||
|
||||
c = config()
|
||||
conf = c.readConf()
|
||||
|
|
@ -1,58 +1,58 @@
|
|||
'''
|
||||
Created on 26.11.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from profile.database import Database
|
||||
|
||||
_sort_lookup = { 'EA_Last' : 'DateTime' }
|
||||
|
||||
def setup(conf):
|
||||
'''
|
||||
establishes a connection to the database and returns a Database object
|
||||
|
||||
@return: Database object
|
||||
'''
|
||||
|
||||
db = Database()
|
||||
db.loadDatabase(strHost = conf['mySQL_server'],
|
||||
intPort = int(conf['mySQL_port']),
|
||||
strUser = conf['mySQL_user'],
|
||||
strPasswd = conf['mySQL_pass'],
|
||||
strDatabase = conf['mySQL_database'],
|
||||
strTable = None)
|
||||
return db
|
||||
|
||||
def loop(db, item):
|
||||
'''
|
||||
sql_values = 'SELECT * FROM %s ORDER BY %s DESC LIMIT 0, 1'
|
||||
'''
|
||||
path = item.split('/')
|
||||
if path[0] == '':
|
||||
path.remove('')
|
||||
|
||||
table = path[len(path) -1]
|
||||
if table == 'device':
|
||||
tmp = { table : {} }
|
||||
for i in db.getInformation()['Table'].keys():
|
||||
tmp[table][i] = {}
|
||||
return tmp
|
||||
|
||||
try:
|
||||
sort = db.getInformation()['Table_Prime'][table][0]
|
||||
except:
|
||||
try:
|
||||
sort = _sort_lookup[table]
|
||||
except:
|
||||
return { 'error/db_error' : { table : 'no Prime-Key / Sort defined' } }
|
||||
|
||||
res = db.readDatabase(intRowOffset=0, intRowNumber=1, strTable=table, strSort=sort, invertSort=True)
|
||||
result = res.to_dict()
|
||||
|
||||
for k in result:
|
||||
result[k] = result[k][0]
|
||||
|
||||
return {item : result}
|
||||
|
||||
def close(cursor):
|
||||
pass
|
||||
'''
|
||||
Created on 26.11.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from profile.database import Database
|
||||
|
||||
_sort_lookup = { 'EA_Last' : 'DateTime' }
|
||||
|
||||
def setup(conf):
|
||||
'''
|
||||
establishes a connection to the database and returns a Database object
|
||||
|
||||
@return: Database object
|
||||
'''
|
||||
|
||||
db = Database()
|
||||
db.loadDatabase(strHost = conf['mySQL_server'],
|
||||
intPort = int(conf['mySQL_port']),
|
||||
strUser = conf['mySQL_user'],
|
||||
strPasswd = conf['mySQL_pass'],
|
||||
strDatabase = conf['mySQL_database'],
|
||||
strTable = None)
|
||||
return db
|
||||
|
||||
def loop(db, item):
|
||||
'''
|
||||
sql_values = 'SELECT * FROM %s ORDER BY %s DESC LIMIT 0, 1'
|
||||
'''
|
||||
path = item.split('/')
|
||||
if path[0] == '':
|
||||
path.remove('')
|
||||
|
||||
table = path[len(path) -1]
|
||||
if table == 'device':
|
||||
tmp = { table : {} }
|
||||
for i in db.getInformation()['Table'].keys():
|
||||
tmp[table][i] = {}
|
||||
return tmp
|
||||
|
||||
try:
|
||||
sort = db.getInformation()['Table_Prime'][table][0]
|
||||
except:
|
||||
try:
|
||||
sort = _sort_lookup[table]
|
||||
except:
|
||||
return { 'error/db_error' : { table : 'no Prime-Key / Sort defined' } }
|
||||
|
||||
res = db.readDatabase(intRowOffset=0, intRowNumber=1, strTable=table, strSort=sort, bSortInvert=True)
|
||||
result = res.to_dict()
|
||||
|
||||
for k in result:
|
||||
result[k] = result[k][0]
|
||||
|
||||
return {item : result}
|
||||
|
||||
def close(cursor):
|
||||
pass
|
||||
38
Connector/modules/modbus.py
Normal file
38
Connector/modules/modbus.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
'''
|
||||
Created on 05.12.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from pymodbus.client.sync import ModbusTcpClient
|
||||
|
||||
def setup(conf):
|
||||
PACS = []
|
||||
|
||||
### INITIALIZE ###
|
||||
PACS.append(ModbusTcpClient('10.2.6.5'))
|
||||
PACS.append(ModbusTcpClient('10.2.6.6'))
|
||||
PACS.append(ModbusTcpClient('10.2.6.7'))
|
||||
PACS.append(ModbusTcpClient('10.2.6.8'))
|
||||
|
||||
### CONNECT ###
|
||||
for PAC in PACS:
|
||||
PAC.connect()
|
||||
|
||||
return PACS
|
||||
|
||||
|
||||
def loop(PACS, item):
|
||||
# PAC[0].write_coil(213, 100,2)
|
||||
# result = PAC[0].read_coils(213,1,unit=2)
|
||||
#
|
||||
# ans = PAC[0].write_registers(213,(1,0),unit=2)
|
||||
# print "Antwort: %s" % ans
|
||||
|
||||
res = PACS[0].read_holding_registers(213,4,unit=2)
|
||||
print "Werte: %s" % res.registers
|
||||
|
||||
|
||||
def close(PACS):
|
||||
### CLOSE ###
|
||||
for PAC in PACS:
|
||||
PAC.close()
|
||||
|
|
@ -1,82 +1,82 @@
|
|||
'''
|
||||
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)
|
||||
'''
|
||||
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
|
||||
|
||||
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)
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
'''
|
||||
Created on 05.12.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from pymodbus.client.sync import ModbusTcpClient
|
||||
|
||||
PAC01, PAC02, PAC03, PAC04 = None, None, None, None
|
||||
|
||||
def setup(conf):
|
||||
### INITIALIZE ###
|
||||
PAC01 = ModbusTcpClient('10.2.6.5')
|
||||
PAC02 = ModbusTcpClient('10.2.6.6')
|
||||
PAC03 = ModbusTcpClient('10.2.6.7')
|
||||
PAC04 = ModbusTcpClient('10.2.6.8')
|
||||
|
||||
### CONNECT ###
|
||||
PAC01.connect()
|
||||
PAC02.connect()
|
||||
PAC03.connect()
|
||||
PAC04.connect()
|
||||
|
||||
|
||||
def loop(cursor, item):
|
||||
# PAC01.write_coil(213, 100,2)
|
||||
# result = PAC01.read_coils(213,1,unit=2)
|
||||
|
||||
ans = PAC01.write_registers(213,(1,0),unit=2)
|
||||
print "Antwort: %s" % ans
|
||||
|
||||
res = PAC01.read_holding_registers(213,4,unit=2)
|
||||
print "Werte: %s" % res.registers
|
||||
|
||||
# print result.bits[0]
|
||||
|
||||
def close():
|
||||
### CLOSE ###
|
||||
PAC01.close()
|
||||
PAC02.close()
|
||||
PAC03.close()
|
||||
PAC04.close()
|
||||
|
|
@ -5,12 +5,12 @@ Created on 15.11.2013
|
|||
@version: 0.02
|
||||
'''
|
||||
from threading import Thread
|
||||
from Swich.switch import Switch, MYSQL
|
||||
from Config.config import Config
|
||||
from Connector.switch import Switch, MYSQL
|
||||
from Config.parser import config
|
||||
#from API.service import Request
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = Config()
|
||||
c = config()
|
||||
conf = c.readConf()
|
||||
|
||||
def startSwitch():
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ Created on 29.01.2014
|
|||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from Config.config import Config
|
||||
from Config.parser import config
|
||||
from API.service import REST_start
|
||||
from CAN.Filter import CAN_start
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = Config()
|
||||
c = config()
|
||||
conf = c.readConf()
|
||||
|
||||
CAN_start(conf)
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ Created on 21.11.2013
|
|||
'''
|
||||
# import CANFilter
|
||||
# import Sym2Lib
|
||||
from Config.config import Config
|
||||
from Config.parser import config
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = Config()
|
||||
c = config()
|
||||
conf = c.readConf()
|
||||
|
||||
### Sym2Lib test ###
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue