story.plugin.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /*
  3. @name Story
  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. @client 2
  9. @description [BETA] Plugin de gestion des scénarios avec leurs causes et leurs effets
  10. */
  11. include(dirname(__FILE__).'/Story.class.php');
  12. include(dirname(__FILE__).'/Cause.class.php');
  13. include(dirname(__FILE__).'/Effect.class.php');
  14. function story_plugin_menu(&$menuItems){
  15. global $_;
  16. $menuItems[] = array('sort'=>10,'content'=>'<a href="index.php?module=story"><i class="fa fa-caret-square-o-right"></i> Scénarios</a>');
  17. }
  18. function story_plugin_page($_){
  19. if(isset($_['module']) && $_['module']=='story'){
  20. switch(@$_['action']){
  21. case 'edit':
  22. require_once(dirname(__FILE__).'/edit.php');
  23. break;
  24. default:
  25. require_once(dirname(__FILE__).'/list.php');
  26. break;
  27. }
  28. }
  29. }
  30. function plugin_story_check(){
  31. require_once('Story.class.php');
  32. Story::check();
  33. }
  34. function story_gpio_change($pin,$state){
  35. require_once('Story.class.php');
  36. Story::check(array('type'=>'gpio','pin'=>$pin,'state'=>$state));
  37. }
  38. function story_plugin_action(){
  39. global $_,$myUser;
  40. switch($_['action']){
  41. case 'plugin_story_get_type_template':
  42. Action::write(
  43. function($_,&$response){
  44. $templates = array_merge(Cause::types(),Effect::types());
  45. $template = $templates[$_['type']];
  46. $_['data']['value'] = stripslashes($_['data']['value']);
  47. preg_match_all("/(\{)(.*?)(\})/", $template['template'], $matches, PREG_SET_ORDER);
  48. foreach($matches as $match){
  49. $template['template'] = str_replace($match[0],$_['data'][$match[2]],$template['template']);
  50. }
  51. $response['html'] =
  52. '<div data-element="line" class="line" data-type="'.$_['type'].'">
  53. <i class="fa '.$template['icon'].'"></i> <strong>'.$template['label'].'</strong> '.$template['template'].' <div class="delete"><i onclick="deleteLine(this);" class="fa fa-times"></i></div>
  54. </div>';
  55. },
  56. array()
  57. );
  58. break;
  59. case 'plugin_story_import':
  60. $story = new Story();
  61. $cause = new Cause();
  62. $effect = new Effect();
  63. $datas = json_decode(file_get_contents($_FILES['import']['tmp_name']),true);
  64. if(!$datas) exit('Mauvais format de données');
  65. $story = $story->fromArray($datas['story']);
  66. unset($story->id);
  67. $story->save();
  68. foreach($datas['causes'] as $data):
  69. $newcause = $cause->fromArray($data);
  70. unset($newcause->id);
  71. $newcause->story = $story->id;
  72. $newcause->save();
  73. endforeach;
  74. foreach($datas['effects'] as $data):
  75. $neweffect = $effect->fromArray($data);
  76. unset($neweffect->id);
  77. $neweffect->story = $story->id;
  78. $neweffect->save();
  79. endforeach;
  80. header('location: index.php?module=story');
  81. break;
  82. case 'plugin_story_export':
  83. $story = new Story();
  84. $cause = new Cause();
  85. $effect = new Effect();
  86. $story = $story->getById($_['id']);
  87. $effects = $effect->loadAll(array('story'=>$story->id),'sort');
  88. $causes = $cause->loadAll(array('story'=>$story->id),'sort');
  89. $json = array();
  90. $json['story'] = $story->toArray();
  91. foreach($causes as $cause):
  92. $json['causes'][] = $cause->toArray();
  93. endforeach;
  94. foreach($effects as $effect):
  95. $json['effects'][] = $effect->toArray();
  96. endforeach;
  97. header('Content-Description: File Transfer');
  98. header('Content-Type: application/json');
  99. header('Content-Disposition: attachment; filename=scenario-'.$story->id.'-'.date('d-m-Y').'.json');
  100. header('Content-Transfer-Encoding: binary');
  101. header('Expires: 0');
  102. header('Cache-Control: must-revalidate');
  103. header('Pragma: public');
  104. echo json_encode($json);
  105. break;
  106. case 'plugin_story_get_causes_effects':
  107. Action::write(
  108. function($_,&$response){
  109. $cause = new Cause();
  110. $effect = new Effect();
  111. $effects = $effect->loadAll(array('story'=>$_['id']),'sort');
  112. $causes = $cause->loadAll(array('story'=>$_['id']),'sort');
  113. $response['results'] = array('causes'=>array(),'effects'=>array());
  114. foreach($causes as $caus){
  115. $data = $caus->getValues();
  116. $response['results']['causes'][]= array(
  117. 'type' => $caus->type,
  118. 'panel'=>"cause",
  119. 'data'=> $data
  120. );
  121. }
  122. foreach($effects as $eff){
  123. $data = $eff->getValues();
  124. $response['results']['effects'][]=array(
  125. 'type' => $eff->type,
  126. 'panel'=>"effect",
  127. 'data'=> $data
  128. );
  129. }
  130. },
  131. array()
  132. );
  133. break;
  134. case 'plugin_story_get_captors_plugins':
  135. Action::write(
  136. function($_,&$response){
  137. $deviceManager = new Device();
  138. $devices = $deviceManager->loadAll(array('state'=>1,'type'=>Device::CAPTOR));
  139. $response['plugins'] = array();
  140. foreach($devices as $device){
  141. if(!isset($response['plugins'][$device->plugin])) $response['plugins'][] = $device->plugin ;
  142. }
  143. },
  144. array()
  145. );
  146. break;
  147. case 'plugin_story_get_captors':
  148. Action::write(
  149. function($_,&$response){
  150. $deviceManager = new Device();
  151. $devices = $deviceManager->loadAll(array('state'=>1,'plugin'=>$_['plugin'],'type'=>Device::CAPTOR));
  152. foreach($devices as $device){
  153. $response['devices'][] = array(
  154. 'plugin' => $device->plugin,
  155. 'label' => $device->label,
  156. 'id' => $device->id
  157. );
  158. }
  159. },
  160. array()
  161. );
  162. break;
  163. case 'plugin_story_get_captor_values':
  164. Action::write(
  165. function($_,&$response){
  166. $deviceManager = new Device();
  167. $device = $deviceManager->getById($_['id']);
  168. $response['values'] = $device->getValues();
  169. },
  170. array()
  171. );
  172. break;
  173. case 'plugin_story_launch_story':
  174. Action::write(
  175. function($_,&$response){
  176. Story::execute($_['id']);
  177. $story = new Story();
  178. $story = $story->getById($_['id']);
  179. $response['log'] = $story->log;
  180. },
  181. array()
  182. );
  183. break;
  184. case 'plugin_story_change_state':
  185. Action::write(
  186. function($_,&$response){
  187. $story = new Story();
  188. $story->change(array('state'=>$_['state']),array('id'=>$_['id']));
  189. },
  190. array()
  191. );
  192. break;
  193. case 'plugin_story_delete_story':
  194. Action::write(
  195. function($_,&$response){
  196. $storyManager = new Story();
  197. $causeManager = new Cause();
  198. $effectManager = new Effect();
  199. $storyManager->delete(array('id'=>$_['id']));
  200. $causeManager->delete(array('story'=>$_['id']));
  201. $effectManager->delete(array('story'=>$_['id']));
  202. },
  203. array()
  204. );
  205. break;
  206. case 'plugin_story_check':
  207. require_once(dirname(__FILE__).'/Story.class.php');
  208. global $conf;
  209. $conf->put('last_sentence',urldecode($_['sentence']),'var');
  210. //plugin_story_check();
  211. Story::check(array('type'=>'sentence','sentence'=>urldecode($_['sentence'])));
  212. break;
  213. case 'plugin_story_save_story':
  214. Action::write(
  215. function($_,&$response){
  216. $causeManager = new Cause();
  217. $effectManager = new Effect();
  218. $story = new Story();
  219. if(isset($_['story']['id']) && $_['story']['id']!='0'){
  220. $story = $story->getById($_['story']['id']);
  221. $causeManager->delete(array('story'=>$story->id));
  222. $effectManager->delete(array('story'=>$story->id));
  223. }
  224. $story->label = $_['story']['label'];
  225. $story->date = time();
  226. $story->state = 1;
  227. $story->save();
  228. $response['id'] = $story->id;
  229. $i = 0;
  230. foreach($_['story']['causes'] as $cause){
  231. $current = new Cause();
  232. $current->type = $cause['type'];
  233. $current->operator = @$cause['operator'];
  234. $current->setValues($cause);
  235. $current->sort = $i;
  236. $current->union = $cause['union'];
  237. $current->story = $story->id;
  238. $current->save();
  239. $i++;
  240. }
  241. $i = 0;
  242. foreach($_['story']['effects'] as $effect){
  243. $current = new Effect();
  244. $current->type = $effect['type'];
  245. $current->setValues($effect);
  246. $current->sort = $i;
  247. $current->union = $cause['union'];
  248. $current->story = $story->id;
  249. $current->save();
  250. $i++;
  251. }
  252. },
  253. array()
  254. );
  255. break;
  256. }
  257. }
  258. function story_vocal_command(&$response,$actionUrl){
  259. global $conf;
  260. require_once(dirname(__FILE__).'/Cause.class.php');
  261. $causeManager = new Cause();
  262. $vocals = $causeManager->loadAll(array('type'=>'listen'));
  263. foreach($vocals as $vocal){
  264. $data = json_decode($vocal->values);
  265. $data->confidence = $data->confidence==0 ? '0.81' : $data->confidence;
  266. $response['commands'][] = array(
  267. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' '.$data->value,
  268. 'url'=>$actionUrl.'?action=plugin_story_check&type=talk&sentence='.urlencode($data->value),'confidence'=>($data->confidence+$conf->get('VOCAL_SENSITIVITY'))
  269. );
  270. }
  271. }
  272. Plugin::addCss("/css/main.css");
  273. Plugin::addJs("/js/main.js");
  274. Gpio::listen('all','story_gpio_change');
  275. Plugin::addHook("menubar_pre_home", "story_plugin_menu");
  276. Plugin::addHook("home", "story_plugin_page");
  277. Plugin::addHook("action_post_case", "story_plugin_action");
  278. Plugin::addHook("vocal_command", "story_vocal_command");
  279. Plugin::addHook("cron", "plugin_story_check");
  280. ?>