Fixed ListPrograms, initial program execution support

ListPrograms had a special case for dev-envs that listed /bin instead of the supplied program folder.
Initial run_program implementation, stopping does not work yet.
This commit is contained in:
Philip Trauner 2017-01-11 22:31:16 +01:00
parent 8bf83b655a
commit 2d0401506c

View file

@ -15,6 +15,12 @@ CHANNEL = 2
IS_WALLABY = Utils.is_wallaby() IS_WALLABY = Utils.is_wallaby()
PATH = "/home/root/Documents/KISS/bin/" if IS_WALLABY else (sys.argv[1] if len(sys.argv) > 1 else None) PATH = "/home/root/Documents/KISS/bin/" if IS_WALLABY else (sys.argv[1] if len(sys.argv) > 1 else None)
PATH = os.path.abspath(PATH)
if PATH[-1] != "/":
PATH = PATH + "/"
LIB_WALLABY = "/usr/lib/libwallaby.so" LIB_WALLABY = "/usr/lib/libwallaby.so"
WALLABY_PROGRAMS = "/root/Documents/KISS/bin/" WALLABY_PROGRAMS = "/root/Documents/KISS/bin/"
@ -41,7 +47,7 @@ class SensorReadout:
SensorReadout.DIGITAL : []} SensorReadout.DIGITAL : []}
self.generate_random_values = False self.generate_random_values = False
# Running on actual hardware? # Running on actual hardware?
if Utils.is_wallaby(): if IS_WALLABY:
if not os.path.exists(LIB_WALLABY): if not os.path.exists(LIB_WALLABY):
Logging.error("The Wallaby library should normally exist on " Logging.error("The Wallaby library should normally exist on "
"a Wallaby. You broke something, mate.") "a Wallaby. You broke something, mate.")
@ -115,10 +121,13 @@ class SensorReadout:
for port in self.readout_required[mode]: for port in self.readout_required[mode]:
current_values[mode][port] = self.get_sensor_value(port, mode) current_values[mode][port] = self.get_sensor_value(port, mode)
for peer in self.peers: for peer in self.peers:
readouts = 0
response = {"analog" : {}, "digital" : {}} response = {"analog" : {}, "digital" : {}}
for mode in SensorReadout.NAMED_MODES: for mode in SensorReadout.NAMED_MODES:
for port in self.peers[peer][mode]: for port in self.peers[peer][mode]:
response[SensorReadout.NAMED_MODES[mode]][port] = current_values[mode][port] response[SensorReadout.NAMED_MODES[mode]][port] = current_values[mode][port]
readouts += 1
if readouts != 0:
self.handler.pipe(response, "sensor", peer) self.handler.pipe(response, "sensor", peer)
time.sleep(self.poll_rate) time.sleep(self.poll_rate)
@ -141,20 +150,42 @@ class Identify(Pipe):
class ListPrograms(Pipe): class ListPrograms(Pipe):
def run(self, data, peer, handler): def run(self, data, peer, handler):
programs = [] programs = []
if IS_WALLABY: if os.path.isdir(PATH):
if os.path.isdir(WALLABY_PROGRAMS): for program in os.listdir(PATH):
for program in os.listdir(WALLABY_PROGRAMS): if "botball_user_program" in os.listdir(PATH + program):
if "botball_user_program" in WALLABY_PROGRAMS + program: programs.append(program)
programs.append(programm)
else: else:
Logging.error("Harrogate folder structure does not exist. " Logging.error("Harrogate folder structure does not exist. "
"You broke something, mate.") "You broke something, mate.")
else:
for program in os.listdir("/bin"):
programs.append(program)
handler.pipe(programs, handler.reverse_routes[self], peer) handler.pipe(programs, handler.reverse_routes[self], peer)
class RunProgram(Pipe):
def __init__(self, output_unbuffer):
self.command = [output_unbuffer, "-i0", "-o0", "-e0"]
def run(self, data, peer, handler):
if type(data) is str:
if data[-1] != "/":
data = data + "/"
path = "%s%s/botball_user_program" % (PATH, data)
if os.path.isfile(path):
program = subprocess.Popen(self.command + [path],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# Poll process for new output until finished
for line in iter(program.stdout.readline, b""):
handler.pipe(line.decode(), "std_stream", peer)
else:
if handler.debug:
Logging.warning("Program '%s' not found." % data)
class StopProgram(Pipe):
def run(self, data, peer, handler):
pass
class Sensor(Pipe): class Sensor(Pipe):
""" """
-> ->
@ -317,7 +348,7 @@ try:
ws.setup({"subscribe" : Subscribe(), "hostname" : Hostname(), ws.setup({"subscribe" : Subscribe(), "hostname" : Hostname(),
"processes" : Processes(), "sensor" : Sensor(), "processes" : Processes(), "sensor" : Sensor(),
"identify" : Identify(), "list_programs" : ListPrograms(), "identify" : Identify(), "list_programs" : ListPrograms(),
"whoami" : WhoAmI()}, "whoami" : WhoAmI(), "run_program" : RunProgram(config.output_unbuffer)},
debug=config.debug) debug=config.debug)
ws.connect() ws.connect()
ws.run_forever() ws.run_forever()