normalDay); } $keywords = $this->request->param('keywords'); $user_id=$this->userid; $where=array(); $whe=array('ID','>',0); if($user_id!=1){ $puser=$this->userList(); $where[]=array('PRIMARYADDR', 'in', $puser); $whe=array('PRIMARYADDR', 'in', $puser); } if(!empty($keywords)) $where[]=array('REPEATERNAME|STATIONNAME', 'like', "%$keywords%"); //站点信息 $index = Db::table('primarydev')->where($where)->select(); foreach ($index as $k => &$v) { $res = $this->bd_decrypt($v['DVICLONGITUDE'], $v['DVICLATITUDE']); $v['DVICLONGITUDE'] = $res['gg_lon']; $v['DVICLATITUDE'] = $res['gg_lat']; $v['status'] = 'normal'; $v['text'] = $v['STATIONNAME']; } $ALARMSTARTTIME =date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME'); $ALARMENDTIME = date('Y-m-d H:i:s'); $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME); $num = Db::table('reportdata')->where([['ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['ALARMENDTIME', '<', $ALARMENDTIME],$whe])->count(); $this->api_return_json(compact('index', 'datatime', 'num','user_id'), 1); } /** * @name * */ /* * @name 报警设备 * */ public function reportdata() { $PRIMARYADDR = $this->request->param('PRIMARYADDR'); $type = $this->request->param('type', 0); $page = $this->request->param('page', 0); if (!intval($this->userid)) { $this->api_return_json(array('msg' => '请重新登录'), 0); } // $this->api_return_json($this->request->param(),1); $ALARMSTARTTIME=date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME'); $ALARMENDTIME=date('Y-m-d H:i:s'); $t=0; if (isset($PRIMARYADDR) && intval($PRIMARYADDR)) { //如果为真查询关注报警设备 if ($type) { $where=array('p.ID','>',0); if($this->userid!=1){ $puser=$this->userList(); $where=array('p.PRIMARYADDR', 'in', $puser); } $t = 1; $reportdata = Db::table('follow') ->alias('f') ->where(array(array('f.UID', '=', $this->userid),$where)) ->join('primarydev p', 'f.PID=p.PRIMARYADDR', 'left') ->join('deviceinfo d', 'f.DID=d.DEVICEID and f.PID=d.PRIMARYADDR', 'left') ->field('p.STATIONNAME,d.*') ->page($page,$this->pages) ->select(); } else { //报警设备 查站点id下的 $reportdata = Db::table('reportdata') ->where(array(array('ALARMSTARTTIME', '>', $ALARMSTARTTIME),array('ALARMENDTIME', '<', $ALARMENDTIME), array('PRIMARYADDR', '=', $PRIMARYADDR))) ->page($page,$this->pages) ->order('ALARMSTARTTIME desc') ->select(); } } else { if ($type) { $where=array('p.ID','>',0); if($this->userid!=1){ $puser=$this->userList(); $where=array('p.PRIMARYADDR', 'in', $puser); } $t = 1; $reportdata = Db::table('follow') ->alias('f') ->where(array(array('f.UID', '=', $this->userid),$where)) ->join('primarydev p', 'f.PID=p.PRIMARYADDR', 'left') ->join('deviceinfo d', 'f.DID=d.DEVICEID and f.PID=d.PRIMARYADDR', 'left') ->field('p.STATIONNAME,d.*') ->page($page,$this->pages) ->select(); } else { $where=array('ID','>',0); if($this->userid!=1){ $puser=$this->userList(); $where=array('PRIMARYADDR', 'in', $puser); } //报警设备 查所有 $reportdata = Db::table('reportdata') ->where(array(array('ALARMSTARTTIME', '>', $ALARMSTARTTIME),array('ALARMENDTIME', '<', $ALARMENDTIME),$where)) ->page($page,$this->pages) ->order('ALARMSTARTTIME desc') ->select(); } echo Db::table('reportdata')->getlastSql(); } // echo Db::table('follow')->getLastSql(); $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃'); $sta = array('在线', '报警', '不在线', '未配置'); foreach ($reportdata as &$v) { if($t){ $report=Db::table('reportdata')->where([['DEVICEID','=',$v['DEVICEID']],['PRIMARYADDR','=',$v['PRIMARYADDR']]])->field('ALARMSTARTTIME,REPORTINFO')->order('ALARMSTARTTIME desc')->find(); $v['REPORTINFO']=$report['REPORTINFO']; $v['ALARMSTARTTIME']=$report['ALARMSTARTTIME']; } $realtimedata=Db::table('realtimedata')->where([['DEVICEID','=',$v['DEVICEID']],['PRIMARYADDR','=',$v['PRIMARYADDR']]])->field('ID,STATUS,LASTTRANSDATE')->order('ID desc')->find(); $v['LASTTRANSDATE']=$realtimedata['LASTTRANSDATE']; $v['TEMPERTHRESHOLD'] = $arr[$v['TEMPERTHRESHOLD']]; if (!isset($realtimedata['STATUS']) || $realtimedata['STATUS'] == null || $realtimedata['STATUS'] == 'null') { $v['statu']=3; $v['STATUS'] = $sta[3]; }else{ $v['statu']=$realtimedata['STATUS']; $v['STATUS'] = $sta[$realtimedata['STATUS']]; } $v['ALARMSTARTTIME'] = empty($v['ALARMSTARTTIME']) ? '无' : $v['ALARMSTARTTIME']; $v['REPORTINFO'] = !empty($v['REPORTINFO']) ? $v['REPORTINFO'] : '无'; //$v['ADDRESS'] = empty($v['STATIONNAME'])?"未命名":$v['STATIONNAME'] . empty($v['POWERDISCABNAME'])?"未命名":$v['POWERDISCABNAME'] . empty($v['CELLNAME'])?"未命名":$v['CELLNAME']; $v['ADDRESS'] = $v['STATIONNAME'] .$v['POWERDISCABNAME'] . $v['CELLNAME']; $v['DEVICENAME'] = $v['DEVICENAME'] ? $v['DEVICENAME'] : '未命名'; $res = Db::table('follow')->where([['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count(); $v['is_follow'] = $res ? 1 : 0; } $this->api_return_json(compact('reportdata'), 1); } /* * @name 删除 * * */ public function delete() { } /* * * @name 添加/编辑设备 * */ public function addequipment() { $param = $this->request->param(); if (!isset($param['PRIMARYADDR']) || empty($param['PRIMARYADDR'])) { $this->api_return_json(array('msg' => '请填写中继器ID')); } if (!isset($param['REPEATERNAME']) || empty($param['REPEATERNAME'])) { $this->api_return_json(array('msg' => '请填写中继器名称')); } if (!isset($param['STATIONNAME']) || empty($param['STATIONNAME'])) { $this->api_return_json(array('msg' => '请填写站点名称')); } if (!isset($param['DVICLONGITUDE']) || empty($param['DVICLONGITUDE'])) { $this->api_return_json(array('msg' => '请填写经度')); } if (!isset($param['DVICLATITUDE']) || empty($param['DVICLATITUDE'])) { $this->api_return_json(array('msg' => '请填写纬度')); } $data = array(); $data['PRIMARYADDR'] = $param['PRIMARYADDR']; $data['REPEATERNAME'] = $param['REPEATERNAME']; $data['STATIONNAME'] = $param['STATIONNAME']; $data['DVICLONGITUDE'] = $param['DVICLONGITUDE']; $data['DVICLATITUDE'] = $param['DVICLATITUDE']; //修改 if (!empty($param['ID'])) { $str="编辑"; $ID = $param['ID']; unset($param['ID']); $is_ok = Db::name('primarydev')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['ID', 'neq', $ID]])->count(); if ($is_ok) { $this->api_return_json(array('msg' => '中继器ID已经存在')); } $res = Db::name('primarydev')->where([['ID', '=', $ID]])->update($data); } else { $str="添加"; $is_ok = Db::name('primarydev')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']]])->count(); if ($is_ok) { $this->api_return_json(array('msg' => '中继器ID已经存在')); } $res = Db::name('primarydev')->insert($data); } if ($res) { $this->api_return_json(array('msg' => $str.'成功'), 1); } else $this->api_return_json(array('msg' => $str.'失败')); } /* * @name 编辑页面数据接口 * */ public function edit() { $ID = $this->request->param('ID'); $edit['PRIMARYADDR'] = ""; $edit['REPEATERNAME'] = ""; $edit['STATIONNAME'] = ""; $edit['DVICLONGITUDE'] = ""; $edit['DVICLATITUDE'] = ""; if ($ID) { $edit = Db::name('primarydev')->where([['ID', '=', $ID]])->find(); } $this->api_return_json(compact('edit'), 1); } /* * @name 添加时间段 * */ public function timeadd() { $day = $this->request->param('day'); //$ALARMENDTIME = $this->request->param('ALARMENDTIME'); if (!empty($day)) { cache('day',$day); //cache('ALARMENDTIME',$ALARMENDTIME); }else{ $this->api_return_json(array('msg' => '时间周期不能为空')); } $this->api_return_json(array('msg' => '设置时间周期成功'),1); } /* * @name 添加时间段 * */ public function datetime() { $day=cache('day'); //$ALARMENDTIME=cache('ALARMENDTIME'); $this->api_return_json(compact('day'), 1); } /* * * @name FPA列表 * */ public function deviceinfo() { $PRIMARYADDR = $this->request->param('PRIMARYADDR', 0); $page = $this->request->param('page', 1); $keywords=$this->request->param('keywords',''); $where=array(array('d.PRIMARYADDR', '=', $PRIMARYADDR)); if(!empty($keywords)) $where[]=array('POWERDISCABNAME|DEVICENAME|CELLNAME', 'like', "%$keywords%"); $deviceinfo = Db::table('deviceinfo') ->alias('d') ->where($where) ->join('realtimedata rt', 'rt.DEVICEID=d.DEVICEID and rt.PRIMARYADDR=d.PRIMARYADDR', 'left') ->field('d.*,rt.STATUS,rt.LASTTRANSDATE') ->page($page, $this->pages) ->select(); $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃'); $sta = array('在线', '报警', '不在线', '未配置'); $che = array('正常', '正在报警'); foreach ($deviceinfo as $k => &$v) { $v['statu']=$v['STATUS']; $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arr) ? $arr[(int)$v['TEMPERTHRESHOLD']] : '80℃'; $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']]; $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count(); $CHE = Db::table('reportdata')->where(array(array('ALARMENDTIME', '=', ''), array('PRIMARYADDR', '=', $v['PRIMARYADDR']), array('DEVICEID', '=', $v['DEVICEID'])))->count(); $v['CHECK'] = $che[$CHE]; $v['POWERDISCABNAME']=empty($v['POWERDISCABNAME'])?'未命名':$v['POWERDISCABNAME']; $v['DEVICENAME']=empty($v['DEVICENAME'])?'未命名':$v['DEVICENAME']; } $user_id=$this->userid; $num = Db::table('deviceinfo')->where(array('PRIMARYADDR' => $PRIMARYADDR))->count(); $this->api_return_json(compact('deviceinfo', 'num','user_id'), 1); } /* * @name 编辑页面数据接口 * */ public function deviceedit() { $ID = $this->request->param('ID'); $deviceedit['DEVICEID'] = ''; $deviceedit['DEVICENAME'] = ''; $deviceedit['TEMPERTHRESHOLD'] = ''; $deviceedit['CELLNAME'] = ''; $deviceedit['POWERDISCABVOLUME'] = ''; $deviceedit['TEMPERPATCHNUM'] = ''; $deviceedit['POWERDISCABNAME'] = ''; if ($ID) { $deviceedit = Db::name('deviceinfo')->where([['ID', '=', $ID]])->find(); } $this->api_return_json(compact('deviceedit'), 1); } /* * * @name FPA添加编辑 * */ public function deviceinfoinfo() { $param = $this->request->param(); if (!isset($param['DEVICEID']) || empty($param['DEVICEID'])) { $this->api_return_json(array('msg' => '请填写监测点ID')); } if (!isset($param['DEVICENAME']) || empty($param['DEVICENAME'])) { $this->api_return_json(array('msg' => '请填写FPA名称')); } if (!isset($param['TEMPERTHRESHOLD']) || empty($param['TEMPERTHRESHOLD'])) { $this->api_return_json(array('msg' => '请选择温度阈值')); } if (!isset($param['CELLNAME']) || empty($param['CELLNAME'])) { $this->api_return_json(array('msg' => '请填写小室名称')); } if (!isset($param['POWERDISCABVOLUME']) || empty($param['POWERDISCABVOLUME'])) { $this->api_return_json(array('msg' => '请填写开关柜小室体积')); } if (!isset($param['TEMPERPATCHNUM']) || empty($param['TEMPERPATCHNUM'])) { $this->api_return_json(array('msg' => '请填写感温贴片数量')); } //修改 $data['DEVICEID'] = $param['DEVICEID']; $data['DEVICENAME'] = $param['DEVICENAME']; $data['TEMPERTHRESHOLD'] = $param['TEMPERTHRESHOLD']; $data['CELLNAME'] = $param['CELLNAME']; $data['POWERDISCABVOLUME'] = $param['POWERDISCABVOLUME']; $data['TEMPERPATCHNUM'] = $param['TEMPERPATCHNUM']; $data['PRIMARYADDR'] = $param['PRIMARYADDR']; $data['POWERDISCABNAME'] = $param['POWERDISCABNAME']; if (!empty($param['ID'])) { $ID = $param['ID']; unset($param['ID']); $is_ok = Db::name('deviceinfo')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['DEVICEID', '=', $param['DEVICEID']], ['ID', 'neq', $ID]])->count(); if ($is_ok) { $this->api_return_json(array('msg' => '监测点ID不能重复')); } $str = '编辑'; $res = Db::name('deviceinfo')->where([['ID', '=', $ID]])->update($data); } else { $is_ok = Db::name('deviceinfo')->where([['PRIMARYADDR', '=', $param['PRIMARYADDR']], ['DEVICEID', '=', $param['DEVICEID']]])->count(); if ($is_ok) { $this->api_return_json(array('msg' => '监测点ID不能重复')); } $str = '添加'; $res = Db::name('deviceinfo')->insert($data); } if ($res) { $this->api_return_json(array('msg' => $str . '成功'), 1); } } /* * @name 数据页面列表 * */ public function datalist() { $page = $this->request->param('page'); $ALARMENDTIME = $this->request->param('ALARMENDTIME');//结束时间 $ALARMSTARTTIME = $this->request->param('ALARMSTARTTIME');//开始时间 $PRIMARYADDR = $this->request->param('PRIMARYADDR');//中继器ID if (empty($ALARMENDTIME)||empty($ALARMSTARTTIME)) { $ALARMENDTIME = date('Y-m-d H:i:s');//cache('ALARMENDTIME'); } if (empty($ALARMSTARTTIME)) { $ALARMSTARTTIME = date('Y-m-d H:i:s',time()-intval(empty(cache('day'))?$this->normalDay*24*3600:(int)cache('day')*24*3600));//cache('ALARMSTARTTIME'); } if ($PRIMARYADDR) { $where = ['re.PRIMARYADDR', '=', $PRIMARYADDR]; $where1 = ['PRIMARYADDR', '=', $PRIMARYADDR]; } else { $where = ['re.ID', '>', 0]; $where1 = ['ID', '>', 0]; if($this->userid!=1){ $puser=$this->userList(); $where=array('re.PRIMARYADDR', 'in', $puser); $where1 = array('PRIMARYADDR', 'in', $puser); } } $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME); $datalist = Db::table('reportdata') ->alias('re') ->where([['re.ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['re.ALARMENDTIME', '<', $ALARMENDTIME], $where]) ->join('realtimedata rt', 're.DEVICEID=rt.DEVICEID and re.PRIMARYADDR=rt.PRIMARYADDR', 'left') ->join('deviceinfo d', 're.DEVICEID=d.DEVICEID and re.PRIMARYADDR=d.PRIMARYADDR', 'left') ->field('d.*,re.ALARMSTARTTIME,re.ALARMENDTIME,re.REPORTINFO,re.STATIONNAME,rt.STATUS') ->order('ID desc') ->page($page, $this->pages) ->select(); $arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃'); $sta = array('在线', '报警', '不在线', '未配置'); //echo Db::table('reportdata')->getLastSql(); foreach ($datalist as $k => &$v) { $v['statu']=$v['STATUS']; $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arr) ? $arr[(int)$v['TEMPERTHRESHOLD']] : '80℃'; $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']]; $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count(); $v['ADDRESS'] = $v['STATIONNAME'] . $v['POWERDISCABNAME'] . $v['CELLNAME']; } $number =Db::table('reportdata')->where([['ALARMSTARTTIME', '>', $ALARMSTARTTIME], ['ALARMENDTIME', '<', $ALARMENDTIME], $where1])->count(); $this->api_return_json(compact('datalist', 'datatime', 'number'), 1); } /* * * @name 数据详情页 * * */ public function dataDetails() { $page = $this->request->param('page'); $index = $this->request->param('index',0); $ALARMENDTIME = $this->request->param('ALARMENDTIME');//结束时间 $ALARMSTARTTIME = $this->request->param('ALARMSTARTTIME');//开始时间 $PRIMARYADDR = $this->request->param('PRIMARYADDR');//中继器ID $DEVICEID = $this->request->param('DEVICEID');//报警监测点ID if (empty($ALARMENDTIME)) { $ALARMENDTIME = date('Y-m-d H:i:s');//cache('ALARMENDTIME'); } if (empty($ALARMSTARTTIME)) { $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'); } if (empty($PRIMARYADDR)||empty($DEVICEID)) { $this->api_return_json(array('msg' => '数据错误请重新打开。')); } //['re.ALARMSTARTTIME', '<', $ALARMSTARTTIME], ['re.ALARMENDTIME', '>', $ALARMENDTIME] $datatime = array('ALARMSTARTTIME' => $ALARMSTARTTIME, 'ALARMENDTIME' => $ALARMENDTIME); $dataDetails = Db::table('reportdata') ->alias('re') ->where([['re.PRIMARYADDR','=',$PRIMARYADDR],['re.DEVICEID','=',$DEVICEID]]) ->join('realtimedata rt', 're.DEVICEID=rt.DEVICEID and re.PRIMARYADDR=rt.PRIMARYADDR', 'left') ->join('deviceinfo d', 're.DEVICEID=d.DEVICEID and re.PRIMARYADDR=d.PRIMARYADDR', 'left') ->field('d.*,re.ALARMSTARTTIME,re.ALARMENDTIME,re.REPORTINFO,re.STATIONNAME,rt.STATUS') ->order('ALARMSTARTTIME desc') //->page($page, $this->pages) ->select(); //$arr = array(1 => '80℃', 2 => '100℃', 3 => '130℃'); $arrt = array(1 => 80, 2 => 100, 3 => 130); $sta = array('在线', '报警', '不在线', '未配置'); $xAxis=$yValue=array(); foreach ($dataDetails as $k => &$v) { if($k==$index){ $TEMPERTHRESHOLD=empty($v['TEMPERTHRESHOLD'])?0:$arrt[$v['TEMPERTHRESHOLD']]; if(!empty($v['ALARMSTARTTIME'])) { array_push($xAxis, date('H:i:s',strtotime($v['ALARMSTARTTIME']))); array_push($yValue, $TEMPERTHRESHOLD); } if(!empty($v['ALARMENDTIME'])) { array_push($xAxis, date('H:i:s',strtotime($v['ALARMENDTIME']))); array_push($yValue, 0); } } $v['statu']=$v['STATUS']; $v['TEMPERTHRESHOLD'] = array_key_exists((int)$v['TEMPERTHRESHOLD'], $arrt) ? $arrt[(int)$v['TEMPERTHRESHOLD']] : '80℃'; $v['STATUS'] = $sta[is_null($v['STATUS']) ? 3 : $v['STATUS']]; $v['is_follow'] = Db::table('follow')->where([['UID', '=', $this->userid], ['PID', '=', $v['PRIMARYADDR']], ['DID', '=', $v['DEVICEID']]])->count(); $v['ADDRESS'] = $v['STATIONNAME'] . $v['POWERDISCABNAME'] . $v['CELLNAME']; } if(count($yValue)==1){ array_push($yValue,$yValue[0]); }else{ array_push($yValue,0); } array_unshift($yValue,0); array_push($xAxis,'23:59:59'); array_unshift($xAxis,'00:00:00'); $data=array('xAxis'=>$xAxis,'yValue'=>$yValue); $this->api_return_json(compact('dataDetails', 'data'), 1); } //自动报警 public function autoBaojing(){ $where=array(array('ALARMENDTIME','=',''));//array('ALARMSTARTTIME','gt',date('Y-m-d').' 00:00:00') $realtimedata=Db::table('reportdata')->where($where)->select(); foreach($realtimedata as $val){ $PID=$val['PRIMARYADDR']; $res=Db::table('primarydev_user')->alias('l') ->join('mobpush m','m.user_id=l.UID','left') ->where(array('l.PID'=>$PID)) ->column('m.reg_id'); foreach($res as $k=>$v) if(empty($v)) unset($res[$k]); $admin_reg_id=Db::table('mobpush')->where(array('user_id'=>1))->column('reg_id'); $res=array_merge($admin_reg_id,$res); if(!empty($res)){ $this->sendPush($res); } } echo 'ok'; } public function recordRegId(){ $reg_id = $this->request->param('reg_id',''); if(empty($this->userid)) $this->api_return_json(array('msg'=>'登录过期')); if(empty($reg_id)) $this->api_return_json(array('msg'=>'no'),1); $res=Db::table('mobpush')->where(array('user_id'=>$this->userid,'reg_id'=>$reg_id))->find(); if(empty($res)) Db::table('mobpush')->insert(array('reg_id'=>$reg_id,'user_id'=>$this->userid)); $this->api_return_json(array('msg'=>'ok'),1); } function pushtest(){ $this->sendPush(['65kykswdtgny41s','65kykt1a27wv3ls']); } //消息推送 public function sendPush($reg_id){ $mobPushConfig = new MobPushConfig(); $mobPushConfig::$appkey = '31c1fc57d33bf'; $mobPushConfig::$appSecret = '754bac00d98fbcfaaf1dc77277e9ee27'; $pushV3Client=new PushV3Client(); /* Registration ID推送 */ $rep=$pushV3Client->pushByRids('', '报警通知', 'FIPRES 收到报警 信息一条,请前往查看', $reg_id); return $rep; } }