IndexController.php 24 KB

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