Skip to content

Commit

Permalink
implement light_color/off in mpf service
Browse files Browse the repository at this point in the history
  • Loading branch information
jabdoa2 committed Feb 5, 2018
1 parent e4a30c4 commit b072adf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
55 changes: 50 additions & 5 deletions mpf/commands/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,24 @@ def __init__(self, bcp_client):
super().__init__()
self.bcp_client = bcp_client
self._known_coils = None
self._known_lights = None

def _build_known_coils(self, list_coils_response):
self._known_coils = []
for coil in list_coils_response[1]["coils"]:
self._known_coils.append(coil[2])

def _build_known_lights(self, list_lights_response):
self._known_lights = []
for light in list_lights_response[1]["lights"]:
self._known_lights.append(light[2])

def do_list_coils(self, args):
"""List all coils."""
del args
self.bcp_client.send("service", {"subcommand": "list_coils"})
message = asyncio.get_event_loop().run_until_complete(self.bcp_client.wait_for_response("list_coils"))
data = [["Board", "Number", "Name"]]
self._known_coils = []
for coil in message[1]["coils"]:
data.append([coil[0], coil[1], coil[2]])

Expand Down Expand Up @@ -60,6 +65,7 @@ def do_list_lights(self, args):
for light in message[1]["lights"]:
data.append([light[0], light[1], light[2], light[3]])

self._build_known_lights(message)
table = AsciiTable(data)
print(table.table)

Expand All @@ -75,6 +81,32 @@ def complete_coil_disable(self, text, line, start_index, end_index):
"""Autocomplete coil names."""
return self.complete_coil_xxx(text, line, start_index, end_index)

def complete_light_color(self, text, line, start_index, end_index):
"""Autocomplete light names."""
return self.complete_light_xxx(text, line, start_index, end_index)

def complete_light_off(self, text, line, start_index, end_index):
"""Autocomplete light names."""
return self.complete_light_xxx(text, line, start_index, end_index)

def complete_light_xxx(self, text, line, start_index, end_index):
"""Autocomplete lights."""
del line
del start_index
del end_index
if not self._known_lights:
self.bcp_client.send("service", {"subcommand": "list_lights"})
message = asyncio.get_event_loop().run_until_complete(self.bcp_client.wait_for_response("list_lights"))
self._build_known_lights(message)

if text:
return [
light for light in self._known_lights
if light.startswith(text)
]
else:
return self._known_lights

def complete_coil_xxx(self, text, line, start_index, end_index):
"""Autocomplete coils."""
del line
Expand Down Expand Up @@ -122,13 +154,26 @@ def do_coil_disable(self, args):

def do_light_color(self, args):
"""Color a light."""
pass
# TODO: implement
try:
light_name, color_name = args.split(" ", 2)
except IndexError:
print("Expects: light_color <light_name> <color_name>")
return
self.bcp_client.send("service", {"subcommand": "light_color", "light": light_name, "color": color_name})
message = asyncio.get_event_loop().run_until_complete(self.bcp_client.wait_for_response("light_color"))
if message[1]["error"]:
print("Error: {}".format(message[1]["error"]))
else:
print("Success")

def do_light_off(self, args):
"""Turn off a light."""
pass
# TODO: implement
self.bcp_client.send("service", {"subcommand": "light_color", "light": args, "color": "off"})
message = asyncio.get_event_loop().run_until_complete(self.bcp_client.wait_for_response("light_color"))
if message[1]["error"]:
print("Error: {}".format(message[1]["error"]))
else:
print("Success")

def do_monitor_switches(self, args):
"""Monitor switches."""
Expand Down
43 changes: 21 additions & 22 deletions mpf/core/bcp/bcp_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,15 @@ def _service(self, client, subcommand, **kwargs):
elif subcommand == "monitor_switches":
pass
elif subcommand == "coil_pulse":
self._coil_pulse(client, kwargs)
self._coil_pulse(client, kwargs.get("coil"))
elif subcommand == "coil_enable":
self._coil_enable(client, kwargs)
self._coil_enable(client, kwargs.get("coil"))
elif subcommand == "coil_disable":
self._coil_disable(client, kwargs)
self._coil_disable(client, kwargs.get("coil"))
elif subcommand == "light_color":
pass
self._light_color(client, kwargs.get("light"), kwargs.get("color"))

def _coil_pulse(self, client, kwargs):
try:
coil_name = kwargs.get("coil")
except KeyError:
self.machine.bcp.transport.send_to_client(client, "coil_pulse", error="Missing parameter")
return
def _coil_pulse(self, client, coil_name):
try:
coil = self.machine.coils[coil_name]
except KeyError:
Expand All @@ -145,12 +140,7 @@ def _coil_pulse(self, client, kwargs):
coil.pulse()
self.machine.bcp.transport.send_to_client(client, "coil_pulse", error=False)

def _coil_disable(self, client, kwargs):
try:
coil_name = kwargs.get("coil")
except KeyError:
self.machine.bcp.transport.send_to_client(client, "coil_disable", error="Missing parameter")
return
def _coil_disable(self, client, coil_name):
try:
coil = self.machine.coils[coil_name]
except KeyError:
Expand All @@ -159,12 +149,7 @@ def _coil_disable(self, client, kwargs):
coil.disable()
self.machine.bcp.transport.send_to_client(client, "coil_disable", error=False)

def _coil_enable(self, client, kwargs):
try:
coil_name = kwargs.get("coil")
except KeyError:
self.machine.bcp.transport.send_to_client(client, "coil_enable", error="Missing parameter")
return
def _coil_enable(self, client, coil_name):
try:
coil = self.machine.coils[coil_name]
except KeyError:
Expand All @@ -178,6 +163,20 @@ def _coil_enable(self, client, kwargs):

self.machine.bcp.transport.send_to_client(client, "coil_enable", error=False)

def _light_color(self, client, light_name, color_name):
try:
light = self.machine.lights[light_name]
except KeyError:
self.machine.bcp.transport.send_to_client(client, "light_color", error="Light not found")
return
try:
light.color(color_name)
except DriverLimitsError as e:
self.machine.bcp.transport.send_to_client(client, "light_color", error=str(e))
return

self.machine.bcp.transport.send_to_client(client, "light_color", error=False)

@asyncio.coroutine
def _bcp_receive_monitor_start(self, client, category):
"""Start monitoring the specified category."""
Expand Down

0 comments on commit b072adf

Please sign in to comment.