Remove tabs, start creation of card class to implements modes

This commit is contained in:
AxelC
2019-07-07 15:55:31 +02:00
parent f1baa4594d
commit c190101711
7 changed files with 192 additions and 123 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@ config.py
.idea/
deviceName.txt
*.bak
__pycache__/

View File

@@ -1,49 +1,107 @@
#
# MUSIC CARD
# Card management
# BIPBIP ZIZIK
# Card List class
# Take cards from csv file and manage it
#
import csv
import os.path
import sys
class Card:
def __init__(self, csv_line):
try:
self.id = csv_line.split(",")[0].strip()
if "" == self.id:
raise ValueError("Incorrect Value for id")
except (IndexError, ValueError):
self.id = "error"
try:
self.cmd = csv_line.split(",")[1].strip()
if "" == self.cmd:
raise ValueError("Incorrect Value for cmd")
except (IndexError, ValueError):
self.cmd = "error"
try:
self.mode = csv_line.split(",")[2].strip()
if "" == self.mode:
raise ValueError("Incorrect Value for mode")
except (IndexError, ValueError):
self.mode = "Normal"
try:
self.comment = csv_line.split(",")[3].strip()
if "" == self.comment:
raise ValueError("Incorrect Value for comment")
except (IndexError, ValueError):
self.comment = "no"
def __str__(self):
return "[" + self.id + "," + self.cmd + "," + self.mode + "," + self.comment + "]"
class CardList:
def __init__(self):
self.path = os.path.dirname(os.path.realpath(__file__))
self.cardList = self.readList()
def readList(self):
with open(self.path + '/cardList.csv', mode='r') as infile:
reader = csv.reader(infile)
cardList = {rows[0]:rows[1] for rows in reader}
# TODO exclude errors lines
# IndexError: list index out of range
infile.close()
return cardList
def getPlaylist(self,card):
self.cardList = self.readList()
try:
return self.cardList[card]
except:
print('Card %s is not card list' % card)
return ''
def addPlaylist(self, card, plist):
try:
if card not in list(self.cardList.keys()):
f = open(self.path + '/cardList.csv', 'a')
f.write(card + ',' + plist + '\n')
self.cardList[card] = plist
else:
print('Card %s is already used' % card)
except:
print('Could not write file')
if not os.path.isfile(self.path + '/cardList.csv'):
print('File cardList.csv does not exist')
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"
return printed_list
def update_list(self):
up_to_date_list = []
for line in open(self.file_path):
# Exclude comments and empty lines
if (not line.startswith("#")) and (not "" == line.strip()):
up_to_date_list.append(Card(line))
return up_to_date_list
def get_card(self, searched_id):
# find first occurrence of searched card
try:
the_card = next(card for card in self.card_list if card.id == searched_id)
return the_card
except StopIteration:
print("Card " + searched_id + " not found in bdd")
return None
# For test purpose
def main():
# Test the card creation
card_test = Card("1,2,3,4")
print(card_test)
card_test = Card(",,,")
print(card_test)
card_test = Card("")
print(card_test)
# Test the card list creation
card_list = CardList()
print(card_list)
# Test the search of card in list
card = card_list.get_card("0013200813") # Existing
if card is not None:
print(card)
card = card_list.get_card("0") # Not Existing
if card is not None:
print(card)
if __name__ == "__main__":
main()

View File

@@ -4,8 +4,6 @@
# Card Reader
#
import string
import csv
import os.path
import sys
@@ -14,33 +12,33 @@ from select import select
class Reader:
def __init__(self):
path = os.path.dirname(os.path.realpath(__file__))
self.keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
if not os.path.isfile(path + '/deviceName.txt'):
sys.exit('Please run config.py first')
else:
with open(path + '/deviceName.txt','r') as f:
deviceName = f.read()
devices = [InputDevice(fn) for fn in list_devices()]
for device in devices:
if device.name == deviceName:
self.dev = device
break
try:
self.dev
except:
sys.exit('Could not find the device %s\n. Make sure is connected' % deviceName)
def readCard(self):
stri=''
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]
# print( keys[ event.code ] )
key = ecodes.KEY[event.code]
return stri[:-1]
def __init__(self):
path = os.path.dirname(os.path.realpath(__file__))
self.keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
if not os.path.isfile(path + '/deviceName.txt'):
sys.exit('Please run config.py first')
else:
with open(path + '/deviceName.txt','r') as f:
deviceName = f.read()
devices = [InputDevice(fn) for fn in list_devices()]
for device in devices:
if device.name == deviceName:
self.dev = device
break
try:
self.dev
except:
sys.exit('Could not find the device %s\n. Make sure is connected' % deviceName)
def read_card(self):
stri = ''
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]
# print( keys[ event.code ] )
key = ecodes.KEY[event.code]
return stri[:-1]

