2017-03-31 00:03:52 -04:00
|
|
|
#!/usr/bin/env python
|
2017-03-30 13:00:23 -04:00
|
|
|
|
|
|
|
import socket, os, time, struct
|
2017-03-31 00:03:52 -04:00
|
|
|
from threading import Thread, Event
|
2017-03-30 13:00:23 -04:00
|
|
|
from daemon import runner
|
2017-03-31 00:03:52 -04:00
|
|
|
import RPi.GPIO as GPIO
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-03-31 00:28:52 -04:00
|
|
|
gpio_state = 11
|
|
|
|
gpio_psw = 12
|
|
|
|
gpio_rsw = 13
|
|
|
|
gpio_pled = 15
|
2017-03-30 13:00:23 -04:00
|
|
|
|
|
|
|
bmcd_state = '/run/bmcd/bmcd.state'
|
|
|
|
bmcd_cmd = '/run/bmcd/bmcd.cmd'
|
|
|
|
pidfile = '/run/bmcd/bmcd.pid'
|
|
|
|
|
2017-03-31 00:03:52 -04:00
|
|
|
is_pled_flashing = Event()
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-03-31 00:03:52 -04:00
|
|
|
def powerled_on():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_pled, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
|
|
|
def powerled_off():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_pled, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
|
|
|
def powerled_flash(is_pled_flashing):
|
|
|
|
while is_pled_flashing.isSet():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_pled, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(1)
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_pled, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(1)
|
|
|
|
is_pled_flashing.clear()
|
|
|
|
return
|
|
|
|
|
|
|
|
def powersw_press():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_psw, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(0.5)
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_psw, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
|
|
|
def powersw_hold():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_psw, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(8)
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_psw, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
|
|
|
def resetsw_press():
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_rsw, 1)
|
2017-03-31 00:03:52 -04:00
|
|
|
time.sleep(0.5)
|
2017-03-31 00:31:28 -04:00
|
|
|
GPIO.output(gpio_rsw, 0)
|
2017-03-31 00:03:52 -04:00
|
|
|
|
|
|
|
def locate_on():
|
|
|
|
is_pled_flashing.set()
|
|
|
|
t = Thread(name='non-block', target=powerled_flash, args=(is_pled_flashing,))
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
def locate_off():
|
|
|
|
is_pled_flashing.clear()
|
|
|
|
|
|
|
|
def readcmd():
|
|
|
|
fcmd = open(bmcd_cmd, 'r+b', 0)
|
|
|
|
while True:
|
|
|
|
line = fcmd.readline()
|
|
|
|
try:
|
|
|
|
globals()[line.rstrip()]()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def writestate(is_pled_flashing):
|
|
|
|
fstate = open(bmcd_state, 'w+b', 0)
|
|
|
|
state_prev = 1
|
|
|
|
while True:
|
|
|
|
state_now = GPIO.input(gpio_state)
|
2017-03-31 00:11:21 -04:00
|
|
|
fstate.write(str(state_now))
|
2017-03-31 00:03:52 -04:00
|
|
|
if not is_pled_flashing.isSet():
|
|
|
|
if state_now == 1:
|
|
|
|
powerled_on()
|
|
|
|
else:
|
|
|
|
powerled_off()
|
|
|
|
|
|
|
|
time.sleep(1)
|
2017-03-30 13:00:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
class App():
|
|
|
|
def __init__(self):
|
|
|
|
self.stdin_path = '/dev/null'
|
|
|
|
self.stdout_path = '/dev/tty'
|
|
|
|
self.stderr_path = '/dev/tty'
|
|
|
|
self.pidfile_path = pidfile
|
|
|
|
self.pidfile_timeout = 5
|
|
|
|
def run(self):
|
|
|
|
if not os.path.exists(bmcd_state):
|
|
|
|
os.mkfifo(bmcd_state)
|
|
|
|
if not os.path.exists(bmcd_cmd):
|
|
|
|
os.mkfifo(bmcd_cmd)
|
|
|
|
|
2017-03-31 00:03:52 -04:00
|
|
|
GPIO.setmode(GPIO.BOARD)
|
|
|
|
GPIO.setup(gpio_state, GPIO.OUT)
|
|
|
|
GPIO.setup(gpio_psw, GPIO.OUT)
|
|
|
|
GPIO.setup(gpio_rsw, GPIO.IN)
|
|
|
|
GPIO.setup(gpio_pled, GPIO.OUT)
|
2017-03-30 13:00:23 -04:00
|
|
|
|
2017-03-31 00:03:52 -04:00
|
|
|
t1 = Thread(target=readcmd)
|
|
|
|
t2 = Thread(target=writestate, args=(is_pled_flashing,))
|
|
|
|
t1.setDaemon(True)
|
|
|
|
t2.setDaemon(True)
|
|
|
|
t1.start()
|
|
|
|
t2.start()
|
|
|
|
while True:
|
|
|
|
pass
|
2017-03-30 13:00:23 -04:00
|
|
|
|
|
|
|
app = App()
|
|
|
|
daemon_runner = runner.DaemonRunner(app)
|
|
|
|
daemon_runner.do_action()
|
|
|
|
|