...
 
Commits (2)
......@@ -44,7 +44,12 @@ class Database():
return res
def select_guest_team(self):
return self.select_one('SELECT id FROM players WHERE fname LIKE "guest"')[0]
return self.select_one('SELECT id FROM Players WHERE fname LIKE "guest"')[0]
def insert_player(self, rfid, fname, lname,):
self._cursor.execute('INSERT INTO Players (rfid, fname, lname, category, pic, private) VALUES (?, ?, ?, ?, ?)', (rfid, fname, lname, None, 0,))
self._connection.commit()
return self._cursor.execute('SELECT seq FROM sqlite_sequence WHERE name="Players"').fetchone()[0]
def insert_team(self, players, goals):
if len(players)<2:
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
"""
import logging
import request
from http import HTTPStatus
class Ginger(object):
URL = 'https://assos.utc.fr/ginger/v1/'
_instance = None
def __init__(self):
if Ginger._instance!=None:
self.url = Settings['ginger.url']
self.api_key = Settings['ginger.key']
@property
@staticmethod
def instance():
if Ginger._instance==None:
Ginger._instance = Ginger()
return Ginger._instance
@staticmethod
def call(endpoint, params={}):
# Add the API key to the parameter list
params['key'] = Ginger.instance.api_key
response = request.get(Ginger.instance.url + endpoint, params)
if response.status_code!=200:
return HTTPStatus(response.status_code)
else:
return response.content
......@@ -8,7 +8,6 @@ import logging
import time
from threading import Thread
import pyautogui # PyPi library
from pirc522 import RFID # PyPi library
from PyQt5.QtCore import QObject, pyqtSignal
......@@ -17,6 +16,7 @@ from Babyfut.core.player import Side
if ON_RASP:
import RPi.GPIO as GPIO
from pirc522 import RFID # PyPi library
class GPIOThread(Thread, QObject):
_keyButtonBindings = {
......@@ -27,17 +27,17 @@ class GPIOThread(Thread, QObject):
17: 'return',
18: 'escape'
}
rfidReceived = pyqtSignal(str)
def __init__(self):
Thread.__init__(self)
QObject.__init__(self)
self.rf_reader = RFID(pin_rst=25, pin_ce=8, pin_irq=24, pin_mode=GPIO.BCM)
self.continueRunning = True
self.lastRFIDReception = 0
if ON_RASP:
self.rf_reader = RFID(pin_rst=25, pin_ce=8, pin_irq=24, pin_mode=GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
......@@ -81,7 +81,8 @@ class GPIOThread(Thread, QObject):
def stop(self):
self.continueRunning = False
# Falsely trigger the rfid reader to stop it waiting
self.rf_reader.irq.set()
if ON_RASP:
self.rf_reader.irq.set()
def clean(self):
GPIOThread.clean()
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 18:34:40 2018
@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
"""
import logging
from enum import Enum
from http import HTTPStatus
from PyQt5.QtCore import Qt, QCoreApplication, QObject, pyqtSlot, QEvent
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QApplication
from Babyfut.babyfut import getMainWin, IMG_PATH
from Babyfut.core.ginger import Ginger
from Babyfut.core.database import Database, DatabaseError
from Babyfut.ui.consent_dialog_ui import Ui_Dialog as ConsentDialogUI
......@@ -64,37 +64,22 @@ class Player(QObject):
_placeholder_pic_path = ':ui/img/placeholder_head.jpg'
def __init__(self, id, rfid, fname, lname, pic_path, stats):
def __init__(self, id, rfid, fname, lname, pic_path, stats=None):
self.id = id
self.rfid = rfid
self.fname = fname
self.lname = lname
self.pic_path = pic_path if pic_path else Player._placeholder_pic_path # Default pic if None
self.stats = stats
if stats==None:
self.stats = { 'time_played': 0, 'goals_scored': 0, 'games_played': 0, 'victories': 0 }
else:
self.stats = stats
@staticmethod
def fromRFID(rfid):
db = Database.instance()
if db.rfid_exists(rfid):
try:
# Retrieve generic informations
id, fname, lname, pic = db.select_one(Player.__query_infos, rfid)
# Retrieve stats
stats = {}
stats['time_played'], stats['goals_scored'], stats['games_played'] = db.select_one(Player.__query_time_goals_games, id, id)
stats['victories'], = db.select_one(Player.__query_victories, id)
for key, val in stats.items():
if val==None:
stats[key] = 0
return Player(id, rfid, fname, lname, pic, stats)
except DatabaseError as e:
logging.warn('DB Error: {}'.format(e))
return PlayerGuest
if Database.instance().rfid_exists(rfid):
player = Player._loadFromDB(rfid)
else:
### Retrieve player from API
# Ask for consent
......@@ -102,12 +87,51 @@ class Player(QObject):
consentDialog.exec()
if consentDialog.result()==QDialog.Accepted:
print('todo')
return PlayerGuest
player = Player._loadFromAPI(rfid)
else:
return PlayerGuest
logging.debug('Consent refused when retrieving a player, returning Guest')
player = PlayerGuest
return player
def displayImg(self, container_widget):
@staticmethod
def _loadFromDB(rfid):
db = Database.instance()
try:
# Retrieve generic informations
id, fname, lname, pic = db.select_one(Player.__query_infos, rfid)
# Retrieve stats
stats = {}
stats['time_played'], stats['goals_scored'], stats['games_played'] = db.select_one(Player.__query_time_goals_games, id, id)
stats['victories'], = db.select_one(Player.__query_victories, id)
for key, val in stats.items():
if val==None:
stats[key] = 0
return Player(id, rfid, fname, lname, pic, stats)
except DatabaseError as e:
logging.warn('DB Error: {}'.format(e))
return PlayerGuest
@staticmethod
def _loadFromAPI(rfid):
'''
Retrieves a player's informations from the Ginger API
'''
response = Ginger.call('badge/{}'.format(rfid))
if isinstance(response, HTTPStatus):
logging.debug('Request to Ginger failed ({}): returning Guest'.format(response.value))
return PlayerGuest
else:
infos = json.loads(response)
Database.instance().insert_player(infos['rfid'], infos['nom'], infos['prenom'])
return Player._loadFromDB(rfid)
self.pic_container = container_widget
if self.pic_path.startswith('http'):
......@@ -127,18 +151,11 @@ class Player(QObject):
Downloader.instance().finished.disconnect(self._downloader_callback)
self.displayImg(self.pic_container)
def save(self):
'''
Update or create the player in database
'''
pass
# DONT SAVE PIC PATH, IT IS CHANGED FOR THE LOCAL PATH
def forgetPicture():
def forgetPicture(self):
self.pic_path = Player._placeholder_pic_path
Database.instance().delete_playerpic(self.id)
def make_private():
def make_private(self):
self.private = True
Database.instance().make_player_private(self.id)
......
......@@ -17,7 +17,7 @@ class Replay(Thread):
Thread.__init__(self)
self.replayPath = getContent('Replay {}.mp4'.format(side.name))
self.shutdown = False
if ON_RASP:
self.camera_detected = Replay.detectCam()
......@@ -81,9 +81,12 @@ class Replay(Thread):
@staticmethod
def detectCam():
camdet = subprocess.check_output(["vcgencmd","get_camera"])
return int(chr(camdet[-2]))
if ON_RASP:
camdet = subprocess.check_output(["vcgencmd","get_camera"])
return int(chr(camdet[-2]))
else:
return False
@staticmethod
def isCamAvailable(self=None):
detected = self.camera_detected if self!=None else Replay.detectCam()
......
......@@ -27,7 +27,7 @@ class AuthQuickModule(AuthModuleBase):
for side in [Side.Left, Side.Right]:
if len(self.players[side])==0:
self.addPlayer(side, PlayerGuest)
self.addPlayer(side, PlayerGuest)
self.updateSides()
......
......@@ -10,11 +10,12 @@ import logging
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QGraphicsBlurEffect, QApplication
from PyQt5.QtCore import Qt, QTime, QTranslator
from PyQt5.QtCore import Qt, QTime, QTranslator, pyqtSlot
from Babyfut import modules
from Babyfut.ui.main_ui import Ui_MainWindow
from Babyfut.core.player import Side
from Babyfut.core.settings import Settings
from Babyfut.ui.main_ui import Ui_MainWindow
class MainWin(QtWidgets.QMainWindow):
DEFAULT_LANG = 'en'
......@@ -80,6 +81,11 @@ class MainWin(QtWidgets.QMainWindow):
for modIdx in modulesIdx:
self.modules[modIdx].other(**msg)
@pyqtSlot(str)
def rfidHandler(self, rfid):
side = Side.Left if Settings['app.side']=='left' else Side.Right
self.dispatchMessage({'rfid': rfid, 'source': side})
def _loadSettings(self):
if Settings['ui.fullscreen']:
self.showFullScreen()
......