Update readme and all script to python3

Fixes #2
This commit is contained in:
AxelC
2019-06-19 22:29:08 +02:00
parent f8fc7b0026
commit f1baa4594d
8 changed files with 44 additions and 32 deletions

1
.gitignore vendored
View File

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

View File

@@ -29,21 +29,21 @@ class CardList:
try:
return self.cardList[card]
except:
print 'Card %s is not card list' % card
print('Card %s is not card list' % card)
return ''
def addPlaylist(self, card, plist):
try:
if card not in self.cardList.keys():
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
print('Card %s is already used' % card)
except:
print 'Could not write file'
print('Could not write file')
if not os.path.isfile(self.path + '/cardList.csv'):
print 'File cardList.csv does not exist'
print('File cardList.csv does not exist')

View File

@@ -5,17 +5,26 @@ This project is a fork of Music Card from [hoveeman/music-cards](https://github.
It has been updated to be able to trig actions on others servers on rfid swipe.
List of actions:
- sonos Api [node-sonos-http-api](https://github.com/jishi/node-sonos-http-api)
- homme assistant (todo, change from hoveeman/music-cards)
- home assistant (todo, change from hoveeman/music-cards)
## Requires
### software:
- python pip. To install:
```bash
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install python3-pip
```
- python evdev. To install:
```bash
wget http://dl.piwall.co.uk/python-evdev_0.4.1-1_armhf.deb
dpkg -i python-evdev_0.4.1-1_armhf.deb
sudo pip3 --default-timeout=1000 install evdev
```
```--default-timeout=1000``` helps if the raspberry is too slow
### hardware:
- [Raspberry Pi Zero (Don't forget micro sd card and power supply)](http://www.microcenter.com/product/486575/Zero_W)
- [USB OTG Hub](https://www.amazon.com/gp/product/B01HYJLZH6/ref=oh_aui_detailpage_o08_s00?ie=UTF8&psc=1)
@@ -35,10 +44,10 @@ Test has to be done with [node-sonos-http-api](https://github.com/jishi/node-son
## Steps to Configure and/or Run once without AutoStart
0. Copy/Downloar/Clone the project in home/pi
1. Run `python setup_reader.py` to select the reader from the inputs
2. Run `python add_card.py` to scan cards and enter your Google Play Music Playlist Name
3. Make .sh scrypts executables : chmod +x sonosplay.sh
4. Run `python box.py` to start the application and verify that it is reading your cards and csv list properly
1. Run `python3 setup_reader.py` to select the reader from the inputs
2. Run `python3 add_card.py` to scan cards and enter your Google Play Music Playlist Name
3. Make .sh scrypts executables : `chmod +x sonosplay.sh`
4. Run `python3 box.py` to start the application and verify that it is reading your cards and csv list properly
## Install Service to AutoStart

View File

@@ -11,6 +11,8 @@ import sys
from evdev import InputDevice, categorize, ecodes, list_devices
from select import select
class Reader:
def __init__(self):
path = os.path.dirname(os.path.realpath(__file__))
@@ -34,11 +36,11 @@ class Reader:
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 ]
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]

View File

@@ -5,10 +5,10 @@ reader = Reader()
cardList = CardList()
while True:
print 'Place the card in the reader'
print('Place the card in the reader')
card = reader.readCard()
plist=raw_input('Specify Google Playlist Name-NoSpaces, q to quit')
plist=input('Specify Google Playlist Name-NoSpaces, q to quit')
if plist=="q":
break
cardList.addPlaylist(card, plist)
print "Exiting"
print("Exiting")

14
box.py
View File

@@ -33,32 +33,32 @@ else:
# Previous card id memory
previousCard = ""
print 'Ready: place a card on top of the reader'
print('Ready: place a card on top of the reader')
while True:
card = reader.readCard()
# Todo clear previousCard after some time (cfg.previousCardTimeout)
try:
print 'Read card : ', card
print('Read card : ', card)
if (previousCard == card) and ("cancel" == cfg.multiReadMode):
print 'Multi read : card canceled'
print('Multi read : card canceled')
else:
previousCard = card
# Card execution
plist = cardList.getPlaylist(card)
print 'Command : ', plist
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)
range(10000) # some payload code
list(range(10000)) # some payload code
time.sleep(0.2) # sane sleep time
except OSError as e:
print "Execution failed:"
range(10000) # some payload code
print("Execution failed:")
list(range(10000)) # some payload code
time.sleep(0.2) # sane sleep time of 0.1 seconds

View File

@@ -6,7 +6,7 @@ After=network.target
Type=simple
User=pi
WorkingDirectory=/home/pi/music-cards
ExecStart=/usr/bin/python -u /home/pi/music-cards/box.py
ExecStart=/usr/bin/python3 -u /home/pi/music-cards/box.py
Restart=always
RestartSec=5

View File

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