38
box.py
View File

@@ -5,10 +5,7 @@
# Application
#
import re
import sys
import subprocess
import os
import time
from CardList import CardList
from Reader import Reader
@@ -36,26 +33,35 @@ previousCard = ""
print('Ready: place a card on top of the reader')
while True:
card = reader.readCard()
read_id = reader.read_card()
# Todo clear previousCard after some time (cfg.previousCardTimeout)
try:
print('Read card : ', card)
print('Read card : ', read_id)
if (previousCard == card) and ("cancel" == cfg.multiReadMode):
print('Multi read : card canceled')
else:
previousCard = card
# Update Card bdd TODO do it every X minutes
cardList.update_list()
# Find the card in bdd
card = cardList.get_card(read_id).cmd
if card is not None:
# Card execution
plist = cardList.getPlaylist(card)
print('Command : ', plist)
if plist != '':
# Check if sonosplay.sh is executable, if not write an error message
subprocess.check_call(["./sonosplay.sh %s" % commandLine + plist], shell=True)
print('Command : ', card.cmd)
print('Modes : ', card.mode)
list(range(10000)) # some payload code
time.sleep(0.2) # sane sleep time
# Update the previous card memory
if (previousCard == read_id) and ("cancel" == cfg.multiReadMode):
print('Multi read : card canceled')
else:
previousCard = read_id
if card.cmd != 'error':
# TODO, direct python implementation without using sh script
subprocess.check_call(["./sonosplay.sh %s" % commandLine + card.cmd], shell=True)
list(range(10000)) # some payload code
time.sleep(0.2) # sane sleep time
except OSError as e:
print("Execution failed:")

View File

