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
|
import traceback
|
||||||
from settings import SensorType
|
from settings import SensorType
|
||||||
|
|
||||||
class Setup(object):
|
class SensorSetup(object):
|
||||||
|
|
||||||
def __init__(self, connection, sensors, cbtimes, cb_generic):
|
def __init__(self, connection, sensors, cbtimes, cb_generic):
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
40
all.py
40
all.py
|
|
@ -1,20 +1,12 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
from tinkerforge.ip_connection import IPConnection
|
|
||||||
except ImportError:
|
|
||||||
print("package 'tinkerforge' not installed, canceling")
|
|
||||||
raise
|
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from Logger import Logger
|
from Logger import Logger
|
||||||
from Setup import Setup
|
from ConnectionSetup import ConnectionSetup
|
||||||
from settings import SensorType
|
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
def check_dirs_and_files():
|
def check_dirs_and_files():
|
||||||
|
|
@ -41,31 +33,36 @@ def obtainLock(lockfile = settings.lockname):
|
||||||
lock.close()
|
lock.close()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def freeLock(lockfile = settings.lockname):
|
def freeLock(lockfile = settings.lockname):
|
||||||
if os.path.exists(lockfile):
|
if os.path.exists(lockfile):
|
||||||
os.remove(lockfile)
|
os.remove(lockfile)
|
||||||
def disconnect(connection):
|
|
||||||
if not connection.get_connection_state() is IPConnection.CONNECTION_STATE_DISCONNECTED:
|
def formatHost(host):
|
||||||
connection.disconnect()
|
return "%s:%d"%(host['name'], host['port'])
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
check_dirs_and_files()
|
check_dirs_and_files()
|
||||||
log=open(settings.logname,'a')
|
log=open(settings.logname,'a')
|
||||||
try:
|
try:
|
||||||
|
log.write('setting up "all" ... @'+time.ctime()+"\n")
|
||||||
while True:
|
while True:
|
||||||
if obtainLock():
|
if obtainLock():
|
||||||
logger=Logger(log, )
|
logger=Logger(log, )
|
||||||
try:
|
try:
|
||||||
ipcon = IPConnection()
|
connections = []
|
||||||
# connect
|
connectedSensors = []
|
||||||
ipcon.connect(settings.HOST, settings.PORT)
|
for con in settings.SENSORS:
|
||||||
log.write('start logging "all" ... @'+time.ctime()+"\n")
|
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()
|
log.flush()
|
||||||
setup = Setup(ipcon, settings.SENSORS, settings.TIMES, logger.cb_generic)
|
|
||||||
connected = setup.setupSensors()
|
|
||||||
raw_input('Press key to restart\n')
|
raw_input('Press key to restart\n')
|
||||||
disconnect(ipcon)
|
|
||||||
log.write('stop logging... @'+time.ctime()+"\n")
|
log.write('stop logging... @'+time.ctime()+"\n")
|
||||||
|
conSetup.disconnectAny(connections)
|
||||||
except Exception as inst:
|
except Exception as inst:
|
||||||
#connection failed, log and exit
|
#connection failed, log and exit
|
||||||
logger.printException(inst)
|
logger.printException(inst)
|
||||||
|
|
@ -77,8 +74,9 @@ if __name__ == "__main__":
|
||||||
print("wait for retry ("+str(settings.waitDelay)+")")
|
print("wait for retry ("+str(settings.waitDelay)+")")
|
||||||
time.sleep(settings.waitDelay)
|
time.sleep(settings.waitDelay)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Interrupted, cleaning up")
|
print(" Interrupted, cleaning up")
|
||||||
disconnect(ipcon)
|
conSetup.disconnectAny(connections)
|
||||||
log.write("keyboard-interrupt happened @"+time.ctime()+"\n")
|
log.write("keyboard-interrupt happened @"+time.ctime()+"\n")
|
||||||
|
log.close()
|
||||||
freeLock()
|
freeLock()
|
||||||
|
|
||||||
|
|
|
||||||
28
settings.py
28
settings.py
|
|
@ -17,13 +17,21 @@ HOST = "192.168.2.60"
|
||||||
PORT = 4223
|
PORT = 4223
|
||||||
|
|
||||||
SENSORS={
|
SENSORS={
|
||||||
"temp1": ["7B5", SensorType.temp],
|
0: {
|
||||||
"temp2": ["8js", SensorType.temp],
|
"host": {
|
||||||
"humi1": ["7RY", SensorType.humi],
|
"name": "192.168.2.60",
|
||||||
"ambi1": ["8Fw", SensorType.ambi],
|
"port": 4223
|
||||||
"ambi2": ["8DJ", SensorType.ambi],
|
},
|
||||||
"baro1": ["bB7", SensorType.baro],
|
"sensors": {
|
||||||
"temp3": ["8ms", SensorType.temp],
|
"temp1": ["7B5", SensorType.temp],
|
||||||
|
"temp2": ["8js", SensorType.temp],
|
||||||
|
"humi1": ["7RY", SensorType.humi],
|
||||||
|
"ambi1": ["8Fw", SensorType.ambi],
|
||||||
|
"ambi2": ["8DJ", SensorType.ambi],
|
||||||
|
"baro1": ["bB7", SensorType.baro],
|
||||||
|
"temp3": ["8ms", SensorType.temp],
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TIMES={
|
TIMES={
|
||||||
|
|
@ -63,5 +71,9 @@ SENSOR_UNITS=[
|
||||||
# no manual change needed #
|
# 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