Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add command line interface #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ Usage
Open the Mopidy Web UI (i.e. ``http://{MOPIDY_IP}:6680/``).
You should see a ``pummeluff`` web client which can be used to regsiter new RFID tags.

## Command line interface

You can also access pummeluff through the command line interface. Write `mopidy pummeluff [COMMAND]` with available commands:

* `list`: Lists all the registered tags with the alias, action and parameter.

Contribution
============

Expand Down
7 changes: 7 additions & 0 deletions mopidy_pummeluff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pkg_resources

from .frontend import PummeluffFrontend
from .commands import PummeluffCommand
from .web import ActionsHandler, LatestHandler, RegisterHandler, RegistryHandler, UnregisterHandler

__version__ = pkg_resources.get_distribution('Mopidy-Pummeluff').version
Expand Down Expand Up @@ -81,3 +82,9 @@ def setup(self, registry):
'name': self.ext_name,
'factory': app_factory,
})

def get_command(self):
'''
Setup the CLI interface.
'''
return PummeluffCommand()
13 changes: 12 additions & 1 deletion mopidy_pummeluff/actions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def execute(cls, core): # pylint: disable=unused-argument

def __init__(self, uid, alias=None, parameter=None):
'''
Concstructor.
Constructor.
'''
self.uid = uid
self.alias = alias
Expand Down Expand Up @@ -106,3 +106,14 @@ def validate(self):

if not parameterised and self.parameter:
raise ValueError('No parameter allowed for this tag')

class EmptyAction(Action):
'''
An action that represents nothing but an uuid.
'''

def __init__(self, uid, alias=None, parameter=None):
'''
Constructor.
'''
super().__init__(uid = uid, alias = alias, parameter = parameter)
39 changes: 39 additions & 0 deletions mopidy_pummeluff/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'''
CLI Interface of this extension.
'''

import logging

from mopidy import commands

from .registry import REGISTRY


logger = logging.getLogger(__name__)


class PummeluffCommand(commands.Command):
'''
Main Command class.
'''
def __init__(self):
super().__init__()
self.add_child("list", ListCommand())

def run(self, args, config):
pass


class ListCommand(commands.Command):
'''
Prints out the stored tags and their appropriate values on the terminal.
'''
def run(self, args, config):
'''
Prints out the stored tags and values on the terminal.
'''

for tag in REGISTRY.values():
logger.info("%s -> %s",tag, tag.as_dict())

return 0
11 changes: 10 additions & 1 deletion mopidy_pummeluff/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from mopidy import core as mopidy_core

from .threads import GPIOHandler, TagReader
from .actions.base import EmptyAction

LOGGER = getLogger(__name__)

Expand All @@ -29,8 +30,15 @@ def __init__(self, config, core):
self.core = core
self.stop_event = Event()
self.gpio_handler = GPIOHandler(core=core, config=config, stop_event=self.stop_event)
self.tag_reader = TagReader(core=core, stop_event=self.stop_event)
self.tag_reader = TagReader(stop_event=self.stop_event, success_event=self.success_event)

def success_event(self, action):
'''
Invoke the action that was stored in the registry.
'''
if not isinstance(action, EmptyAction):
action(self.core)

def on_start(self):
'''
Start GPIO handler & tag reader threads.
Expand All @@ -44,3 +52,4 @@ def on_stop(self):
stop their operations.
'''
self.stop_event.set()

13 changes: 7 additions & 6 deletions mopidy_pummeluff/threads/tag_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pirc522 import RFID # pylint: disable=import-error
from RPi import GPIO # pylint: disable=import-error

from mopidy_pummeluff.actions.base import EmptyAction
from mopidy_pummeluff.actions.base import Action
from mopidy_pummeluff.registry import REGISTRY
from mopidy_pummeluff.sound import play_sound
Expand All @@ -38,17 +39,17 @@ class TagReader(Thread):
daemon = True
latest = None

def __init__(self, core, stop_event):
def __init__(self, stop_event, success_event):
'''
Class constructor.

:param mopidy.core.Core core: The mopidy core instance
:param threading.Event stop_event: The stop event
:param success_event: callback method when a tag has been read successfully
'''
super().__init__()
self.core = core
self.stop_event = stop_event
self.rfid = RFID()
self.success_event = success_event

def run(self):
'''
Expand Down Expand Up @@ -107,12 +108,12 @@ def handle_uid(self, uid):
action = REGISTRY[str(uid)]
LOGGER.info('Triggering action of registered tag')
play_sound('success.wav')
action(self.core)

except KeyError:
LOGGER.info('Tag is not registered, thus doing nothing')
play_sound('fail.wav')
action = Action(uid=uid)

action = EmptyAction(uid=uid)

self.success_event(action)
action.scanned = time()
TagReader.latest = action