@@ -1,29 +1,34 @@
Card Id,Command,Comment
0013397903,playpause
0013403195,next
0013354327,volume/+5
0005690629,volume/-5
0013365376,spotify/now/spotify:album:4yYVqX2KierVI3nDV0M2UL, M - Lettre Infinie
0013193487,spotify/now/spotify:playlist:37i9dQZF1DWXTHBOfJ8aI7, Légendes du Rock
0003586576,spotify/now/spotify:album:45Hm9e77uZVaRnEIYzzpzM, Adebert 2
0013381409,spotify/now/spotify:album:3qU4wXm0Qngbtnr5PiLbFX, Caravan Palace
0013356493,spotify/now/spotify:album:5lFcL4pj96ZRsoIiHpFl79, Shaka Ponk
0005585628,spotify/now/spotify:album:0bUTHlWbkSQysoM3VsWldT, Gorillaz
0013343924,spotify/now/spotify:album:3bRM4GQgoFjBRRzhp87Ugb, Kavinsky
0005690638,tunein/play/24875,Radio France Inter
0013385899,tunein/play/17696,Radio Nova
0005589167,tunein/play/50486,Radio RTL2
0013336496,tunein/play/2960,Radio Nostalgie
HUGO,Command,Comment
0013200813,spotify/now/spotify:album:1xhy7WWxO28XoPKuFlnxSZ, Aldebert Enfantillage 1
0013352322,spotify/now/spotify:album:45Hm9e77uZVaRnEIYzzpzM, Aldebert Enfantillage 2
0013362342,spotify/now/spotify:album:77kv2o5PJeW3mim1yWPiMA, Aldebert Enfantillage 3
0013177875,spotify/now/spotify:album:3qU4wXm0Qngbtnr5PiLbFX, Caravan Palace
0013397291,spotify/now/spotify:track:35VKLRwEjuR5IuFyGqjMaf, Les petits poissons
0013199764,spotify/now/spotify:track:7zwcj8LYBpHfcPTgR5LkFg, Bateau sur l'eau
0013400272,spotify/now/spotify:track:3yCoWlqfp2wnOS4PeNsADE, Pirouette Cacahuete
0013337950,spotify/now/spotify:track:16nCFJAHubC7sj9LsTt3KF, Un grand Cerf
0013385163,spotify/now/spotify:track:7IDzwsobKLjgaqg6keknrE, Planter les choux
0013353316,spotify/now/spotify:track:2eu7C32YZKEyILfPHPwVa3, Coucou Hibou
Later,Command,Comment
dead,spotify/now/spotify:playlist:5p0Mw9D1i1EjkJyIafFsvH, Hugo Dance
# Card Id, Command, Mode, Comment
# For Parents
0013397903,playpause, ,
0013403195,next, ,
0013354327,volume/+5, ,
0005690629,volume/-5, ,
0013365376,spotify/now/spotify:album:4yYVqX2KierVI3nDV0M2UL, , M - Lettre Infinie
0013193487,spotify/now/spotify:playlist:37i9dQZF1DWXTHBOfJ8aI7, , Légendes du Rock
0003586576,spotify/now/spotify:album:45Hm9e77uZVaRnEIYzzpzM, , Adebert 2
0013381409,spotify/now/spotify:album:3qU4wXm0Qngbtnr5PiLbFX, , Caravan Palace
0013356493,spotify/now/spotify:album:5lFcL4pj96ZRsoIiHpFl79, , Shaka Ponk
0005585628,spotify/now/spotify:album:0bUTHlWbkSQysoM3VsWldT, , Gorillaz
0013343924,spotify/now/spotify:album:3bRM4GQgoFjBRRzhp87Ugb, , Kavinsky
0005690638,tunein/play/24875, , Radio France Inter
0013385899,tunein/play/17696, , Radio Nova
0005589167,tunein/play/50486, , Radio RTL2
0013336496,tunein/play/2960, , Radio Nostalgie
# For hugo, Command, Mode, Comment
0013200813,spotify/now/spotify:album:1xhy7WWxO28XoPKuFlnxSZ, ClearQueue, Aldebert Enfantillage 1
0013352322,spotify/now/spotify:album:45Hm9e77uZVaRnEIYzzpzM, ClearQueue, Aldebert Enfantillage 2
0013362342,spotify/now/spotify:album:77kv2o5PJeW3mim1yWPiMA, ClearQueue, Aldebert Enfantillage 3
0013177875,spotify/now/spotify:album:3qU4wXm0Qngbtnr5PiLbFX, ClearQueue, Caravan Palace
0013397291,spotify/now/spotify:track:35VKLRwEjuR5IuFyGqjMaf, ClearQueue, Les petits poissons
0013199764,spotify/now/spotify:track:7zwcj8LYBpHfcPTgR5LkFg, ClearQueue, Bateau sur l'eau
0013400272,spotify/now/spotify:track:3yCoWlqfp2wnOS4PeNsADE, ClearQueue, Pirouette Cacahuete
0013337950,spotify/now/spotify:track:16nCFJAHubC7sj9LsTt3KF, ClearQueue, Un grand Cerf
0013385163,spotify/now/spotify:track:7IDzwsobKLjgaqg6keknrE, ClearQueue, Planter les choux
0013353316,spotify/now/spotify:track:2eu7C32YZKEyILfPHPwVa3, ClearQueue, Coucou Hibou
# To be checked
#playlist doesn't work, spotify/now/spotify:playlist:5p0Mw9D1i1EjkJyIafFsvH, , Hugo Dance
1 Card Id,Command,Comment # Card Id, Command, Mode, Comment
2 0013397903,playpause # For Parents
3 0013403195,next 0013397903,playpause, ,
4 0013354327,volume/+5 0013403195,next, ,
5 0005690629,volume/-5 0013354327,volume/+5, ,
6 0013365376,spotify/now/spotify:album:4yYVqX2KierVI3nDV0M2UL, M - Lettre Infinie 0005690629,volume/-5, ,
7 0013193487,spotify/now/spotify:playlist:37i9dQZF1DWXTHBOfJ8aI7, Légendes du Rock 0013365376,spotify/now/spotify:album:4yYVqX2KierVI3nDV0M2UL, , M - Lettre Infinie
8 0003586576,spotify/now/spotify:album:45Hm9e77uZVaRnEIYzzpzM, Adebert 2 0013193487,spotify/now/spotify:playlist:37i9dQZF1DWXTHBOfJ8aI7, , Légendes du Rock
9 0013381409,spotify/now/spotify:album:3qU4wXm0Qngbtnr5PiLbFX, Caravan Palace 0003586576,spotify/now/spotify:album:45Hm9e77uZVaRnEIYzzpzM, , Adebert 2
10 0013356493,spotify/now/spotify:album:5lFcL4pj96ZRsoIiHpFl79, Shaka Ponk 0013381409,spotify/now/spotify:album:3qU4wXm0Qngbtnr5PiLbFX, , Caravan Palace
11 0005585628,spotify/now/spotify:album:0bUTHlWbkSQysoM3VsWldT, Gorillaz 0013356493,spotify/now/spotify:album:5lFcL4pj96ZRsoIiHpFl79, , Shaka Ponk
12 0013343924,spotify/now/spotify:album:3bRM4GQgoFjBRRzhp87Ugb, Kavinsky 0005585628,spotify/now/spotify:album:0bUTHlWbkSQysoM3VsWldT, , Gorillaz
13 0005690638,tunein/play/24875,Radio France Inter 0013343924,spotify/now/spotify:album:3bRM4GQgoFjBRRzhp87Ugb, , Kavinsky
14 0013385899,tunein/play/17696,Radio Nova 0005690638,tunein/play/24875, , Radio France Inter
15 0005589167,tunein/play/50486,Radio RTL2 0013385899,tunein/play/17696, , Radio Nova
16 0013336496,tunein/play/2960,Radio Nostalgie 0005589167,tunein/play/50486, , Radio RTL2
17 HUGO,Command,Comment 0013336496,tunein/play/2960, , Radio Nostalgie
18 0013200813,spotify/now/spotify:album:1xhy7WWxO28XoPKuFlnxSZ, Aldebert Enfantillage 1 # For hugo, Command, Mode, Comment
19 0013352322,spotify/now/spotify:album:45Hm9e77uZVaRnEIYzzpzM, Aldebert Enfantillage 2 0013200813,spotify/now/spotify:album:1xhy7WWxO28XoPKuFlnxSZ, ClearQueue, Aldebert Enfantillage 1
20 0013362342,spotify/now/spotify:album:77kv2o5PJeW3mim1yWPiMA, Aldebert Enfantillage 3 0013352322,spotify/now/spotify:album:45Hm9e77uZVaRnEIYzzpzM, ClearQueue, Aldebert Enfantillage 2
21 0013177875,spotify/now/spotify:album:3qU4wXm0Qngbtnr5PiLbFX, Caravan Palace 0013362342,spotify/now/spotify:album:77kv2o5PJeW3mim1yWPiMA, ClearQueue, Aldebert Enfantillage 3
22 0013397291,spotify/now/spotify:track:35VKLRwEjuR5IuFyGqjMaf, Les petits poissons 0013177875,spotify/now/spotify:album:3qU4wXm0Qngbtnr5PiLbFX, ClearQueue, Caravan Palace
23 0013199764,spotify/now/spotify:track:7zwcj8LYBpHfcPTgR5LkFg, Bateau sur l'eau 0013397291,spotify/now/spotify:track:35VKLRwEjuR5IuFyGqjMaf, ClearQueue, Les petits poissons
24 0013400272,spotify/now/spotify:track:3yCoWlqfp2wnOS4PeNsADE, Pirouette Cacahuete 0013199764,spotify/now/spotify:track:7zwcj8LYBpHfcPTgR5LkFg, ClearQueue, Bateau sur l'eau
25 0013337950,spotify/now/spotify:track:16nCFJAHubC7sj9LsTt3KF, Un grand Cerf 0013400272,spotify/now/spotify:track:3yCoWlqfp2wnOS4PeNsADE, ClearQueue, Pirouette Cacahuete
26 0013385163,spotify/now/spotify:track:7IDzwsobKLjgaqg6keknrE, Planter les choux 0013337950,spotify/now/spotify:track:16nCFJAHubC7sj9LsTt3KF, ClearQueue, Un grand Cerf
27 0013353316,spotify/now/spotify:track:2eu7C32YZKEyILfPHPwVa3, Coucou Hibou 0013385163,spotify/now/spotify:track:7IDzwsobKLjgaqg6keknrE, ClearQueue, Planter les choux
28 Later,Command,Comment 0013353316,spotify/now/spotify:track:2eu7C32YZKEyILfPHPwVa3, ClearQueue, Coucou Hibou
29 dead,spotify/now/spotify:playlist:5p0Mw9D1i1EjkJyIafFsvH, Hugo Dance # To be checked
30 #playlist doesn't work, spotify/now/spotify:playlist:5p0Mw9D1i1EjkJyIafFsvH, , Hugo Dance
31
32
33
34

View File

@@ -6,13 +6,13 @@ path = os.path.dirname(os.path.realpath(__file__))
i = 0
print("Choose the reader from list")
for dev in devices:
print(i, dev.name)
i += 1
print(i, dev.name)
i += 1
dev_id = int(input('Device Number: '))
with open(path + '/deviceName.txt','w') as f:
f.write(devices[dev_id].name)
f.close()
f.write(devices[dev_id].name)
f.close()

View File

@@ -1,4 +1,5 @@
ssh pi@192.168.1.73
ssh pi@192.168.1.73 Salon
ssh pi@192.168.1.70 Hugo
REM To get systemd logs
REM journalctl -u musiccards