Energy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. namespace app\api\controller\ue;
  3. use app\common\model\EnergyParam;
  4. use app\hander\HelpHander;
  5. use think\Controller;
  6. use think\Db;
  7. class Energy extends Base {
  8. // 首页统计
  9. public function sytj(){
  10. $deviceCount = Db::name('energy_device')->where('org_id',$this->orgId)->where('del',0)->count(); // 设备总数
  11. $deviceCount1 = Db::name('energy_device')->where('org_id',$this->orgId)->where('del',0)->where('status',1)->count(); // 在线数
  12. // 今日消耗
  13. $day = date('Y-m-d');
  14. $degree = (new EnergyParam())->getDegreeByDay($this->orgId,$day);
  15. // 昨日消耗
  16. $yday = date('Y-m-d',time() - 24*60*60);
  17. $ydegree = (new EnergyParam())->getDegreeByDay($this->orgId,$yday);
  18. // 实时功率
  19. $power = (new EnergyParam())->getPower($this->orgId);
  20. $data = [
  21. 'deviceCount' => $deviceCount,
  22. 'deviceCount1' => $deviceCount1,
  23. 'degree' => round($degree,2),
  24. 'ydegree' => round($ydegree,2),
  25. 'power' => round($power,2),
  26. ];
  27. HelpHander::success($data);
  28. }
  29. // 配电房数据
  30. public function room(){
  31. $rooms = Db::name('energy_room')->where('org_id',$this->orgId)->where('del',0)->field('id,title,ueid')->select();
  32. foreach ($rooms as $k=>$v){
  33. $temp = '';
  34. $humi = '';
  35. // 温湿度设备
  36. $wsddevice = Db::name('energy_device')->where('org_id',$this->orgId)->where('del',0)->where('cate',3)->find();
  37. if($wsddevice){
  38. $wsdparam = Db::name('energy_param')->where('device_sn',$wsddevice['sn'])->order('report_time desc')->find();
  39. if($wsdparam){ // {"Temp":"23.500000","Humi":"49.000000"}
  40. $content = json_decode($wsdparam['content'],true);
  41. if(isset($content['Temp'])){
  42. $temp = $content['Temp'];
  43. }
  44. if(isset($content['Temp'])){
  45. $humi = $content['Humi'];
  46. }
  47. }
  48. }
  49. $rooms[$k]['temp'] = $temp;
  50. $rooms[$k]['humi'] = $humi;
  51. // 开关量设备
  52. $door = "";
  53. $smoke = '';
  54. $water1 = '';
  55. $water2 = '';
  56. $dalarms = [];
  57. $kgdevice = Db::name('energy_device')->where('org_id',$this->orgId)->where('del',0)->where('cate',4)->find();
  58. if($kgdevice){
  59. $kgparam = Db::name('energy_param')->where('device_sn',$kgdevice['sn'])->order('report_time desc')->select();
  60. if($kgparam){ //{"遥信3":"0","遥信2":"1","遥信1":"1","遥信8":"1","遥信7":"1","遥信6":"1","遥信5":"1","遥信4":"1"}
  61. $content = json_decode($wsdparam['content'],true);
  62. if(isset($content['遥信1'])){
  63. $water1 = $content['遥信1'] == 1?"有":"无";
  64. }
  65. if(isset($content['遥信2'])){
  66. $water2 = $content['遥信2'] == 1?"有":"无";
  67. }
  68. if(isset($content['遥信3'])){
  69. $smoke = $content['遥信3'] == 1?"有":"无";
  70. }
  71. if(isset($content['遥信4'])){
  72. $door = $content['遥信4'] == 1?"打开":"关闭";
  73. }
  74. }
  75. $dalarms = Db::name('energy_alarm')->where('alarm_type',"状态变化")->field('id,alarm_time as report_time,current_value')->order('alarm_time desc')->limit(7)->select();
  76. foreach ($dalarms as $kk=>$vv){
  77. $dalarms[$kk]['current_value'] = "关闭";
  78. if($vv['current_value'] == 1){
  79. $dalarms[$kk]['current_value'] = "打开";
  80. }
  81. }
  82. }
  83. $rooms[$k]['dalarms'] = $dalarms;
  84. $rooms[$k]['door'] = $door;
  85. $rooms[$k]['smoke'] = $smoke;
  86. $rooms[$k]['water1'] = $water1;
  87. $rooms[$k]['water2'] = $water2;
  88. }
  89. HelpHander::success($rooms);
  90. }
  91. // 楼栋日能耗分析
  92. public function addrs(){
  93. $addrs = Db::name('energy_address')
  94. ->where('org_id',$this->orgId)
  95. ->where('pid',0)
  96. ->where('enable',1)
  97. ->where('del',0)
  98. ->select();
  99. $day = date('Y-m-d');
  100. $x = [];
  101. $y = [];
  102. foreach ($addrs as $k=>$v){
  103. $x[] = $v['title'];
  104. $degree = 0;
  105. // 获取所有子级
  106. $cids = Db::name('energy_address')
  107. ->where('pid',$v['id'])
  108. ->where('enable',1)
  109. ->where('del',0)
  110. ->column('id');
  111. if($cids){
  112. $cids2 = Db::name('energy_address')
  113. ->where('pid','in',$cids)
  114. ->where('enable',1)
  115. ->where('del',0)
  116. ->column('id');
  117. if($cids2){
  118. $cids = array_merge($cids,$cids2);
  119. }
  120. }
  121. $cids[] = $v['id'];
  122. // 获取子级所有电表设备
  123. $devices = Db::name('energy_device')
  124. ->where('del',0)
  125. ->where('cate',1)
  126. ->where('address_id','in',$cids)
  127. ->column('sn');
  128. if($devices){
  129. $degree = (new EnergyParam())->getDegreeByDay($this->orgId,$day,$devices);
  130. }
  131. $y[] = round($degree,2);
  132. }
  133. HelpHander::success(['x'=>$x,'y'=>$y]);
  134. }
  135. // 总能耗逐时分析
  136. public function hours(){
  137. $x = [];
  138. $y1 = [];
  139. $y2 = [];
  140. $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];
  141. $day = date('Y-m-d');
  142. $yday = date('Y-m-d',time()-24*60*60);
  143. foreach ($hours as $k=>$v){
  144. $x[] = $v;
  145. $degree1 = (new EnergyParam())->getDegreeByHour($this->orgId,$day,$v,[]);
  146. $y1[] = round($degree1,2);
  147. $degree2 = (new EnergyParam())->getDegreeByHour($this->orgId,$yday,$v,[]);
  148. $y2[] = round($degree2,2);
  149. }
  150. HelpHander::success(['x'=>$x,'y1'=>$y1,'y2'=>$y2]);
  151. }
  152. // 待处理报警记录
  153. public function alarms(){
  154. $lists = Db::name('energy_alarm_event')
  155. ->alias('a')
  156. ->join('energy_alarm b','a.alarm_id = b.id')
  157. ->where('b.org_id',$this->orgId)
  158. ->where('a.status','in',[0,1])
  159. ->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')
  160. ->order('a.id desc')
  161. ->select();
  162. $rooms = [];
  163. $roomIds = [];
  164. foreach ($lists as $k=>$v){
  165. $lists[$k]['room_id'] = 0;
  166. $lists[$k]['content'] = "";
  167. $lists[$k]['cate_title'] = "";
  168. $lists[$k]['room_title'] = "";
  169. $room = Db::name('energy_device')
  170. ->alias('a')
  171. ->join('energy_room c','c.id = a.room_id')
  172. ->join('energy_device_cate d','d.id = a.cate','left')
  173. ->where('a.sn',$v['device_sn'])
  174. ->field('c.id,c.title,c.ueid,a.title as device_title,d.title as cate_title')
  175. ->find();
  176. if($room){
  177. $lists[$k]['room_id'] = $room['id'];
  178. $lists[$k]['content'] = $room['device_title']."-".$v['point_label']."-当前值:".$v['current_value'];
  179. $lists[$k]['cate_title'] = $room['cate_title'];
  180. $lists[$k]['room_title'] = $room['title'];
  181. if(!in_array($room['id'],$roomIds)){
  182. $roomIds[] = $room['id'];
  183. $rooms[] = [
  184. 'id' => $room['id'],
  185. 'title' => $room['title'],
  186. 'ueid' => $room['ueid']
  187. ];
  188. }
  189. }
  190. }
  191. $nrooms = [];
  192. foreach ($rooms as $k=>$v){
  193. $garr = [
  194. 'alarm_type' => "高限报警",
  195. 'count' => 0,
  196. 'records' => []
  197. ];
  198. $darr = [
  199. 'alarm_type' => "低限报警",
  200. 'count' => 0,
  201. 'records' => []
  202. ];
  203. $grecords = [];
  204. $drecords = [];
  205. foreach ($lists as $kk=>$vv){
  206. if($vv['room_id'] == $v['id']){
  207. if($vv['alarm_type'] == $garr['alarm_type']){
  208. $grecords[] = [
  209. "id" => $vv['id'],
  210. "status" => $vv['status'],
  211. "alarm_type" => $vv['alarm_type'],
  212. "room_title" => $vv['room_title'],
  213. "cate_title" => $vv['cate_title'],
  214. "content" => $vv['content'],
  215. "alarm_time" => $vv['alarm_time']
  216. ];
  217. }
  218. if($vv['alarm_type'] == $darr['alarm_type']){
  219. $drecords[] = [
  220. "id" => $vv['id'],
  221. "status" => $vv['status'],
  222. "alarm_type" => $vv['alarm_type'],
  223. "room_title" => $vv['room_title'],
  224. "cate_title" => $vv['cate_title'],
  225. "content" => $vv['content'],
  226. "alarm_time" => $vv['alarm_time']
  227. ];
  228. }
  229. }
  230. }
  231. $garr['count'] = count($grecords);
  232. $garr['records'] = $grecords;
  233. $v['garr'] = $garr;
  234. $darr['count'] = count($drecords);
  235. $darr['records'] = $drecords;
  236. $v['darr'] = $darr;
  237. $v['count'] = $garr['count'] + $darr['count'];
  238. if($v['count'] > 0){
  239. $nrooms[] = $v;
  240. }
  241. }
  242. HelpHander::success($nrooms);
  243. }
  244. // 处理报警
  245. public function dealAlarm(){
  246. $id = input('id/d',0);
  247. $status = input('status/d',0);
  248. $reason = input('reason','','trim');
  249. if($status != 1 && $status != 2 && $id <= 0){
  250. HelpHander::error("参数错误");
  251. }
  252. $info = Db::name('energy_alarm_event')
  253. ->alias('a')
  254. ->join('energy_alarm b','a.alarm_id = b.id')
  255. ->where('b.org_id',$this->orgId)
  256. ->where('a.id',$id)
  257. ->field('a.*')
  258. ->find();
  259. if(!$info){
  260. HelpHander::error("报警记录不存在");
  261. }
  262. if($status == 1 && $info['status'] != 0){
  263. HelpHander::error("无权限操作");
  264. }
  265. if($status == 2 && $info['status'] != 1){
  266. HelpHander::error("无权限操作");
  267. }
  268. if($status == 1){
  269. $sc = time() - strtotime($info['create_time']);
  270. $res = Db::name($this->table)->where('id',$id)->update([
  271. "xytime" => $sc,
  272. "deal_time" => date('Y-m-d H:i:s'),
  273. 'update_time' => date('Y-m-d H:i:s'),
  274. 'deal_user' => $this->userId,
  275. 'status' => 1
  276. ]);
  277. }else{
  278. $sc = time() - strtotime($info['create_time']);
  279. $res = Db::name($this->table)->where('id',$id)->update([
  280. "hftime" => $sc,
  281. "finish_time" => date('Y-m-d H:i:s'),
  282. 'update_time' => date('Y-m-d H:i:s'),
  283. "reason" => $reason,
  284. 'status' => 2
  285. ]);
  286. }
  287. if($res){
  288. HelpHander::success([],'操作成功');
  289. }else{
  290. HelpHander::error("操作失败");
  291. }
  292. }
  293. // 监控
  294. public function monitor(){
  295. $lists = Db::name('monitor')->where('org_id',$this->orgId)->where('del',0)->field('id,title,ueid')->select();
  296. foreach ($lists as $k=>$v){
  297. $lists[$k]['url'] = url('h5/Index/monitor',['id'=>think_encrypt($v['id'])],false,true);
  298. }
  299. HelpHander::success($lists);
  300. }
  301. // 楼层统计
  302. public function floor(){
  303. $address = Db::name('energy_address')
  304. ->where('org_id',$this->orgId)
  305. ->where('del',0)
  306. ->where('enable',1)
  307. ->field('id,title,ueid,pid')
  308. ->select();
  309. $pids = [];
  310. foreach ($address as $k=>$v){
  311. if($v['pid'] == 0){
  312. $pids[] = $v['id'];
  313. }
  314. }
  315. $lists = [];
  316. if($pids){
  317. foreach ($address as $k=>$v){
  318. if(in_array($v['pid'],$pids) && !empty($v['ueid'])){
  319. $lists[] = $v;
  320. }
  321. }
  322. }
  323. $devices = Db::name('energy_device')
  324. ->where('del',0)
  325. ->field('id,sn,address_id')
  326. ->select();
  327. $day = date('Y-m-d');
  328. foreach($lists as $k=>$v){
  329. $cids = [];
  330. foreach ($address as $kk=>$vv){
  331. if($vv['pid'] == $v['id']){
  332. $cids[] = $vv['id'];
  333. }
  334. }
  335. $cids[] = $v['id'];
  336. $deviceSns = [];
  337. foreach ($devices as $dk=>$dv){
  338. if(in_array($dv['address_id'],$cids)){
  339. $deviceSns[] = $dv['sn'];
  340. }
  341. }
  342. $degree = 0;
  343. if($deviceSns){
  344. $degree = (new EnergyParam())->getDegreeByDay($this->orgId,$day,$deviceSns);
  345. }
  346. $lists[$k]['degree'] = round($degree,2);
  347. }
  348. HelpHander::success($lists);
  349. }
  350. }