eventmanager.plugin.disabled.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /*
  3. @name Event manager
  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 la programmation d'évenements yana client (parole,commande,son...) selon un horaire fixé
  9. */
  10. function eventmanager_action(){
  11. global $_,$conf,$myUser;
  12. switch($_['action']){
  13. case 'eventmanager_save_event':
  14. if($myUser->can('event','c') || $myUser->can('event','u')){
  15. $event = new Event();
  16. $event =($_['eventId']!=''?$event->getById($_['eventId']):$event);
  17. $event->setName($_['eventName']);
  18. $event->setYear($_['eventYear']);
  19. $event->setMonth($_['eventMonth']);
  20. $event->setDay($_['eventDay']);
  21. $event->setHour($_['eventHour']);
  22. $event->setMinut($_['eventMinut']);
  23. $event->setRepeat('0');
  24. $content = array();
  25. //Todo, prendre en compte le multi action ([1],[2]...)
  26. $event->setRecipients(array());
  27. $event->addRecipient($_['eventTarget']);
  28. $content[0]['type'] = $_['eventType'];
  29. $event->setState($_['eventState']);
  30. switch($content[0]['type']){
  31. case 'talk':
  32. $content[0]['sentence'] = $_['eventContent'];
  33. break;
  34. case 'sound':
  35. $content[0]['file'] = $_['eventContent'];
  36. break;
  37. case 'command':
  38. $content[0]['program'] = $_['eventContent'];
  39. break;
  40. case 'gpio':
  41. $content[0]['gpios'] = $_['eventContent'];
  42. break;
  43. }
  44. $event->setContent($content[0]);
  45. //$event->setRecipients('all'); //@TODO
  46. $event->save();
  47. header('location:index.php?module=eventmanager');
  48. }else{
  49. header('location:index.php?module=eventmanager&error=Vous n\'avez pas le droit de faire ça!');
  50. }
  51. break;
  52. case 'eventmanager_delete_event':
  53. if($myUser->can('event','d')){
  54. $event = new Event();
  55. $event->delete(array('id'=>$_['id']));
  56. header('location:index.php?module=eventmanager');
  57. }else{
  58. header('location:index.php?module=eventmanager&error=Vous n\'avez pas le droit de faire ça!');
  59. }
  60. break;
  61. }
  62. }
  63. function eventmanager_plugin_menu(&$menuItems){
  64. global $_;
  65. $menuItems[] = array('sort'=>1,'content'=>'<a href="index.php?module=eventmanager"><i class="fa fa-clock-o"></i> Événements</a>');
  66. }
  67. function eventmanager_plugin_page($_){
  68. if(isset($_['module']) && $_['module']=='eventmanager'){
  69. $eventManager = new Event();
  70. $currentEvent = new Event();
  71. $currentEvent->setYear('*');
  72. $currentEvent->setMonth('*');
  73. $currentEvent->setDay('*');
  74. $currentEvent->setHour('*');
  75. $currentEvent->setMinut('*');
  76. $currentEvent = (isset($_['id'])?$eventManager->getById($_['id']):$currentEvent);
  77. ?>
  78. <div class="span12">
  79. <h1>Événements</h1>
  80. <form action="action.php?action=eventmanager_save_event" method="POST">
  81. <fieldset>
  82. <legend>Gestion des événements</legend>
  83. <p>Ce module vous permet de créer un événement en fonction d'une date que le client (yana windows ou yana for android)
  84. ou le serveur (yana-server sur le rapsberry PI) pourra retranscrire.
  85. <br/>Pour le client, l'événement peut être une action parole (prononce une phrase), une commande (une commande est lancée sur
  86. le poste qui execute yana client), ou encore un son à jouer (le son doit être un .wav situé dans le repertoire son de yana-windows)
  87. <br/><br/>Pour le serveur, l'événement peut être une commande (lancée sur le rapsberry PI), ou un changement d'état GPIO.</p>
  88. <div class="row">
  89. <div class="flatBloc blue-color">
  90. <h3><i class="fa fa-clock-o"></i> Événement : heure/date</h3>
  91. <div class="span2">
  92. <label for="eventMinut">Minute</label>
  93. <select class="input-medium" name="eventMinut" id="eventMinut">
  94. <option <?php if($currentEvent->getMinut()=='*') echo 'selected="selected"'; ?> value="*">Toutes</option>
  95. <?php for($i=0;$i<60;$i++){ ?>
  96. <option <?php if($currentEvent->getMinut()==''.$i) echo 'selected="selected"'; ?>><?php echo $i; ?></option>
  97. <?php } ?>
  98. </select>
  99. </div>
  100. <div class="span2">
  101. <label for="eventHour">Heure</label>
  102. <select class="input-medium" name="eventHour" id="eventHour">
  103. <option <?php if($currentEvent->getHour()=='*'){ echo 'selected="selected"'; } ?> value="*">Toutes</option>
  104. <?php for($i=0;$i<24;$i++){ ?>
  105. <option <?php if($currentEvent->getHour()==''.$i) echo 'selected="selected"'; ?>><?php echo $i; ?></option>
  106. <?php } ?>
  107. </select>
  108. </div>
  109. <div class="span2">
  110. <label for="eventDay">Jour</label>
  111. <select class="input-medium" name="eventDay" id="eventDay">
  112. <option <?php if($currentEvent->getDay()=='*') echo 'selected="selected"'; ?> value="*">Tous</option>
  113. <?php for($i=1;$i<32;$i++){ ?>
  114. <option <?php if($currentEvent->getDay()==''.$i) echo 'selected="selected"'; ?>><?php echo $i; ?></option>
  115. <?php } ?>
  116. </select>
  117. </div>
  118. <div class="span2">
  119. <label for="eventMonth">Mois</label>
  120. <select class="input-medium" name="eventMonth" id="eventMonth">
  121. <option <?php if($currentEvent->getMonth()=='*') echo 'selected="selected"'; ?> value="*">Tous</option>
  122. <?php for($i=1;$i<13;$i++){ ?>
  123. <option <?php if($currentEvent->getMonth()==''.$i) echo 'selected="selected"'; ?>><?php echo $i; ?></option>
  124. <?php } ?>
  125. </select>
  126. </div>
  127. <div class="span3">
  128. <label for="eventYear">Année (taper * pour toutes)</label>
  129. <input class="input-medium" type="text" value="<?php echo $currentEvent->getYear(); ?>" name="eventYear" id="eventYear" placeholder="1988" />
  130. </div>
  131. <div class="clear"></div>
  132. </div>
  133. </div>
  134. <div class="row">
  135. <div class="flatBloc green-color">
  136. <h3><i class="fa fa-bomb"></i> Action résultante</h3>
  137. <div class="span4">
  138. <label for="eventName">Nom</label>
  139. <input class="input-xlarge" type="text" id="eventName" value="<?php echo $currentEvent->getName(); ?>" name="eventName" placeholder="ex : Signale un anniversaire"/>
  140. </div>
  141. <div class="span2">
  142. <?php
  143. $recipients = $currentEvent->getRecipients();
  144. $content = $currentEvent->getContent();
  145. $action = $content;
  146. ?>
  147. <label for="eventTarget">Cible</label>
  148. <select class="input-medium" name="eventTarget" id="eventTarget" onready="setActionTypeList('<?php echo $action['type']; ?>');" onchange="setActionTypeList('<?php echo $action['type']; ?>');">
  149. <option <?php echo ($recipients[0]=='client'?'selected="selected"':''); ?> value="client">Client</option>
  150. <option <?php echo ($recipients[0]=='server'?'selected="selected"':''); ?> value="server">Serveur</option>
  151. </select>
  152. </div>
  153. <div class="span2">
  154. <label for="eventType">Action</label>
  155. <select class="input-medium" id="eventType" name="eventType" value="<?php echo $action['type']; ?>"></select>
  156. </div>
  157. <div class="span2">
  158. <label for="eventState">État</label>
  159. <select class="input-medium" id="eventState" name="eventState">
  160. <option value="1" <?php echo ($currentEvent->getState()=='1'?'selected="selected"':''); ?>>Actif</option>
  161. <option value="0" <?php echo ($currentEvent->getState()=='0'?'selected="selected"':''); ?>>Inactif</option>
  162. </select>
  163. </div>
  164. <div class="span4">
  165. <label for="eventContent">Contenu</label>
  166. <textarea class="span10" name="eventContent" id="eventContent"><?php
  167. switch($action['type']){
  168. case 'talk':
  169. echo $action['sentence'];
  170. break;
  171. case 'sound':
  172. echo $action['file'];
  173. break;
  174. case 'command':
  175. echo $action['program'];
  176. break;
  177. case 'gpio':
  178. echo $action['gpios'];
  179. break;
  180. }
  181. ?></textarea>
  182. <input type="hidden" name="eventId" value="<?php echo $currentEvent->getId(); ?>" >
  183. </div>
  184. <div class="clear"></div>
  185. </div>
  186. </div>
  187. <div class="clear"></div>
  188. <br/><button type="submit" class="btn">Enregistrer</button>
  189. </fieldset>
  190. </form>
  191. <table class="table table-striped table-bordered table-hover">
  192. <thead>
  193. <tr>
  194. <th>Nom</th>
  195. <th>Heure Date</th>
  196. <th>Type</th>
  197. <th>Contenu</th>
  198. <th>Dernier lancement</th>
  199. <th>Cibles</th>
  200. <th>Etat</th>
  201. <th></th>
  202. </tr>
  203. </thead>
  204. <?php
  205. $eventManager = new Event();
  206. $events = $eventManager->populate();
  207. ;
  208. foreach($events as $event){
  209. $action = $event->getContent();
  210. $recipients = $event->getRecipients();
  211. //$action = $action[0];
  212. ?>
  213. <tr>
  214. <td><?php echo $event->getName(); ?></td>
  215. <td><?php echo $event->getHour().':'.$event->getMinut().' '.$event->getDay().'/'.$event->getMonth().'/'.$event->getYear(); ?></td>
  216. <td><?php echo $action['type']; ?></td>
  217. <td><?php switch($action['type']){
  218. case 'talk':
  219. echo $action['sentence'];
  220. break;
  221. case 'sound':
  222. echo $action['file'];
  223. break;
  224. case 'command':
  225. echo $action['program'];
  226. break;
  227. case 'gpio':
  228. echo $action['gpios'];
  229. break;
  230. }; ?></td>
  231. <td><?php echo $event->getRepeat(); ?></td>
  232. <td><?php echo implode(',',$recipients) ?></td>
  233. <td><?php echo ($event->getState()=='1'?'Actif':'Inactif');?></td>
  234. <td>
  235. <a class="btn" href="index.php?module=eventmanager&id=<?php echo $event->getId(); ?>"><i class="fa fa-pencil"></i></a>
  236. <a class="btn" href="action.php?action=eventmanager_delete_event&id=<?php echo $event->getId(); ?>"><i class="fa fa-times"></i></a></td>
  237. </tr>
  238. <?php } ?>
  239. </table>
  240. <strong>Important: </strong>Pour profiter du gestionnaire d'événements de yana <code>coté serveur</code>, vous devez ajouter une tâche
  241. planifiée sur le raspberry PI, pour cela tapez :
  242. <code>sudo crontab -e</code>
  243. puis ajoutez la ligne
  244. <?php
  245. $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  246. $url = $protocol.str_replace('//','/','localhost/'.str_replace('index.php','',$_SERVER['PHP_SELF']).'/action.php?action=GET_EVENT&checker=server');
  247. echo '<code>*/1 * * * * wget '.$url.' -O /dev/null 2>&1</code>'; ?>
  248. puis sauvegardez (<code>ctrl</code>+<code>x</code> puis <code>O</code> puis <code>Entrée</code>)
  249. <br/><br/> <br/><br/>
  250. </div>
  251. <?php
  252. }
  253. }
  254. Plugin::addJs('/js/main.js');
  255. Plugin::addHook("action_post_case", "eventmanager_action");
  256. Plugin::addHook("menubar_pre_home", "eventmanager_plugin_menu");
  257. Plugin::addHook("home", "eventmanager_plugin_page");
  258. //Exemple vide d'interception d'évenement sur un plugin tiers (plugin radiorelay, changement d'etat)
  259. Event::on('relay_change_state',function($data,$state){
  260. // $data = classe RadioRelay correspondante à la machine
  261. // $state = etat de la machine
  262. });
  263. ?>