Skip to content

Commit

Permalink
Add basestation_interface.cmd_interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRisch committed Jan 24, 2021
1 parent 72e3ff8 commit 39183cf
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
12 changes: 9 additions & 3 deletions config/config_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@ log:

basestation:
enabled: true # Boolean. Enable the Base Station component.
type: 'v2' # Enum(v1, v2, cmd). Version of your Base Stations or 'cmd' for custom commands.

# ==== FOR V1 AND V2 BASE STATIONS ====
attempt_count_set: 5 # Int >0. Number of attempts to set the power state of Base Station.
bluetooth_interface: 0 # Int. Number of the bluetooth interface (hci{number}) to use.
# Run `hciconfig -a` for a list of bluetooth interfaces.

# ==== FOR V2 BASE STATIONS ONLY ====
type: 'v2' # Enum(v1, v2). Version of your Base Stations.
attempt_count_scan: 5 # Int >0. Maximum number of attempts to find Base Stations.

# ==== FOR V1 BASE STATIONS ONLY ====
# change the 'type' (a few lines up) to 'v1'
# (change the 'type' (a few lines up) to 'v1')
lh_b_mac: '' # String(XX:XX:XX:XX:XX:XX). Bluetooth MAC address of Lighthouse B.
lh_b_id: '' # String(XXXXXXXX). ID of Lighthouse B (printed on the back).
lh_c_mac: '' # String(XX:XX:XX:XX:XX:XX). Bluetooth MAC address of Lighthouse C.
lh_c_id: '' # String(XXXXXXXX). ID of Lighthouse C (printed on the back).
# See https://github.com/risa2000/lhctrl for details.
# If your Base Stations operate in A/B mode (with a sy cable), fill the fields intended for C with the values from A.
# If your Base Stations operate in A/B mode (with a sync cable), fill the fields intended for C with the values from A.

# ==== FOR CUSTOM BASE STATIONS COMMANDS ONLY ====
# (change the 'type' (a few lines up) to 'cmd')
command_on: [ 'bash', '-c', 'echo command_on | tee /tmp/steamvr_basestation_command_test' ] # List of strings. Command to execute in order to turn Base Stations on.
command_off: [ 'bash', '-c', 'echo command_off | tee /tmp/steamvr_basestation_command_test' ] # List of strings. Command to execute in order to turn Base Stations off.
# This can be used to control the base stations in an unconventional setup (e.g. https://github.com/DavidRisch/steamvr_utils/issues/8).

audio:
enabled: true # Boolean. Enable the Base Station component.
Expand Down
1 change: 1 addition & 0 deletions scripts/basestation_interface/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .cmd_interface import CmdBasestationInterface
from .interface import BasestationInterface
from .v1_interface import V1BasestationInterface
from .v2_interface import V2BasestationInterface
Expand Down
34 changes: 34 additions & 0 deletions scripts/basestation_interface/cmd_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import subprocess

import log

from .interface import BasestationInterface


class CmdBasestationInterface(BasestationInterface):

def __init__(self, config):
super().__init__(config)

def execute_command(self, command):
if command is None:
log.i("Base Station command is None, nothing to do")
return

log.i("Executing Base Station command: {}".format(command))

process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

log.i("Base Station command result:\nreturn code: {}\nstdout:\n{}\nstderr:\n{}".format(process.returncode,
process.stdout.decode(),
process.stderr.decode()))

def action(self, action):
if action == self.Action.ON:
command = self.config.basestation_command('on')
elif action == self.Action.OFF:
command = self.config.basestation_command('off')
else:
raise NotImplementedError()

self.execute_command(command)
16 changes: 15 additions & 1 deletion scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def basestation_enabled(self):
def basestation_type(self):
if 'basestation' in self.data and 'type' in self.data['basestation']:
basestation_type = self.data['basestation']['type']
valid_types = ['v1', 'v2']
valid_types = ['v1', 'v2', 'cmd']
if basestation_type not in valid_types:
raise RuntimeError('Invalid value for basestation.type, valid options: {}'.format(valid_types))
return basestation_type
Expand Down Expand Up @@ -98,6 +98,20 @@ def basestation_id(self, mode):

raise RuntimeError('Value basestation.{} must be set when using V1 Base Stations'.format(field_name))

def basestation_command(self, target):
if target not in ['on', 'off']:
raise RuntimeError()

field_name = 'command_{}'.format(target)

if 'basestation' in self.data and field_name in self.data['basestation']:
command = self.data['basestation'][field_name]
if command is not None and not isinstance(command, list):
raise RuntimeError('Command must be a list of strings (or null), found: {}'.format(command))
return command

return None

def audio_enabled(self):
if 'audio' in self.data and 'enabled' in self.data['audio']:
return bool(self.data['audio']['enabled'])
Expand Down
12 changes: 8 additions & 4 deletions scripts/steamvr_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import log
from audio_switcher import AudioSwitcher
from config import Config
from steamvr_daemon import SteamvrDaemon
from config_helper import ConfigHelper
from steamvr_daemon import SteamvrDaemon


class SteamvrUtils:
class Action(enum.Enum):
Expand All @@ -28,10 +29,14 @@ def __init__(self, config):

if self.config.basestation_enabled():
basestation_type = self.config.basestation_type()
if basestation_type == "v1":
if basestation_type == 'v1':
self.basestation_power_interface = basestation_interface.V1BasestationInterface(config)
elif basestation_type == "v2":
elif basestation_type == 'v2':
self.basestation_power_interface = basestation_interface.V2BasestationInterface(config)
elif basestation_type == 'cmd':
self.basestation_power_interface = basestation_interface.CmdBasestationInterface(config)
else:
raise NotImplementedError()

if self.config.audio_enabled():
self.audio_switcher = AudioSwitcher(config)
Expand Down Expand Up @@ -104,7 +109,6 @@ def main():
# noinspection PyBroadException
try:


selected_action = None
for action in SteamvrUtils.Action:
if args.action in actions[action]:
Expand Down

0 comments on commit 39183cf

Please sign in to comment.