added support for multiple connections
parent
3046329535
commit
7177e470e5
|
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
try:
|
||||
from tinkerforge.ip_connection import IPConnection
|
||||
except ImportError:
|
||||
print("package 'tinkerforge' not installed, canceling")
|
||||
raise
|
||||
from SensorSetup import SensorSetup
|
||||
|
||||
class ConnectionSetup(object):
|
||||
|
||||
def setupConnection(self, host):
|
||||
ipcon = IPConnection()
|
||||
ipcon.connect(host['name'], host['port'])
|
||||
return (ipcon)
|
||||
|
||||
def setupConnectionAndSensors(self, host, sensors, cbtimes, cb_generic):
|
||||
hostname = host['name']
|
||||
port = host['port']
|
||||
ipcon = IPConnection()
|
||||
ipcon.connect(hostname, port)
|
||||
sensorSetup = SensorSetup(ipcon, sensors, cbtimes, cb_generic)
|
||||
connectedSensors = sensorSetup.setupSensors()
|
||||
return (ipcon, connectedSensors)
|
||||
|
||||
def disconnectAny(self, connections):
|
||||
for connection in connections:
|
||||
if not connection.get_connection_state() is IPConnection.CONNECTION_STATE_DISCONNECTED:
|
||||
connection.disconnect()
|
||||
|
||||
|
|
@ -13,7 +13,7 @@ from functools import partial
|
|||
import traceback
|
||||
from settings import SensorType
|
||||
|
||||
class Setup(object):
|
||||
class SensorSetup(object):
|
||||
|
||||
def __init__(self, connection, sensors, cbtimes, cb_generic):
|
||||
self.connection = connection
|
||||
38
all.py
38
all.py
|
|
@ -1,20 +1,12 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
try:
|
||||
from tinkerforge.ip_connection import IPConnection
|
||||
except ImportError:
|
||||
print("package 'tinkerforge' not installed, canceling")
|
||||
raise
|
||||
|
||||
import os.path
|
||||
import os
|
||||
import time
|
||||
|
||||
from Logger import Logger
|
||||
from Setup import Setup
|
||||
from settings import SensorType
|
||||
from ConnectionSetup import ConnectionSetup
|
||||
import settings
|
||||
|
||||
def check_dirs_and_files():
|
||||
|
|
@ -41,31 +33,36 @@ def obtainLock(lockfile = settings.lockname):
|
|||
lock.close()
|
||||
return True
|
||||
return False
|
||||
|
||||
def freeLock(lockfile = settings.lockname):
|
||||
if os.path.exists(lockfile):
|
||||
os.remove(lockfile)
|
||||
def disconnect(connection):
|
||||
if not connection.get_connection_state() is IPConnection.CONNECTION_STATE_DISCONNECTED:
|
||||
connection.disconnect()
|
||||
|
||||
def formatHost(host):
|
||||
return "%s:%d"%(host['name'], host['port'])
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_dirs_and_files()
|
||||
log=open(settings.logname,'a')
|
||||
try:
|
||||
log.write('setting up "all" ... @'+time.ctime()+"\n")
|
||||
while True:
|
||||
if obtainLock():
|
||||
logger=Logger(log, )
|
||||
try:
|
||||
ipcon = IPConnection()
|
||||
# connect
|
||||
ipcon.connect(settings.HOST, settings.PORT)
|
||||
log.write('start logging "all" ... @'+time.ctime()+"\n")
|
||||
connections = []
|
||||
connectedSensors = []
|
||||
for con in settings.SENSORS:
|
||||
con = settings.SENSORS[con]
|
||||
conSetup = ConnectionSetup()
|
||||
connection, sensors = conSetup.setupConnectionAndSensors(con['host'], con['sensors'], settings.TIMES, logger.cb_generic)
|
||||
connections.append(connection)
|
||||
connectedSensors.append(sensors)
|
||||
log.write("started logging at %s ... @ %s\n"% (formatHost(con['host']), time.ctime()))
|
||||
log.flush()
|
||||
setup = Setup(ipcon, settings.SENSORS, settings.TIMES, logger.cb_generic)
|
||||
connected = setup.setupSensors()
|
||||
raw_input('Press key to restart\n')
|
||||
disconnect(ipcon)
|
||||
log.write('stop logging... @'+time.ctime()+"\n")
|
||||
conSetup.disconnectAny(connections)
|
||||
except Exception as inst:
|
||||
#connection failed, log and exit
|
||||
logger.printException(inst)
|
||||
|
|
@ -78,7 +75,8 @@ if __name__ == "__main__":
|
|||
time.sleep(settings.waitDelay)
|
||||
except KeyboardInterrupt:
|
||||
print(" Interrupted, cleaning up")
|
||||
disconnect(ipcon)
|
||||
conSetup.disconnectAny(connections)
|
||||
log.write("keyboard-interrupt happened @"+time.ctime()+"\n")
|
||||
log.close()
|
||||
freeLock()
|
||||
|
||||
|
|
|
|||
14
settings.py
14
settings.py
|
|
@ -17,6 +17,12 @@ HOST = "192.168.2.60"
|
|||
PORT = 4223
|
||||
|
||||
SENSORS={
|
||||
0: {
|
||||
"host": {
|
||||
"name": "192.168.2.60",
|
||||
"port": 4223
|
||||
},
|
||||
"sensors": {
|
||||
"temp1": ["7B5", SensorType.temp],
|
||||
"temp2": ["8js", SensorType.temp],
|
||||
"humi1": ["7RY", SensorType.humi],
|
||||
|
|
@ -25,6 +31,8 @@ SENSORS={
|
|||
"baro1": ["bB7", SensorType.baro],
|
||||
"temp3": ["8ms", SensorType.temp],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TIMES={
|
||||
SensorType.temp: 30000,
|
||||
|
|
@ -63,5 +71,9 @@ SENSOR_UNITS=[
|
|||
# no manual change needed #
|
||||
###########################
|
||||
|
||||
tempSensors=len(list(filter(lambda a: True if SENSORS[a][1]==SensorType.temp else False, SENSORS)))
|
||||
tempSensors=0
|
||||
for i in SENSORS:
|
||||
for j in SENSORS[i]['sensors']:
|
||||
if SENSORS[i]['sensors'][j][1] == SensorType.temp:
|
||||
tempSensors+=1
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue