Video.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  4. class Video extends Base_Controller {
  5. public function __construct() {
  6. parent::__construct();
  7. $this->load->model('Videomonitor_model');
  8. $this->load->model('Project_model');
  9. $this->load->model('Network_model');
  10. $this->load->model('System_config_model');
  11. }
  12. // 视屏监控列表
  13. public function getlist()
  14. {
  15. $userid = $this->get_user_info('id');
  16. $role = $this->get_user_info('role');
  17. $companyid = $this->get_user_info('companyid');
  18. $page = $this->input->post('page',true);
  19. $count = $this->input->post('count',true);
  20. $page = empty($page) ? 1 : $page;
  21. $count = empty($count) ? 16 : $count;
  22. $project_id = intval($this->input->post('project_id',true));
  23. // $network_id = $this->input->post('network_id',true);
  24. // $lamp_id = $this->input->post('lamp_id',true);
  25. // $action = $this->input->post('action',true);
  26. $keyword = $this->input->post('keyword',true);
  27. $filter = [];
  28. if ($project_id > 0) $filter['projectid'] = $project_id;
  29. $field = "V.name as video_name,
  30. V.address,
  31. V.id,
  32. V.channel,
  33. V.devid,
  34. V.password,
  35. V.type,
  36. V.image";
  37. if (!empty($keyword)) {
  38. $filter['keyword'] = $keyword;
  39. }
  40. $accessToken = $this->get_accessToken();
  41. $total = $this->Videomonitor_model->getTotal($filter,$role,$userid,$companyid,$project_id);
  42. $data = $this->Videomonitor_model->get_list_by_role($role,$userid,$companyid,$field,$page,$count,$project_id,$filter);
  43. foreach ($data as &$v) {
  44. if (!empty($v['image'])) {
  45. $v['image'] = base_url($v['image']);
  46. }
  47. }
  48. json_result('0000',$this->response['0000'],array('list'=>$data,'total'=>ceil($total/$count),'accessToken'=>$accessToken));
  49. // if (empty($action)) { // (视屏监控页)带分页的列表
  50. // }else{ // (项目管理页)不带分页的列表
  51. // if ($network_id > 0) $filter['networkid'] = $network_id;
  52. // if ($lamp_id > 0) $filter['lampid'] = $lamp_id;
  53. // $data = $this->Videomonitor_model->get_list_by_filter($filter,$field);
  54. // json_result('0000',$this->response['0000'],array('video_list'=>$data));
  55. // }
  56. }
  57. // 获取萤石云accessToken
  58. private function get_accessToken(){
  59. $accessToken = $this->System_config_model->get_system_config('YSYAccessToken');
  60. $expireTime = $this->System_config_model->get_system_config('YSYAccessTokenExpireTime');
  61. if (empty($accessToken) || empty($expireTime) || strtotime($expireTime) < time()) {
  62. // token不存在或者token过期,重新获取token
  63. $url = 'https://open.ys7.com/api/lapp/token/get';
  64. $appKey = $this->config->config['YSYAppkey'];
  65. $appSecret = $this->config->config['YXYSecret'];
  66. $resData = request_post($url,array('appKey'=>$appKey,'appSecret'=>$appSecret));
  67. $resData = json_decode($resData);
  68. if ($resData->code != 200) {
  69. exit(json_result('0814',$this->response['0814'],array()));
  70. }
  71. // 获取token成功,记录token值与过期时间
  72. $accessToken = $resData->data->accessToken;
  73. $this->System_config_model->set_system_config('YSYAccessToken',$resData->data->accessToken);
  74. $expireTime = floor($resData->data->expireTime/1000) - 24*3600;
  75. $expireDate = date('Y-m-d H:i:s',$expireTime);
  76. $this->System_config_model->set_system_config('YSYAccessTokenExpireTime',$expireDate);
  77. }
  78. return $accessToken;
  79. }
  80. // 翻译提示信息
  81. private function translation_info($code){
  82. if (!isset($code) || empty($code)) {
  83. return 'Unknown error';
  84. }
  85. $data = array(
  86. '20002' => 'Equipment does not exist',
  87. '20007' => 'Equipment is not online',
  88. '20010' => 'Device verification code error',
  89. '20011' => 'Device add failure',
  90. '20013' => 'The equipment has been added to others',
  91. '20014' => 'DeviceSerial illegality',
  92. '20017' => 'The device has been added to its own',
  93. '10004' => 'User does not exist',
  94. '20018' => 'The user does not have the device',
  95. '20032' => 'The user\'s lower channel does not exist',
  96. '60020' => 'The command is not supported',
  97. '60061' => 'Account flow exceeded or not purchased, limit opening',
  98. '60060' => 'Unbound address',
  99. '49999' => 'Data anomaly',
  100. );
  101. return empty($data['code']) ? 'Unknown error' : $data['code'];
  102. }
  103. // 添加/编辑视频监控
  104. public function update(){
  105. $where['id'] = $this->input->post('id',true);
  106. $role = $this->get_user_info('role');
  107. $data['name'] = $this->input->post('name',true);
  108. $data['projectid'] = $this->input->post('projectid',true);
  109. $data['devid'] = $this->input->post('devid',true);
  110. $data['password'] = $this->input->post('password',true);
  111. $data['type'] = intval($this->input->post('type',true));
  112. $address = $this->input->post('address',true);
  113. $channel = $this->input->post('channel',true);
  114. $longitude = $this->input->post('longitude',true);
  115. $latitude = $this->input->post('latitude',true);
  116. if ($data['type'] == 0) {
  117. $data['channel'] = 1;
  118. }else{
  119. $data['channel'] = $channel;
  120. }
  121. if(isset($address)) $data['address'] = $address;
  122. if(isset($longitude)) $data['longitude'] = $longitude;
  123. if(isset($latitude)) $data['latitude'] = $latitude;
  124. if(empty($data['name'])) exit(json_result('0803',$this->response['0803'],array()));
  125. if(empty($data['projectid'])) exit(json_result('0308',$this->response['0308'],array()));
  126. if(empty($data['devid'])) exit(json_result('0808',$this->response['0808'],array()));
  127. if(empty($data['password'])) exit(json_result('0809',$this->response['0809'],array()));
  128. if($data['type'] < 0) exit(json_result('0810',$this->response['0810'],array()));
  129. if($data['type'] == 1 && empty($channel)) exit(json_result('0811',$this->response['0811'],array()));
  130. // 获取accessToken
  131. $data['devid'] = strtoupper($data['devid']);
  132. $accessToken = $this->get_accessToken();
  133. $version = $this->session->userdata('version');
  134. if (empty($where['id'])) { // 添加数据
  135. if ($this->Videomonitor_model->getDataCount(array('name'=>$data['name'],'projectid'=>$data['projectid']))) {
  136. exit(json_result('0801',$this->response['0801'],array()));
  137. }
  138. if ($data['type'] == 1 && $this->Videomonitor_model->getDataCount(array('devid'=>$data['devid'],'channel'=>$data['channel']))) {
  139. exit(json_result('0812',$this->response['0812'],array()));
  140. }
  141. //1. 添加设备
  142. $url = 'https://open.ys7.com/api/lapp/device/add';
  143. $res1 = request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'validateCode'=>$data['password']));
  144. $res1 = json_decode($res1);
  145. if ($res1->code != 200) {
  146. if (empty($version)) {
  147. exit(json_result('other',$res1->msg,array()));
  148. }else{
  149. exit(json_result('other',$this->translation_info($res1->code),array()));
  150. }
  151. }
  152. // 关闭视频加密
  153. $url = 'https://open.ys7.com/api/lapp/device/encrypt/off';
  154. request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'validateCode'=>$data['password']));
  155. // 修改设备名称
  156. $url = 'https://open.ys7.com/api/lapp/device/name/update';
  157. request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'deviceName'=>$data['name']));
  158. // 2.开通直播
  159. $url = 'https://open.ys7.com/api/lapp/live/video/open';
  160. $res2 = request_post($url,array('accessToken'=>$accessToken,'source'=>$data['devid'].':'.$data['channel']));
  161. $res2 = json_decode($res2);
  162. if ($res2->code != 200) {
  163. if (empty($version)) {
  164. exit(json_result('other',$res1->msg,array()));
  165. }else{
  166. exit(json_result('other',$this->translation_info($res1->code),array()));
  167. }
  168. }else{
  169. if ($res2->data[0]->ret != 200 && $res2->data[0]->ret != 60062) {
  170. if (empty($version)) {
  171. exit(json_result('other',$res2->data[0]->desc,array()));
  172. }else{
  173. exit(json_result('other',$this->translation_info($res2->data[0]->ret),array()));
  174. }
  175. }
  176. }
  177. // 3.获取播放地址
  178. $url = 'https://open.ys7.com/api/lapp/live/address/get';
  179. $res3 = request_post($url,array('accessToken'=>$accessToken,'source'=>$data['devid'].':'.$data['channel']));
  180. $res3 = json_decode($res3);
  181. if ($res3->code != 200) {
  182. if (empty($version)) {
  183. exit(json_result('other',$res3->msg,array()));
  184. }else{
  185. exit(json_result('other',$this->translation_info($res3->code),array()));
  186. }
  187. }
  188. $res = $res3->data[0];
  189. $data['hls'] = $res->hls;
  190. $data['hlsHd'] = $res->hlsHd;
  191. $data['rtmp'] = $res->rtmp;
  192. $data['rtmpHd'] = $res->rtmpHd;
  193. // 4.获取图片
  194. $url = 'https://open.ys7.com/api/lapp/device/capture';
  195. $res4 = request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'channelNo'=>$data['channel']));
  196. $res4 = json_decode($res4);
  197. if ($res4->code == 200) {
  198. $image = file_get_contents($res4->data->picUrl);
  199. $filename = FCPATH.'file/image/C24816636_'.date('YmdHis').'.jpg';
  200. $file = fopen($filename, 'w');
  201. fwrite($file, $image);
  202. fclose($file);
  203. $data['image'] = 'api/file/image/C24816636_'.date('YmdHis').'.jpg';
  204. }
  205. $data['createtime'] = date('Y-m-d H-i-s',time());
  206. $id = $this->Videomonitor_model->insert($data);
  207. $data['count'] = 1;
  208. // 设置项目跟网络的监控数
  209. $this->Project_model->add_monitor_count($data);
  210. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  211. $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目下添加视屏监控,监控名称\"{$data['name']}\"",0);
  212. $this->add_operation_log('insert',"Add video monitor.Monitor name:\"{$data['name']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  213. }else{ // 编辑数据
  214. if ($this->Videomonitor_model->getDataCount(array('name'=>$data['name'],'projectid'=>$data['projectid']),$where['id'])) {
  215. exit(json_result('0801',$this->response['0801'],array()));
  216. }
  217. if ($data['type'] == 1 && $this->Videomonitor_model->getDataCount(array('devid'=>$data['devid'],'channel'=>$data['channel']),$where['id'])) {
  218. exit(json_result('0812',$this->response['0812'],array()));
  219. }
  220. $oldData = $this->Videomonitor_model->getOne($where['id']);
  221. $this->Videomonitor_model->update(array('id'=>$where['id']),$data);
  222. $data['count'] = 1;
  223. $oldData['count'] = 1;
  224. if ($oldData['projectid'] != $data['projectid']) {
  225. $this->Project_model->add_monitor_count($data);
  226. $this->Project_model->minus_monitor_count($oldData);
  227. }
  228. if ($oldData['name'] != $data['name']) {
  229. $url = 'https://open.ys7.com/api/lapp/device/name/update';
  230. request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'deviceName'=>$data['name']));
  231. }
  232. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  233. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的视屏监控,监控名称\"{$data['name']}\"",0);
  234. $this->add_operation_log('update',"Update video monitor.Monitor name:\"{$data['name']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  235. $id = $where['id'];
  236. }
  237. exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
  238. }
  239. // 删除视频监控(支持批量操作)
  240. public function del(){
  241. $videoIds = $this->input->post('id',true);
  242. $role = $this->get_user_info('role');
  243. if (empty($videoIds)) {
  244. exit(json_result('0007',$this->response['0007'],array()));
  245. }
  246. $ids = explode(',', $videoIds);
  247. $data = $this->Videomonitor_model->get_list_by_filter(array('id'=>$ids),'V.id,V.projectid,V.devid,V.name,V.image');
  248. $url = 'https://open.ys7.com/api/lapp/device/delete';
  249. $accessToken = $this->get_accessToken();
  250. foreach ($data as $v) {
  251. @unlink($v['image']);
  252. // 删除萤石云上监控
  253. request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$v['devid']));
  254. $v['count'] = 1;
  255. $this->Project_model->minus_monitor_count($v);
  256. $projectData = $this->Project_model->getData(array('id'=>$v['projectid']),'projectname');
  257. $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目下的视屏监控,监控名称\"{$v['name']}\"",0);
  258. $this->add_operation_log('delete',"Delete video monitor.Monitor name:\"{$v['name']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  259. }
  260. $this->Videomonitor_model->delData(array('id'=>$ids));
  261. // 删除视频日志信息
  262. $this->Videomonitor_model->del_video_info_log(array('videoid'=>$ids));
  263. exit(json_result('0000',$this->response['0000'],array()));
  264. }
  265. // 视屏监控详情
  266. public function video_info(){
  267. $id = $this->input->post('id',true);
  268. if (empty($id)) {
  269. exit(json_result('0007',$this->response['0007'],array()));
  270. }
  271. $field = "id,name,address,type,projectid,devid,password,channel,status,longitude,latitude,hls,hlsHD,rtmp,rtmpHD,image";
  272. $data = $this->Videomonitor_model->getOne($id,$field);
  273. $data['accessToken'] = $this->get_accessToken();
  274. exit(json_result('0000',$this->response['0000'],$data));
  275. }
  276. // 播放视频监控
  277. public function video_play(){
  278. $id = $this->input->post('id',true);
  279. if (empty($id)) {
  280. exit(json_result('0007',$this->response['0007'],array()));
  281. }
  282. $field = "id,name,address,type,projectid,devid,password,channel,status,longitude,latitude,hls,hlsHD,rtmp,rtmpHD,image";
  283. $data = $this->Videomonitor_model->getOne($id,$field);
  284. // 4.获取图片
  285. $accessToken = $this->get_accessToken();
  286. $url = 'https://open.ys7.com/api/lapp/device/capture';
  287. $res4 = request_post($url,array('accessToken'=>$accessToken,'deviceSerial'=>$data['devid'],'channelNo'=>$data['channel']));
  288. $res4 = json_decode($res4);
  289. if ($res4->code == 200) {
  290. $image = file_get_contents($res4->data->picUrl);
  291. $filename = FCPATH.'file/image/C24816636_'.date('YmdHis').'.jpg';
  292. $file = fopen($filename, 'w');
  293. fwrite($file, $image);
  294. fclose($file);
  295. $image = 'api/file/image/C24816636_'.date('YmdHis').'.jpg';
  296. $this->Videomonitor_model->update(array('id'=>$id),array('image'=>$image));
  297. @unlink($data['image']);
  298. $data['image'] = $image;
  299. }
  300. $data['accessToken'] = $this->get_accessToken();
  301. exit(json_result('0000',$this->response['0000'],$data));
  302. }
  303. // 云台控制
  304. public function yun_start(){
  305. $deviceSerial = $this->input->post('deviceSerial',true);
  306. $channelNo = $this->input->post('channelNo',true);
  307. $direction = intval($this->input->post('direction',true));
  308. $speed = intval($this->input->post('speed',true));
  309. if (empty($channelNo) || empty($deviceSerial)) {
  310. exit(json_result('0007',$this->response['0007'],array()));
  311. }
  312. $accessToken = $this->get_accessToken();
  313. $version = $this->session->userdata('version');
  314. // 开始云台控制
  315. $url1 = 'https://open.ys7.com/api/lapp/device/ptz/start';
  316. $request_data = array('accessToken'=>$accessToken,'deviceSerial'=>$deviceSerial,'channelNo'=>$channelNo,'direction'=>$direction,'speed'=>$speed);
  317. // var_dump($request_data);
  318. $data = request_post($url1,$request_data);
  319. $data = json_decode($data);
  320. if ($data->code != '200') {
  321. if (empty($version)) {
  322. exit(json_result('other',$data->msg,array()));
  323. }else{
  324. exit(json_result('other',$this->translation_info($data->msg),array()));
  325. }
  326. }
  327. // sleep(1);
  328. // var_dump($data);
  329. // 停止云台控制
  330. // $url2 = 'https://open.ys7.com/api/lapp/device/ptz/stop';
  331. // $data2 = request_post($url2,array('accessToken'=>$accessToken,'deviceSerial'=>$deviceSerial,'channelNo'=>$channelNo,'direction'=>$direction));
  332. // $data2 = json_decode($data2);
  333. exit(json_result('0000',$this->response['0000'],array()));
  334. }
  335. public function yun_end(){
  336. $deviceSerial = $this->input->post('deviceSerial',true);
  337. $channelNo = $this->input->post('channelNo',true);
  338. $direction = intval($this->input->post('direction',true));
  339. $accessToken = $this->get_accessToken();
  340. $version = $this->session->userdata('version');
  341. if (empty($channelNo) || empty($deviceSerial)) {
  342. exit(json_result('0007',$this->response['0007'],array()));
  343. }
  344. $url2 = 'https://open.ys7.com/api/lapp/device/ptz/stop';
  345. $data2 = request_post($url2,array('accessToken'=>$accessToken,'deviceSerial'=>$deviceSerial,'channelNo'=>$channelNo,'direction'=>$direction));
  346. $data2 = json_decode($data2);
  347. if ($data2->code != 200) {
  348. if (empty($version)) {
  349. exit(json_result('other',$data2->msg,array()));
  350. }else{
  351. exit(json_result('other',$this->translation_info($data2->msg),array()));
  352. }
  353. }
  354. exit(json_result('0000',$this->response['0000'],array()));
  355. }
  356. }
  357. ?>