From 2f537a8d287e030b769ec995b8587fcaf1a96c53 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Wed, 11 Jul 2018 16:15:46 -0400 Subject: [PATCH] Switch to relay based stuff --- libremanage | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/libremanage b/libremanage index 8776159..0d696dd 100755 --- a/libremanage +++ b/libremanage @@ -50,6 +50,7 @@ import time import os.path def open_ssh(server, command, force_tty=False): + print(command) config = server["ssh"] args = ["ssh"] + (["-t"] if force_tty else []) + [config["username"] + "@" + config["host"], "-p", str(config["port"]), command] subprocess.run(args) @@ -76,23 +77,20 @@ def get_server_handle(name): return server -def gpio_export(server, pin, mode): - if mode: - open_ssh(server, "echo " + str(pin) + " > /sys/class/gpio/export") - open_ssh(server, "echo out > /sys/class/gpio/gpio" + str(pin) + "/direction") +def power_write(server, conf, state): + if conf["type"] == "hidusb-relay-cmd": + verb = "on" if state == 1 else "off" + open_ssh(server, "hidusb-relay-cmd ID=" + conf["relay"] + " " + verb + " " + str(conf["channel"])) else: - open_ssh(server, "echo " + str(pin) + " > /sys/class/gpio/unexport") + print("Unknown power type " + conf["type"]) -def gpio_write(server, pin, value): - open_ssh(server, "echo " + str(value) + " > /sys/class/gpio/gpio" + str(pin) + "/value") - -def power_button(server, pin, state): +def power_button(server, conf, state): # Hold down the power to force off (via the EC), # or just flick on to turn on - gpio_write(server, pin, 1) - time.sleep(2 if state == POWER_OFF else 0.5) - gpio_write(server, pin, 0) + power_write(server, conf, 1) + time.sleep(6 if state == POWER_OFF else 1) + power_write(server, conf, 0) POWER_OFF = 0 POWER_ON = 1 @@ -101,22 +99,14 @@ POWER_REBOOT = 2 def set_server_power(state, server): conf = server["power"] - # TODO: Invert - # Export pin, configure, write value, unexport - pin = conf["pin"] - - gpio_export(server, pin, True) - # Act like a power button if state == POWER_OFF or state == POWER_ON: - power_button(server, pin, state) + power_button(server, conf, state) elif state == POWER_REBOOT: # Requires that we already be online. - power_button(server, pin, POWER_OFF) - power_button(server, pin, POWER_ON) - - gpio_export(server, pin, False) + power_button(server, conf, POWER_OFF) + power_button(server, conf, POWER_ON) def open_tty(s): if s["tty"]["uncolor"]: -- 2.25.1