Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play a music playlist #1 #2

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions Android_App/02_TCP_client_1/appmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,83 @@
#include "appmanager.h"
#include <QDebug>
#include <QTextCodec>

QStringList *AppManager::getMp_availablePlaylists() const
{
return mp_availablePlaylists;
}

AppManager::AppManager(QObject *parent) : QObject(parent)
{
mp_socket = new QTcpSocket();
mp_availablePlaylists = new QStringList();
m_appState = HOME;

connect(mp_socket, &QIODevice::readyRead, this, &AppManager::receivedDataTCP);
connectTCP();
}

AppManager::~AppManager()
{
mp_socket->disconnectFromHost();
delete mp_socket;
}

void AppManager::launchApp(QString message)
void AppManager::launchApp(qint8 appstate)
{
m_appState = static_cast<AppState>(appstate);

writeTCP(QString::number(appstate));
}

void AppManager::connectTCP()
{
mp_socket->connectToHost("192.168.1.19", 10000);
}

bool AppManager::writeTCP(QString message)
{
qDebug()<<__FUNCTION__<<message;

if(mp_socket->state() == QAbstractSocket::ConnectedState){
QByteArray ba = message.toLocal8Bit();
mp_socket->write(ba.data(), message.length());
return 1;
}
else{
qDebug()<<"socket not connected, current state :"<<mp_socket->state();
connectTCP();
return 0;
}
}

void AppManager::connectTCP()
void AppManager::parseTCP(QString response)
{
mp_socket->connectToHost("192.168.1.19", 10000);
switch (m_appState) {
case HOME:
qDebug()<<"No response supported in HOME state";
break;
case MUSIC:
mp_availablePlaylists->clear();
mp_availablePlaylists->append(response.split(";"));

emit availablePlaylistChanged();
break;
case VIDEO:
qDebug()<<"No response supported in VIDEO state";
break;
default:
break;
}
}

void AppManager::receivedDataTCP()
{
QByteArray ba = mp_socket->readAll();
QString s(ba);

qDebug()<<"New message:["<<s<<"]";

parseTCP(s);
}

24 changes: 22 additions & 2 deletions Android_App/02_TCP_client_1/appmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,37 @@
class AppManager : public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList availablePlaylists READ getMp_availablePlaylists NOTIFY availablePlaylistChanged)

public:
enum AppState{
HOME = 0,
MUSIC,
VIDEO,
};
Q_ENUM(AppState);

private :
QTcpSocket* mp_socket;
AppState m_appState;
QStringList* mp_availablePlaylists;

public:
explicit AppManager(QObject *parent = nullptr);
~AppManager();

Q_INVOKABLE void launchApp(QString message);
Q_INVOKABLE void launchApp(qint8 appstate);
Q_INVOKABLE void connectTCP();
Q_INVOKABLE bool writeTCP(QString message);
void parseTCP(QString response);

signals:
QStringList *getMp_availablePlaylists() const;

public slots:
void receivedDataTCP();

signals:
void availablePlaylistChanged();
};

#endif // APPMANAGER_H
1 change: 1 addition & 0 deletions Android_App/02_TCP_client_1/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int main(int argc, char *argv[])

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("AppManager", appManager);
qmlRegisterType<AppManager>("StateEnum",1,0,"AppState");

const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
Expand Down
24 changes: 17 additions & 7 deletions Android_App/02_TCP_client_1/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5
import QtQuick.Extras 1.4

import StateEnum 1.0

Window {
visible: true
Expand All @@ -18,27 +21,34 @@ Window {
Case_Coloree{
color : "red"
iconPath: "Images/alarm.png"
onClicked: AppManager.launchApp("Alarme");
onClicked: console.log("red button")//AppManager.launchApp(AppState.HOME);
}
Case_Coloree{
color: "green"
iconPath: "Images/console.png"
onClicked: AppManager.launchApp("Jeux");
onClicked: console.log("jeux button")//AppManager.launchApp(AppState.HOME);
}
Case_Coloree{
color: "yellow"
iconPath: "Images/youtube.png"
onClicked: AppManager.launchApp("Youtube");
onClicked: AppManager.launchApp(AppState.VIDEO);
}
Case_Coloree{
color: "blue"
iconPath: "Images/music-player.png"
onClicked: AppManager.launchApp("Musique");
onClicked: AppManager.launchApp(AppState.MUSIC);
}
}
Button{
text: "Reconnect"
onClicked: AppManager.connectTCP()
Row{
Button{
text: "Reconnect"
onClicked: AppManager.connectTCP()
}
StatusIndicator{

}
}


} // end mainLayout
}
7 changes: 7 additions & 0 deletions rpi/tcp_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import socket
import sys
import os
import subprocess

IP_ADDR = "192.168.1.19"
TCP_PORT = 10000
Expand All @@ -22,5 +24,10 @@
print("Connection from {} ".format(client_address))
data = connection.recv(16)
print("Data : %s" % data)
if data == "Musique":
proc = subprocess.Popen(['ls', '/home/pi/Music'], stdout=subprocess.PIPE)
list_playlist = proc.stdout.read().split()
print(list_playlist)
connection.sendall(";".join(list_playlist))
else:
connection.close()