-
Notifications
You must be signed in to change notification settings - Fork 0
/
hu_commands.py
640 lines (586 loc) · 19.3 KB
/
hu_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#!/usr/bin/python
#
# Commands
# Venema, S.R.G.
# 2018-06-11
#
# Commands, arguments and MQ-mapper.
#
class Commands(object):
def __init__(self):
self.command_list = []
self.function_mq_map = [
{
'name': 'SOURCE',
'description':'''Retrieve source details for current or given (sub)index.''',
'command': 'GET',
'params': [
{ 'name':'index', 'datatype':(int,), 'required':False, 'help':'''Source index; '-1' returns all.''' },
{ 'name':'subindex', 'datatype':(int,), 'required':False, 'help':'''Sub source index; '-1' returns all.''' }
],
'path':'/source'
},
{
'name': 'SOURCE-SELECT',
'description':'''Switch to given (sub)source and start playback.''',
'command': 'PUT',
'params': [
{ 'name':'index', 'datatype':(int,), 'required':True, 'help':'''Source index''' },
{ 'name':'subindex', 'datatype':(int,), 'required':False, 'help':'''Sub source index''' }
],
'path':'/source'
},
{
'name': 'SOURCE-DELETE',
'description':'''Remove specified (sub)source.''',
'command': 'DEL',
'params': [
{ 'name':'index', 'datatype':(int,), 'required':True, 'help':'''Source index''' },
{ 'name':'subindex', 'datatype':(int,), 'required':False, 'help':'''Sub source index''' }
],
'path':'/source'
},
{
'name': 'SOURCE-NEXT',
'description':'''Switch to next available (sub)source and start playback.''',
'command': 'PUT',
'params': [
{ 'name':'primary', 'datatype':(bool,), 'required':False, 'help':'''Skip to to next primary source''' }
],
'path':'/source/next'
},
{
'name': 'SOURCE-PREV',
'description':'''Switch to previous available (sub)source and start playback.''',
'command': 'PUT',
'params': [
{ 'name':'primary', 'datatype':(bool,), 'required':False, 'help':'''Skip to to previous primary source''' }
],
'path':'/source/prev'
},
{
'name': 'SOURCE-ENABLE',
'description':'''Mark (sub)source as (un)available.''',
'command': 'PUT',
'params': [
{ 'name':'index', 'datatype':(int,), 'required':True, 'help':'''Source index''' },
{ 'name':'subindex', 'datatype':(int,), 'required':True, 'help':'''Sub source index; '-1' marks all.''', 'default':-1 },
{ 'name':'available', 'datatype':(bool,), 'required':True, 'help':'''Set Available''', 'choices':[True, False] }
],
'path':'/source/available'
},
{
'name': 'SOURCE-UPDATE',
'description':'''Update current or given (sub)source.''',
'command': 'PUT',
'params': [
{ 'name':'index', 'datatype':(int,), 'required':False, 'help':'''Source index''' },
{ 'name':'subindex', 'datatype':(int,), 'required':False, 'help':'''Sub source index''' }
],
'path':'/source/update'
},
{
'name': 'SOURCE-CHECK',
'description':'''Check current or given (sub)source for availability.''',
'command': 'PUT',
'params': [
{ 'name':'index', 'datatype':(int,), 'required':False, 'help':'''Source index; '-1' checks all.''' },
{ 'name':'subindex', 'datatype':(int,), 'required':False, 'help':'''Sub source index; '-1' checks all.''' }
],
'path':'/source/check'
},
{
'name': 'PLAYER-METADATA',
'description':'''Retrieve all available data, incl ID3 tag, of currently playing media.''',
'command': 'GET',
'params': None,
'path':'/player/metadata'
},
{
'name': 'PLAYER-PLS',
'description':'''Retrieve current playlist.''',
'command': 'GET',
'params': None,
'path':'/player/playlists'
},
{
'name': 'PLAYER-PLS-LOAD',
'description':'''Load playlist.''',
'command': 'PUT',
'params': [
{ 'name':'playlist', 'datatype':(str,), 'required':True, 'help':'''Playlist to load''' }
],
'path':'/player/playlists/load'
},
{
'name': 'PLAYER-NEXT',
'description':'''Next track/station.''',
'command': 'PUT',
'params': [
{ 'name':'advance_count', 'datatype':(int,), 'required':False, 'help':'''Number of tracks to advance.''' }
],
'path':'/player/next'
},
{
'name': 'PLAYER-PREV',
'description':'''Prev track/station.''',
'command': 'PUT',
'params': [
{ 'name':'jump_to_start', 'datatype':(bool,), 'required':False, 'help':'''Jump to beginning of track (counts as 1 reverse), if supported.''', 'default':True },
{ 'name':'reverse_count', 'datatype':(int,), 'required':False, 'help':'''Number of tracks to reverse.''' }
],
'path':'/player/prev'
},
{
'name': 'PLAYER-PLAY-POS',
'description':'''Play track at specified position in playlist.''',
'command': 'PUT',
'params': [
{ 'name':'position', 'datatype':(int,), 'required':True, 'help':'''Position in playlist''' }
],
'path':'/player/play_positition'
},
{
'name': 'PLAYER-FOLDERS',
'description':'''Retrieve list of playlist position to folder mapping.''',
'command': 'GET',
'params': None,
'path':'/player/folders'
},
{
'name': 'PLAYER-FOLDER-NEXT',
'description':'''Advance to next folder.''',
'command': 'PUT',
'params': [
{ 'name':'advance_count', 'datatype':(int,), 'required':False, 'help':'''Number of folders to advance.''', 'default':1 }
],
'path':'/player/folder/next'
},
{
'name': 'PLAYER-FOLDER-PREV',
'description':'''Return to previous folder.''',
'command': 'PUT',
'params': [
{ 'name':'jump_to_start', 'datatype':(bool,), 'required':False, 'help':'''Jump to first track in folder (counts as 1 reverse).''', 'default':True },
{ 'name':'reverse_count', 'datatype':(int,), 'required':False, 'help':'''Number of folders to reverse.''', 'default':1 }
],
'path':'/player/folder/prev'
},
{
'name': 'PLAYER-PLAY',
'description':'''Play.''',
'command': 'PUT',
'params': [
{ 'name':'state', 'datatype':(str,), 'required':False, 'help':'''Play state''', 'choices':['on','off','toggle'], 'default':'toggle' }
],
'path':'/player/play'
},
{
'name': 'PLAYER-PAUSE',
'description':'''Pause.''',
'command': 'PUT',
'params': [
{ 'name':'state', 'datatype':(str,), 'required':False, 'help':'''Play state''', 'choices':['on','off','toggle'], 'default':'toggle' }
],
'path':'/player/pause'
},
{
'name': 'PLAYER-STOP',
'description':'''Stop.''',
'command': 'PUT',
'params': [
{ 'name':'state', 'datatype':(str,), 'required':False, 'help':'''Play state''', 'choices':['on','off','toggle'], 'default':'toggle' }
],
'path':'/player/stop'
},
{
'name': 'PLAYER-STATE',
'description':'''Retrieve player state (play/pause/stop).''',
'command': 'GET',
'params': None,
'path':'/player/state'
},
{
'name': 'PLAYER-RANDOM-SET',
'description':'''Set random mode.''',
'command': 'PUT',
'params': [
{ 'name':'mode', 'datatype':(str,unicode,), 'required':False, 'help':'''Random mode''', 'choices':['off','next','prev','folder','artist','genre','playlist'], 'default':'next' }
],
'path':'/player/random'
},
{
'name': 'PLAYER-RANDOM',
'description':'''Retrieves current random mode.''',
'command': 'GET',
'params': None,
'path':'/player/random'
},
{
'name': 'PLAYER-RANDOM-LIST',
'description':'''Retrieves list of supported random modes for currently playing source.''',
'command': 'GET',
'params': None,
'path':'/player/random/supported'
},
{
'name': 'PLAYER-RANDOM-NEXT',
'description':'''Selects next random mode.''',
'command': 'PUT',
'params': None,
'path':'/player/random/next'
},
{
'name': 'PLAYER-RANDOM-PREV',
'description':'''Selects previous random mode.''',
'command': 'PUT',
'params': None,
'path':'/player/random/prev'
},
{
'name': 'PLAYER-SEEK',
'description':'''Seek forward or reverse. Use a negative value to reverse.''',
'command': 'PUT',
'params': [
{ 'name':'seek_seconds', 'datatype':(int,), 'required':False, 'help':'''Seconds to seek forward/reverse''' }
],
'path':'/player/seek'
},
{
'name': 'PLAYER-UPDATE',
'description':'''Update MPD database, preferablly specify a location to update.''',
'command': 'PUT',
'params': [
{ 'name':'location', 'datatype':(str,), 'required':False, 'help':'''Location, as seen from MPD''' }
],
'path':'/player/update'
},
{
'name': 'SYSTEM-SHUTDOWN',
'description':'''Shutdown.''',
'command': 'PUT',
'params': [
{ 'name':'timer_seconds', 'datatype':(int,), 'required':False, 'help':'''Seconds to wait''', 'default':0 }
],
'path':'/system/shutdown'
},
{
'name': 'SYSTEM-REBOOT',
'description':'''Reboot.''',
'command': 'PUT',
'params': [
{ 'name':'timer_seconds', 'datatype':(int,), 'required':False, 'help':'''Seconds to wait''', 'default':0 }
],
'path':'/system/reboot'
},
{
'name': 'VOLUME',
'description':'''Retrieve current volume''',
'command': 'GET',
'params': None,
'path':'/volume/master'
},
{
'name': 'VOLUME-SET',
'description':'''Set volume, when using n+, n-, n=percentage)''',
'command': 'PUT',
'params': [
{ 'name':'volume', 'datatype':(str,unicode,int,float,), 'required':True, 'help':'''Volume''', 'choices':['up','down','FLOAT_PERCENTAGE','FLOAT_SIGNED','att','mute'] }
],
'path':'/volume/master'
},
{
'name': 'VOLUME-INCR',
'description':'''Increase volume by percentage''',
'command': 'PUT',
'params': [
{ 'name':'percentage', 'datatype':(str,unicode,int,float,), 'required':True, 'help':'''Percentage to increase''', 'choices':['FLOAT_PERCENTAGE'] }
],
'path':'/volume/master/increase'
},
{
'name': 'VOLUME-DECR',
'description':'''Decrease volume by percentage''',
'command': 'PUT',
'params': [
{ 'name':'percentage', 'datatype':(str,unicode,int,float,), 'required':True, 'help':'''Percentage to decrease''', 'choices':['FLOAT_PERCENTAGE'] }
],
'path':'/volume/master/decrease'
},
{
'name': 'VOLUME-ATT',
'description':'''Set volume to ATT level. Optionally, set (override) level in %.''',
'command': 'PUT',
'params': [
{ 'name':'state', 'datatype':(str,unicode,), 'required':False, 'help':'''Att state''', 'choices':['on','off','toggle'], 'default':'toggle' },
{ 'name':'percentage', 'datatype':(str,unicode,int,float,), 'required':False, 'help':'''Volume percentage''', 'choices':['FLOAT_PERCENTAGE'] }
],
'path':'/volume/att'
},
{
'name': 'VOLUME-MUTE',
'description':'''Mute volume (default: toggle)''',
'command': 'PUT',
'params': [
{ 'name':'state', 'datatype':(str,), 'required':False, 'help':'''Mute state''', 'choices':['on','off','toggle'], 'default':'toggle' }
],
'path':'/volume/mute'
},
{
'name': 'ECA-CS',
'description':'''Retrieve (file)name of current chainsetup.''',
'command': 'GET',
'params': None,
'path':'/eca/chainsetup'
},
{
'name': 'ECA-CS-SET',
'description':'''Load chainsetup by name or from file.''',
'command': 'PUT',
'params': [
{ 'name':'chainsetup', 'datatype':(str,unicode,), 'required':True, 'help':'''Filename or Name''' }
],
'path':'/eca/chainsetup'
},
{
'name': 'MODE',
'description':'''Retrieve list of all modes and their states.''',
'command': 'GET',
'params': None,
'path':'/mode/all'
},
{
'name': 'MODE-ACTIVE',
'description':'''Retrieve list of all active modes.''',
'command': 'GET',
'params': None,
'path':'/mode/active'
},
{
'name': 'MODE-CHANGE',
'description':'''(De)activate multiple modes in one go. Example: volume,off,bass,on.''',
'command': 'PUT',
'params': [
{ 'name':'mode', 'datatype':(str,unicode,), 'required':True, 'help':'''Mode name.''' },
{ 'name':'state', 'datatype':(bool,), 'required':True, 'help':'''Activate/Deactivate.''', 'choices':[True,False] }
],
'params_repeat':True,
'path':'/mode/change'
},
{
'name': 'MODE-ACTIVATE',
'description':'''Activate mode.''',
'command': 'PUT',
'params': [
{ 'name':'mode', 'datatype':(str,unicode,), 'required':True, 'help':'''Mode name.''' }
],
'path':'/mode/set'
},
{
'name': 'MODE-DEACTIVATE',
'description':'''Deactivate mode.''',
'command': 'PUT',
'params': [
{ 'name':'mode', 'datatype':(str,unicode,), 'required':True, 'help':'''Mode name.''' }
],
'path':'/mode/unset'
}
]
for command in self.function_mq_map:
self.command_list.append(command['name'])
def get_command(self, command):
ix = self.command_list.index(command)
if ix is not None:
return self.function_mq_map[ix]
def get_command_by_path(self, mq_path, mq_cmd):
# TODO, add postfix slash to search
for cmd in self.function_mq_map:
if cmd['path'].upper() == mq_path.upper() and cmd['command'].upper() == mq_cmd.upper():
return cmd
def validate(self, command=None):
""" Decorator function.
Validates command arguments.
Used in conjunction with MQ registered functions
@messaging.register( ... )
@command.validate() or @command.validate( 'SOURCE-SELECT' )
def thing(path=None, cmd=None, args=None, data=None )
The function "thing" will be executed by hu_msg.execute with kwargs path, cmd, args and data.
Q. / TBD / TODO
Returns list(!) of valid arguments, None (no arguments) or False (invalid)
If command could not be found then ???
"""
def decorator(fn):
def wrapper(*args,**kwargs):
# add an empty args, if not present
if 'args' not in kwargs:
kwargs['args'] = None
# derive from MQ-command mapping table
if ( command is None and
'path' in kwargs and
'cmd' in kwargs ):
command_to_validate = self.get_command_by_path(kwargs['path'],kwargs['cmd'])
if command_to_validate is not None:
try:
kwargs['args'] = self.validate_args(command_to_validate['name'],*kwargs['args'])
except ValueError as err:
print("ERROR: {0}".format(err))
return False #??????????????
else:
return False #??????????????
# use provided command
#else:
# kwargs['args'] = self.validate_args(command,list_of_args)
return fn(*args,**kwargs)
return wrapper
return decorator
def validate_args(self, command, *args): #, repeat=False):
"""
args must be a list of arguments
Returns list of args (including defaults) if valid (may return None if no params)
Raises a ValueError if invalid.
"""
#print "DEBUG.. hello from validate_args Command={0}".format(command)
def strint_to_bool(value):
if isinstance(value, (str,unicode)) and value.lower() in ['true','on','1','t']:
return True
elif isinstance(value, (str,unicode)) and value.lower() in ['false','off','0','f']:
return False
elif isinstance(value, int) and value in [1]:
return True
elif isinstance(value, int) and value in [0]:
return False
else:
return None
def isint(value):
"""
Return true if value is an int
"""
if value[0] == '+' or value[0] == '-': value = value[1:]
try:
ret = float(value).is_integer()
return ret
except:
return False
def isfloatint(value):
"""
Return true if value is a float OR an int
"""
if value[0] == '+' or value[0] == '-': value = value[1:]
try:
ret = float(value)
return True
except:
return False
cmd_def = self.get_command(command)
arg_defs = cmd_def['params']
# START CONVERSION TO *args FORMAT
if isinstance(args, tuple):
arg_list = []
for arg in args:
arg_list.append(arg)
args = arg_list
print "CONVERTED BACK TO LIST: {0}".format(args)
# no arguments
if arg_defs is None and (args is None or args == []):
return None
# no arguments, but some given
elif arg_defs is None and len(args) > 0:
return None # ignoring extra arguments
# check if arguments are provided as a list
if not isinstance(args, list):
print "second argument must be a list"
return None
# make a copy
defs = arg_defs[:] # cuz we might manipulate it, and python is stupid
if defs is None:
return None
# generate repeat definitions
if 'params_repeat' in cmd_def and cmd_def['params_repeat']:
repeat = True
else:
repeat = False
if repeat:
for i in range(len(args)/len(arg_defs)-1):
defs.extend(arg_defs)
# error handling
error_raise = False
error_msg = ""
error_msg_datatype = ""
error_msg_choices = ""
# LOOP THROUGH ALL PROVIDED ARGUMENTS
for i, arg in enumerate(args):
# datatype
if isinstance(arg, defs[i]['datatype']):
pass
else:
if bool in defs[i]['datatype'] and strint_to_bool(arg) is not None:
args[i] = strint_to_bool(arg)
elif int in defs[i]['datatype'] and isinstance(arg, (str,unicode,)) and isint(arg):
# an int hidden as a string...
args[i] = int(arg)
else:
#print "hu_commands.py: Validate: Datatype: FAIL; {0}".format(type(args[i]))
error_raise = True
error_msg_datatype += " param:{0}, datatype:{1}, must be:{2}".format(defs[i]['name'],type(arg),defs[i]['datatype'])
# choices (case insensitive, valid for strings ONLY)
if ( 'choices' in defs[i] and
isinstance(arg, str) and
not arg.lower() in [choice.lower() for choice in defs[i]['choices']] ):
if 'FLOAT_PERCENTAGE' in defs[i]['choices'] and arg.endswith('%') and isfloatint(arg[:-1]):
pass
elif 'INT_PERCENTAGE' in defs[i]['choices'] and arg.endswith('%') and isint(arg[:-1]):
pass
elif 'FLOAT_SIGNED' in defs[i]['choices'] and (arg.startswith('+') or arg.startswith('-')) and isfloatint(arg[1:]):
pass
elif 'INT_SIGNED' in defs[i]['choices'] and (arg.startswith('+') or arg.startswith('-')) and isint(arg[1:]):
pass
else:
error_raise = True
error_msg_choices += "{0} must be in: {1}".format(defs[i]['name'],",".join(defs[i]['choices']))
if error_raise:
if error_msg_datatype != "" and error_msg_choices != "":
error_msg_datatype = "Invalid datatype(s): " + error_msg_datatype
error_msg_choices = "Invalid choice: " + error_msg_choices
raise ValueError(error_msg_datatype,error_msg_choices)
elif error_msg_datatype != "":
error_msg_datatype = "Invalid datatype(s): " + error_msg_datatype
raise ValueError(error_msg_datatype)
elif error_msg_choices != "":
error_msg_choices = "Invalid choice: " + error_msg_choices
raise ValueError(error_msg_choices)
'''
errors = []
if error_msg_datatype != "":
#error_msg += "Invalid datatype(s):" + error_msg_datatype
errors.append(error_msg_datatype)
if error_msg_choices != "":
#error_msg += "Invalid choice:" + error_msg_choices
errors.append(error_msg_choices)
print errors
raise ValueError(errors)
'''
# add default values / check if enough arguments present
i=0
for arg_def in defs[len(args):len(defs)]:
if 'required' in arg_def and arg_def['required']:
i += 1
args_req = i
args_missing = args_req-len(args)
i=0
for arg_def in defs[len(args):len(defs)]:
if 'default' in arg_def:
i += 1
defaults_available = i
if args_missing < 0:
pass
elif args_missing == 0 and args_missing == defaults_available:
# all ok
pass
elif args_missing > 0 and args_missing == defaults_available:
for arg_def in defs[len(args):len(defs)]:
#print arg_def
args.append(arg_def['default'])
else:
raise ValueError("Not enough arguments")
# everything OK
return args