diff --git a/app/__init__.py b/app/__init__.py index eda34d1..f8889c7 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,7 +2,7 @@ from flask import Flask from app.views import routes from flask import jsonify from app.settings import powerbars -from app.utils import get_baytech_status_outlet +from app.utils import get_baytech_status_outlet_telnet from flask_socketio import SocketIO, emit from gevent import monkey @@ -25,7 +25,9 @@ def create_app(): if powerbar_info['type'] == 'baytech': try: print("Is a baytech powerbar, getting outlet status") - outlets = get_baytech_status_outlet(powerbar_info['host'], powerbar_info['port']) + if powerbar_info['method'] == "telnet": + outlets = get_baytech_status_outlet_telnet(powerbar_info['host'], powerbar_info['port']) + try: for outlet_id, outlet_info in powerbar_info['outlets'].items(): powerbar_info['outlets'][outlet_id]['state'] = outlets[outlet_id].lower() diff --git a/app/settings.py b/app/settings.py index efa29c5..2fbd8d5 100644 --- a/app/settings.py +++ b/app/settings.py @@ -20,6 +20,7 @@ powerbars = { "host" : "10.209.10.111", "port" : 3696, "type" : "baytech", + "method" : "telnet", "description" : "Powerbar Controlling Aux Space", "outlets" : { 4 : { "name" : "Power solderlane" }, @@ -40,6 +41,7 @@ powerbars = { "host" : "10.209.10.111", "port" : 3697, "type" : "baytech", + "method" : "telnet", "description" : "Powerbar Controlling Main Space", "outlets" : { 9 : { "name" : "3D Printers" }, diff --git a/app/utils.py b/app/utils.py index b2c5aeb..5f551d9 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,7 +1,7 @@ import telnetlib import re -def get_baytech_status_outlet(hostname,port): +def get_baytech_status_outlet_telnet(hostname,port): try: # Create a Telnet object and connect to the host tn = telnetlib.Telnet(hostname, port) diff --git a/app/views.py b/app/views.py index cfb91e7..f680152 100644 --- a/app/views.py +++ b/app/views.py @@ -126,8 +126,10 @@ def powerbar_control(powerbar, outlet, action): if action in ['on', 'off']: try: print(f"Turning {action} powerbar {powerbar} outlet {outlet}") - tn = get_telnet_connection(powerbar) - run_telnet_command(powerbar,f"{action} {outlet}") + + if powerbars[powerbar]['method'] == "telnet": + run_telnet_command(powerbar,f"{action} {outlet}") + powerbars[powerbar]['outlets'][outlet]['state'] = action app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') print(f"Turned {action} powerbar {powerbar} outlet {outlet}") @@ -136,31 +138,39 @@ def powerbar_control(powerbar, outlet, action): return jsonify({'error': 'Telnet error'}), 500 if action == 'cycle': - tn = get_telnet_connection(powerbar) - print(f"Turning Off powerbar {powerbar} outlet {outlet}") run_telnet_command(powerbar,f"off {outlet}") + if powerbars[powerbar]['method'] == "telnet": + print(f"Turning Off powerbar {powerbar} outlet {outlet}") + + powerbars[powerbar]['outlets'][outlet]['state'] = "off" app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') time.sleep(5) + print(f"Turning On powerbar {powerbar} outlet {outlet}") - run_telnet_command(powerbar,f"on {outlet}") - powerbars[powerbar]['outlets'][outlet]['state'] = "on" + if powerbars[powerbar]['method'] == "telnet": + run_telnet_command(powerbar,f"on {outlet}") + powerbars[powerbar]['outlets'][outlet]['state'] = "on" + app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') if action == 'toggle': print(powerbars[powerbar]['outlets'][outlet]['state']) - if powerbars[powerbar]['outlets'][outlet]['state'] == "on": - tn = get_telnet_connection(powerbar) + print(f"Turning Off powerbar {powerbar} outlet {outlet}") - run_telnet_command(powerbar,f"off {outlet}") + if powerbars[powerbar]['method'] == "telnet": + run_telnet_command(powerbar,f"off {outlet}") + powerbars[powerbar]['outlets'][outlet]['state'] = "off" app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') elif powerbars[powerbar]['outlets'][outlet]['state'] == "off": - tn = get_telnet_connection(powerbar) + print(f"Turning On powerbar {powerbar} outlet {outlet}") - run_telnet_command(powerbar,f"on {outlet}") - powerbars[powerbar]['outlets'][outlet]['state'] = "on" + if powerbars[powerbar]['method'] == "telnet": + run_telnet_command(powerbar,f"on {outlet}") + powerbars[powerbar]['outlets'][outlet]['state'] = "on" + app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')}) @@ -240,11 +250,13 @@ def powebar_group_action(group, action): try: for device in group["devices"]: powerbar = device["powerbar"] - tn = get_telnet_connection(powerbar) print(f"Powerbar: {powerbar}") outlets = ",".join([str(x) for x in device['outlets']]) print(f"Turning {action} powerbar {powerbar} outlet {outlets}") - run_telnet_command(powerbar,f"{action} {outlets}") + + if powerbars[powerbar]['method'] == "telnet": + run_telnet_command(powerbar,f"{action} {outlets}") + for outlet in device['outlets']: if outlet in powerbars[powerbar]['outlets']: powerbars[powerbar]['outlets'][outlet]['state'] = action diff --git a/old_powerbarconfig.py b/old_powerbarconfig.py deleted file mode 100644 index 3b5bfef..0000000 --- a/old_powerbarconfig.py +++ /dev/null @@ -1,138 +0,0 @@ -#!/usr/bin/env python3 - -from __future__ import print_function - -from bar import BayTechPowerBar -from httpbar import HTTPPowerBar - -# This is the main config for TechInc (http://techinc.nl) -# The config for the other bar can be found in configs/techinc-west.py - -bars = [] - -FIRST_BAR = BayTechPowerBar('/dev/ttyS0', name='MLP') -AUX_BAR = HTTPPowerBar(host='http://powerbar2.ti:5000', name='AUX') - -bars += [ - FIRST_BAR, - AUX_BAR -] - -def make_bar(bar, name, ident=None): - if ident is None: - ident = name - - __s = bar.make_socket(name, ident) - globals()[name] = __s - bar.sockets[name] = __s - -# Bar 1 - -make_bar(FIRST_BAR, 'LIGHT_MAKERTABLE', 4) -make_bar(FIRST_BAR, 'LIGHT_TABLE', 5) -make_bar(FIRST_BAR, 'LIGHT_HAM', 6) -make_bar(FIRST_BAR, 'LIGHT_ENTRANCE', 7) -make_bar(FIRST_BAR, 'LIGHT_KITCHEN', 8) -make_bar(FIRST_BAR, 'LIGHT_WHEEL', 9) -make_bar(FIRST_BAR, 'LIGHT_SOFA', 10) - -make_bar(FIRST_BAR, 'LIGHT_KITCHEN_SPOTS', 11) -make_bar(FIRST_BAR, 'LEDWALL', 12) - -make_bar(FIRST_BAR, 'AUDIO_AMPLIFIER', 13) -make_bar(FIRST_BAR, 'AUDIO_MIXER', 14) -make_bar(FIRST_BAR, 'LIGHT_MAKERLANE', 15) - -make_bar(FIRST_BAR, 'RADIO_LANE_CTRL_OUTLETS', 16) -make_bar(FIRST_BAR, 'MONITOR_3D_1', 17) -make_bar(FIRST_BAR, 'MONITOR_3D_2', 18) - -#make_bar(FIRST_BAR, 'PRINTER', 18) -make_bar(FIRST_BAR, 'MONITOR_AV_1', 19) -make_bar(FIRST_BAR, 'MONITOR_AV_2', 20) - -make_bar(AUX_BAR, 'W_POWER_NW_TABLE') -make_bar(AUX_BAR, 'W_POWER_SW_TABLE') -make_bar(AUX_BAR, 'W_POWER_MAIN_TABLE') -make_bar(AUX_BAR, 'W_POWER_PILLAR') - -make_bar(AUX_BAR, 'W_AIR_FAN') - -make_bar(AUX_BAR, 'W_LIGHT_TABLE') -make_bar(AUX_BAR, 'W_LIGHT_TL_DOOR') -make_bar(AUX_BAR, 'W_LIGHT_SOLDER') -make_bar(AUX_BAR, 'W_LIGHT_SOLDER_2') -make_bar(AUX_BAR, 'W_LIGHT_GLASS') - - -# Groups make it easier to handle multiple sockets - they are refcounted -# so enabled both 'general' and 'lightwest' will make all the shared sockets -# have a refcount of 'two'. Just disabling the 'general' socket will not -# actually turn off shared sockets. If the global IGNORE_REFCOUNT boolean is set -# however, they are just turned off. -groups = { - 'general' : [LIGHT_ENTRANCE, LIGHT_KITCHEN, LIGHT_SOFA, LIGHT_TABLE, LIGHT_WHEEL], - 'hamshack' : [LIGHT_HAM], - 'kitchenspots': [LIGHT_KITCHEN_SPOTS], - - 'audio' : [AUDIO_AMPLIFIER, AUDIO_MIXER], - 'displays' : [MONITOR_AV_1, MONITOR_AV_2, MONITOR_3D_1, MONITOR_3D_2], - 'av' : [MONITOR_AV_1, MONITOR_AV_2], - 'lighteast' : [LIGHT_MAKERLANE, LIGHT_MAKERTABLE, LIGHT_TABLE, LIGHT_HAM, - LIGHT_ENTRANCE, LIGHT_KITCHEN, LIGHT_WHEEL, LIGHT_SOFA, - LIGHT_KITCHEN_SPOTS, LEDWALL], - 'makerlane' : [LIGHT_MAKERLANE, LIGHT_MAKERTABLE, MONITOR_3D_1, MONITOR_3D_2], - - 'soldering' : [W_POWER_NW_TABLE, W_POWER_SW_TABLE, W_LIGHT_SOLDER, W_LIGHT_SOLDER_2], - 'powerwest' : [W_POWER_NW_TABLE, W_POWER_MAIN_TABLE, W_POWER_SW_TABLE, W_POWER_PILLAR], - 'lightwest' : [W_LIGHT_TABLE, W_LIGHT_GLASS, W_LIGHT_SOLDER, W_LIGHT_SOLDER_2], - 'tlwest' : [W_LIGHT_TL_DOOR], - 'airfan' : [W_AIR_FAN], - 'led' : [LEDWALL], -} - - -groups_state = {} -for _ in groups.keys(): - groups_state[_] = None - -GROUPS_LIGHT = ['general', 'makerlane', 'lightwest', 'craft'] - -# Presets will execute 'On' and 'Off', always. Regardless of the parameters. So -# you cannot 'turn on/off' a preset. -presets = { - 'lightsoff' : { - 'Off' : GROUPS_LIGHT + ['airfan'] + ['tlwest'], - 'On' : [], - }, - 'lightson' : { - 'Off' : [], - 'On' : GROUPS_LIGHT, - }, - 'alloff' :{ - 'Off' : list(groups.keys()), - }, - 'allon' :{ - 'On' : list(groups.keys()), - }, - 'mainspaceon' : { - 'Off' : [], - 'On' : ['general','craft','audio','makerlane','av','audio'], - }, - 'auxspaceon' : { - 'Off' : [], - 'On' : ['soldering','powerwest','lightwest'], - }, - 'social' : { - 'Off' : [], - 'On' : ['general','craft','airfan','audio','lightwest','displays','av','audio','soldering','powerwest','makerlane'], - }, - 'presentation' : { - 'Off' : GROUPS_LIGHT + ['airfan'], - 'On' : ['audio'], - }, - 'vacuuming' : { - 'Off' : [], - 'On' : GROUPS_LIGHT, - }, -}