Skip to content

Commit

Permalink
FEATURE: Add unregister action
Browse files Browse the repository at this point in the history
  • Loading branch information
domibarton committed Apr 15, 2020
1 parent fbe9560 commit 5858745
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 121 deletions.
12 changes: 7 additions & 5 deletions mopidy_pummeluff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import mopidy

from .frontend import PummeluffFrontend
from .web import LatestHandler, RegistryHandler, RegisterHandler, ActionClassesHandler
from .web import LatestHandler, RegistryHandler, RegisterHandler, UnregisterHandler, \
ActionClassesHandler


def app_factory(config, core): # pylint: disable=unused-argument
Expand All @@ -21,10 +22,11 @@ def app_factory(config, core): # pylint: disable=unused-argument
:rtype: list
'''
return [
('/latest/', LatestHandler, {'core': core}),
('/registry/', RegistryHandler, {'core': core}),
('/register/', RegisterHandler, {'core': core}),
('/action-classes/', ActionClassesHandler, {'core': core}),
('/latest/', LatestHandler),
('/registry/', RegistryHandler),
('/register/', RegisterHandler),
('/unregister/', UnregisterHandler),
('/action-classes/', ActionClassesHandler),
]


Expand Down
11 changes: 11 additions & 0 deletions mopidy_pummeluff/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,16 @@ def register(self, action_class, uid, alias=None, parameter=None):

return action

def unregister(self, uid):
'''
Unregister a tag from the registry.
:param str uid: The UID
'''
LOGGER.info('Unregistering tag %s', uid)

del self[uid]
self.write()


REGISTRY = RegistryDict()
63 changes: 33 additions & 30 deletions mopidy_pummeluff/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'LatestHandler',
'RegistryHandler',
'RegisterHandler',
'UnregisterHandler',
'ActionClassesHandler',
)

Expand All @@ -26,14 +27,6 @@ class LatestHandler(RequestHandler): # pylint: disable=abstract-method
Request handler which returns the latest scanned tag.
'''

def initialize(self, core): # pylint: disable=arguments-differ
'''
Initialize request handler with Mopidy core.
:param mopidy.core.Core mopidy_core: The mopidy core instance
'''
self.core = core # pylint: disable=attribute-defined-outside-init

def get(self, *args, **kwargs): # pylint: disable=unused-argument
'''
Handle GET request.
Expand Down Expand Up @@ -65,14 +58,6 @@ class RegistryHandler(RequestHandler): # pylint: disable=abstract-method
Request handler which returns all registered tags.
'''

def initialize(self, core): # pylint: disable=arguments-differ
'''
Initialize request handler with Mopidy core.
:param mopidy.core.Core mopidy_core: The mopidy core instance
'''
self.core = core # pylint: disable=attribute-defined-outside-init

def get(self, *args, **kwargs): # pylint: disable=unused-argument
'''
Handle GET request.
Expand All @@ -97,14 +82,6 @@ class RegisterHandler(RequestHandler): # pylint: disable=abstract-method
Request handler which registers an RFID tag in the registry.
'''

def initialize(self, core): # pylint: disable=arguments-differ
'''
Initialize request handler with Mopidy core.
:param mopidy.core.Core mopidy_core: The mopidy core instance
'''
self.core = core # pylint: disable=attribute-defined-outside-init

