123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- <?php
- namespace app\api\controller\ue;
- use app\common\model\EnergyParam;
- use app\hander\HelpHander;
- use think\Controller;
- use think\Db;
- class Energy extends Base {
- // 首页统计
- public function sytj(){
- $deviceCount = Db::name('energy_device')->where('org_id',$this->orgId)->where('del',0)->count(); // 设备总数
- $deviceCount1 = Db::name('energy_device')->where('org_id',$this->orgId)->where('del',0)->where('status',1)->count(); // 在线数
- // 今日消耗
- $day = date('Y-m-d');
- $degree = (new EnergyParam())->getDegreeByDay($this->orgId,$day);
- // 昨日消耗
- $yday = date('Y-m-d',time() - 24*60*60);
- $ydegree = (new EnergyParam())->getDegreeByDay($this->orgId,$yday);
- // 实时功率
- $power = (new EnergyParam())->getPower($this->orgId);
- $data = [
- 'deviceCount' => $deviceCount,
- 'deviceCount1' => $deviceCount1,
- 'degree' => round($degree,2),
- 'ydegree' => round($ydegree,2),
- 'power' => round($power,2),
- ];
- HelpHander::success($data);
- }
- // 配电房数据
- public function room(){
- $rooms = Db::name('energy_room')->where('org_id',$this->orgId)->where('del',0)->field('id,title,ueid')->select();
- foreach ($rooms as $k=>$v){
- $temp = '';
- $humi = '';
- // 温湿度设备
- $wsddevice = Db::name('energy_device')->where('org_id',$this->orgId)->where('del',0)->where('cate',3)->find();
- if($wsddevice){
- $wsdparam = Db::name('energy_param')->where('device_sn',$wsddevice['sn'])->order('report_time desc')->find();
- if($wsdparam){ // {"Temp":"23.500000","Humi":"49.000000"}
- $content = json_decode($wsdparam['content'],true);
- if(isset($content['Temp'])){
- $temp = $content['Temp'];
- }
- if(isset($content['Temp'])){
- $humi = $content['Humi'];
- }
- }
- }
- $rooms[$k]['temp'] = $temp;
- $rooms[$k]['humi'] = $humi;
- // 开关量设备
- $door = "";
- $smoke = '';
- $water1 = '';
- $water2 = '';
- $dalarms = [];
- $kgdevice = Db::name('energy_device')->where('org_id',$this->orgId)->where('del',0)->where('cate',4)->find();
- if($kgdevice){
- $kgparam = Db::name('energy_param')->where('device_sn',$kgdevice['sn'])->order('report_time desc')->select();
- if($kgparam){ //{"遥信3":"0","遥信2":"1","遥信1":"1","遥信8":"1","遥信7":"1","遥信6":"1","遥信5":"1","遥信4":"1"}
- $content = json_decode($wsdparam['content'],true);
- if(isset($content['遥信1'])){
- $water1 = $content['遥信1'] == 1?"有":"无";
- }
- if(isset($content['遥信2'])){
- $water2 = $content['遥信2'] == 1?"有":"无";
- }
- if(isset($content['遥信3'])){
- $smoke = $content['遥信3'] == 1?"有":"无";
- }
- if(isset($content['遥信4'])){
- $door = $content['遥信4'] == 1?"打开":"关闭";
- }
- }
- $dalarms = Db::name('energy_alarm')->where('alarm_type',"状态变化")->field('id,alarm_time as report_time,current_value')->order('alarm_time desc')->limit(7)->select();
- foreach ($dalarms as $kk=>$vv){
- $dalarms[$kk]['current_value'] = "关闭";
- if($vv['current_value'] == 1){
- $dalarms[$kk]['current_value'] = "打开";
- }
- }
- }
- $rooms[$k]['dalarms'] = $dalarms;
- $rooms[$k]['door'] = $door;
- $rooms[$k]['smoke'] = $smoke;
- $rooms[$k]['water1'] = $water1;
- $rooms[$k]['water2'] = $water2;
- }
- HelpHander::success($rooms);
- }
- // 楼栋日能耗分析
- public function addrs(){
- $addrs = Db::name('energy_address')
- ->where('org_id',$this->orgId)
- ->where('pid',0)
- ->where('enable',1)
- ->where('del',0)
- ->select();
- $day = date('Y-m-d');
- $x = [];
- $y = [];
- foreach ($addrs as $k=>$v){
- $x[] = $v['title'];
- $degree = 0;
- // 获取所有子级
- $cids = Db::name('energy_address')
- ->where('pid',$v['id'])
- ->where('enable',1)
- ->where('del',0)
- ->column('id');
- if($cids){
- $cids2 = Db::name('energy_address')
- ->where('pid','in',$cids)
- ->where('enable',1)
- ->where('del',0)
- ->column('id');
- if($cids2){
- $cids = array_merge($cids,$cids2);
- }
- }
- $cids[] = $v['id'];
- // 获取子级所有电表设备
- $devices = Db::name('energy_device')
- ->where('del',0)
- ->where('cate',1)
- ->where('address_id','in',$cids)
- ->column('sn');
- if($devices){
- $degree = (new EnergyParam())->getDegreeByDay($this->orgId,$day,$devices);
- }
- $y[] = round($degree,2);
- }
- HelpHander::success(['x'=>$x,'y'=>$y]);
- }
- // 总能耗逐时分析
- public function hours(){
- $x = [];
- $y1 = [];
- $y2 = [];
- $hours = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
- $day = date('Y-m-d');
- $yday = date('Y-m-d',time()-24*60*60);
- foreach ($hours as $k=>$v){
- $x[] = $v;
- $degree1 = (new EnergyParam())->getDegreeByHour($this->orgId,$day,$v,[]);
- $y1[] = round($degree1,2);
- $degree2 = (new EnergyParam())->getDegreeByHour($this->orgId,$yday,$v,[]);
- $y2[] = round($degree2,2);
- }
- HelpHander::success(['x'=>$x,'y1'=>$y1,'y2'=>$y2]);
- }
- // 待处理报警记录
- public function alarms(){
- $lists = Db::name('energy_alarm_event')
- ->alias('a')
- ->join('energy_alarm b','a.alarm_id = b.id')
- ->where('b.org_id',$this->orgId)
- ->where('a.status','in',[0,1])
- ->field('a.id,a.status,a.alarm_id,b.device_sn,b.alarm_time,b.point_label,b.alarm_type,b.current_value,b.setting_value')
- ->order('a.id desc')
- ->select();
- $rooms = [];
- $roomIds = [];
- foreach ($lists as $k=>$v){
- $lists[$k]['room_id'] = 0;
- $lists[$k]['content'] = "";
- $lists[$k]['cate_title'] = "";
- $lists[$k]['room_title'] = "";
- $room = Db::name('energy_device')
- ->alias('a')
- ->join('energy_room c','c.id = a.room_id')
- ->join('energy_device_cate d','d.id = a.cate','left')
- ->where('a.sn',$v['device_sn'])
- ->field('c.id,c.title,c.ueid,a.title as device_title,d.title as cate_title')
- ->find();
- if($room){
- $lists[$k]['room_id'] = $room['id'];
- $lists[$k]['content'] = $room['device_title']."-".$v['point_label']."-当前值:".$v['current_value'];
- $lists[$k]['cate_title'] = $room['cate_title'];
- $lists[$k]['room_title'] = $room['title'];
- if(!in_array($room['id'],$roomIds)){
- $roomIds[] = $room['id'];
- $rooms[] = [
- 'id' => $room['id'],
- 'title' => $room['title'],
- 'ueid' => $room['ueid']
- ];
- }
- }
- }
- $nrooms = [];
- foreach ($rooms as $k=>$v){
- $garr = [
- 'alarm_type' => "高限报警",
- 'count' => 0,
- 'records' => []
- ];
- $darr = [
- 'alarm_type' => "低限报警",
- 'count' => 0,
- 'records' => []
- ];
- $grecords = [];
- $drecords = [];
- foreach ($lists as $kk=>$vv){
- if($vv['room_id'] == $v['id']){
- if($vv['alarm_type'] == $garr['alarm_type']){
- $grecords[] = [
- "id" => $vv['id'],
- "status" => $vv['status'],
- "alarm_type" => $vv['alarm_type'],
- "room_title" => $vv['room_title'],
- "cate_title" => $vv['cate_title'],
- "content" => $vv['content'],
- "alarm_time" => $vv['alarm_time']
- ];
- }
- if($vv['alarm_type'] == $darr['alarm_type']){
- $drecords[] = [
- "id" => $vv['id'],
- "status" => $vv['status'],
- "alarm_type" => $vv['alarm_type'],
- "room_title" => $vv['room_title'],
- "cate_title" => $vv['cate_title'],
- "content" => $vv['content'],
- "alarm_time" => $vv['alarm_time']
- ];
- }
- }
- }
- $garr['count'] = count($grecords);
- $garr['records'] = $grecords;
- $v['garr'] = $garr;
- $darr['count'] = count($drecords);
- $darr['records'] = $drecords;
- $v['darr'] = $darr;
- $v['count'] = $garr['count'] + $darr['count'];
- if($v['count'] > 0){
- $nrooms[] = $v;
- }
- }
- HelpHander::success($nrooms);
- }
- // 处理报警
- public function dealAlarm(){
- $id = input('id/d',0);
- $status = input('status/d',0);
- $reason = input('reason','','trim');
- if($status != 1 && $status != 2 && $id <= 0){
- HelpHander::error("参数错误");
- }
- $info = Db::name('energy_alarm_event')
- ->alias('a')
- ->join('energy_alarm b','a.alarm_id = b.id')
- ->where('b.org_id',$this->orgId)
- ->where('a.id',$id)
- ->field('a.*')
- ->find();
- if(!$info){
- HelpHander::error("报警记录不存在");
- }
- if($status == 1 && $info['status'] != 0){
- HelpHander::error("无权限操作");
- }
- if($status == 2 && $info['status'] != 1){
- HelpHander::error("无权限操作");
- }
- if($status == 1){
- $sc = time() - strtotime($info['create_time']);
- $res = Db::name($this->table)->where('id',$id)->update([
- "xytime" => $sc,
- "deal_time" => date('Y-m-d H:i:s'),
- 'update_time' => date('Y-m-d H:i:s'),
- 'deal_user' => $this->userId,
- 'status' => 1
- ]);
- }else{
- $sc = time() - strtotime($info['create_time']);
- $res = Db::name($this->table)->where('id',$id)->update([
- "hftime" => $sc,
- "finish_time" => date('Y-m-d H:i:s'),
- 'update_time' => date('Y-m-d H:i:s'),
- "reason" => $reason,
- 'status' => 2
- ]);
- }
- if($res){
- HelpHander::success([],'操作成功');
- }else{
- HelpHander::error("操作失败");
- }
- }
- // 监控
- public function monitor(){
- $lists = Db::name('monitor')->where('org_id',$this->orgId)->where('del',0)->field('id,title,ueid')->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['url'] = url('h5/Index/monitor',['id'=>think_encrypt($v['id'])],false,true);
- }
- HelpHander::success($lists);
- }
- // 楼层统计
- public function floor(){
- $address = Db::name('energy_address')
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->where('enable',1)
- ->field('id,title,ueid,pid')
- ->select();
- $pids = [];
- foreach ($address as $k=>$v){
- if($v['pid'] == 0){
- $pids[] = $v['id'];
- }
- }
- $lists = [];
- if($pids){
- foreach ($address as $k=>$v){
- if(in_array($v['pid'],$pids) && !empty($v['ueid'])){
- $lists[] = $v;
- }
- }
- }
- $devices = Db::name('energy_device')
- ->where('del',0)
- ->field('id,sn,address_id')
- ->select();
- $day = date('Y-m-d');
- foreach($lists as $k=>$v){
- $cids = [];
- foreach ($address as $kk=>$vv){
- if($vv['pid'] == $v['id']){
- $cids[] = $vv['id'];
- }
- }
- $cids[] = $v['id'];
- $deviceSns = [];
- foreach ($devices as $dk=>$dv){
- if(in_array($dv['address_id'],$cids)){
- $deviceSns[] = $dv['sn'];
- }
- }
- $degree = 0;
- if($deviceSns){
- $degree = (new EnergyParam())->getDegreeByDay($this->orgId,$day,$deviceSns);
- }
- $lists[$k]['degree'] = round($degree,2);
- }
- HelpHander::success($lists);
- }
- }
|