Weather.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  4. class Weather extends Base_Controller
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->load->model('Weathermonitor_model');
  10. $this->load->model('Project_model');
  11. $this->load->model('Network_model');
  12. // $this->load->model('Zone_model');
  13. }
  14. // 环境监控列表
  15. public function getlist()
  16. {
  17. $userid = $this->get_user_info('id');
  18. $role = $this->get_user_info('role');
  19. $companyid = $this->get_user_info('companyid');
  20. $page = $this->input->post('page',true);
  21. $count = $this->input->post('count',true);
  22. $project_id = $this->input->post('project_id',true);
  23. $network_id = $this->input->post('network_id',true);
  24. $id = $this->input->post('id',true);
  25. $id = trim($id,',');
  26. $action = $this->input->post('action',true);
  27. $keyword = $this->input->post('keyword',true);
  28. $download = $this->input->post('download',true);
  29. $print = $this->input->post('print',true);
  30. $filter = [];
  31. // 需要获取的字段
  32. $field = "W.id,
  33. W.number,
  34. WI.temperature,
  35. WI.humidity,
  36. WI.atmospressue,
  37. WI.PM25,
  38. WI.PM10,
  39. WI.noise,
  40. WI.updatetime,
  41. P.projectname";
  42. if (empty($action)) { // (环境监控页)带分页的列表
  43. if (!empty($keyword)) {
  44. $filter['keyword'] = $keyword;
  45. }
  46. if (!empty($id)) {
  47. $filter['W.id'] = explode(',', $id);
  48. }
  49. if (empty($download)) {
  50. if (isset($print) && !empty($print)) {
  51. $data = $this->Weathermonitor_model->get_list_by_role($role,$userid,$companyid,$project_id,$field,$page,$count,$filter,1);
  52. }else{
  53. $data = $this->Weathermonitor_model->get_list_by_role($role,$userid,$companyid,$project_id,$field,$page,$count,$filter);
  54. }
  55. foreach ($data as &$v) {
  56. // 设置固定大气压强
  57. if (!empty($v['atmospressue'])) {
  58. $v['atmospressue'] = 10;
  59. }
  60. foreach ($v as &$s) {
  61. $s = empty($s) ? ' ' : $s;
  62. }
  63. }
  64. $total = $this->Weathermonitor_model->getTotal($filter,$role,$userid,$companyid,$project_id);
  65. $sum = 0;
  66. if (!empty($count) && !empty($page)) {
  67. $sum = ceil($total/$count);
  68. }
  69. json_result('0000',$this->response['0000'],array('list'=>$data,'total'=>$sum));
  70. }else{
  71. $data = $this->Weathermonitor_model->get_list_by_role($role,$userid,$companyid,$project_id,$field,null,null,$filter);
  72. // foreach ($data as &$v) {
  73. // foreach ($v as &$s) {
  74. // $s = empty($s) ? ' ' : $s;
  75. // }
  76. // }
  77. // 导出到Excel
  78. $version = $this->session->userdata('version');
  79. if (empty($version)) {
  80. $title = array(
  81. array('编号','监控名称','温度(℃)','湿度(RH)','PM2.5(μg/m³)','PM10.0(g/m3)','噪音(dB)','大气压强(kPa)','更新时间'),
  82. );
  83. }else{
  84. $title = array(
  85. array('Number','Monitoring number','temperature(℃)','humidity(RH)','PM2.5(μg/m³)','PM10.0(g/m3)','noise(dB)','atmospheric pressure(kPa)','Update time'),
  86. );
  87. }
  88. $temp = array();
  89. $i = 1;
  90. foreach ($data as $k=>$value) {
  91. $temp[$k][] = $i;
  92. $temp[$k][] = $value['number'];
  93. $temp[$k][] = $value['temperature'];
  94. $temp[$k][] = $value['humidity'];
  95. $temp[$k][] = $value['PM25'];
  96. $temp[$k][] = $value['PM10'];
  97. $temp[$k][] = $value['noise'];
  98. $temp[$k][] = empty($value['atmospressue']) ? '' : 10; // 设置固定大气压强
  99. $temp[$k][] = $value['updatetime'];
  100. $i ++;
  101. }
  102. $data = array_merge($title,$temp);
  103. $path = push_excel($data,'weatherExcel_'.date('Ymd'));
  104. exit(json_result('0000',$this->response['0000'],array('path'=>$path)));
  105. }
  106. }else{ // (项目管理页)不带分页的列表
  107. if ($project_id > 0) $filter['projectid'] = $project_id;
  108. if ($network_id > 0) $filter['networkid'] = $network_id;
  109. if ($lamp_id > 0) $filter['lampid'] = $lamp_id;
  110. $data = $this->Weathermonitor_model->get_list_by_filter($filter,$field);
  111. json_result('0000',$this->response['0000'],array('video_list'=>$data));
  112. }
  113. }
  114. // 添加/编辑环境监控
  115. public function update(){
  116. $where['id'] = $this->input->post('id',true);
  117. $role = $this->get_user_info('role');
  118. // if ($role == COMPANY_CUSTOMER) {
  119. // exit(json_result('0011', $this->response['0011'], array()));
  120. // }
  121. $data['number'] = $this->input->post('number',true);
  122. $data['projectid'] = $this->input->post('projectid',true);
  123. $section = $this->input->post('section',true);
  124. $data['address'] = $this->input->post('address',true);
  125. $longitude = $this->input->post('longitude',true);
  126. $latitude = $this->input->post('latitude',true);
  127. $monitortype = $this->input->post('monitortype',true);
  128. $batteryah = $this->input->post('batteryah',true);
  129. $protocoltype = $this->input->post('protocoltype',true);
  130. if(isset($longitude)) $data['longitude'] = $longitude;
  131. if(isset($latitude)) $data['latitude'] = $latitude;
  132. if(isset($monitortype)) $data['monitortype'] = $monitortype;
  133. if(isset($batteryah)) $data['batteryah'] = $batteryah;
  134. if(isset($section)) $data['section'] = $section;
  135. if(!isset($data['number'])) exit(json_result('0804',$this->response['0804'],array()));
  136. if(!is_numeric($data['number']) || $data['number'] < 1 || $data['number'] > 999999) exit(json_result('0813',$this->response['0813'],array()));
  137. if(empty($data['projectid'])) exit(json_result('0308',$this->response['0308'],array()));
  138. if(empty($data['address'])) exit(json_result('0805',$this->response['0805'],array()));
  139. $is_true = preg_match('/^[A-Fa-f0-9]+$/', $data['address']);
  140. if (empty($is_true)) exit(json_result('0414', $this->response['0414'], array()));
  141. if (empty($where['id'])) { // 添加数据
  142. if ($this->Weathermonitor_model->getDataCount(array('number'=>$data['number'],'projectid'=>$data['projectid']))) {
  143. exit(json_result('0801',$this->response['0801'],array()));
  144. }
  145. if ($this->Weathermonitor_model->getDataCount(array('address'=>$data['address']))) { // 无线模块地址被使用
  146. exit(json_result('0404',$this->response['0404'],array()));
  147. }
  148. $number = mb_strlen($data['address']) == 13 ? base_convert($data['address'], 16, 10) : $data['address'];
  149. // 创建网络
  150. // $networkData['id'] = $this->Zone_model->insert(array(
  151. // 'name' => $number,
  152. // 'parent' => $data['projectid'] ,
  153. // 'level' => 4
  154. // ));
  155. $networkData['networkid'] = $number;
  156. $networkData['gatewaytype'] = 'direct';
  157. $networkData['devicetype'] = 1;
  158. $networkData['protocoltype'] = isset($protocoltype) ? $protocoltype : 1;
  159. $networkData['projectid'] = $data['projectid'];
  160. $networkData['devicesn'] = $number;
  161. $networkData['networkname'] = $number;
  162. $networkData['type'] = 1;
  163. $networkData['createtime'] = date('Y-m-d H:i:s');
  164. // $data['networkid'] = $networkData['id'];
  165. $data['networkid'] = $this->Network_model->insert($networkData);
  166. $this->Project_model->add_network_count(array('projectid'=>$data['projectid'],'lampcount'=>0,'faultcount'=>0));
  167. $data['createtime'] = date('Y-m-d H-i-s',time());
  168. $data['updatetime'] = date('Y-m-d H-i-s',time());
  169. $id = $this->Weathermonitor_model->insert($data);
  170. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  171. $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目下添加环境监控,监控编号\"{$data['number']}\"",0);
  172. $this->add_operation_log('insert',"Add environmental monitor.Monitor number:\"{$data['number']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  173. }else{ // 编辑数据
  174. if ($this->Weathermonitor_model->getDataCount(array('number'=>$data['number'],'projectid'=>$data['projectid']),$where['id'])) {
  175. exit(json_result('0801',$this->response['0801'],array()));
  176. }
  177. if ($this->Weathermonitor_model->getDataCount(array('address'=>$data['address']),$where['id'])) { // 无线模块地址被使用
  178. exit(json_result('0404',$this->response['0404'],array()));
  179. }
  180. $old = $this->Weathermonitor_model->get_data_by_filter($where,'address,networkid,projectid,protocoltype');
  181. if ($old['address'] != $data['address']) {
  182. // 删除原有网络
  183. // $this->Zone_model->delZone($old['networkid']);
  184. $this->Network_model->delData(array('id'=>$old['networkid']));
  185. $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$old['networkid'].'}';
  186. send_cmd($cmdstr,0,0,$old['protocoltype']);
  187. $number = mb_strlen($data['address']) == 13 ? base_convert($data['address'], 16, 10) : $data['address'];
  188. // 创建网络
  189. // $networkData['id'] = $this->Zone_model->insert(array(
  190. // 'name' => $number,
  191. // 'parent' => $data['projectid'] ,
  192. // 'level' => 4
  193. // ));
  194. $networkData['networkid'] = $number;
  195. $networkData['gatewaytype'] = 'direct';
  196. $networkData['devicetype'] = 1;
  197. $networkData['projectid'] = $data['projectid'];
  198. $networkData['devicesn'] = $number;
  199. $networkData['networkname'] = $number;
  200. $networkData['type'] = 1;
  201. $networkData['protocoltype'] = isset($protocoltype) ? $protocoltype : 1;
  202. // $data['networkid'] = $networkData['id'];
  203. $data['networkid'] = $this->Network_model->insert($networkData);
  204. }else {
  205. $netData = $this->Network_model->getData(array('id'=>$old['networkid']),'protocoltype');
  206. if ($netData['protocoltype'] != $protocoltype && isset($protocoltype)) {
  207. $this->Network_model->update(array('id'=>$old['networkid']),array('protocoltype'=>$protocoltype));
  208. }
  209. }
  210. if ($old['projectid'] != $data['projectid']) {
  211. $this->Project_model->add_network_count(array('projectid'=>$data['projectid'],'lampcount'=>0,'faultcount'=>0));
  212. $this->Project_model->minus_network_count(array('projectid'=>$old['projectid'],'lampcount'=>0,'faultcount'=>0));
  213. }
  214. $data['updatetime'] = date('Y-m-d H-i-s',time());
  215. $id = $this->Weathermonitor_model->update(array('id'=>$where['id']),$data);
  216. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  217. $this->add_operation_log('update',"编辑\"{$projectData['projectname']}\"项目下的环境监控,监控编号\"{$data['number']}\"",0);
  218. $this->add_operation_log('update',"Update environmental monitor.Monitor number:\"{$data['number']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  219. }
  220. exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
  221. }
  222. // 删除环境监控(支持批量操作)
  223. public function del(){
  224. $videoIds = $this->input->post('id',true);
  225. $role = $this->get_user_info('role');
  226. // if ($role == COMPANY_CUSTOMER) {
  227. // exit(json_result('0011',$this->response['0011'],array()));
  228. // }
  229. if (empty($videoIds)) {
  230. exit(json_result('0007',$this->response['0007'],array()));
  231. }
  232. $ids = explode(',', $videoIds);
  233. foreach ($ids as $v) {
  234. $old = $this->Weathermonitor_model->get_data_by_filter(array('id'=>$v),'networkid,projectid,number,protocoltype');
  235. // 删除网络
  236. if (!empty($old['networkid'])) {
  237. // $this->Zone_model->delOne($old['networkid']);
  238. $this->Network_model->delData(array('id'=>$old['networkid']));
  239. $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$old['networkid'].'}';
  240. send_cmd($cmdstr,0,0,,$old['protocoltype']);
  241. }
  242. // 修改项目下的网络数
  243. $this->Project_model->minus_network_count(array('projectid'=>$old['projectid'],'lampcount'=>0,'faultcount'=>0));
  244. $projectData = $this->Project_model->getData(array('id'=>$old['projectid']),'projectname');
  245. $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目下的环境监控,监控编号\"{$old['number']}\"",0);
  246. $this->add_operation_log('delete',"Delete environmental monitor.Monitor number:\"{$old['number']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  247. }
  248. $data = $this->Weathermonitor_model->get_list_by_filter(array('id'=>$ids),'W.id,W.projectid,W.networkid');
  249. $this->Weathermonitor_model->delData(array('id'=>$ids));
  250. exit(json_result('0000',$this->response['0000'],array()));
  251. }
  252. // 环境监控详情
  253. public function info(){
  254. $weatherid = $this->input->post('id',true);
  255. if (empty($weatherid)) {
  256. exit(json_result('0007',$this->response['0007'],array()));
  257. }
  258. $field = "id,
  259. number,
  260. projectid,
  261. address,
  262. section,
  263. longitude,
  264. latitude,
  265. monitortype,
  266. batteryah,
  267. networkid";
  268. $data = $this->Weathermonitor_model->get_data_by_filter(array('id'=>$weatherid),$field);
  269. $netData = $this->Network_model->getData(array('id'=>$data['networkid']),'protocoltype');
  270. $data['protocoltype'] = $netData['protocoltype'];
  271. exit(json_result('0000',$this->response['0000'],$data));
  272. }
  273. // 数据报表
  274. public function data_list(){
  275. // 监控id
  276. $id = $this->input->post('id',true);
  277. if (empty($id)) {
  278. exit(json_result('0007',$this->response['0007'],array()));
  279. }
  280. $type = $this->input->post('type',true);
  281. $date_type = $this->input->post('date_type',true);
  282. $date = $this->input->post('date',true);
  283. $dataList = $this->data($id,$type,$date_type,$date);
  284. exit(json_result('0000',$this->response['0000'],$dataList));
  285. }
  286. // 数据对比
  287. public function data_com(){
  288. $ids = $this->input->post('ids',true);
  289. $ids = trim($ids,',');
  290. if (empty($ids)) {
  291. exit(json_result('0007',$this->response['0007'],array()));
  292. }
  293. $type = $this->input->post('type',true);
  294. $date_type = $this->input->post('date_type',true);
  295. $date = $this->input->post('date',true);
  296. $idArr = explode(',', $ids);
  297. $dataArr = array();
  298. foreach ($idArr as $v) {
  299. $dataArr[] = $this->data($v,$type,$date_type,$date);
  300. }
  301. exit(json_result('0000',$this->response['0000'],array('list'=>$dataArr)));
  302. }
  303. // 获取报表数据
  304. private function data($id,$type,$date_type,$date){
  305. if (empty($type)) $type = 'temperature';
  306. if (empty($date_type)) $date_type = 'year';
  307. if ($date_type == 'year') {
  308. $date = empty($date) ? date('Y') : date('Y',strtotime($date.'-01'));
  309. }elseif ($date_type == 'month') {
  310. $date = empty($date) ? date('Y-m') : date('Y-m',strtotime($date));
  311. }elseif ($date_type == 'day') {
  312. // $date = empty($date) ? date('Y-m-d') : date('Y-m-d',strtotime($date));
  313. }else{
  314. $date = '';
  315. }
  316. if ($date_type == 'day') {
  317. if (date('Y-m-d',strtotime($date)) == date('Y-m-d',time())) {
  318. $end = intval(date('H', time()));
  319. }else{
  320. $end = 24;
  321. }
  322. $dataList = array();
  323. for($start = 1; $start <= $end; $start++){
  324. $startStr = $start-1;
  325. $startStr = $startStr < 10 ? "0".$startStr : $startStr;
  326. $data = $this->Weathermonitor_model->reportData($id,$type, $date.' '.$startStr.':00:00',$date.' '.$startStr.':59:59');
  327. $dataList['time'][] = $start < 10 ? "0".$start.':00' : $start.':00';
  328. // 设置固定大气压强
  329. if ($type == 'atmospressue') {
  330. if (!empty($data['max'])) {
  331. $data['max'] = 10;
  332. $data['min'] = 10;
  333. $data['avg'] = 10;
  334. }
  335. }
  336. $dataList['value']['max'][] = round($data['max'],1);
  337. $dataList['value']['min'][] = round($data['min'],1);
  338. $dataList['value']['avg'][] = round($data['avg'],1);
  339. }
  340. }elseif ($date_type == 'month') {
  341. $beginDateMonth = date('Y-m-01', strtotime($date.'-01 00:00:00'));
  342. $comDate = date('Y-m-01 00:00:00', time());
  343. if (strtotime($beginDateMonth) > strtotime($comDate)) {
  344. $end = 0;
  345. }elseif (strtotime($beginDateMonth) == strtotime($comDate)) {
  346. $end = intval(date('d', time()));
  347. }else{
  348. $end = date('d', strtotime("$beginDateMonth +1 month -1 day"));
  349. }
  350. $dataList = array();
  351. for($start = 1; $start <= $end; $start++){
  352. $startStr = $start;
  353. if ($start < 10) {
  354. $startStr = "0".$start;
  355. }
  356. $data = $this->Weathermonitor_model->reportData($id,$type, $date.'-'.$startStr,$date.'-'.$startStr.' 23:59:59');
  357. $dataList['time'][] = $date.'-'.$startStr;
  358. // 设置固定大气压强
  359. if ($type == 'atmospressue') {
  360. if (!empty($data['max'])) {
  361. $data['max'] = 10;
  362. $data['min'] = 10;
  363. $data['avg'] = 10;
  364. }
  365. }
  366. $dataList['value']['max'][] = round($data['max'],1);
  367. $dataList['value']['min'][] = round($data['min'],1);
  368. $dataList['value']['avg'][] = round($data['avg'],1);
  369. }
  370. } elseif ($date_type == 'year') {
  371. $dataList = array();
  372. $y = intval(date('Y',time()));
  373. if ($y == $date) {
  374. $end = intval(date('m',time()));
  375. }elseif($date > $y){
  376. $end = 0;
  377. }else{
  378. $end = 12;
  379. }
  380. for($start = 1; $start <= $end; $start++){
  381. $startStr = $start;
  382. if ($start < 10) {
  383. $startStr = "0".$start;
  384. }
  385. $beginDateMonth = date('Y-m-01', strtotime($date."-{$startStr}-01 00:00:00"));
  386. $m = date('Y-m-d', strtotime("$beginDateMonth +1 month -1 day"));
  387. $data = $this->Weathermonitor_model->reportData($id,$type, $date."-".$startStr.'-1 00:00:00',$m.' 23:59:59');
  388. $dataList['time'][] = $date.'-'.$startStr;
  389. if ($type == 'atmospressue') {
  390. if (!empty($data['max'])) {
  391. $data['max'] = 10;
  392. $data['min'] = 10;
  393. $data['avg'] = 10;
  394. }
  395. }
  396. $dataList['value']['max'][] = round($data['max'],1);
  397. $dataList['value']['min'][] = round($data['min'],1);
  398. $dataList['value']['avg'][] = round($data['avg'],1);
  399. }
  400. } else {
  401. $dataList = array();
  402. for($start = $this->Weathermonitor_model->getStartYear($id); $start <= intval(date('Y')); $start++){
  403. $startStr = $start;
  404. $data = $this->Weathermonitor_model->reportData($id,$type, $startStr);
  405. $dataList['time'][] = $startStr;
  406. if ($type == 'atmospressue') {
  407. if (!empty($data['max'])) {
  408. $data['max'] = 10;
  409. $data['min'] = 10;
  410. $data['avg'] = 10;
  411. }
  412. }
  413. $dataList['value']['max'][] = round($data['max'],1);
  414. $dataList['value']['min'][] = round($data['min'],1);
  415. $dataList['value']['avg'][] = round($data['avg'],1);
  416. }
  417. }
  418. $data = $this->Weathermonitor_model->getOne($id,'number');
  419. $dataList['number'] = $data['number'];
  420. return $dataList;
  421. }
  422. }
  423. ?>