ipcam.plugin.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /*
  3. @name IpCam
  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 Visualisation en streaming de camera(s) IP
  9. */
  10. function ipcam_action(){
  11. global $_,$conf;
  12. switch($_['action']){
  13. case 'ipcam_save_camera':
  14. Action::write(
  15. function($_,&$response){
  16. require_once('Camera.class.php');
  17. $camera = new Camera();
  18. if(empty($_['labelCamera'])) throw new Exception("Le nom est obligatoire");
  19. if(empty($_['ipCamera'])) throw new Exception("L'IP est obligatoire");
  20. $camera = !empty($_['id']) ? $camera->getById($_['id']): new Camera();
  21. $camera->label = $_['labelCamera'];
  22. $camera->location = $_['locationCamera'];
  23. $camera->pattern = $_['patternCamera'];
  24. $camera->ip = $_['ipCamera'];
  25. $camera->login = $_['loginCamera'];
  26. $camera->password = $_['passwordCamera'];
  27. $camera->save();
  28. $response['message'] = 'Caméra enregistrée avec succès';
  29. },
  30. array('ipcam'=>'c')
  31. );
  32. break;
  33. case 'ipcam_delete_camera':
  34. Action::write(
  35. function($_,$response){
  36. require_once('Camera.class.php');
  37. $camera = new Camera();
  38. $camera->delete(array('id'=>$_['id']));
  39. },
  40. array('ipcam'=>'d')
  41. );
  42. break;
  43. case 'ipcam_load_widget':
  44. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  45. Action::write(
  46. function($_,&$response){
  47. $widget = new Widget();
  48. $widget = $widget->getById($_['id']);
  49. $parameters = $widget->data();
  50. if(empty($parameters['camera'])){
  51. $content = 'Choisissez une camera en cliquant sur l \'icone <i class="fa fa-wrench"></i> de la barre du widget';
  52. }else{
  53. global $conf;
  54. require_once('Camera.class.php');
  55. $camera = new Camera();
  56. $camera = $camera->getById($parameters['camera']);
  57. $room = new Room();
  58. $room = $room->getById($camera->location);
  59. $response['title'] = 'Sonde '.$camera->label.' ('.$room->getName().')';
  60. $content = '
  61. <!-- CSS -->
  62. <style>
  63. </style>
  64. <!-- HTML -->';
  65. $url = str_replace(array(
  66. '{{login}}',
  67. '{{password}}',
  68. '{{ip}}'
  69. ),array(
  70. $camera->login,
  71. $camera->password,
  72. $camera->ip
  73. ),$camera->pattern);
  74. ///pour ma cam, a la fin de l'ip : http://{{login}}:{{password}}@{{ip}}/videostream.cgi
  75. $content .= '<div class="ipcam_widget"><img name="main" id="main" border="0" src="'.$url.'">';
  76. $content .= '</div>';
  77. $content .= '
  78. <!-- JS -->
  79. <script type="text/javascript">
  80. </script>
  81. ';
  82. }
  83. $response['content'] = $content;
  84. }
  85. );
  86. break;
  87. case 'ipcam_edit_widget':
  88. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  89. require_once(dirname(__FILE__).'/Camera.class.php');
  90. $widget = new Widget();
  91. $widget = $widget->getById($_['id']);
  92. $data = $widget->data();
  93. $camera = new Camera();
  94. $cameras = $camera->populate();
  95. $content = '<h3>Choix de la camera</h3>';
  96. if(count($cameras) == 0):
  97. $content = 'Aucune camera enregistrée,<a href="setting.php?section=ipcam">enregistrer une camera<a>';
  98. else :
  99. $content .= '<select id="camera">';
  100. foreach($cameras as $camera)
  101. $content .= '<option '.($camera->id==$data['camera']?'selected="selected"':'').' value="'.$camera->id.'">'.$camera->label.' ('.$camera->uid.')</option>';
  102. $content .= '</select>';
  103. endif;
  104. echo $content;
  105. break;
  106. case 'ipcam_save_widget':
  107. require_once(dirname(__FILE__).'/../dashboard/Widget.class.php');
  108. $widget = new Widget();
  109. $widget = $widget->getById($_['id']);
  110. $data = $widget->data();
  111. $data['camera'] = $_['camera'];
  112. $widget->data($data);
  113. $widget->save();
  114. echo $content;
  115. break;
  116. }
  117. }
  118. function ipcam_plugin_setting_page(){
  119. global $_,$myUser,$conf;
  120. if(isset($_['section']) && $_['section']=='ipcam' ){
  121. require_once('Camera.class.php');
  122. if(!$myUser) throw new Exception('Vous devez être connecté pour effectuer cette action');
  123. $cameraManager = new Camera();
  124. $cameras = $cameraManager->populate();
  125. $roomManager = new Room();
  126. $rooms = $roomManager->loadAll(array('state'=>'0'));
  127. $selected = new Camera();
  128. $selected->pattern = 'http://{{login}}:{{password}}@{{ip}}/videostream.cgi';
  129. //Si on est en mode modification
  130. if (isset($_['id']))
  131. $selected = $cameraManager->getById($_['id']);
  132. ?>
  133. <div class="span9 userBloc">
  134. <h1>Camera</h1>
  135. <p>Gestion des cameras IP</p>
  136. <fieldset>
  137. <legend>Ajouter/Modifier une camera</legend>
  138. <div class="left">
  139. <label for="labelCamera">Nom</label>
  140. <input type="hidden" id="id" value="<?php echo $selected->id; ?>">
  141. <input type="text" id="labelCamera" value="<?php echo $selected->label; ?>" placeholder="Sonde du salon"/>
  142. <label for="ipCamera">IP/Adresse du stream</label>
  143. <input type="text" value="<?php echo $selected->ip; ?>" id="ipCamera" placeholder="ex : 192.168.11.27:87" />
  144. <label for="loginCamera">Login</label>
  145. <input type="text" value="<?php echo $selected->login; ?>" id="loginCamera" placeholder="" />
  146. <label for="passwordCamera">Password</label>
  147. <input type="text" value="<?php echo $selected->password; ?>" id="passwordCamera" placeholder="" />
  148. <label>Marque de camera</label>
  149. <select onchange="plugin_ipcam_brand(this)">
  150. <?php foreach(Camera::brands() as $brand=>$pattern): ?>
  151. <option value="<?php echo $pattern; ?>"><?php echo $brand; ?></option>
  152. <?php endforeach;?>
  153. </select>
  154. <label for="patternCamera">Adresse de visualisation</label>
  155. <small>Adresse complète vers le streaming de votre camera, les balises entre {{}} seront remplacée par les valeurs des champs ci dessus.</small>
  156. <input type="text" class="input-xxlarge" value="<?php echo $selected->pattern; ?>" id="patternCamera" placeholder="http://{{login}}:{{password}}@{{ip}}/videostream.cgi" />
  157. <label for="locationCamera">Pièce de la maison</label>
  158. <select id="locationCamera">
  159. <?php foreach($rooms as $room){ ?>
  160. <option <?php if ($selected->location == $room->getId()){echo "selected";} ?> value="<?php echo $room->getId(); ?>"><?php echo $room->getName(); ?></option>
  161. <?php } ?>
  162. </select>
  163. </div>
  164. <div class="clear"></div>
  165. <br/><button onclick="plugin_ipcam_save(this)" class="btn">Enregistrer</button>
  166. </fieldset>
  167. <br/>
  168. <fieldset>
  169. <legend>Consulter les sondes existants</legend>
  170. <table class="table table-striped table-bordered table-hover">
  171. <thead>
  172. <tr>
  173. <th>Nom</th>
  174. <th>IP</th>
  175. <th>Pièce</th>
  176. <th colspan="2"></th>
  177. </tr>
  178. </thead>
  179. <?php foreach($cameras as $camera){
  180. $room = $roomManager->load(array('id'=>$camera->location));
  181. ?>
  182. <tr>
  183. <td><?php echo $camera->label; ?></td>
  184. <td><?php echo $camera->ip; ?></td>
  185. <td><?php echo $room->getName(); ?></td>
  186. <td>
  187. <a class="btn" href="setting.php?section=ipcam&id=<?php echo $camera->id; ?>"><i class="fa fa-pencil"></i></a>
  188. <div class="btn" onclick="plugin_ipcam_delete(<?php echo $camera->id; ?>,this);"><i class="fa fa-times"></i></div>
  189. </td>
  190. </td>
  191. </tr>
  192. <?php } ?>
  193. </table>
  194. </fieldset>
  195. </div>
  196. <?php
  197. }
  198. }
  199. function ipcam_plugin_setting_menu(){
  200. global $_;
  201. echo '<li '.(isset($_['section']) && $_['section']=='ipcam'?'class="active"':'').'><a href="setting.php?section=ipcam"><i class="fa fa-angle-right"></i> Caméra IP</a></li>';
  202. }
  203. function ipcam_plugin_widget(&$widgets){
  204. $widgets[] = array(
  205. 'uid' => 'dash_ipcam',
  206. 'icon' => 'fa fa-video-camera',
  207. 'label' => 'Camera',
  208. 'background' => '#C511D6',
  209. 'color' => '#fffffff',
  210. 'onLoad' => 'action.php?action=ipcam_load_widget',
  211. 'onEdit' => 'action.php?action=ipcam_edit_widget',
  212. 'onSave' => 'action.php?action=ipcam_save_widget',
  213. );
  214. }
  215. Plugin::addJs("/js/main.js");
  216. //Lie wireRelay_plugin_setting_page a la zone réglages
  217. Plugin::addHook("setting_bloc", "ipcam_plugin_setting_page");
  218. //Lie wireRelay_plugin_setting_menu au menu de réglages
  219. Plugin::addHook("setting_menu", "ipcam_plugin_setting_menu");
  220. Plugin::addHook("action_post_case", "ipcam_action");
  221. Plugin::addHook("widgets", "ipcam_plugin_widget");
  222. ?>