def post(self, *args, **kwargs): # pylint: disable=unused-argument
'''
Handle POST request.
Expand Down Expand Up @@ -141,18 +118,44 @@ def put(self, *args, **kwargs): # pylint: disable=unused-argument
self.post()


class ActionClassesHandler(RequestHandler): # pylint: disable=abstract-method
class UnregisterHandler(RequestHandler): # pylint: disable=abstract-method
'''
Request handler which returns all action classes.
Request handler which unregisters an RFID tag from the registry.
'''

def initialize(self, core): # pylint: disable=arguments-differ
def post(self, *args, **kwargs): # pylint: disable=unused-argument
'''
Handle POST request.
'''
Initialize request handler with Mopidy core.
try:
REGISTRY.unregister(uid=self.get_argument('uid'))

data = {
'success': True,
'message': 'Tag successfully unregistered',
}

except ValueError as ex:
self.set_status(400)
data = {
'success': False,
'message': str(ex)
}

self.set_header('Content-type', 'application/json')
self.write(dumps(data))

:param mopidy.core.Core mopidy_core: The mopidy core instance
def put(self, *args, **kwargs): # pylint: disable=unused-argument
'''
Handle PUT request.
'''
self.core = core # pylint: disable=attribute-defined-outside-init
self.post()


class ActionClassesHandler(RequestHandler): # pylint: disable=abstract-method
'''
Request handler which returns all action classes.
'''

def get(self, *args, **kwargs): # pylint: disable=unused-argument
'''
Expand Down
1 change: 1 addition & 0 deletions mopidy_pummeluff/webui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h2>Register New Tag</h2>
<label for="parameter">Parameter</label>
<input id="parameter" name="parameter" type="text" placeholder="A type-specific parameter">
<button id="register-button" role="submit">✓ Register Tag</button>
<button id="unregister-button" role="button">× Unregister Tag</button>
</form>
</div>
</div>
Expand Down
121 changes: 58 additions & 63 deletions mopidy_pummeluff/webui/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ class API {
* Send AJAX request to REST API endpoint.
*/

request(endpoint, data, callback)
{
request = (endpoint, data, callback) => {
let init = {}
if(data)
init = { method: 'POST', body: data }

fetch(endpoint, init)
.then(function(response)
{
.then((response) => {
return response.json()
})
.then(callback)
Expand All @@ -27,24 +25,21 @@ class API {
* Refresh the registry.
*/

refreshRegistry()
{
let callback = function(response)
{
refreshRegistry = () => {
let callback = (response) => {
let tagsContainer = document.getElementById('tags')
while(tagsContainer.firstChild)
while(tagsContainer.firstChild) {
tagsContainer.removeChild(tagsContainer.firstChild)
}

for(let tag of response.tags)
{
for(let tag of response.tags) {
let tagElement = document.createElement('div')
tagElement.setAttribute('class', 'tag')

let args = new Array('alias', 'uid', 'action_class', 'parameter')
for(let arg of args)
{
for(let arg of args) {
let spanElement = document.createElement('span')
let value = tag[arg] ? tag[arg] : '-'
let value = tag[arg] ? tag[arg] : '-'
spanElement.setAttribute('class', arg.replace('_', '-'))
spanElement.innerHTML = value
tagElement.appendChild(spanElement)
Expand All @@ -61,16 +56,13 @@ class API {
* Refresh the tags.
*/

refreshActionClasses()
{
let callback = function(response)
{
refreshActionClasses = () => {
let callback = (response) => {
let select = document.getElementById('action-class');
while(select.firstChild)
select.removeChild(select.firstChild)

for(let action_class in response.action_classes)
{
for(let action_class in response.action_classes) {
let option = document.createElement('option')
option.setAttribute('value', action_class)
option.innerHTML = action_class + ' (' + response.action_classes[action_class] + ')'
Expand All @@ -81,61 +73,65 @@ class API {
this.request('/pummeluff/action-classes/', false, callback)
}

/*
* Reset the form.
*/

formCallback = (response) => {
if(response.success) {
this.refreshRegistry()
document.getElementById('uid').value = ''
document.getElementById('alias').value = ''
document.getElementById('parameter').value = ''
document.getElementById('action-class').selectIndex = 0
} else {
window.alert(response.message)
}
}

/*
* Register a new tag.
*/

register()
{
register = () => {
let form = document.getElementById('register-form')
let data = new FormData(form)
this.request('/pummeluff/register/', data, this.formCallback)
}

let callback = function(response)
{
if(response.success)
{
api.refreshRegistry()
document.getElementById('uid').value = ''
document.getElementById('alias').value = ''
document.getElementById('parameter').value = ''
document.getElementById('action-class').selectIndex = 0
}
else
{
window.alert(response.message)
}
}
/*
* Unregister an existing tag.
*/

this.request('/pummeluff/register/', data, callback)
unregister = () => {
let form = document.getElementById('register-form')
let data = new FormData(form)
this.request('/pummeluff/unregister/', data, this.formCallback)
}

/*
* Get latest scanned tag.
*/

getLatestTag()
{
getLatestTag = () => {
let latest_tag = undefined

let uid_field = document.getElementById('uid')
let alias_field = document.getElementById('alias')
let parameter_field = document.getElementById('parameter')
let uid_field = document.getElementById('uid')
let alias_field = document.getElementById('alias')
let parameter_field = document.getElementById('parameter')
let action_class_select = document.getElementById('action-class')

uid_field.value = ''
alias_field.value = ''
parameter_field.value = ''
action_class_select.selectIndex = 0

let link = document.getElementById('read-rfid-tag')
let link = document.getElementById('read-rfid-tag')
link.classList.add('reading')

let do_request = function()
{
let callback = function(response)
{
if(latest_tag && response.success && JSON.stringify(response) != JSON.stringify(latest_tag))
{
let do_request = () => {
let callback = (response) => {
if(latest_tag && response.success && JSON.stringify(response) != JSON.stringify(latest_tag)) {
uid_field.value = response.uid

if(response.alias)
Expand All @@ -148,9 +144,7 @@ class API {
action_class_select.value = response.action_class

link.classList.remove('reading')
}
else
{
} else {
setTimeout(() => do_request(), 1000)
}

Expand All @@ -167,27 +161,28 @@ class API {

api = new API()

api.refreshRegistry();
api.refreshActionClasses();
api.refreshRegistry()
api.refreshActionClasses()

document.addEventListener('click', function(event)
{
document.addEventListener('click', (event) => {
let target = event.target
let div = target.closest('div')

if(div && div.classList.contains('tag'))
{
for(let child of div.children)
{
if(div && div.classList.contains('tag')) {
for(let child of div.children) {
document.getElementById(child.className).value = child.innerHTML.replace(/^-$/, '')
}
}
})

document.getElementById('register-form').onsubmit = function()
{
document.getElementById('register-form').onsubmit = () => {
api.register()
return false;
}

document.getElementById('unregister-button').onclick = () => {
api.unregister()
return false
}

document.getElementById('read-rfid-tag').onclick = () => api.getLatestTag()
Loading

0 comments on commit 5858745

Please sign in to comment.