door.plugin.disabled.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /*
  3. @name Door
  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 Plugin de gestion des portes avec gache éléctrique + detection ouverture/fermeture (capteur a effet de hall)
  9. */
  10. include('Door.class.php');
  11. function door_plugin_setting_page(){
  12. global $_,$myUser;
  13. if(isset($_['section']) && $_['section']=='door' ){
  14. if($myUser!=false){
  15. $doorManager = new Door();
  16. $doors = $doorManager->populate();
  17. $roomManager = new Room();
  18. $rooms = $roomManager->loadAll(array('state'=>'0'));
  19. ?>
  20. <div class="span9 userBloc">
  21. <h1>Porte</h1>
  22. <p>Gestion des portes</p>
  23. <form action="action.php?action=door_add_door" method="POST">
  24. <fieldset>
  25. <legend>Ajout d'un porte</legend>
  26. <div class="left">
  27. <label for="nameDoor">Nom</label>
  28. <input type="text" id="nameDoor" onkeyup="$('#vocalCommand').html($(this).val());" name="nameDoor" placeholder="Lumiere Canapé…"/>
  29. <small>Commande vocale associée : "YANA, ouvre <span id="vocalCommand"></span>"</small>
  30. <label for="descriptionDoor">Description</label>
  31. <input type="text" name="descriptionDoor" id="descriptionDoor" placeholder="Porte de l'entrée…" />
  32. <label for="pinDoorRelay">Pin GPIO verouillage (relais)</label>
  33. <input type="text" name="pinDoorRelay" id="pinDoorRelayRelay" placeholder="0,1,2…" />
  34. <label for="pinDoorRelay">Pin GPIO etat (capteur hall)</label>
  35. <input type="text" name="pinDoorCaptor" id="pinDoorCaptor" placeholder="0,1,2…" />
  36. <label for="roomDoor">Pièce</label>
  37. <select name="roomDoor" id="roomDoor">
  38. <?php foreach($rooms as $room){ ?>
  39. <option value="<?php echo $room->getId(); ?>"><?php echo $room->getName(); ?></option>
  40. <?php } ?>
  41. </select>
  42. </div>
  43. <div class="clear"></div>
  44. <br/><button type="submit" class="btn">Ajouter</button>
  45. </fieldset>
  46. <br/>
  47. </form>
  48. <table class="table table-striped table-bordered table-hover">
  49. <thead>
  50. <tr>
  51. <th>Nom</th>
  52. <th>Description</th>
  53. <th>Pin GPIO verouillage</th>
  54. <th>Pin GPIO etat</th>
  55. <th>Pièce</th>
  56. </tr>
  57. </thead>
  58. <?php foreach($doors as $door){
  59. $room = $roomManager->load(array('id'=>$door->getRoom()));
  60. ?>
  61. <tr>
  62. <td><?php echo $door->getName(); ?></td>
  63. <td><?php echo $door->getDescription(); ?></td>
  64. <td><?php echo $door->getPinRelay(); ?></td>
  65. <td><?php echo $door->getPinCaptor(); ?></td>
  66. <td><?php echo $room->getName(); ?></td>
  67. <td><a class="btn" href="action.php?action=door_delete_door&id=<?php echo $door->getId(); ?>"><i class="icon-remove"></i></a></td>
  68. </tr>
  69. <?php } ?>
  70. </table>
  71. </div>
  72. <?php }else{ ?>
  73. <div id="main" class="wrapper clearfix">
  74. <article>
  75. <h3>Vous devez être connecté</h3>
  76. </article>
  77. </div>
  78. <?php
  79. }
  80. }
  81. }
  82. function door_plugin_setting_menu(){
  83. global $_;
  84. echo '<li '.(isset($_['section']) && $_['section']=='door'?'class="active"':'').'><a href="setting.php?section=door"><i class="icon-chevron-right"></i> Porte</a></li>';
  85. }
  86. function door_display($room){
  87. global $_;
  88. $doorManager = new Door();
  89. $doors = $doorManager->loadAll(array('room'=>$room->getId()));
  90. foreach ($doors as $door) {
  91. ?>
  92. <div class="span3">
  93. <h5><?php echo $door->getName() ?></h5>
  94. <p><?php echo $door->getDescription() ?>
  95. </p>
  96. <div id="state<?php echo $door->getId() ?>">
  97. </div>
  98. </div>
  99. <ul>
  100. <li>PIN GPIO verouillage : <code><?php echo $door->getPinRelay() ?></code></li>
  101. <li>PIN GPIO état : <code><?php echo $door->getPinCaptor() ?></code></li>
  102. <li>Type : <code>Verrou</code></li>
  103. <li>Emplacement : <code><?php echo $room->getName() ?></code></li>
  104. </ul>
  105. <div class="btn-toolbar">
  106. <div class="btn-group">
  107. <a class="btn btn-success" href="action.php?action=door_change_state&engine=<?php echo $door->getId() ?>&amp;code=<?php echo $door->getPinRelay() ?>&amp;state=1"><i class="icon-thumbs-up icon-white"></i></a>
  108. <a class="btn" href="action.php?action=door_change_state&engine=<?php echo $door->getId() ?>&amp;code=<?php echo $door->getPinRelay() ?>&amp;state=0"><i class="icon-thumbs-down "></i></a>
  109. </div>
  110. </div>
  111. </div>
  112. <?php
  113. }
  114. }
  115. function door_vocal_command(&$response,$actionUrl){
  116. $doorManager = new Door();
  117. $doors = $doorManager->populate();
  118. foreach($doors as $door){
  119. $response['commands'][] = array('command'=>VOCAL_ENTITY_NAME.', ouvre '.$door->getName(),'url'=>$actionUrl.'?action=door_change_state&engine='.$door->getId().'&state=1&webservice=true','confidence'=>'0.9');
  120. $response['commands'][] = array('command'=>VOCAL_ENTITY_NAME.', ferme '.$door->getName(),'url'=>$actionUrl.'?action=door_change_state&engine='.$door->getId().'&state=0&webservice=true','confidence'=>'0.9');
  121. $response['commands'][] = array('command'=>VOCAL_ENTITY_NAME.', etat '.$door->getName(),'url'=>$actionUrl.'?action=door_get_state&engine='.$door->getId().'&webservice=true','confidence'=>'0.9');
  122. }
  123. }
  124. function door_action_door(){
  125. global $_,$conf,$myUser;
  126. switch($_['action']){
  127. case 'door_delete_door':
  128. if($myUser->can('porte','d')){
  129. $doorManager = new Door();
  130. $doorManager->delete(array('id'=>$_['id']));
  131. }
  132. header('location:setting.php?section=door');
  133. break;
  134. case 'door_add_door':
  135. if($myUser->can('porte','c')){
  136. $door = new Door();
  137. $door->setName($_['nameDoor']);
  138. $door->setDescription($_['descriptionDoor']);
  139. $door->setPinRelay($_['pinDoorRelay']);
  140. $door->setPinCaptor($_['pinDoorCaptor']);
  141. $door->setRoom($_['roomDoor']);
  142. $door->save();
  143. }
  144. header('location:setting.php?section=door');
  145. break;
  146. case 'door_get_state':
  147. if($myUser->can('porte','r')){
  148. $door = new Door();
  149. $door = $door->getById($_['engine']);
  150. $cmd = '/usr/local/bin/gpio mode '.$door->getPinCaptor().' in';
  151. system($cmd,$out);
  152. $cmd = '/usr/local/bin/gpio read '.$door->getPinCaptor();
  153. exec($cmd,$out);
  154. if(!isset($_['webservice'])){
  155. echo $out[0];
  156. }else{
  157. $affirmation = (trim($out[0])?'Ouvert':'Fermé');
  158. $response = array('responses'=>array(
  159. array('type'=>'talk','sentence'=>$affirmation)
  160. )
  161. );
  162. $json = json_encode($response);
  163. echo ($json=='[]'?'{}':$json);
  164. }
  165. }
  166. break;
  167. case 'door_change_state':
  168. global $_,$myUser;
  169. if($myUser->can('porte','u')){
  170. $door = new Door();
  171. $door = $door->getById($_['engine']);
  172. $cmd = '/usr/local/bin/gpio mode '.$door->getPinRelay().' out';
  173. system($cmd,$out);
  174. $cmd = '/usr/local/bin/gpio write '.$door->getPinRelay().' '.$_['state'];
  175. system($cmd,$out);
  176. //TODO change bdd state
  177. if(!isset($_['webservice'])){
  178. header('location:index.php?module=room&id='.$door->getRoom());
  179. }else{
  180. $affirmations = array( 'A vos ordres!',
  181. 'Bien!',
  182. 'Oui commandant!',
  183. 'Avec plaisir!',
  184. 'J\'aime vous obéir!',
  185. 'Avec plaisir!',
  186. 'Certainement!',
  187. 'Je fais ça sans tarder!',
  188. 'Avec plaisir!',
  189. 'Oui chef!');
  190. $affirmation = $affirmations[rand(0,count($affirmations)-1)];
  191. $response = array('responses'=>array(
  192. array('type'=>'talk','sentence'=>$affirmation)
  193. )
  194. );
  195. $json = json_encode($response);
  196. echo ($json=='[]'?'{}':$json);
  197. }
  198. }else{
  199. $response = array('responses'=>array(
  200. array('type'=>'talk','sentence'=>'Je ne vous connais pas, je refuse de faire ça!')
  201. )
  202. );
  203. echo json_encode($response);
  204. }
  205. break;
  206. }
  207. }
  208. Plugin::addJs("/js/main.js");
  209. Plugin::addCss("/css/style.css");
  210. Plugin::addHook("action_post_case", "door_action_door");
  211. Plugin::addHook("node_display", "door_display");
  212. Plugin::addHook("setting_bloc", "door_plugin_setting_page");
  213. Plugin::addHook("setting_menu", "door_plugin_setting_menu");
  214. Plugin::addHook("vocal_command", "door_vocal_command");
  215. ?>