wireRelay.plugin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. /*
  3. @name WireRelay
  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 Prise relais filaire
  9. */
  10. //On appelle les entités de base de données
  11. include('WireRelay.class.php');
  12. //Cette fonction ajoute une commande vocale
  13. function wirerelay_plugin_vocal_command(&$response,$actionUrl){
  14. global $conf;
  15. $wireRelayManager = new WireRelay();
  16. $wireRelays = $wireRelayManager->populate();
  17. foreach($wireRelays as $wireRelay){
  18. if(!empty($wireRelay->oncommand))
  19. $response['commands'][] = array('command'=>$conf->get('VOCAL_ENTITY_NAME').', '.$wireRelay->oncommand,'url'=>$actionUrl.'?action=wireRelay_vocal_change_state&engine='.$wireRelay->id.'&state=1','confidence'=>('0.90'+$conf->get('VOCAL_SENSITIVITY')));
  20. if(!empty($wireRelay->offcommand))
  21. $response['commands'][] = array('command'=>$conf->get('VOCAL_ENTITY_NAME').', '.$wireRelay->offcommand,'url'=>$actionUrl.'?action=wireRelay_vocal_change_state&engine='.$wireRelay->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 wirerelay_plugin_action(){
  26. global $_,$conf,$myUser;
  27. //Action de réponse à la commande vocale "Yana, commande de test"
  28. switch($_['action']){
  29. case 'wireRelay_save_wireRelay':
  30. Action::write(
  31. function($_,&$response){
  32. $wireRelayManager = new WireRelay();
  33. if(empty($_['nameWireRelay'])) throw new Exception("Le nom est obligatoire");
  34. if(!is_numeric($_['pinWireRelay'])) throw new Exception("Le PIN GPIO est obligatoire et doit être numerique");
  35. $wireRelay = !empty($_['id']) ? $wireRelayManager->getById($_['id']): new WireRelay();
  36. $wireRelay->name = $_['nameWireRelay'];
  37. $wireRelay->description = $_['descriptionWireRelay'];
  38. $wireRelay->pin = $_['pinWireRelay'];
  39. $wireRelay->room = $_['roomWireRelay'];
  40. $wireRelay->pulse = $_['pulseWireRelay'];
  41. $wireRelay->oncommand = $_['onWireRelay'];
  42. $wireRelay->offcommand = $_['offWireRelay'];
  43. $wireRelay->reverse = $_['reverseWireRelay'];
  44. $wireRelay->icon = $_['iconWireRelay'];
  45. $wireRelay->save();
  46. //Reference device for other plugins
  47. $device = new Device();
  48. $device->label = $wireRelay->name;
  49. $device->plugin = 'wireRelay';
  50. $device->type = Device::ACTUATOR;
  51. $device->location = $wireRelay->room;
  52. $device->icon = $wireRelay->icon;
  53. $device->setValue('value',0);
  54. $device->state = 1;
  55. $device->uid = $wireRelay->id;
  56. $device->save();
  57. $response['message'] = 'Relais enregistré avec succès';
  58. },
  59. array('plugin_wirerelay'=>'c')
  60. );
  61. break;
  62. case 'wireRelay_delete_wireRelay':
  63. Action::write(
  64. function($_,$response){
  65. $wireRelayManager = new WireRelay();
  66. $wireRelayManager->delete(array('id'=>$_['id']));
  67. },
  68. array('plugin_wirerelay'=>'d')
  69. );
  70. break;
  71. case 'wireRelay_plugin_setting':
  72. Action::write(
  73. function($_,$response){
  74. $conf->put('plugin_wireRelay_emitter_pin',$_['emiterPin']);
  75. $conf->put('plugin_wireRelay_emitter_code',$_['emiterCode']);
  76. },
  77. array('plugin_wirerelay'=>'c')
  78. );
  79. break;
  80. case 'wireRelay_manual_change_state':
  81. Action::write(
  82. function($_,&$response){
  83. wirerelay_plugin_change_state($_['engine'],$_['state']);
  84. },
  85. array('plugin_wirerelay'=>'c')
  86. );
  87. break;
  88. case 'wireRelay_vocal_change_state':
  89. global $_,$myUser;
  90. try{
  91. $response['responses'][0]['type'] = 'talk';
  92. if(!$myUser->can('plugin_wirerelay','u')) throw new Exception ('Je ne vous connais pas, ou alors vous n\'avez pas le droit, je refuse de faire ça!');
  93. wirerelay_plugin_change_state($_['engine'],$_['state']);
  94. $response['responses'][0]['sentence'] = Personality::response('ORDER_CONFIRMATION');
  95. }catch(Exception $e){
  96. $response['responses'][0]['sentence'] = Personality::response('WORRY_EMOTION').'! '.$e->getMessage();
  97. }
  98. $json = json_encode($response);
  99. echo ($json=='[]'?'{}':$json);
  100. break;
  101. case 'wireRelay_load_widget':
  102. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  103. Action::write(
  104. function($_,&$response){
  105. $widget = new Widget();
  106. $widget = $widget->getById($_['id']);
  107. if(!is_object($widget)) return;
  108. $data = $widget->data();
  109. if(empty($data['relay'])){
  110. $content = 'Choisissez un relais en cliquant sur l \'icone <i class="fa fa-wrench"></i> de la barre du widget';
  111. }else{
  112. $relay = new WireRelay();
  113. $relay = $relay->getById($data['relay']);
  114. $response['title'] = $relay->name;
  115. $content = '
  116. <!-- CSS -->
  117. <style>
  118. .relay_pane {
  119. background: none repeat scroll 0 0 #50597b;
  120. list-style-type: none;
  121. margin: 0;
  122. cursor:default;
  123. width: 100%;
  124. }
  125. .relay_pane li {
  126. background: none repeat scroll 0 0 #50597b;
  127. display: inline-block;
  128. margin: 0 1px 0 0;
  129. padding: 10px;
  130. cursor:default;
  131. vertical-align: top;
  132. }
  133. .relay_pane li h2 {
  134. color: #ffffff;
  135. font-size: 16px;
  136. margin: 0 0 5px;
  137. padding: 0;
  138. cursor:default;
  139. }
  140. .relay_pane li h1 {
  141. color: #B6BED9;
  142. font-size: 14px;
  143. margin: 0 0 10px;
  144. padding: 0;
  145. cursor:default;
  146. }
  147. .relay_pane li.wirerelay-case{
  148. background-color: #373f59;
  149. cursor:pointer;
  150. width: 55px;
  151. }
  152. .wirerelay-case i{
  153. color:#8b95b8;
  154. font-size:50px;
  155. transition: all 0.2s ease-in-out;
  156. }
  157. .wirerelay-case.active i{
  158. color:#ffffff;
  159. text-shadow: 0 0 10px #ffffff;
  160. }
  161. .wirerelay-case.active i.fa-lightbulb-o{
  162. color:#FFED00;
  163. text-shadow: 0 0 10px #ffdc00;
  164. }
  165. .wirerelay-case.active i.fa-power-off{
  166. color:#BDFF00;
  167. text-shadow: 0 0 10px #4fff00;
  168. }
  169. .wirerelay-case.active i.fa-flash{
  170. color:#FFFFFF;
  171. text-shadow: 0 0 10px #00FFD9;
  172. }
  173. .wirerelay-case.active i.fa-gears{
  174. color:#FFFFFF;
  175. text-shadow: 0 0 10px #FF00E4;
  176. }
  177. </style>
  178. <!-- CSS -->
  179. <ul class="relay_pane">
  180. <li class="wirerelay-case '.(Gpio::read($relay->pin)?'active':'').'" data-id="'.$relay->id.'" onclick="plugin_wirerelay_change(this);" style="text-align:center;">
  181. <i title="On/Off" class="'.$relay->icon.'"></i>
  182. </li>
  183. <li>
  184. <h2>'.$relay->description.'</h2>
  185. <h1>PIN '.$relay->pin.($relay->pulse!=0?' - Pulse '.$relay->pulse.'µs':'').'</h1>
  186. </li>
  187. </ul>
  188. <!-- JS -->
  189. <script type="text/javascript">
  190. function plugin_wirerelay_change(element){
  191. var state = $(element).hasClass(\'active\') ? 0 : 1 ;
  192. $.action(
  193. {
  194. action : \'wireRelay_manual_change_state\',
  195. engine: $(element).data("id"),
  196. state: state
  197. },
  198. function(response){
  199. $(element).toggleClass("active");
  200. }
  201. );
  202. }
  203. </script>
  204. ';
  205. }
  206. $response['content'] = $content;
  207. }
  208. );
  209. break;
  210. case 'wireRelay_edit_widget':
  211. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  212. $widget = new Widget();
  213. $widget = $widget->getById($_['id']);
  214. $data = $widget->data();
  215. $relayManager = new WireRelay();
  216. $relays = $relayManager->populate();
  217. $content = '<h3>Relais ciblé</h3>';
  218. if(count($relays) == 0){
  219. $content = 'Aucun relais existant dans yana, <a href="setting.php?section=wireRelay">Créer un relais ?</a>';
  220. }else{
  221. $content .= '<select id="relay">';
  222. $content .= '<option value="">-</option>';
  223. foreach ($relays as $relay) {
  224. $content .= '<option value="'.$relay->id.'">'.$relay->name.'</option>';
  225. }
  226. $content .= '</select>';
  227. }
  228. echo $content;
  229. break;
  230. case 'wireRelay_save_widget':
  231. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  232. $widget = new Widget();
  233. $widget = $widget->getById($_['id']);
  234. $data = $widget->data();
  235. $data['relay'] = $_['relay'];
  236. $widget->data($data);
  237. $widget->save();
  238. echo $content;
  239. break;
  240. }
  241. }
  242. function wirerelay_plugin_change_state($engine,$state){
  243. $wireRelay = new WireRelay();
  244. $wireRelay = $wireRelay->getById($engine);
  245. Gpio::mode($wireRelay->pin,'out');
  246. //Permet l'inversion des commandes en cas de relais inversé
  247. $state = $state - $wireRelay->reverse;
  248. if($wireRelay->pulse==0){
  249. Gpio::write($wireRelay->pin,$state);
  250. }else{
  251. Gpio::pulse($wireRelay->pin,$wireRelay->pulse,1);
  252. }
  253. //Reference device state change for other plugins
  254. $device = new Device();
  255. $device = $device->load(array('plugin'=>'wireRelay','uid'=>$wireRelay->id));
  256. if(is_object($device)){
  257. $device->setValue('value',$state);
  258. $device->save();
  259. }
  260. }
  261. function wireRelay_plugin_setting_page(){
  262. global $_,$myUser,$conf;
  263. if(isset($_['section']) && $_['section']=='wireRelay' ){
  264. if(!$myUser) throw new Exception('Vous devez être connecté pour effectuer cette action');
  265. $wireRelayManager = new WireRelay();
  266. $wireRelays = $wireRelayManager->populate();
  267. $roomManager = new Room();
  268. $rooms = $roomManager->loadAll(array('state'=>'0'));
  269. $selected = new WireRelay();
  270. $selected->pulse = 0;
  271. $selected->icon = 'fa fa-flash';
  272. //Si on est en mode modification
  273. if (isset($_['id']))
  274. $selected = $wireRelayManager->getById($_['id']);
  275. $icons = array(
  276. 'fa fa-lightbulb-o',
  277. 'fa fa-power-off',
  278. 'fa fa-flash',
  279. 'fa fa-gears',
  280. 'fa fa-align-justify',
  281. 'fa fa-adjust',
  282. 'fa fa-arrow-circle-o-right',
  283. 'fa fa-desktop',
  284. 'fa fa-music',
  285. 'fa fa-bell-o',
  286. 'fa fa-beer',
  287. 'fa fa-bullseye',
  288. 'fa fa-automobile',
  289. 'fa fa-book',
  290. 'fa fa-bomb',
  291. 'fa fa-clock-o',
  292. 'fa fa-cutlery',
  293. 'fa fa-microphone',
  294. 'fa fa-tint'
  295. );
  296. ?>
  297. <div class="span9 userBloc">
  298. <h1>Relais</h1>
  299. <p>Gestion des relais filaires</p>
  300. <fieldset>
  301. <legend>Ajouter/Modifier un relais filaire</legend>
  302. <div class="left">
  303. <label for="nameWireRelay">Nom</label>
  304. <input type="hidden" id="id" value="<?php echo $selected->id; ?>">
  305. <input type="text" id="nameWireRelay" value="<?php echo $selected->name; ?>" placeholder="Lumiere Canapé…"/>
  306. <label for="descriptionWireRelay">Description</label>
  307. <input type="text" value="<?php echo $selected->description; ?>" id="descriptionWireRelay" placeholder="Relais sous le canapé…" />
  308. <label for="iconWireRelay">Icone</label>
  309. <input type="hidden" value="<?php echo $selected->icon; ?>" id="iconWireRelay" />
  310. <div>
  311. <div style='margin:5px;'>
  312. <?php foreach($icons as $i=>$icon){
  313. if($i%6==0) echo '</div><div style="margin:5px;">';
  314. ?>
  315. <i style="width:25px;" onclick="plugin_wirerelay_set_icon(this,'<?php echo $icon; ?>');" class="<?php echo $icon; ?> btn <?php echo $selected->icon==$icon?'btn-success':''; ?>"></i>
  316. <?php } ?>
  317. </div>
  318. </div>
  319. <label for="onWireRelay">Commande vocale "ON" associée</label>
  320. <?php echo $conf->get('VOCAL_ENTITY_NAME') ?>, <input type="text" id="onWireRelay" value="<?php echo $selected->oncommand; ?>" placeholder="Allume la lumière, Ouvre le volet…"/>
  321. <label for="offWireRelay">Commande vocale "OFF" associée</label>
  322. <?php echo $conf->get('VOCAL_ENTITY_NAME') ?>, <input type="text" id="offWireRelay" value="<?php echo $selected->offcommand; ?>" placeholder="Eteinds la lumière, Ferme le volet…"/>
  323. <label for="pinWireRelay">Pin GPIO (Numéro Wiring PI) relié au relais</label>
  324. <input type="number" value="<?php echo $selected->pin; ?>" id="pinWireRelay" placeholder="0,1,2…" />
  325. <label for="roomWireRelay">Pièce de la maison</label>
  326. <select id="roomWireRelay">
  327. <?php foreach($rooms as $room){ ?>
  328. <option <?php if ($selected->room == $room->getId()){echo "selected";} ?> value="<?php echo $room->getId(); ?>"><?php echo $room->getName(); ?></option>
  329. <?php } ?>
  330. </select>
  331. <label for="pinWireRelay">Mode impulsion (micro secondes)</label>
  332. <input type="number" value="<?php echo $selected->pulse; ?>" id="pulseWireRelay" placeholder="0" />
  333. <small>laisser à zéro pour désactiver le mode impulsion</small>
  334. <label for="reverseWireRelay">Inversion de l'état</label>
  335. <input type="checkbox" <?php echo $selected->reverse?'checked="checked"':''; ?> id="reverseWireRelay" />
  336. <small>Cocher si votre relais fonctionne dans le mauvais sens</small>
  337. </div>
  338. <div class="clear"></div>
  339. <br/><button onclick="plugin_wirerelay_save(this)" class="btn">Enregistrer</button>
  340. </fieldset>
  341. <br/>
  342. <fieldset>
  343. <legend>Consulter les relais filaires existants</legend>
  344. <table class="table table-striped table-bordered table-hover">
  345. <thead>
  346. <tr>
  347. <th>Nom</th>
  348. <th>Description</th>
  349. <th>Pin GPIO</th>
  350. <th>Pièce</th>
  351. <th colspan="2">Impulsion</th>
  352. </tr>
  353. </thead>
  354. <?php foreach($wireRelays as $wireRelay){
  355. $room = $roomManager->load(array('id'=>$wireRelay->room));
  356. ?>
  357. <tr>
  358. <td><?php echo $wireRelay->name; ?></td>
  359. <td><?php echo $wireRelay->description; ?></td>
  360. <td><?php echo $wireRelay->pin; ?></td>
  361. <td><?php echo (is_object($room)?$room->getName():'Pièce supprimée'); ?></td>
  362. <td><?php echo $wireRelay->pulse; ?></td>
  363. <td>
  364. <a class="btn" href="setting.php?section=wireRelay&id=<?php echo $wireRelay->id; ?>"><i class="fa fa-pencil"></i></a>
  365. <div class="btn" onclick="plugin_wirerelay_delete(<?php echo $wireRelay->id; ?>,this);"><i class="fa fa-times"></i></div>
  366. </td>
  367. </td>
  368. </tr>
  369. <?php } ?>
  370. </table>
  371. </fieldset>
  372. </div>
  373. <?php
  374. }
  375. }
  376. function wireRelay_plugin_setting_menu(){
  377. global $_;
  378. echo '<li '.(isset($_['section']) && $_['section']=='wireRelay'?'class="active"':'').'><a href="setting.php?section=wireRelay"><i class="fa fa-angle-right"></i> Relais filaires</a></li>';
  379. }
  380. function wireRelay_plugin_listen($command,$text,$confidence){
  381. //echo 'diction de la commande : '.$command;
  382. }
  383. function wireRelay_plugin_widget(&$widgets){
  384. $widgets[] = array(
  385. 'uid' => 'dash_wirerelay',
  386. 'icon' => 'fa fa-flash',
  387. 'label' => 'Relais',
  388. 'background' => '#50597b',
  389. 'color' => '#fffffff',
  390. 'onLoad' => 'action.php?action=wireRelay_load_widget',
  391. 'onEdit' => 'action.php?action=wireRelay_edit_widget',
  392. 'onSave' => 'action.php?action=wireRelay_save_widget',
  393. );
  394. }
  395. Plugin::addCss("/css/main.css");
  396. Plugin::addJs("/js/main.js");
  397. //Lie wireRelay_plugin_setting_page a la zone réglages
  398. Plugin::addHook("setting_bloc", "wireRelay_plugin_setting_page");
  399. //Lie wireRelay_plugin_setting_menu au menu de réglages
  400. Plugin::addHook("setting_menu", "wireRelay_plugin_setting_menu");
  401. //Lie wirerelay_plugin_action a la page d'action qui perme d'effecuer des actionx ajax ou ne demdnant pas de retour visuels
  402. Plugin::addHook("action_post_case", "wirerelay_plugin_action");
  403. //Lie wirerelay_plugin_vocal_command a la gestion de commandes vocales proposées par yana
  404. Plugin::addHook("vocal_command", "wirerelay_plugin_vocal_command");
  405. //Lie wireRelay_plugin_widget aux widgets de la dashboard
  406. Plugin::addHook("widgets", "wireRelay_plugin_widget");
  407. Plugin::addHook("listen", "wireRelay_plugin_listen");
  408. ?>