Skip to content

Commit

Permalink
avancements nouveau client socket
Browse files Browse the repository at this point in the history
  • Loading branch information
ldleman committed Oct 26, 2015
1 parent 2d925a0 commit eabbaa8
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 84 deletions.
83 changes: 42 additions & 41 deletions classes/Client.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,64 @@


class Client {
public $id;
public $socket;


public static function send($msg){
echo PHP_EOL.'Creation du socket d auto connexion';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
public function connect(){
echo PHP_EOL.'Creation du socket d auto connexion';
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$response = '';
if ($socket !== false) {
echo PHP_EOL.'Connexion au serveur socket depuis yana-server';
$result = socket_connect($socket, '127.0.0.1', 9999);
if ($result !== false) {
echo PHP_EOL.'Envois du message au client: '.$in;
$in = json_encode($msg);
$in .= '<EOF>';
socket_write($socket, $in, strlen($in));
$out = '';
echo PHP_EOL.'Déconnexion au serveur socket depuis yana-server';
socket_shutdown($socket);
socket_close($socket);
$socket = null;
}else{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
echo PHP_EOL.' Tentative de connexion au serveur socket depuis yana-server : ECHEC '.$errormsg;
}
if ($this->socket !== false) {
echo PHP_EOL.'Connexion au serveur socket depuis yana-server';
$result = socket_connect( $this->socket, '127.0.0.1', 9999);
return $result;
}
return false;
}

public function disconnect(){
socket_shutdown($this->socket);
socket_close($this->socket);
$this->socket = null;
}

return $response;
public function send($msg){
echo PHP_EOL.'Envois du message au client: '.$in;
$in = json_encode($msg);
$in .= '<EOF>';
socket_write( $this->socket, $in, strlen($in));
$int = '';
}


public static function sound($parameter){
Client::send(array("action"=>"SOUND","parameter"=>$parameter));
}
public static function talk($parameter){
echo 'Execution envois de parole vers un client : '.$parameter;
Client::send(array("action"=>"TALK","parameter"=>$parameter));
public function talk($parameter){
echo 'Execution envois de parole vers un client : '.$parameter;
$this->send(array("action"=>"TALK","parameter"=>$parameter));
}

public function sound($parameter){
$this->send(array("action"=>"SOUND","parameter"=>$parameter));
}
public static function execute($parameter){
Client::send(array("action"=>"EXECUTE","parameter"=>$parameter));

public function execute($parameter){
$this->send(array("action"=>"EXECUTE","parameter"=>$parameter));
}
public static function emotion($emotion){
Client::send(array("action"=>"EMOTION","parameter"=>$emotion));
public function emotion($emotion){
$this->send(array("action"=>"EMOTION","parameter"=>$emotion));
}
public static function image($image){
Client::send(array("action"=>"IMAGE","parameter"=>$image));
public function image($image){
$this->send(array("action"=>"IMAGE","parameter"=>$image));
}


public static function connect() {
// public static function connect() {

if(is_null(self::$instance)) {
self::$instance = new Client();
}
// if(is_null(self::$instance)) {
// self::$instance = new Client();
// }

return self::$instance;
}
// return self::$instance;
// }


}
Expand Down
Binary file added plugins/vocal_infos/img/yana.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
124 changes: 88 additions & 36 deletions plugins/vocal_infos/vocalinfo.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
@description Permet la récuperations d'informations locales ou sur le web comme la météo, les séries TV, l'heure, la date et l'état des GPIO
*/



define('VOCALINFO_COMMAND_FILE','cmd.json');

function vocalinfo_vocal_command(&$response,$actionUrl){
Expand All @@ -29,67 +31,117 @@ function vocalinfo_vocal_command(&$response,$actionUrl){
'confidence'=>0.8);

$response['commands'][] = array(
'command'=>$conf->get('VOCAL_ENTITY_NAME').' montre moi',
'command'=>$conf->get('VOCAL_ENTITY_NAME').' présente toi',
'callback'=>'vocalinfo_give_me_all',
'confidence'=>0.8);

$response['commands'][] = array(
'command'=>$conf->get('VOCAL_ENTITY_NAME').' enerve toi',
'callback'=>'vocalinfo_emotion_angry',
'confidence'=>0.8);

$response['commands'][] = array(
'command'=>$conf->get('VOCAL_ENTITY_NAME').' montre toi',
'callback'=>'vocalinfo_show_you',
'confidence'=>0.8);

$response['commands'][] = array(
'command'=>$conf->get('VOCAL_ENTITY_NAME').' lance le programme',
'callback'=>'vocalinfo_launch_program',
'confidence'=>0.8);

$response['commands'][] = array(
'command'=>$conf->get('VOCAL_ENTITY_NAME').' fait la poule',
'callback'=>'vocalinfo_chicken',
'confidence'=>0.8);
}

function vocalinfo_define_word($text,$confidence,$parameters){
$cli = new Client();
$cli->connect();
if($text=='bistro'){
Client::talk("Un bistro est un lieu de cultes, ou les sages de ce siècle vont se receuillir");
$cli->talk("Un bistro est un lieu de cultes, ou les sages de ce siècle vont se recueillir");
}else{
$json = json_decode(file_get_contents('https://fr.wikipedia.org/w/api.php?action=opensearch&search='.$text),true);
$define = $json[2][0];
if($json==false || trim($define)==""){Client::talk("Le mot ".$text." ne fait pas partie de mot vocabulaire, essayez plutot avec le mot bistro");
if($json==false || trim($define)==""){$cli->talk("Le mot ".$text." ne fait pas partie de mot vocabulaire, essayez plutot avec le mot bistro");
$cli->disconnect();
return;
}
Client::talk($define);
$cli->talk($define);
}
$cli->disconnect();
//Client::execute("D:\Programme_installes\Qt\Tools\QtCreator\bin\qtcreator.exe");
}

function vocalinfo_show_you($text,$confidence,$parameters){
$cli = new Client();
$cli->connect();
$cli->image(YANA_URL."/plugins/vocal_infos/img/yana.jpg");
$cli->talk("Est ce que tu me trouve jolie?");
$cli->disconnect();
}

function vocalinfo_emotion_angry($text,$confidence,$parameters){
Client::emotion("angry");
Client::talk("Je me fache toute rouge");
$cli = new Client();
$cli->connect();
$cli->talk("Tu vois ce qui se passe quand tu me prend la tête ?");
$cli->emotion("angry");
$cli->disconnect();
}

function vocalinfo_chicken($text,$confidence,$parameters){
$cli = new Client();
$cli->connect();
$cli->sound("C:/poule.wav");
$cli->disconnect();
}

function vocalinfo_launch_program($text,$confidence,$parameters){
$cli = new Client();
$cli->connect();
switch($text){
case 'sublime texte':
$cli->execute("C:\Program Files\Sublime Text 2\sublime_text.exe");
$cli->talk("Programme en cours de lancement");
break;
default:
$cli->talk("Je ne connais pas le programme : ".$text);
break;
}



$cli->disconnect();
}


function vocalinfo_give_me_all($text,$confidence,$parameters){

//Client::talk("Ok patron");
//sleep(5);
//Client::talk("Je peux parler, evidemment, et t\'écouter plus précisement qu\'avant");
// sleep(2);
// Client::talk("Je peux eprouver et montrer des sentiments");
// sleep(2);
// Client::talk("Comme la colère");
// Client::emotion("angry");
// sleep(2);
// Client::talk("Ou la timidité");
// Client::emotion("shy");
// sleep(2);
// Client::talk("Et tout un tas d\'autres lubies humaines");
// sleep(2);
// Client::talk("Je peux aussi executer un programme");
// Client::execute("D:\Programme_installes\Qt\Tools\QtCreator\bin\qtcreator.exe");
// sleep(2);
// Client::talk("ou un son");
// Client::sound("C:/poule.wav");
// sleep(2);
// Client::talk("ou te montrer des images");
// Client::image("yana.jpg");
// sleep(2);
// Client::talk("ou executer une commande domotique");
// system('gpio write 1 1');
// sleep(2);
// Client::talk("ou executer un humain");
// sleep(2);
// Client::talk("non je déconne.");
//
$cli = new Client();
$cli->connect();

$cli->talk("Je peux parler, evidemment, et t\'écouter plus précisément qu\'avant");
$cli->talk("Je peux eprouver et montrer des sentiments");
$cli->talk("Comme la colère");
$cli->emotion("angry");
$cli->talk("Ou la timidité");
$cli->emotion("shy");
$cli->talk("Et tout un tas d\'autres lubies humaines");
$cli->talk("Je peux aussi exécuter un programme");
$cli->execute("D:\Programme_installes\Qt\Tools\QtCreator\bin\qtcreator.exe");
$cli->talk("ou un son");
$cli->sound("C:/poule.wav");
$cli->talk("ou te montrer des images");
$cli->image("yana.jpg");
$cli->talk("ou executer une commande domotique");
//system('gpio write 1 1');

//$cli->talk("ou executer un humain");

//$cli->talk("non je déconne.");

$cli->disconnect();
}

function vocalinfo_action(){
Expand Down
19 changes: 12 additions & 7 deletions socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ class ClientSocket extends SocketServer {
public $connected = array();
private $received = array();
function onDataReceived($socket,$data) {
$socketId = (int)$socket;

$socketId = (int)$socket;
if(!isset($received[$socketId] )) $received[$socketId] = '';
$received[$socketId].= $data;

if(substr($received[$socketId],-5)=='<EOF>'){

$received[$socketId] = substr($received[$socketId],0,-5);
$this->handleData($this->connected[(int)$socket],$received[$socketId]);
$received[$socketId] = '';
}
//$this->handleData($this->connected[(int)$socket],$data);



//OLD - $this->sendBroadcast("\r\n".$this->$connected[$socket]->name . ' : ' . $data);



}
function onClientConnected($socket) {
$this->log('New client connected: ' . $socket);
Expand All @@ -43,6 +41,9 @@ function onClientDisconnected($socket) {
function handleData($client,$data){
$this->log("Try to parse received data : ".$data);
try{

$datas = explode('<EOF>',$data);
foreach($datas as $data){
$_ = json_decode($data,true);

if(!$_) throw new Exception("Unable to parse data : ".$data);
Expand Down Expand Up @@ -98,6 +99,8 @@ function handleData($client,$data){
$response = "";
$this->log("Call listen hook (v2.0 plugins) with params ".$_['command']." > ".$_['text']." > ".$_['confidence']);
Plugin::callHook('listen',array($_['command'],trim(str_replace($_['command'],'',$_['text'])),$_['confidence']));


break;
case '':
default:
Expand All @@ -109,11 +112,13 @@ function handleData($client,$data){
}

$this->updateClient($client);
}
}catch(Exception $e){
$this->log("ERROR : ".$e->getMessage());
}
//system('php '.realpath(dirname(__FILE__)).'\action.php '.$json['action'],$out);
//$this->send($socket,$out);

}


Expand Down

0 comments on commit eabbaa8

Please sign in to comment.