IndexController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. namespace app\web\controller;
  3. use think\Db;
  4. use app\common\plugin\Varbinary;
  5. use PhpOffice\PhpSpreadsheet\IOFactory;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  8. use PhpOffice\PhpSpreadsheet\Style\Border;
  9. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  10. use MobTech\MobPush\Config\MobPushConfig;
  11. use MobTech\MobPush\Client\Push\PushV3Client;
  12. /**
  13. * @author lyuis
  14. * 小程序首页相应功能类
  15. */
  16. class IndexController extends MainController
  17. {
  18. private $pages = 8;
  19. public $normalDay=7;//默认天数
  20. /*
  21. * @name 站点列表
  22. *
  23. * */
  24. public function index()
  25. {
  26. if(empty(cache('day'))){
  27. cache('day',$this->normalDay);
  28. }
  29. $keywords = $this->request->param('keywords');
  30. $user_id=$this->userid;
  31. $where=array();
  32. $whe=array('ID','>',0);
  33. if($user_id!=1){
  34. $puser=$this->userList();
  35. $where[]=array('PRIMARYADDR', 'in', $puser);
  36. $whe=array('PRIMARYADDR', 'in', $puser);
  37. }
  38. if(!empty($keywords))
  39. $where[]=array('REPEATERNAME|STATIONNAME', 'like', "%$keywords%");
  40. //站点信息
  41. $index = Db::table('primarydev')->where($where)->select();
  42. foreach ($index as $k => &$v) {
  43. $res = $this->bd_decrypt($v['DVICLONGITUDE'], $v['DVICLATITUDE']);
  44. $v['DVICLONGITUDE'] = $res['gg_lon'];
  45. $v['DVICLATITUDE'] = $res['gg_lat'];
  46. $v['status'] = 'normal';
  47. $v['text'] = $v['STATIONNAME'];
  48. }
  49. $ALARMSTARTTIME =date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME');
  50. $ALARMENDTIME = date('Y-m-d H:i:s');
  51. $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME);
  52. $num = Db::table('reportdata')->where([['ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['ALARMENDTIME', '<', $ALARMENDTIME],$whe])->count();
  53. $this->api_return_json(compact('index', 'datatime', 'num','user_id'), 1);
  54. }
  55. /**
  56. * @name
  57. * */
  58. /*
  59. * @name 报警设备
  60. * */
  61. public function reportdata()
  62. {
  63. $PRIMARYADDR = $this->request->param('PRIMARYADDR');
  64. $type = $this->request->param('type', 0);
  65. $page = $this->request->param('page', 0);
  66. if (!intval($this->userid)) {
  67. $this->api_return_json(array('msg' => '请重新登录'), 0);
  68. }
  69. // $this->api_return_json($this->request->param(),1);
  70. $ALARMSTARTTIME=date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME');
  71. $ALARMENDTIME=date('Y-m-d H:i:s');
  72. $t=0;
  73. if (isset($PRIMARYADDR) && intval($PRIMARYADDR)) {
  74. //如果为真查询关注报警设备
  75. if ($type) {
  76. $where=array('p.ID','>',0);
  77. if($this->userid!=1){
  78. $puser=$this->userList();
  79. $where=array('p.PRIMARYADDR', 'in', $puser);
  80. }
  81. $t = 1;
  82. $reportdata = Db::table('follow')
  83. ->alias('f')
  84. ->where(array(array('f.UID', '=', $this->userid),$where))
  85. ->join('primarydev p', 'f.PID=p.PRIMARYADDR', 'left')
  86. ->join('deviceinfo d', 'f.DID=d.DEVICEID and f.PID=d.PRIMARYADDR', 'left')
  87. ->field('p.STATIONNAME,d.*')
  88. ->page($page,$this->pages)
  89. ->select();
  90. } else {
  91. //报警设备 查站点id下的
  92. $reportdata = Db::table('reportdata')
  93. ->where(array(array('ALARMSTARTTIME', '>', $ALARMSTARTTIME),array('ALARMENDTIME', '<', $ALARMENDTIME), array('PRIMARYADDR', '=', $PRIMARYADDR)))
  94. ->page($page,$this->pages)
  95. ->order('ALARMSTARTTIME desc')
  96. ->select();
  97. }
  98. } else {
  99. if ($type) {
  100. $where=array('p.ID','>',0);
  101. if($this->userid!=1){
  102. $puser=$this->userList();
  103. $where=array('p.PRIMARYADDR', 'in', $puser);
  104. }
  105. $t = 1;
  106. $reportdata = Db::table('follow')
  107. ->alias('f')
  108. ->where(array(array('f.UID', '=', $this->userid),$where))
  109. ->join('primarydev p', 'f.PID=p.PRIMARYADDR', 'left')
  110. ->join('deviceinfo d', 'f.DID=d.DEVICEID and f.PID=d.PRIMARYADDR', 'left')
  111. ->field('p.STATIONNAME,d.*')
  112. ->page($page,$this->pages)
  113. ->select();
  114. } else {
  115. $where=array('ID','>',0);
  116. if($this->userid!=1){
  117. $puser=$this->userList();
  118. $where=array('PRIMARYADDR', 'in', $puser);
  119. }
  120. //报警设备 查所有
  121. $reportdata = Db::table('reportdata')
  122. ->where(array(array('ALARMSTARTTIME', '>', $ALARMSTARTTIME),array('ALARMENDTIME', '<', $ALARMENDTIME),$where))
  123. ->page($page,$this->pages)
  124. ->order('ALARMSTARTTIME desc')
  125. ->select();
  126. }
  127. echo Db::table('reportdata')->getlastSql();
  128. }
  129. // echo Db::table('follow')->getLastSql();
  130. $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃');
  131. $sta = array('在线', '报警', '不在线', '未配置');
  132. foreach ($reportdata as &$v) {
  133. if($t){
  134. $report=Db::table('reportdata')->where([['DEVICEID','=',$v['DEVICEID']],['PRIMARYADDR','=',$v['PRIMARYADDR']]])->field('ALARMSTARTTIME,REPORTINFO')->order('ALARMSTARTTIME desc')->find();
  135. $v['REPORTINFO']=$report['REPORTINFO'];
  136. $v['ALARMSTARTTIME']=$report['ALARMSTARTTIME'];
  137. }
  138. $realtimedata=Db::table('realtimedata')->where([['DEVICEID','=',$v['DEVICEID']],['PRIMARYADDR','=',$v['PRIMARYADDR']]])->field('ID,STATUS,LASTTRANSDATE')->order('ID desc')->find();
  139. $v['LASTTRANSDATE']=$realtimedata['LASTTRANSDATE'];
  140. $v['TEMPERTHRESHOLD'] = $arr[$v['TEMPERTHRESHOLD']];
  141. if (!isset($realtimedata['STATUS']) || $realtimedata['STATUS'] == null || $realtimedata['STATUS'] == 'null') {
  142. $v['statu']=3;
  143. $v['STATUS'] = $sta[3];
  144. }else{
  145. $v['statu']=$realtimedata['STATUS'];
  146. $v['STATUS'] = $sta[$realtimedata['STATUS']];
  147. }
  148. $v['ALARMSTARTTIME'] = empty($v['ALARMSTARTTIME']) ? '无' : $v['ALARMSTARTTIME'];
  149. $v['REPORTINFO'] = !empty($v['REPORTINFO']) ? $v['REPORTINFO'] : '无';
  150. //$v['ADDRESS'] = empty($v['STATIONNAME'])?"未命名":$v['STATIONNAME'] . empty($v['POWERDISCABNAME'])?"未命名":$v['POWERDISCABNAME'] . empty($v['CELLNAME'])?"未命名":$v['CELLNAME'];
  151. $v['ADDRESS'] = $v['STATIONNAME'] .$v['POWERDISCABNAME'] . $v['CELLNAME'];
  152. $v['DEVICENAME'] = $v['DEVICENAME'] ? $v['DEVICENAME'] : '未命名';
  153. $res = Db::table('follow')->where([['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count();
  154. $v['is_follow'] = $res ? 1 : 0;
  155. }
  156. $this->api_return_json(compact('reportdata'), 1);
  157. }
  158. /*
  159. * @name 删除
  160. *
  161. * */
  162. public function delete()
  163. {
  164. }
  165. /*
  166. *
  167. * @name 添加/编辑设备
  168. * */
  169. public function addequipment()
  170. {
  171. $param = $this->request->param();
  172. if (!isset($param['PRIMARYADDR']) || empty($param['PRIMARYADDR'])) {
  173. $this->api_return_json(array('msg' => '请填写中继器ID'));
  174. }
  175. if (!isset($param['REPEATERNAME']) || empty($param['REPEATERNAME'])) {
  176. $this->api_return_json(array('msg' => '请填写中继器名称'));
  177. }
  178. if (!isset($param['STATIONNAME']) || empty($param['STATIONNAME'])) {
  179. $this->api_return_json(array('msg' => '请填写站点名称'));
  180. }
  181. if (!isset($param['DVICLONGITUDE']) || empty($param['DVICLONGITUDE'])) {
  182. $this->api_return_json(array('msg' => '请填写经度'));
  183. }
  184. if (!isset($param['DVICLATITUDE']) || empty($param['DVICLATITUDE'])) {
  185. $this->api_return_json(array('msg' => '请填写纬度'));
  186. }
  187. $data = array();
  188. $data['PRIMARYADDR'] = $param['PRIMARYADDR'];
  189. $data['REPEATERNAME'] = $param['REPEATERNAME'];
  190. $data['STATIONNAME'] = $param['STATIONNAME'];
  191. $data['DVICLONGITUDE'] = $param['DVICLONGITUDE'];
  192. $data['DVICLATITUDE'] = $param['DVICLATITUDE'];
  193. //修改
  194. if (!empty($param['ID'])) {
  195. $str="编辑";
  196. $ID = $param['ID'];
  197. unset($param['ID']);
  198. $is_ok = Db::name('primarydev')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['ID', 'neq', $ID]])->count();
  199. if ($is_ok) {
  200. $this->api_return_json(array('msg' => '中继器ID已经存在'));
  201. }
  202. $res = Db::name('primarydev')->where([['ID', '=', $ID]])->update($data);
  203. } else {
  204. $str="添加";
  205. $is_ok = Db::name('primarydev')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']]])->count();
  206. if ($is_ok) {
  207. $this->api_return_json(array('msg' => '中继器ID已经存在'));
  208. }
  209. $res = Db::name('primarydev')->insert($data);
  210. }
  211. if ($res) {
  212. $this->api_return_json(array('msg' => $str.'成功'), 1);
  213. } else
  214. $this->api_return_json(array('msg' => $str.'失败'));
  215. }
  216. /*
  217. * @name 编辑页面数据接口
  218. * */
  219. public function edit()
  220. {
  221. $ID = $this->request->param('ID');
  222. $edit['PRIMARYADDR'] = "";
  223. $edit['REPEATERNAME'] = "";
  224. $edit['STATIONNAME'] = "";
  225. $edit['DVICLONGITUDE'] = "";
  226. $edit['DVICLATITUDE'] = "";
  227. if ($ID) {
  228. $edit = Db::name('primarydev')->where([['ID', '=', $ID]])->find();
  229. }
  230. $this->api_return_json(compact('edit'), 1);
  231. }
  232. /*
  233. * @name 添加时间段
  234. * */
  235. public function timeadd()
  236. {
  237. $day = $this->request->param('day');
  238. //$ALARMENDTIME = $this->request->param('ALARMENDTIME');
  239. if (!empty($day)) {
  240. cache('day',$day);
  241. //cache('ALARMENDTIME',$ALARMENDTIME);
  242. }else{
  243. $this->api_return_json(array('msg' => '时间周期不能为空'));
  244. }
  245. $this->api_return_json(array('msg' => '设置时间周期成功'),1);
  246. }
  247. /*
  248. * @name 添加时间段
  249. * */
  250. public function datetime()
  251. {
  252. $day=cache('day');
  253. //$ALARMENDTIME=cache('ALARMENDTIME');
  254. $this->api_return_json(compact('day'), 1);
  255. }
  256. /*
  257. *
  258. * @name FPA列表
  259. * */
  260. public function deviceinfo()
  261. {
  262. $PRIMARYADDR = $this->request->param('PRIMARYADDR', 0);
  263. $page = $this->request->param('page', 1);
  264. $keywords=$this->request->param('keywords','');
  265. $where=array(array('d.PRIMARYADDR', '=', $PRIMARYADDR));
  266. if(!empty($keywords))
  267. $where[]=array('POWERDISCABNAME|DEVICENAME|CELLNAME', 'like', "%$keywords%");
  268. $deviceinfo = Db::table('deviceinfo')
  269. ->alias('d')
  270. ->where($where)
  271. ->join('realtimedata rt', 'rt.DEVICEID=d.DEVICEID and rt.PRIMARYADDR=d.PRIMARYADDR', 'left')
  272. ->field('d.*,rt.STATUS,rt.LASTTRANSDATE')
  273. ->page($page, $this->pages)
  274. ->select();
  275. $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃');
  276. $sta = array('在线', '报警', '不在线', '未配置');
  277. $che = array('正常', '正在报警');
  278. foreach ($deviceinfo as $k => &$v) {
  279. $v['statu']=$v['STATUS'];
  280. $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arr) ? $arr[(int)$v['TEMPERTHRESHOLD']] : '80℃';
  281. $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']];
  282. $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count();
  283. $CHE = Db::table('reportdata')->where(array(array('ALARMENDTIME', '=', ''), array('PRIMARYADDR', '=', $v['PRIMARYADDR']), array('DEVICEID', '=', $v['DEVICEID'])))->count();
  284. $v['CHECK'] = $che[$CHE];
  285. $v['POWERDISCABNAME']=empty($v['POWERDISCABNAME'])?'未命名':$v['POWERDISCABNAME'];
  286. $v['DEVICENAME']=empty($v['DEVICENAME'])?'未命名':$v['DEVICENAME'];
  287. }
  288. $user_id=$this->userid;
  289. $num = Db::table('deviceinfo')->where(array('PRIMARYADDR' => $PRIMARYADDR))->count();
  290. $this->api_return_json(compact('deviceinfo', 'num','user_id'), 1);
  291. }
  292. /*
  293. * @name 编辑页面数据接口
  294. * */
  295. public function deviceedit()
  296. {
  297. $ID = $this->request->param('ID');
  298. $deviceedit['DEVICEID'] = '';
  299. $deviceedit['DEVICENAME'] = '';
  300. $deviceedit['TEMPERTHRESHOLD'] = '';
  301. $deviceedit['CELLNAME'] = '';
  302. $deviceedit['POWERDISCABVOLUME'] = '';
  303. $deviceedit['TEMPERPATCHNUM'] = '';
  304. $deviceedit['POWERDISCABNAME'] = '';
  305. if ($ID) {
  306. $deviceedit = Db::name('deviceinfo')->where([['ID', '=', $ID]])->find();
  307. }
  308. $this->api_return_json(compact('deviceedit'), 1);
  309. }
  310. /*
  311. *
  312. * @name FPA添加编辑
  313. * */
  314. public function deviceinfoinfo()
  315. {
  316. $param = $this->request->param();
  317. if (!isset($param['DEVICEID']) || empty($param['DEVICEID'])) {
  318. $this->api_return_json(array('msg' => '请填写监测点ID'));
  319. }
  320. if (!isset($param['DEVICENAME']) || empty($param['DEVICENAME'])) {
  321. $this->api_return_json(array('msg' => '请填写FPA名称'));
  322. }
  323. if (!isset($param['TEMPERTHRESHOLD']) || empty($param['TEMPERTHRESHOLD'])) {
  324. $this->api_return_json(array('msg' => '请选择温度阈值'));
  325. }
  326. if (!isset($param['CELLNAME']) || empty($param['CELLNAME'])) {
  327. $this->api_return_json(array('msg' => '请填写小室名称'));
  328. }
  329. if (!isset($param['POWERDISCABVOLUME']) || empty($param['POWERDISCABVOLUME'])) {
  330. $this->api_return_json(array('msg' => '请填写开关柜小室体积'));
  331. }
  332. if (!isset($param['TEMPERPATCHNUM']) || empty($param['TEMPERPATCHNUM'])) {
  333. $this->api_return_json(array('msg' => '请填写感温贴片数量'));
  334. }
  335. //修改
  336. $data['DEVICEID'] = $param['DEVICEID'];
  337. $data['DEVICENAME'] = $param['DEVICENAME'];
  338. $data['TEMPERTHRESHOLD'] = $param['TEMPERTHRESHOLD'];
  339. $data['CELLNAME'] = $param['CELLNAME'];
  340. $data['POWERDISCABVOLUME'] = $param['POWERDISCABVOLUME'];
  341. $data['TEMPERPATCHNUM'] = $param['TEMPERPATCHNUM'];
  342. $data['PRIMARYADDR'] = $param['PRIMARYADDR'];
  343. $data['POWERDISCABNAME'] = $param['POWERDISCABNAME'];
  344. if (!empty($param['ID'])) {
  345. $ID = $param['ID'];
  346. unset($param['ID']);
  347. $is_ok = Db::name('deviceinfo')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['DEVICEID', '=', $param['DEVICEID']], ['ID', 'neq', $ID]])->count();
  348. if ($is_ok) {
  349. $this->api_return_json(array('msg' => '监测点ID不能重复'));
  350. }
  351. $str = '编辑';
  352. $res = Db::name('deviceinfo')->where([['ID', '=', $ID]])->update($data);
  353. } else {
  354. $is_ok = Db::name('deviceinfo')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['DEVICEID', '=', $param['DEVICEID']]])->count();
  355. if ($is_ok) {
  356. $this->api_return_json(array('msg' => '监测点ID不能重复'));
  357. }
  358. $str = '添加';
  359. $res = Db::name('deviceinfo')->insert($data);
  360. }
  361. if ($res) {
  362. $this->api_return_json(array('msg' => $str . '成功'), 1);
  363. }
  364. }
  365. /*
  366. * @name 数据页面列表
  367. * */
  368. public function datalist()
  369. {
  370. $page = $this->request->param('page');
  371. $ALARMENDTIME = $this->request->param('ALARMENDTIME');//结束时间
  372. $ALARMSTARTTIME = $this->request->param('ALARMSTARTTIME');//开始时间
  373. $PRIMARYADDR = $this->request->param('PRIMARYADDR');//中继器ID
  374. if (empty($ALARMENDTIME)||empty($ALARMSTARTTIME)) {
  375. $ALARMENDTIME = date('Y-m-d H:i:s');//cache('ALARMENDTIME');
  376. }
  377. if (empty($ALARMSTARTTIME)) {
  378. $ALARMSTARTTIME = date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME');
  379. }
  380. if ($PRIMARYADDR) {
  381. $where = ['re.PRIMARYADDR', '=', $PRIMARYADDR];
  382. $where1 = ['PRIMARYADDR', '=', $PRIMARYADDR];
  383. } else {
  384. $where = ['re.ID', '>', 0];
  385. $where1 = ['ID', '>', 0];
  386. if($this->userid!=1){
  387. $puser=$this->userList();
  388. $where=array('re.PRIMARYADDR', 'in', $puser);
  389. $where1 = array('PRIMARYADDR', 'in', $puser);
  390. }
  391. }
  392. $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME);
  393. $datalist = Db::table('reportdata')
  394. ->alias('re')
  395. ->where([['re.ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['re.ALARMENDTIME', '<', $ALARMENDTIME], $where])
  396. ->join('realtimedata rt', 're.DEVICEID=rt.DEVICEID and re.PRIMARYADDR=rt.PRIMARYADDR', 'left')
  397. ->join('deviceinfo d', 're.DEVICEID=d.DEVICEID and re.PRIMARYADDR=d.PRIMARYADDR', 'left')
  398. ->field('d.*,re.ALARMSTARTTIME,re.ALARMENDTIME,re.REPORTINFO,re.STATIONNAME,rt.STATUS')
  399. ->order('ID desc')
  400. ->page($page, $this->pages)
  401. ->select();
  402. $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃');
  403. $sta = array('在线', '报警', '不在线', '未配置');
  404. //echo Db::table('reportdata')->getLastSql();
  405. foreach ($datalist as $k => &$v) {
  406. $v['statu']=$v['STATUS'];
  407. $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arr) ? $arr[(int)$v['TEMPERTHRESHOLD']] : '80℃';
  408. $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']];
  409. $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count();
  410. $v['ADDRESS'] = $v['STATIONNAME'] . $v['POWERDISCABNAME'] . $v['CELLNAME'];
  411. }
  412. $number =Db::table('reportdata')->where([['ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['ALARMENDTIME', '<', $ALARMENDTIME], $where1])->count();
  413. $this->api_return_json(compact('datalist', 'datatime', 'number'), 1);
  414. }
  415. /*
  416. *
  417. * @name 数据详情页
  418. *
  419. * */
  420. public function dataDetails()
  421. {
  422. $page = $this->request->param('page');
  423. $index = $this->request->param('index',0);
  424. $ALARMENDTIME = $this->request->param('ALARMENDTIME');//结束时间
  425. $ALARMSTARTTIME = $this->request->param('ALARMSTARTTIME');//开始时间
  426. $PRIMARYADDR = $this->request->param('PRIMARYADDR');//中继器ID
  427. $DEVICEID = $this->request->param('DEVICEID');//报警监测点ID
  428. if (empty($ALARMENDTIME)) {
  429. $ALARMENDTIME = date('Y-m-d H:i:s');//cache('ALARMENDTIME');
  430. }
  431. if (empty($ALARMSTARTTIME)) {
  432. $ALARMSTARTTIME = date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME');//cache('ALARMSTARTTIME');
  433. }
  434. if (empty($PRIMARYADDR)||empty($DEVICEID)) {
  435. $this->api_return_json(array('msg' => '数据错误请重新打开。'));
  436. }
  437. //['re.ALARMSTARTTIME', '<', $ALARMSTARTTIME], ['re.ALARMENDTIME', '>', $ALARMENDTIME]
  438. $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME);
  439. $dataDetails = Db::table('reportdata')
  440. ->alias('re')
  441. ->where([['re.PRIMARYADDR','=',$PRIMARYADDR],['re.DEVICEID','=',$DEVICEID]])
  442. ->join('realtimedata rt', 're.DEVICEID=rt.DEVICEID and re.PRIMARYADDR=rt.PRIMARYADDR', 'left')
  443. ->join('deviceinfo d', 're.DEVICEID=d.DEVICEID and re.PRIMARYADDR=d.PRIMARYADDR', 'left')
  444. ->field('d.*,re.ALARMSTARTTIME,re.ALARMENDTIME,re.REPORTINFO,re.STATIONNAME,rt.STATUS')
  445. ->order('ALARMSTARTTIME desc')
  446. //->page($page, $this->pages)
  447. ->select();
  448. //$arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃');
  449. $arrt = array(1 => 80, 2 => 100, 3 => 130);
  450. $sta = array('在线', '报警', '不在线', '未配置');
  451. $xAxis=$yValue=array();
  452. foreach ($dataDetails as $k => &$v) {
  453. if($k==$index){
  454. $TEMPERTHRESHOLD=empty($v['TEMPERTHRESHOLD'])?0:$arrt[$v['TEMPERTHRESHOLD']];
  455. if(!empty($v['ALARMSTARTTIME'])) {
  456. array_push($xAxis, date('H:i:s',strtotime($v['ALARMSTARTTIME'])));
  457. array_push($yValue, $TEMPERTHRESHOLD);
  458. }
  459. if(!empty($v['ALARMENDTIME'])) {
  460. array_push($xAxis, date('H:i:s',strtotime($v['ALARMENDTIME'])));
  461. array_push($yValue, 0);
  462. }
  463. }
  464. $v['statu']=$v['STATUS'];
  465. $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arrt) ? $arrt[(int)$v['TEMPERTHRESHOLD']] : '80℃';
  466. $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']];
  467. $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count();
  468. $v['ADDRESS'] = $v['STATIONNAME'] . $v['POWERDISCABNAME'] . $v['CELLNAME'];
  469. }
  470. if(count($yValue)==1){
  471. array_push($yValue,$yValue[0]);
  472. }else{
  473. array_push($yValue,0);
  474. }
  475. array_unshift($yValue,0);
  476. array_push($xAxis,'23:59:59');
  477. array_unshift($xAxis,'00:00:00');
  478. $data=array('xAxis'=>$xAxis,'yValue'=>$yValue);
  479. $this->api_return_json(compact('dataDetails', 'data'), 1);
  480. }
  481. //自动报警
  482. public function autoBaojing(){
  483. $where=array(array('ALARMENDTIME','=',''));//array('ALARMSTARTTIME','gt',date('Y-m-d').' 00:00:00')
  484. $realtimedata=Db::table('reportdata')->where($where)->select();
  485. foreach($realtimedata as $val){
  486. $PID=$val['PRIMARYADDR'];
  487. $res=Db::table('primarydev_user')->alias('l')
  488. ->join('mobpush m','m.user_id=l.UID','left')
  489. ->where(array('l.PID'=>$PID))
  490. ->column('m.reg_id');
  491. foreach($res as $k=>$v)
  492. if(empty($v))
  493. unset($res[$k]);
  494. $admin_reg_id=Db::table('mobpush')->where(array('user_id'=>1))->column('reg_id');
  495. $res=array_merge($admin_reg_id,$res);
  496. if(!empty($res)){
  497. $this->sendPush($res);
  498. }
  499. }
  500. echo 'ok';
  501. }
  502. public function recordRegId(){
  503. $reg_id = $this->request->param('reg_id','');
  504. if(empty($this->userid))
  505. $this->api_return_json(array('msg'=>'登录过期'));
  506. if(empty($reg_id))
  507. $this->api_return_json(array('msg'=>'no'),1);
  508. $res=Db::table('mobpush')->where(array('user_id'=>$this->userid,'reg_id'=>$reg_id))->find();
  509. if(empty($res))
  510. Db::table('mobpush')->insert(array('reg_id'=>$reg_id,'user_id'=>$this->userid));
  511. $this->api_return_json(array('msg'=>'ok'),1);
  512. }
  513. function pushtest(){
  514. $this->sendPush(['65kykswdtgny41s','65kykt1a27wv3ls']);
  515. }
  516. //消息推送
  517. public function sendPush($reg_id){
  518. $mobPushConfig = new MobPushConfig();
  519. $mobPushConfig::$appkey = '31c1fc57d33bf';
  520. $mobPushConfig::$appSecret = '754bac00d98fbcfaaf1dc77277e9ee27';
  521. $pushV3Client=new PushV3Client();
  522. /* Registration ID推送 */
  523. $rep=$pushV3Client->pushByRids('', '报警通知', 'FIPRES 收到报警
  524. 信息一条,请前往查看', $reg_id);
  525. return $rep;
  526. }
  527. }