1
0
Fork 0
baconscript/bacon/hog4.py

122 lines
4.2 KiB
Python
Raw Normal View History

2020-11-23 12:31:44 -05:00
"""hog4.py: Class methods for Hog 4 representation."""
2019-11-18 21:49:11 -05:00
import logging
from time import sleep
2020-11-25 09:38:24 -05:00
from typing import Any, Dict, Union
from pythonosc import udp_client, osc_message_builder, osc_bundle, osc_message
from antlr4 import ParserRuleContext
2019-11-18 21:49:11 -05:00
2022-05-12 19:16:44 -04:00
__all__ = [
"HogNet",
]
2019-11-18 21:49:11 -05:00
2022-05-12 19:53:08 -04:00
log = logging.getLogger(__name__)
2019-11-18 22:23:51 -05:00
class HogNet:
2020-11-23 12:31:44 -05:00
"""Class definition of a hognet participant."""
2019-11-18 21:49:11 -05:00
# button state constants
buttonDOWN = 1
buttonUP = 0
2020-11-25 09:39:42 -05:00
buttonMap: Dict[str, str] = {
"backspace": "/hog/hardware/backspace",
"beam": "/hog/hardware/beam",
"blind": "/hog/hardware/blind",
"clear": "/hog/hardware/clear",
"colour": "/hog/hardware/colour",
"copy": "/hog/hardware/copy",
"cue": "/hog/hardware/cue",
"delete": "/hog/hardware/delete",
"down": "/hog/hardware/down",
"effect": "/hog/hardware/effect",
"enter": "/hog/hardware/enter",
"fan": "/hog/hardware/fan",
"fixture": "/hog/hardware/fixture",
"goto": "/hog/hardware/goto",
"group": "/hog/hardware/group",
"highlight": "/hog/hardware/highlight",
"intensity": "/hog/hardware/intensity",
"left": "/hog/hardware/left",
"list": "/hog/hardware/list",
"live": "/hog/hardware/live",
"macro": "/hog/hardware/macro",
"merge": "/hog/hardware/merge",
"move": "/hog/hardware/move",
"open": "/hog/hardware/open",
"page": "/hog/hardware/page",
"pig": "/hog/hardware/pig",
"position": "/hog/hardware/position",
"record": "/hog/hardware/record",
"right": "/hog/hardware/right",
"scene": "/hog/hardware/scene",
"set": "/hog/hardware/set",
"setup": "/hog/hardware/setup",
"time": "/hog/hardware/time",
"up": "/hog/hardware/up",
"update": "/hog/hardware/update",
".": "/hog/hardware/period",
"@": "/hog/hardware/at",
"-": "/hog/hardware/minus",
"+": "/hog/hardware/plus",
"/": "/hog/hardware/slash",
"0": "/hog/hardware/zero",
"1": "/hog/hardware/one",
"2": "/hog/hardware/two",
"3": "/hog/hardware/three",
"4": "/hog/hardware/four",
"5": "/hog/hardware/five",
"6": "/hog/hardware/six",
"7": "/hog/hardware/seven",
"8": "/hog/hardware/eight",
"9": "/hog/hardware/nine"
}
2019-11-18 21:49:11 -05:00
2020-11-25 09:38:24 -05:00
def __init__(self, servers: Dict[int, object]) -> None:
2020-11-23 12:31:44 -05:00
"""Init method."""
2019-11-18 21:49:11 -05:00
self.servers = servers
2020-11-25 09:39:42 -05:00
def number_entry(self, device: ParserRuleContext,
number: str) -> None:
"""Press digit buttons to make a number."""
for digit in number:
try:
self.button_press(device, self.buttonMap[digit])
except KeyError:
continue
2020-11-25 09:38:24 -05:00
def button_press(self, device: ParserRuleContext,
path: str, delay: float = 0.05) -> None:
2020-11-23 12:31:44 -05:00
"""Button presses are a pair of up/down OSC."""
2019-11-18 21:49:11 -05:00
self.send_message(device, path, self.buttonDOWN)
sleep(delay)
self.send_message(device, path, self.buttonUP)
2020-11-25 09:39:42 -05:00
sleep(delay)
2019-11-18 21:49:11 -05:00
2020-11-25 09:38:24 -05:00
def send_message(self, device: ParserRuleContext,
path: str, arg: Any) -> None:
2020-11-23 12:31:44 -05:00
"""Send a simple OSC message with one argument."""
2019-11-18 21:49:11 -05:00
msg = osc_message_builder.OscMessageBuilder(address=path)
msg.add_arg(arg)
self.send(device, msg.build())
2020-11-25 09:38:24 -05:00
def send(self, device: ParserRuleContext,
msg: Union[osc_message.OscMessage, osc_bundle.OscBundle]) -> None:
2020-11-23 12:31:44 -05:00
"""Send python-osc messages."""
2019-11-18 21:49:11 -05:00
if device is None:
2020-11-25 09:38:24 -05:00
# first configured server
osc: udp_client = list(self.servers.values())[0]
2019-11-18 21:49:11 -05:00
else:
2020-11-23 12:31:44 -05:00
if device.nodeType().getText().lower() != 'h':
2022-05-12 19:53:08 -04:00
log.error("ERROR: Only Hog type devices are supported.")
2019-11-18 21:49:11 -05:00
return
2020-11-23 12:31:44 -05:00
try:
osc = self.servers[device.number().value]
except KeyError:
2022-05-12 19:53:08 -04:00
log.error("ERROR: Net# %d not found.",
2020-11-23 12:31:44 -05:00
device.number().value)
return
2019-11-18 21:49:11 -05:00
try:
osc.send(msg)
2020-11-23 12:31:44 -05:00
except OSError as exception:
2022-05-12 19:53:08 -04:00
log.error(exception)