0
0

Elector.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace app\api\controller\energy;
  3. use app\common\model\EnergyParam;
  4. use app\common\util\YsUtil;
  5. use app\hander\HelpHander;
  6. use think\Controller;
  7. use think\Db;
  8. class Elector extends Base
  9. {
  10. public function deviceCount(){
  11. $count = Db::name('energy_device')
  12. ->where('org_id',$this->orgId)
  13. ->where('del',0)
  14. ->count();
  15. $count1 = Db::name('energy_device')
  16. ->where('org_id',$this->orgId)
  17. ->where('del',0)
  18. ->where('status',0)
  19. ->count();
  20. $count2 = Db::name('energy_device')
  21. ->where('org_id',$this->orgId)
  22. ->where('del',0)
  23. ->where('status',1)
  24. ->count();
  25. $count3 = Db::name('energy_alarm_event')
  26. ->alias('eae')
  27. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  28. ->where('ea.org_id',$this->orgId)
  29. ->where('eae.status',1)
  30. ->count();
  31. $day = date('Y-m-d');
  32. $kwh = (new EnergyParam())->getDegreeByDay($this->orgId,$day);
  33. $data = [
  34. 'count'=>$count,
  35. 'count1'=>$count1,
  36. 'count2'=>$count2,
  37. 'count3'=>$count3,
  38. 'kwh'=>round($kwh,1),
  39. ];
  40. HelpHander::success($data);
  41. }
  42. public function warningList(){
  43. $lists = Db::name('energy_alarm_event')
  44. ->alias('eae')
  45. ->field('ea.alarm_time,ea.point_id,ea.point_label,ea.alarm_type,ea.current_value,eae.status')
  46. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  47. ->where('ea.org_id',$this->orgId)
  48. // ->where('eae.status','in',[0,1])
  49. ->where('eae.status','=',0)
  50. ->order('eae.id desc')
  51. ->limit(10)
  52. ->select();
  53. foreach ($lists as $k=>$v){
  54. $lists[$k]['time'] = date('m-d H:i',strtotime($v['alarm_time']));
  55. $lists[$k]['content'] = $v['point_id'].' '.$v['point_label'].' '.$v['alarm_type'].'-高水位告警当前值:'.$v['current_value'];
  56. }
  57. HelpHander::success($lists);
  58. }
  59. public function roomInfo(){
  60. $t = Db::name('energy_param')
  61. ->alias("pa")
  62. ->join("energy_device a","a.sn = pa.device_sn")
  63. ->field("pa.content")
  64. ->where('pa.org_id',$this->orgId)
  65. ->where('a.cate',3)
  66. ->order('pa.id desc')
  67. ->find();
  68. $content = isset($t['content']) && !empty($t['content']) ? json_decode($t['content'],true):[];
  69. $temp = ['men'=>'', 'yan'=>'', 'wenshi'=>'', 'shui'=>'','shui1'=>'','monitor'=>'','monitor1'=>''];
  70. if(isset($content['Temp']) && !empty($content['Temp'])){
  71. $wen = round($content['Temp'],1).'℃/';
  72. $shi = round($content['Humi'],0).'%RH';
  73. $temp['wenshi'] = $wen.$shi;
  74. }
  75. $t1 = Db::name('energy_param')
  76. ->alias("pa")
  77. ->join("energy_device a","a.sn = pa.device_sn")
  78. ->join("energy_device_cate b","a.cate = b.id")
  79. ->field("pa.content,a.*,b.title as cate_title")
  80. ->where('pa.org_id',$this->orgId)
  81. ->where('b.id',4)
  82. ->order('pa.id desc')
  83. ->find();
  84. $content1 = isset($t1['content']) && !empty($t1['content']) ? json_decode($t1['content'],true):[];
  85. if(isset($content1['遥信1']) && !empty($content1['遥信1'])){
  86. $temp['shui'] = $content1['遥信1'] == 1?"有":"无";
  87. $temp['shui1'] = $content1['遥信2'] == 1?"有":"无";
  88. $temp['yan'] = $content1['遥信3'] == 1?"开":"关";
  89. $temp['men'] = $content1['遥信4'] == 1?"开":"关";
  90. }
  91. $monitorList = Db::name('energy_monitor')
  92. ->where('org_id',$this->orgId)
  93. ->where('del',0)
  94. ->where('enable',1)
  95. ->order('id asc')
  96. ->limit(2)
  97. ->select();
  98. if($monitorList){
  99. $ysUtil = new YsUtil();
  100. foreach ($monitorList as $k=>$v){
  101. $v['url'] = $ysUtil->getLiveUrl($v['device_sn'],$v['channel']);
  102. if(isset($k) && $k==0){
  103. $temp['monitor'] = $v;
  104. }
  105. if(isset($k) && $k==1){
  106. $temp['monitor1'] = $v;
  107. }
  108. }
  109. }
  110. HelpHander::success($temp);
  111. }
  112. public function roomParamList(){
  113. $lists = Db::name('energy_room')
  114. ->field('id,title')
  115. ->where('org_id',$this->orgId)
  116. ->where('del',0)
  117. ->limit(5)
  118. ->select();
  119. foreach ($lists as $k=>$v){
  120. $lists[$k]['status'] = 1;
  121. $count = 0;
  122. $deviceList = Db::name('energy_device')
  123. ->field('id,sn,title,cate,address_id')
  124. ->where('del',0)
  125. ->where('room_id',$v['id'])
  126. ->where('cate',1)
  127. ->group('sn')
  128. ->select();
  129. $lists[$k]['cate_count'] = count($deviceList);
  130. $lists[$k]['cate_title'] = Db::name('energy_device_cate')->where('id',1)->value('title');
  131. foreach ($deviceList as $kk=>$vv){
  132. $deviceList[$kk]['title'] = Db::name('energy_address')->where('id',$vv['address_id'])->value('title');
  133. $count +=1;
  134. $paramInfo = Db::name('energy_param')
  135. ->field('id,content,status')
  136. ->where('device_sn',$vv['sn'])
  137. ->order('report_time desc,id desc')
  138. ->find();
  139. $contents = $paramInfo ? json_decode($paramInfo['content'],true):[];
  140. $ua = $ub = $uc = $switch = $s = '0.00'; //电压
  141. $wa = $wb = $wc = '32'; //温度
  142. $bl = '1'; //占比
  143. if(isset($contents['EPI'])){
  144. $ua = isset($contents['Ua']) ? $contents['Ua'] :'0.00';
  145. $ub = isset($contents['Ub']) ? $contents['Ub'] :'0.00';
  146. $uc = isset($contents['Uc']) ? $contents['Uc'] :'0.00';
  147. $s = isset($contents['S']) ? $contents['S'] :'0.00';
  148. if(isset($contents['Switch']) && $contents['Switch'] == '1'){
  149. $switch = '合闸';
  150. }else{
  151. $switch = '分闸';
  152. }
  153. }
  154. if(!empty($paramInfo['status']) && $paramInfo['status'] == 0){
  155. $statusTxt = '离线';
  156. }elseif(!empty($paramInfo['status']) && $paramInfo['status'] == 1){
  157. $statusTxt = '在线';
  158. }else{
  159. $statusTxt = '';
  160. }
  161. $deviceList[$kk]['status_txt'] = $statusTxt;
  162. $deviceList[$kk]['ua'] = round($ua,1);
  163. $deviceList[$kk]['ub'] = round($ub,1);
  164. $deviceList[$kk]['uc'] = round($uc,1);
  165. $deviceList[$kk]['switch'] = $switch;
  166. $deviceList[$kk]['wa'] = round($wa,1);
  167. $deviceList[$kk]['wb'] = round($wb,1);
  168. $deviceList[$kk]['wc'] = round($wc,1);
  169. $deviceList[$kk]['bl'] = $bl;
  170. $deviceList[$kk]['s'] = round($s,1);
  171. }
  172. $lists[$k]['list'] = $deviceList;
  173. $deviceList1 = Db::name('energy_device')
  174. ->field('id,sn,title,cate,address_id')
  175. ->where('del',0)
  176. ->where('room_id',$v['id'])
  177. ->where('cate',5)
  178. ->group('sn')
  179. ->select();
  180. $lists[$k]['cate_count5'] = count($deviceList1);
  181. $lists[$k]['cate_title5'] = Db::name('energy_device_cate')->where('id',5)->value('title');
  182. foreach ($deviceList1 as $kkk=>$vvv){
  183. $deviceList1[$kkk]['title'] = Db::name('energy_address')->where('id',$vvv['address_id'])->value('title');
  184. $count +=1;
  185. $paramInfo1 = Db::name('energy_param')
  186. ->field('id,content,status')
  187. ->where('device_sn',$vvv['sn'])
  188. ->order('report_time desc,id desc')
  189. ->find();
  190. $contents1 = $paramInfo1 ? json_decode($paramInfo1['content'],true):[];
  191. $ua1 = $ub1 = $uc1 = $switch1 = $s1 ='0.00'; //电压
  192. $wa1 = $wb1 = $wc1 = '32'; //温度
  193. $bl1 = '1'; //占比
  194. if(isset($contents1['EPI'])){
  195. $ua1 = isset($contents1['Ua']) ? $contents1['Ua'] :'0.00';
  196. $ub1 = isset($contents1['Ub']) ? $contents1['Ub'] :'0.00';
  197. $uc1 = isset($contents1['Uc']) ? $contents1['Uc'] :'0.00';
  198. $s1 = isset($contents1['S']) ? $contents1['S'] :'0.00';
  199. if(isset($contents1['Switch']) && $contents1['Switch'] == '1'){
  200. $switch1 = '合闸';
  201. }else{
  202. $switch1 = '分闸';
  203. }
  204. }
  205. if(!empty($paramInfo1['status']) && $paramInfo1['status'] == 0){
  206. $statusTxt1 = '离线';
  207. }elseif(!empty($paramInfo1['status']) && $paramInfo1['status'] == 1){
  208. $statusTxt1 = '在线';
  209. }else{
  210. $statusTxt1 = '';
  211. }
  212. $deviceList1[$kkk]['status_txt'] = $statusTxt1;
  213. $deviceList1[$kkk]['ua'] = round($ua1,1);
  214. $deviceList1[$kkk]['ub'] = round($ub1,1);
  215. $deviceList1[$kkk]['uc'] = round($uc1,1);
  216. $deviceList1[$kkk]['switch'] = $switch1;
  217. $deviceList1[$kkk]['wa'] = round($wa1,1);
  218. $deviceList1[$kkk]['wb'] = round($wb1,1);
  219. $deviceList1[$kkk]['wc'] = round($wc1,1);
  220. $deviceList1[$kkk]['bl'] = $bl1;
  221. $deviceList1[$kkk]['s'] = round($s1,1);
  222. }
  223. $lists[$k]['list1'] = $deviceList1;
  224. $lists[$k]['count'] = $count;
  225. }
  226. HelpHander::success($lists);
  227. }
  228. }