radioRelay.plugin.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. /*
  3. @name Radio Relay
  4. @author Valentin CARRUESCO <idleman@idleman.fr>
  5. @link Http://blog.idleman.fr
  6. @licence Cc -by-nc-sa
  7. @version 1.1
  8. @description Plugin de gestion des relais radio 433mhz au protocole home easy (compatible chacon et prises "maison" du blog.idleman.fr)
  9. */
  10. //On appelle les entités de base de données
  11. require_once(dirname(__FILE__).'/RadioRelay.class.php');
  12. //Cette fonction ajoute une commande vocale
  13. function radiorelay_plugin_vocal_command(&$response,$actionUrl){
  14. global $conf;
  15. $radioRelayManager = new RadioRelay();
  16. $radioRelays = $radioRelayManager->populate();
  17. foreach($radioRelays as $radioRelay){
  18. if(!empty($radioRelay->onCommand))
  19. $response['commands'][] = array('command'=>$conf->get('VOCAL_ENTITY_NAME').', '.$radioRelay->onCommand,'url'=>$actionUrl.'?action=radioRelay_vocal_change_state&engine='.$radioRelay->id.'&state=1','confidence'=>('0.90'+$conf->get('VOCAL_SENSITIVITY')));
  20. if(!empty($radioRelay->offCommand))
  21. $response['commands'][] = array('command'=>$conf->get('VOCAL_ENTITY_NAME').', '.$radioRelay->offCommand,'url'=>$actionUrl.'?action=radioRelay_vocal_change_state&engine='.$radioRelay->id.'&state=0','confidence'=>('0.90'+$conf->get('VOCAL_SENSITIVITY')));
  22. }
  23. }
  24. //cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html
  25. function radiorelay_plugin_action(){
  26. global $_,$conf,$myUser;
  27. //Action de réponse à la commande vocale "Yana, commande de test"
  28. switch($_['action']){
  29. case 'radioRelay_save_radioRelay':
  30. Action::write(
  31. function($_,&$response){
  32. $radioRelayManager = new RadioRelay();
  33. if(empty($_['nameRadioRelay'])) throw new Exception("Le nom est obligatoire");
  34. if(!is_numeric($_['radioCodeRadioRelay'])) throw new Exception("Le code radio est obligatoire et doit être numerique");
  35. $radioRelay = !empty($_['id']) ? $radioRelayManager->getById($_['id']): new RadioRelay();
  36. $radioRelay->name = $_['nameRadioRelay'];
  37. $radioRelay->description = $_['descriptionRadioRelay'];
  38. $radioRelay->room = $_['roomRadioRelay'];
  39. $radioRelay->pulse = $_['pulseRadioRelay'];
  40. $radioRelay->onCommand = $_['onRadioRelay'];
  41. $radioRelay->offCommand = $_['offRadioRelay'];
  42. $radioRelay->icon = $_['iconRadioRelay'];
  43. $radioRelay->radiocode = $_['radioCodeRadioRelay'];
  44. $radioRelay->save();
  45. $response['message'] = 'Relais enregistré avec succès';
  46. },
  47. array('plugin_radiorelay'=>'c')
  48. );
  49. break;
  50. case 'radioRelay_delete_radioRelay':
  51. Action::write(
  52. function($_,$response){
  53. $radioRelayManager = new RadioRelay();
  54. $radioRelayManager->delete(array('id'=>$_['id']));
  55. },
  56. array('plugin_radiorelay'=>'d')
  57. );
  58. break;
  59. case 'radioRelay_plugin_setting':
  60. Action::write(
  61. function($_,&$response){
  62. global $conf;
  63. $conf->put('plugin_radioRelay_emitter_pin',$_['emiterPin']);
  64. $conf->put('plugin_radioRelay_emitter_code',$_['emiterCode']);
  65. $response['message'] = 'Configuration enregistrée';
  66. },
  67. array('plugin_radiorelay'=>'c')
  68. );
  69. break;
  70. case 'radioRelay_manual_change_state':
  71. Action::write(
  72. function($_,&$response){
  73. radiorelay_plugin_change_state($_['engine'],$_['state']);
  74. },
  75. array('plugin_radiorelay'=>'c')
  76. );
  77. break;
  78. case 'radioRelay_vocal_change_state':
  79. global $_,$myUser;
  80. try{
  81. $response['responses'][0]['type'] = 'talk';
  82. if(!$myUser->can('plugin_radiorelay','u')) throw new Exception ('Je ne vous connais pas, ou alors vous n\'avez pas le droit, je refuse de faire ça!');
  83. radiorelay_plugin_change_state($_['engine'],$_['state']);
  84. $response['responses'][0]['sentence'] = Personality::response('ORDER_CONFIRMATION');
  85. }catch(Exception $e){
  86. $response['responses'][0]['sentence'] = Personality::response('WORRY_EMOTION').'! '.$e->getMessage();
  87. }
  88. $json = json_encode($response);
  89. echo ($json=='[]'?'{}':$json);
  90. break;
  91. case 'radioRelay_plugin_setting':
  92. Action::write(
  93. function($_,&$response){
  94. global $conf;
  95. $conf->put('plugin_radioRelay_emitter_pin',$_['emiterPin']);
  96. $conf->put('plugin_radioRelay_emitter_code',$_['emiterCode']);
  97. $response['message'] = 'Configuration modifiée avec succès';
  98. },
  99. array('plugin_radiorelay'=>'u')
  100. );
  101. break;
  102. case 'radioRelay_load_widget':
  103. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  104. Action::write(
  105. function($_,&$response){
  106. $widget = new Widget();
  107. $widget = $widget->getById($_['id']);
  108. $data = $widget->data();
  109. $content = '';
  110. if(empty($data['relay'])){
  111. $content = 'Choisissez un relais en cliquant sur l \'icone <i class="fa fa-wrench"></i> de la barre du widget';
  112. }else{
  113. $radioPermission = fileperms(Plugin::path().'radioEmission')!='36333';
  114. if(($radioPermission !='36333') && ($radioPermission !='35913'))
  115. $content .= '<div style="margin:0px;" class="flatBloc pink-color">Attention, les droits vers le fichier <br/> radioEmission sont mal réglés.<br/> Référez vous à <span style="cursor:pointer;text-decoration:underline;" onclick="window.location.href=\'https://github.com/ldleman/yana-server#installation\';">la doc</span> pour les régler</div>';
  116. $relay = new RadioRelay();
  117. $relay = $relay->getById($data['relay']);
  118. $response['title'] = $relay->name;
  119. $content .= '
  120. <!-- CSS -->
  121. <style>
  122. .radiorelay_relay_pane {
  123. background: none repeat scroll 0 0 #50597b;
  124. list-style-type: none;
  125. margin: 0;
  126. cursor:default;
  127. width: 100%;
  128. }
  129. .radiorelay_relay_pane li {
  130. background: none repeat scroll 0 0 #50597b;
  131. display: inline-block;
  132. margin: 0 1px 0 0;
  133. padding: 10px;
  134. cursor:default;
  135. vertical-align: top;
  136. }
  137. .radiorelay_relay_pane li h2 {
  138. color: #ffffff;
  139. font-size: 16px;
  140. margin: 0 0 5px;
  141. padding: 0;
  142. cursor:default;
  143. }
  144. .radiorelay_relay_pane li h1 {
  145. color: #B6BED9;
  146. font-size: 14px;
  147. margin: 0 0 10px;
  148. padding: 0;
  149. cursor:default;
  150. }
  151. .radiorelay_relay_pane li.radiorelay-case{
  152. background-color: #373f59;
  153. width: 55px;
  154. cursor:pointer;
  155. }
  156. .radiorelay-case i{
  157. color:#8b95b8;
  158. font-size:50px;
  159. transition: all 0.2s ease-in-out;
  160. }
  161. .radiorelay-case.active i{
  162. color:#ffffff;
  163. text-shadow: 0 0 10px #ffffff;
  164. }
  165. .radiorelay-case.active i.fa-lightbulb-o{
  166. color:#FFED00;
  167. text-shadow: 0 0 10px #ffdc00;
  168. }
  169. .radiorelay-case.active i.fa-power-off{
  170. color:#BDFF00;
  171. text-shadow: 0 0 10px #4fff00;
  172. }
  173. .radiorelay-case.active i.fa-flash{
  174. color:#FFFFFF;
  175. text-shadow: 0 0 10px #00FFD9;
  176. }
  177. .radiorelay-case.active i.fa-gears{
  178. color:#FFFFFF;
  179. text-shadow: 0 0 10px #FF00E4;
  180. }
  181. </style>
  182. <!-- CSS -->
  183. <ul class="radiorelay_relay_pane">
  184. <li class="radiorelay-case '.($relay->state?'active':'').'" onclick="plugin_radiorelay_change(this,'.$relay->id.');" style="text-align:center;">
  185. <i title="On/Off" class="'.$relay->icon.'"></i>
  186. </li>
  187. <li>
  188. <h2>'.$relay->description.'</h2>
  189. <h1>CODE '.$relay->radiocode.($relay->pulse!=0?' - Pulse '.$relay->pulse.'µs':'').'</h1>
  190. </li>
  191. </ul>
  192. <!-- JS -->
  193. <script type="text/javascript">
  194. function plugin_radiorelay_change(element,id){
  195. var state = $(element).hasClass(\'active\') ? 0 : 1 ;
  196. $.action(
  197. {
  198. action : \'radioRelay_manual_change_state\',
  199. engine: id,
  200. state: state
  201. },
  202. function(response){
  203. $(element).toggleClass("active");
  204. }
  205. );
  206. }
  207. </script>
  208. ';
  209. }
  210. $response['content'] = $content;
  211. }
  212. );
  213. break;
  214. case 'radioRelay_edit_widget':
  215. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  216. $widget = new Widget();
  217. $widget = $widget->getById($_['id']);
  218. $data = $widget->data();
  219. $relayManager = new RadioRelay();
  220. $relays = $relayManager->populate();
  221. $content = '<h3>Relais ciblé</h3>';
  222. if(count($relays) == 0){
  223. $content = 'Aucun relais existant dans yana, <a href="setting.php?section=radioRelay">Créer un relais ?</a>';
  224. }else{
  225. $content .= '<select id="relay">';
  226. $content .= '<option value="">-</option>';
  227. foreach ($relays as $relay) {
  228. $content .= '<option value="'.$relay->id.'">'.$relay->name.'</option>';
  229. }
  230. $content .= '</select>';
  231. }
  232. echo $content;
  233. break;
  234. case 'radioRelay_save_widget':
  235. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  236. $widget = new Widget();
  237. $widget = $widget->getById($_['id']);
  238. $data = $widget->data();
  239. $data['relay'] = $_['relay'];
  240. $widget->data($data);
  241. $widget->save();
  242. echo $content;
  243. break;
  244. }
  245. }
  246. function radiorelay_plugin_change_state($engine,$state){
  247. global $conf;
  248. $radioRelay = new RadioRelay();
  249. $radioRelay = $radioRelay->getById($engine);
  250. $cmd = dirname(__FILE__).'/radioEmission '.$conf->get('plugin_radioRelay_emitter_pin').' '.$conf->get('plugin_radioRelay_emitter_code').' '.$radioRelay->radiocode.' ';
  251. $cmd .= $radioRelay->pulse ==0 ? ($state==1?'on':'off') : 'pulse '.$radioRelay->pulse;
  252. $radioRelay->state = $state;
  253. Functions::log('Launch system command : '.$cmd);
  254. system($cmd,$out);
  255. $radioRelay->save();
  256. }
  257. function radioRelay_plugin_setting_page(){
  258. global $_,$myUser,$conf;
  259. if(isset($_['section']) && $_['section']=='radioRelay' ){
  260. if(!$myUser) throw new Exception('Vous devez être connecté pour effectuer cette action');
  261. $radioRelayManager = new RadioRelay();
  262. $radioRelays = $radioRelayManager->populate();
  263. $roomManager = new Room();
  264. $rooms = $roomManager->loadAll(array('state'=>'0'));
  265. $selected = new RadioRelay();
  266. $selected->pulse = 0;
  267. $selected->icon = 'fa fa-flash';
  268. //Si on est en mode modification
  269. if (isset($_['id']))
  270. $selected = $radioRelayManager->getById($_['id']);
  271. $icons = array(
  272. 'fa fa-lightbulb-o',
  273. 'fa fa-power-off',
  274. 'fa fa-flash',
  275. 'fa fa-gears',
  276. 'fa fa-align-justify',
  277. 'fa fa-adjust',
  278. 'fa fa-arrow-circle-o-right',
  279. 'fa fa-desktop',
  280. 'fa fa-music',
  281. 'fa fa-bell-o',
  282. 'fa fa-beer',
  283. 'fa fa-bullseye',
  284. 'fa fa-automobile',
  285. 'fa fa-book',
  286. 'fa fa-bomb',
  287. 'fa fa-clock-o',
  288. 'fa fa-cutlery',
  289. 'fa fa-microphone',
  290. 'fa fa-tint'
  291. );
  292. ?>
  293. <div class="span9 userBloc">
  294. <h1>Relais</h1>
  295. <p>Gestion des relais radios</p>
  296. <fieldset>
  297. <legend>Ajouter/Modifier un relais radio</legend>
  298. <div class="left">
  299. <label for="nameRadioRelay">Nom</label>
  300. <input type="hidden" id="id" value="<?php echo $selected->id; ?>">
  301. <input type="text" id="nameRadioRelay" value="<?php echo $selected->name; ?>" placeholder="Lumiere Canapé…"/>
  302. <label for="descriptionRadioRelay">Description</label>
  303. <input type="text" value="<?php echo $selected->description; ?>" id="descriptionRadioRelay" placeholder="Relais sous le canapé…" />
  304. <label for="iconRadioRelay">Icone</label>
  305. <input type="hidden" value="<?php echo $selected->icon; ?>" id="iconRadioRelay" />
  306. <div>
  307. <div style='margin:5px;'>
  308. <?php foreach($icons as $i=>$icon){
  309. if($i%6==0) echo '</div><div style="margin:5px;">';
  310. ?>
  311. <i style="width:25px;" onclick="plugin_radiorelay_set_icon(this,'<?php echo $icon; ?>');" class="<?php echo $icon; ?> btn <?php echo $selected->icon==$icon?'btn-success':''; ?>"></i>
  312. <?php } ?>
  313. </div>
  314. </div>
  315. <label for="radioCodeRadioRelay">Code radio</label>
  316. <input type="text" value="<?php echo $selected->radiocode; ?>" name="radioCodeRadioRelay" id="radioCodeRadioRelay" placeholder="1234,1111,0022..." />
  317. <label for="onRadioRelay">Commande vocale "ON" associée</label>
  318. <?php echo $conf->get('VOCAL_ENTITY_NAME') ?>, <input type="text" id="onRadioRelay" value="<?php echo $selected->onCommand; ?>" placeholder="Allume la lumière, Ouvre le volet…"/>
  319. <label for="offRadioRelay">Commande vocale "OFF" associée</label>
  320. <?php echo $conf->get('VOCAL_ENTITY_NAME') ?>, <input type="text" id="offRadioRelay" value="<?php echo $selected->offCommand; ?>" placeholder="Eteinds la lumière, Ferme le volet…"/>
  321. <label for="roomRadioRelay">Pièce de la maison</label>
  322. <select id="roomRadioRelay">
  323. <?php foreach($rooms as $room){ ?>
  324. <option <?php if ($selected->room == $room->getId()){echo "selected";} ?> value="<?php echo $room->getId(); ?>"><?php echo $room->getName(); ?></option>
  325. <?php } ?>
  326. </select>
  327. <label for="pinRadioRelay">Mode impulsion (micro secondes)</label>
  328. <input type="number" value="<?php echo $selected->pulse; ?>" id="pulseRadioRelay" placeholder="0" />
  329. <small>laisser à zéro pour désactiver le mode impulsion</small>
  330. </div>
  331. <div class="clear"></div>
  332. <br/><button onclick="plugin_radiorelay_save(this)" class="btn">Enregistrer</button>
  333. </fieldset>
  334. <br/>
  335. <fieldset>
  336. <legend>Consulter les relais radios existants</legend>
  337. <table class="table table-striped table-bordered table-hover">
  338. <thead>
  339. <tr>
  340. <th>Nom</th>
  341. <th>Description</th>
  342. <th>Code</th>
  343. <th>Pièce</th>
  344. <th colspan="2">Impulsion</th>
  345. </tr>
  346. </thead>
  347. <?php foreach($radioRelays as $radioRelay){
  348. $room = $roomManager->load(array('id'=>$radioRelay->room));
  349. ?>
  350. <tr>
  351. <td><?php echo $radioRelay->name; ?></td>
  352. <td><?php echo $radioRelay->description; ?></td>
  353. <td><?php echo $radioRelay->radiocode; ?></td>
  354. <td><?php echo $room->getName(); ?></td>
  355. <td><?php echo $radioRelay->pulse; ?></td>
  356. <td>
  357. <a class="btn" href="setting.php?section=radioRelay&id=<?php echo $radioRelay->id; ?>"><i class="fa fa-pencil"></i></a>
  358. <div class="btn" onclick="plugin_radiorelay_delete(<?php echo $radioRelay->id; ?>,this);"><i class="fa fa-times"></i></div>
  359. </td>
  360. </td>
  361. </tr>
  362. <?php } ?>
  363. </table>
  364. </fieldset>
  365. </div>
  366. <?php
  367. }
  368. }
  369. function radioRelay_plugin_setting_menu(){
  370. global $_;
  371. echo '<li '.(isset($_['section']) && $_['section']=='radioRelay'?'class="active"':'').'><a href="setting.php?section=radioRelay"><i class="fa fa-angle-right"></i> Relais radio</a></li>';
  372. }
  373. function radioRelay_plugin_widget(&$widgets){
  374. $widgets[] = array(
  375. 'uid' => 'dash_radiorelay',
  376. 'icon' => 'fa fa-bullseye',
  377. 'label' => 'Relais Radio',
  378. 'background' => '#50597b',
  379. 'color' => '#fffffff',
  380. 'onLoad' => 'action.php?action=radioRelay_load_widget',
  381. 'onEdit' => 'action.php?action=radioRelay_edit_widget',
  382. 'onSave' => 'action.php?action=radioRelay_save_widget',
  383. );
  384. }
  385. function radioRelay_plugin_preference_menu(){
  386. global $_;
  387. echo '<li '.(@$_['block']=='radioRelay'?'class="active"':'').'><a href="setting.php?section=preference&block=radioRelay"><i class="fa fa-angle-right"></i> Radio Relais</a></li>';
  388. }
  389. function radioRelay_plugin_preference_page(){
  390. global $myUser,$_,$conf;
  391. if((isset($_['section']) && $_['section']=='preference' && @$_['block']=='radioRelay' ) ){
  392. if($myUser!=false){
  393. ?>
  394. <div class="span9 userBloc">
  395. <div>
  396. <label>Pin du raspberry PI branché à l'émetteur radio: </label>
  397. <input type="text" class="input-large" id="emiterPin" value="<?php echo $conf->get('plugin_radioRelay_emitter_pin');?>" placeholder="Pin wiring PI...">
  398. <label>Code de la télécommande pris par le raspberry pi: </label>
  399. <input type="text" class="input-large" id="emiterCode" value="<?php echo $conf->get('plugin_radioRelay_emitter_code');?>" placeholder="par exemple 8217034...">
  400. <button onclick="plugin_radiorelay_save_settings(this);" class="btn">Enregistrer</button>
  401. </div>
  402. </div>
  403. <?php }else{ ?>
  404. <div id="main" class="wrapper clearfix">
  405. <article>
  406. <h3>Vous devez être connecté</h3>
  407. </article>
  408. </div>
  409. <?php
  410. }
  411. }
  412. }
  413. Plugin::addCss("/css/main.css");
  414. Plugin::addJs("/js/main.js",true);
  415. //Lie radioRelay_plugin_preference_menu au menu de réglages
  416. Plugin::addHook("preference_menu", "radioRelay_plugin_preference_menu");
  417. //Lie radioRelay_plugin_preference_page a la page de réglages
  418. Plugin::addHook("preference_content", "radioRelay_plugin_preference_page");
  419. //Lie radioRelay_plugin_setting_page a la zone réglages
  420. Plugin::addHook("setting_bloc", "radioRelay_plugin_setting_page");
  421. //Lie radioRelay_plugin_setting_menu au menu de réglages
  422. Plugin::addHook("setting_menu", "radioRelay_plugin_setting_menu");
  423. //Lie radiorelay_plugin_action a la page d'action qui perme d'effecuer des actionx ajax ou ne demdnant pas de retour visuels
  424. Plugin::addHook("action_post_case", "radiorelay_plugin_action");
  425. //Lie radiorelay_plugin_vocal_command a la gestion de commandes vocales proposées par yana
  426. Plugin::addHook("vocal_command", "radiorelay_plugin_vocal_command");
  427. //Lie radioRelay_plugin_widget aux widgets de la dashboard
  428. Plugin::addHook("widgets", "radioRelay_plugin_widget");
  429. ?>