speechcommands.plugin.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /*
  3. @name Speech commands
  4. @author Valentin CARRUESCO <idleman@idleman.fr>
  5. @link http://blog.idleman.fr
  6. @licence CC by nc sa
  7. @version 1.0.0
  8. @description Permet l'ajout de phrases à yana et la définition des conséquences de ces phrases
  9. */
  10. function speechcommands_vocal_command(&$response,$actionUrl){
  11. global $conf;
  12. require_once('SpeechCommand.class.php');
  13. $command = new SpeechCommand();
  14. $commands = $command->populate();
  15. foreach($commands as $command){
  16. if($command->state !=1) continue;
  17. $response['commands'][] = array(
  18. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' '.$command->command,
  19. 'url'=>$actionUrl.'?action=speechcommands_execute&command='.$command->id,'confidence'=>($command->confidence+$conf->get('VOCAL_SENSITIVITY'))
  20. );
  21. }
  22. }
  23. function speechcommands_action(){
  24. global $_,$conf,$myUser;
  25. switch($_['action']){
  26. case 'plugin_speechcommands_save':
  27. if(!$myUser->can('speech_command','c')) exit('Permissions insufisantes');
  28. require_once('SpeechCommand.class.php');
  29. $command = new SpeechCommand();
  30. $command = !empty($_['id']) ? $command->getById($_['id']): new SpeechCommand();
  31. $command->command= $_['command'];
  32. $command->action = $_['type'];
  33. $command->parameter = $_['parameter'];
  34. $command->confidence = $_['confidence'];
  35. $command->state = $_['state']=='on'?1:0;
  36. $command->save();
  37. header('location: setting.php?section=speechcommands');
  38. break;
  39. case 'plugin_speechcommands_delete' :
  40. if(!$myUser->can('speech_command','d')) exit('Permissions insufisantes');
  41. require_once('SpeechCommand.class.php');
  42. $command = new SpeechCommand();
  43. $command->delete(array('id'=>$_['id']));
  44. header('location: setting.php?section=speechcommands');
  45. break;
  46. case 'speechcommands_execute':
  47. global $_;
  48. require_once('SpeechCommand.class.php');
  49. $command = new SpeechCommand();
  50. $command = $command->getById($_['command']);
  51. set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
  52. // error was suppressed with the @-operator
  53. if (0 === error_reporting()) {
  54. return false;
  55. }
  56. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  57. });
  58. try{
  59. switch($command->action){
  60. case 'talk':
  61. $response = array(
  62. 'responses'=>array(
  63. array('type'=>'talk',
  64. 'sentence'=>$command->parameter
  65. )
  66. )
  67. );
  68. $json = json_encode($response);
  69. echo ($json=='[]'?'{}':$json);
  70. break;
  71. case 'gpio':
  72. list($pin,$value) = explode(',',$command->parameter);
  73. Gpio::write($pin,$value,true);
  74. break;
  75. case 'server_command':
  76. $result = System::commandSilent(html_entity_decode($command->parameter));
  77. $response = array(
  78. 'responses'=>array(
  79. array('type'=>'talk',
  80. 'sentence'=>utf8_encode($result)
  81. )
  82. )
  83. );
  84. $json = json_encode($response);
  85. echo ($json=='[]'?'{}':$json);
  86. break;
  87. case 'client_command':
  88. $response = array(
  89. 'responses'=>array(
  90. array('type'=>'command',
  91. 'program'=>$command->parameter
  92. )
  93. )
  94. );
  95. $json = json_encode($response);
  96. echo ($json=='[]'?'{}':$json);
  97. break;
  98. case 'sound':
  99. $response = array(
  100. 'responses'=>array(
  101. array('type'=>'sound',
  102. 'file'=>$command->parameter
  103. )
  104. )
  105. );
  106. $json = json_encode($response);
  107. echo ($json=='[]'?'{}':$json);
  108. break;
  109. case 'url':
  110. $content = file_get_contents($command->parameter);
  111. $response = array(
  112. 'responses'=>array(
  113. array('type'=>'talk',
  114. 'sentence'=>$content
  115. )
  116. )
  117. );
  118. $json = json_encode($response);
  119. echo ($json=='[]'?'{}':$json);
  120. break;
  121. default:
  122. throw new Exception('Aucun action n\'est spécifiée');
  123. break;
  124. }
  125. }catch(Exception $e){
  126. $response = array(
  127. 'responses'=>array(
  128. array('type'=>'talk',
  129. 'sentence'=>Personality::response('WORRY_EMOTION').', le problème viens de : '.$e->getMessage()
  130. )
  131. )
  132. );
  133. $json = json_encode($response);
  134. echo ($json=='[]'?'{}':$json);
  135. }
  136. break;
  137. }
  138. }
  139. function speechcommands_plugin_preference_menu(){
  140. global $_;
  141. echo '<li '.(@$_['section']=='speechcommands'?'class="active"':'').'><a href="setting.php?section=speechcommands"><i class="fa fa-angle-right"></i> Commandes Vocales</a></li>';
  142. }
  143. function speechcommands_plugin_preference_page(){
  144. global $myUser,$_,$conf;
  145. if((isset($_['section']) && $_['section']=='speechcommands' ) ){
  146. if($myUser!=false){
  147. require_once('SpeechCommand.class.php');
  148. $command = new SpeechCommand();
  149. $commands = $command->populate();
  150. $command->state = 1;
  151. $command->confidence = '0.8';
  152. $command = isset($_['id'])?$command->getById($_['id']):$command;
  153. ?>
  154. <div class="span9 userBloc">
  155. <legend>Commandes</legend>
  156. <form action="action.php?action=plugin_speechcommands_save" method="POST">
  157. <input type="hidden" value="<?php echo $command->id; ?>" name="id"/>
  158. <table class="table table-striped table-bordered">
  159. <tr>
  160. <th>Commande</th>
  161. <th>Confidence</th>
  162. <th>Action</th>
  163. <th>Parametre</th>
  164. <th>Etat</th>
  165. <th></th>
  166. </tr>
  167. <tr class="command">
  168. <td><?php echo $conf->get('VOCAL_ENTITY_NAME').', <input type="text" class="input-medium" value="'.$command->command.'" placeholder="ma phrase ici" name="command">' ?></td>
  169. <td><input type="number" min="0" max="1" step=".01" class="input-mini" name="confidence" value="<?php echo $command->confidence; ?>"/></td>
  170. <td>
  171. <select name="type" class="type input-small">
  172. <option <?php echo $command->action=='gpio'?'selected="selected"':''; ?> value="gpio">Changer un GPIO (sur le serveur)</option>
  173. <option <?php echo $command->action=='server_command'?'selected="selected"':''; ?> value="server_command">Executer une commande (sur le serveur)</option>
  174. <option <?php echo $command->action=='url'?'selected="selected"':''; ?> value="url">Executer une adresse web (sur le serveur)</option>
  175. <option <?php echo $command->action=='client_command'?'selected="selected"':''; ?> value="client_command">Executer une commande (sur le client)</option>
  176. <option <?php echo $command->action=='talk'?'selected="selected"':''; ?> value="talk">Parler (sur le client)</option>
  177. <option <?php echo $command->action=='sound'?'selected="selected"':''; ?> value="sound">Son (sur le client)</option>
  178. </select>
  179. </td>
  180. <td><input type="text" name="parameter" class="input-medium" value="<?php echo $command->parameter; ?>"/></td>
  181. <td><input type="checkbox" name="state" <?php echo $command->state=='1'?'checked=""checked""':''; ?> /></td>
  182. <td><input class="btn" type="submit" value="Enregistrer"/></td>
  183. </tr>
  184. <?php foreach($commands as $command){ ?>
  185. <tr class="command">
  186. <td><?php echo $conf->get('VOCAL_ENTITY_NAME').', '.$command->command; ?></td>
  187. <td><?php echo $command->confidence; ?></td>
  188. <td><?php echo $command->action; ?></td>
  189. <td><?php echo $command->parameter; ?></td>
  190. <td><?php echo $command->state=='1'?'Actif':'Inactif'; ?></td>
  191. <td>
  192. <a class="btn" title="modifier" href="setting.php?section=speechcommands&id=<?php echo $command->id; ?>"><i class="fa fa-edit"></i></a>
  193. <a class="btn" title="supprimer" href="action.php?action=plugin_speechcommands_delete&id=<?php echo $command->id; ?>"><i class="fa fa-times"></i></a>
  194. </td>
  195. </tr>
  196. <?php } ?>
  197. </table>
  198. </form>
  199. <h2>
  200. <i class="fa fa-book"></i> Explications</h2>
  201. <p>Ce plugin permet d'ajouter des actions en fonction de certaines phrases prononcées que vous pouvez choisir et ajouter à l\'infini.</p>
  202. <p>Notez bien que ce plugin est optimisé pour yana android et qu'en fonction de l'action souhaitée, celle ci s'execute sur le serveur ou sur le client (
  203. Ceci est spéficié dans l'action à choisir).
  204. </p>
  205. <ul>
  206. <li>Le champ <strong>Commande</strong> représente la phrase a enoncer pour lancer l'action spécifiée</li>
  207. <li>Le champ <strong>Confidence</strong> représente la sensibilité de reconnaissance de la phrase (chiffre entre 0 et 1) plus cette valeur est basse, plus la phrase sera reconnue facilement</li>
  208. <li>Le champ <strong>Action</strong> représente le type d'action a effectuer (lancer une url, parler, etc..)</li>
  209. <li>Le champ <strong>Parametre</strong> représente la valeur de cette action (ex : si l'action est "parler" parametre sera la phrase qui doit être dite). <br><strong>NB :</strong> Pour l'action "gpio" le paramêtre doit être au format : "n°gpio,etat" par exemple pour mettre le gpio 2 à 1 : "2,1". </li>
  210. <li>Le champ <strong>Etat</strong> active ou desactive cette commande, vous pouvez ainsi la désactiver temporairement sans la supprimer définitivement</li>
  211. </ul>
  212. <h4>Examples</h4>
  213. <img src="<?php echo Plugin::path(); ?>/img/sample.png">
  214. </div>
  215. <?php }else{ ?>
  216. <div id="main" class="wrapper clearfix">
  217. <article>
  218. <h3>Vous devez être connecté</h3>
  219. </article>
  220. </div>
  221. <?php
  222. }
  223. }
  224. }
  225. Plugin::addHook("setting_menu", "speechcommands_plugin_preference_menu");
  226. Plugin::addHook("setting_bloc", "speechcommands_plugin_preference_page");
  227. Plugin::addHook("action_post_case", "speechcommands_action");
  228. Plugin::addHook("vocal_command", "speechcommands_vocal_command");
  229. ?>