From 48d752afa9c3d562c0cf3baae96eb4d42aa72076 Mon Sep 17 00:00:00 2001 From: AxelC Date: Sun, 7 Jul 2019 22:15:06 +0200 Subject: [PATCH] Implement clearqueu mode in box Implement modes as list in CardList --- CardList.py | 46 +++++++++++++++++++++++++++++++++++++--------- Reader.py | 12 ++++++------ box.py | 9 ++++++--- sonosplay.sh | 19 +++++++------------ 4 files changed, 56 insertions(+), 30 deletions(-) diff --git a/CardList.py b/CardList.py index cd9e8f1..076e11e 100644 --- a/CardList.py +++ b/CardList.py @@ -9,6 +9,7 @@ import os.path class Card: def __init__(self, csv_line): + try: self.id = csv_line.split(",")[0].strip() if "" == self.id: @@ -24,11 +25,16 @@ class Card: self.cmd = "error" try: - self.mode = csv_line.split(",")[2].strip() - if "" == self.mode: + self.modes = [] + + raw_mode = csv_line.split(",")[2].strip() + if "" == raw_mode: raise ValueError("Incorrect Value for mode") + else: + for element in raw_mode.split(";"): + self.modes.append(element) except (IndexError, ValueError): - self.mode = "Normal" + self.modes.append("Normal") try: self.comment = csv_line.split(",")[3].strip() @@ -38,20 +44,39 @@ class Card: self.comment = "no" def __str__(self): - return "[" + self.id + "," + self.cmd + "," + self.mode + "," + self.comment + "]" + + return "[" + self.id + ", " + self.cmd + ", " + self.str_modes() + ", " + self.comment + "]" + + def str_modes(self): + modes_string = "(" + modes_string += ", ".join(self.modes) + modes_string += ")" + return modes_string + + def has_mode(self, mode): + + if 0 != self.modes.count(mode): + mode_exist = True + + else: + mode_exist = False + + return mode_exist class CardList: def __init__(self): + self.file_path = os.path.dirname(os.path.realpath(__file__)) + '/cardList.csv' # TODO use relative path? self.card_list = self.update_list() def __str__(self): - # return [print("[" + element.id + " " + element.cmd + " " + element.mode + "]") for element in self.cardList] + printed_list = self.card_list.__len__().__str__() + " Recorded cards:\n" + for element in self.card_list: - printed_list = printed_list + element.__str__() + "\n" + printed_list += element.__str__() + "\n" return printed_list @@ -82,8 +107,11 @@ class CardList: def main(): # Test the card creation - card_test = Card("1,2,3,4") - print(card_test) + card_test = Card("1,2,mode1;mode2;mode3,4") + print("Card: " + card_test.__str__()) + print(card_test.has_mode("mode2")) + print(card_test.has_mode("mode4")) + card_test = Card(",,,") print(card_test) card_test = Card("") @@ -98,7 +126,7 @@ def main(): if card is not None: print(card) - card = card_list.get_card("0") # Not Existing + card = card_list.get_card("0-0") # Not Existing if card is not None: print(card) diff --git a/Reader.py b/Reader.py index 183752a..6075ba1 100644 --- a/Reader.py +++ b/Reader.py @@ -19,26 +19,26 @@ class Reader: sys.exit('Please run config.py first') else: with open(path + '/deviceName.txt','r') as f: - deviceName = f.read() + device_name = f.read() devices = [InputDevice(fn) for fn in list_devices()] for device in devices: - if device.name == deviceName: + if device.name == device_name: self.dev = device break try: self.dev except: - sys.exit('Could not find the device %s\n. Make sure is connected' % deviceName) + sys.exit('Could not find the device %s\n. Make sure is connected' % device_name) def read_card(self): - stri = '' + keys_input = '' key = '' while key != 'KEY_ENTER': r,w,x = select([self.dev], [], []) for event in self.dev.read(): if event.type == 1 and event.value == 1: - stri += self.keys[event.code] + keys_input += self.keys[event.code] # print( keys[ event.code ] ) key = ecodes.KEY[event.code] - return stri[:-1] + return keys_input[:-1] diff --git a/box.py b/box.py index f5424df..4153629 100755 --- a/box.py +++ b/box.py @@ -56,12 +56,15 @@ while True: else: previousCard = read_id + if card.has_mode("ClearQueue"): + subprocess.check_call(["./sonosplay.sh " + commandLine + "clearqueue"], shell=True) + if card.cmd != 'error': # TODO, direct python implementation without using sh script - subprocess.check_call(["./sonosplay.sh %s" % commandLine + card.cmd], shell=True) + subprocess.check_call(["./sonosplay.sh " + commandLine + card.cmd], shell=True) - list(range(10000)) # some payload code - time.sleep(0.2) # sane sleep time + list(range(10000)) # some payload code + time.sleep(0.2) # sane sleep time except OSError as e: print("Execution failed:") diff --git a/sonosplay.sh b/sonosplay.sh index 0372836..fa5b0e5 100644 --- a/sonosplay.sh +++ b/sonosplay.sh @@ -3,22 +3,17 @@ # Get arguments addr=$1 # Mendatory - exemple : http://192.168.0.140:5005 cmd=$2 # Mendatory - exemple : volume/50 -where=$3 # Optional - exemple : salon TODO echo "Execute the command '$cmd' on the server at '$addr'" # Execute the sonos api request using curl -if [ -z $where ] -then - # Clear the queu and load the new album/playlist - # Todo, check if needed for all (maybe not for tracks - # curl -X GET $addr/clearqueue - curl -X GET $addr/$cmd -else - echo "only for the room $where" - # curl -X GET $addr/$where/clearqueue - curl -X GET $addr/$where/$cmd -fi + +# Clear the queu and load the new album/playlist +# Todo, check if needed for all (maybe not for tracks +# curl -X GET $addr/clearqueue +curl -X GET $addr/$cmd + +