Switch to relay based stuff
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Wed, 11 Jul 2018 20:15:46 +0000 (16:15 -0400)
committerAlyssa Rosenzweig <alyssa@rosenzweig.io>
Wed, 11 Jul 2018 20:15:46 +0000 (16:15 -0400)
libremanage

index 8776159b1539f039133ea8d0823a9748676b74d6..0d696dd69297448b569c1b4bcc8bbfcffbf925e5 100755 (executable)
@@ -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"]: