123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- <?php
- if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- include_once(FCPATH . 'application/controllers/Base_Controller.php');
- class Weather extends Base_Controller
- {
-
- public function __construct()
- {
- parent::__construct();
- $this->load->model('Weathermonitor_model');
- $this->load->model('Project_model');
- $this->load->model('Network_model');
- // $this->load->model('Zone_model');
- }
- // 环境监控列表
- public function getlist()
- {
- $userid = $this->get_user_info('id');
- $role = $this->get_user_info('role');
- $companyid = $this->get_user_info('companyid');
- $page = $this->input->post('page',true);
- $count = $this->input->post('count',true);
- $project_id = $this->input->post('project_id',true);
- $network_id = $this->input->post('network_id',true);
- $id = $this->input->post('id',true);
- $id = trim($id,',');
- $action = $this->input->post('action',true);
- $keyword = $this->input->post('keyword',true);
- $download = $this->input->post('download',true);
- $print = $this->input->post('print',true);
- $filter = [];
-
- // 需要获取的字段
- $field = "W.id,
- W.number,
- WI.temperature,
- WI.humidity,
- WI.atmospressue,
- WI.PM25,
- WI.PM10,
- WI.noise,
- WI.updatetime,
- P.projectname";
- if (empty($action)) { // (环境监控页)带分页的列表
- if (!empty($keyword)) {
- $filter['keyword'] = $keyword;
- }
- if (!empty($id)) {
- $filter['W.id'] = explode(',', $id);
- }
-
- if (empty($download)) {
- if (isset($print) && !empty($print)) {
- $data = $this->Weathermonitor_model->get_list_by_role($role,$userid,$companyid,$project_id,$field,$page,$count,$filter,1);
- }else{
- $data = $this->Weathermonitor_model->get_list_by_role($role,$userid,$companyid,$project_id,$field,$page,$count,$filter);
- }
-
- foreach ($data as &$v) {
- // 设置固定大气压强
- if (!empty($v['atmospressue'])) {
- $v['atmospressue'] = 10;
- }
- foreach ($v as &$s) {
- $s = empty($s) ? ' ' : $s;
- }
- }
- $total = $this->Weathermonitor_model->getTotal($filter,$role,$userid,$companyid,$project_id);
- $sum = 0;
- if (!empty($count) && !empty($page)) {
- $sum = ceil($total/$count);
- }
- json_result('0000',$this->response['0000'],array('list'=>$data,'total'=>$sum));
- }else{
- $data = $this->Weathermonitor_model->get_list_by_role($role,$userid,$companyid,$project_id,$field,null,null,$filter);
- // foreach ($data as &$v) {
- // foreach ($v as &$s) {
- // $s = empty($s) ? ' ' : $s;
- // }
- // }
- // 导出到Excel
- $version = $this->session->userdata('version');
- if (empty($version)) {
- $title = array(
- array('编号','监控名称','温度(℃)','湿度(RH)','PM2.5(μg/m³)','PM10.0(g/m3)','噪音(dB)','大气压强(kPa)','更新时间'),
- );
- }else{
- $title = array(
- array('Number','Monitoring number','temperature(℃)','humidity(RH)','PM2.5(μg/m³)','PM10.0(g/m3)','noise(dB)','atmospheric pressure(kPa)','Update time'),
- );
- }
-
- $temp = array();
- $i = 1;
- foreach ($data as $k=>$value) {
- $temp[$k][] = $i;
- $temp[$k][] = $value['number'];
- $temp[$k][] = $value['temperature'];
- $temp[$k][] = $value['humidity'];
- $temp[$k][] = $value['PM25'];
- $temp[$k][] = $value['PM10'];
- $temp[$k][] = $value['noise'];
- $temp[$k][] = empty($value['atmospressue']) ? '' : 10; // 设置固定大气压强
- $temp[$k][] = $value['updatetime'];
- $i ++;
- }
- $data = array_merge($title,$temp);
- $path = push_excel($data,'weatherExcel_'.date('Ymd'));
- exit(json_result('0000',$this->response['0000'],array('path'=>$path)));
- }
- }else{ // (项目管理页)不带分页的列表
- if ($project_id > 0) $filter['projectid'] = $project_id;
- if ($network_id > 0) $filter['networkid'] = $network_id;
- if ($lamp_id > 0) $filter['lampid'] = $lamp_id;
- $data = $this->Weathermonitor_model->get_list_by_filter($filter,$field);
- json_result('0000',$this->response['0000'],array('video_list'=>$data));
- }
- }
- // 添加/编辑环境监控
- public function update(){
- $where['id'] = $this->input->post('id',true);
- $role = $this->get_user_info('role');
- // if ($role == COMPANY_CUSTOMER) {
- // exit(json_result('0011', $this->response['0011'], array()));
- // }
-
- $data['number'] = $this->input->post('number',true);
- $data['projectid'] = $this->input->post('projectid',true);
- $section = $this->input->post('section',true);
- $data['address'] = $this->input->post('address',true);
- $longitude = $this->input->post('longitude',true);
- $latitude = $this->input->post('latitude',true);
- $monitortype = $this->input->post('monitortype',true);
- $batteryah = $this->input->post('batteryah',true);
- $protocoltype = $this->input->post('protocoltype',true);
- if(isset($longitude)) $data['longitude'] = $longitude;
- if(isset($latitude)) $data['latitude'] = $latitude;
- if(isset($monitortype)) $data['monitortype'] = $monitortype;
- if(isset($batteryah)) $data['batteryah'] = $batteryah;
- if(isset($section)) $data['section'] = $section;
- if(!isset($data['number'])) exit(json_result('0804',$this->response['0804'],array()));
- if(!is_numeric($data['number']) || $data['number'] < 1 || $data['number'] > 999999) exit(json_result('0813',$this->response['0813'],array()));
- if(empty($data['projectid'])) exit(json_result('0308',$this->response['0308'],array()));
- if(empty($data['address'])) exit(json_result('0805',$this->response['0805'],array()));
- $is_true = preg_match('/^[A-Fa-f0-9]+$/', $data['address']);
- if (empty($is_true)) exit(json_result('0414', $this->response['0414'], array()));
- if (empty($where['id'])) { // 添加数据
- if ($this->Weathermonitor_model->getDataCount(array('number'=>$data['number'],'projectid'=>$data['projectid']))) {
- exit(json_result('0801',$this->response['0801'],array()));
- }
- if ($this->Weathermonitor_model->getDataCount(array('address'=>$data['address']))) { // 无线模块地址被使用
- exit(json_result('0404',$this->response['0404'],array()));
- }
- $number = mb_strlen($data['address']) == 13 ? base_convert($data['address'], 16, 10) : $data['address'];
- // 创建网络
- // $networkData['id'] = $this->Zone_model->insert(array(
- // 'name' => $number,
- // 'parent' => $data['projectid'] ,
- // 'level' => 4
- // ));
- $networkData['networkid'] = $number;
- $networkData['gatewaytype'] = 'direct';
- $networkData['devicetype'] = 1;
- $networkData['protocoltype'] = isset($protocoltype) ? $protocoltype : 1;
- $networkData['projectid'] = $data['projectid'];
- $networkData['devicesn'] = $number;
- $networkData['networkname'] = $number;
- $networkData['type'] = 1;
- $networkData['createtime'] = date('Y-m-d H:i:s');
- // $data['networkid'] = $networkData['id'];
- $data['networkid'] = $this->Network_model->insert($networkData);
- $this->Project_model->add_network_count(array('projectid'=>$data['projectid'],'lampcount'=>0,'faultcount'=>0));
- $data['createtime'] = date('Y-m-d H-i-s',time());
- $data['updatetime'] = date('Y-m-d H-i-s',time());
- $id = $this->Weathermonitor_model->insert($data);
- $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
- $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目下添加环境监控,监控编号\"{$data['number']}\"",0);
- $this->add_operation_log('insert',"Add environmental monitor.Monitor number:\"{$data['number']}\".Project name:\"{$projectData['projectname']}\"",0,1);
- }else{ // 编辑数据
- if ($this->Weathermonitor_model->getDataCount(array('number'=>$data['number'],'projectid'=>$data['projectid']),$where['id'])) {
- exit(json_result('0801',$this->response['0801'],array()));
- }
- if ($this->Weathermonitor_model->getDataCount(array('address'=>$data['address']),$where['id'])) { // 无线模块地址被使用
- exit(json_result('0404',$this->response['0404'],array()));
- }
- $old = $this->Weathermonitor_model->get_data_by_filter($where,'address,networkid,projectid,protocoltype');
- if ($old['address'] != $data['address']) {
- // 删除原有网络
- // $this->Zone_model->delZone($old['networkid']);
- $this->Network_model->delData(array('id'=>$old['networkid']));
- $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$old['networkid'].'}';
- send_cmd($cmdstr,0,0,$old['protocoltype']);
- $number = mb_strlen($data['address']) == 13 ? base_convert($data['address'], 16, 10) : $data['address'];
- // 创建网络
- // $networkData['id'] = $this->Zone_model->insert(array(
- // 'name' => $number,
- // 'parent' => $data['projectid'] ,
- // 'level' => 4
- // ));
- $networkData['networkid'] = $number;
- $networkData['gatewaytype'] = 'direct';
- $networkData['devicetype'] = 1;
- $networkData['projectid'] = $data['projectid'];
- $networkData['devicesn'] = $number;
- $networkData['networkname'] = $number;
- $networkData['type'] = 1;
- $networkData['protocoltype'] = isset($protocoltype) ? $protocoltype : 1;
- // $data['networkid'] = $networkData['id'];
- $data['networkid'] = $this->Network_model->insert($networkData);
- }else {
- $netData = $this->Network_model->getData(array('id'=>$old['networkid']),'protocoltype');
- if ($netData['protocoltype'] != $protocoltype && isset($protocoltype)) {
- $this->Network_model->update(array('id'=>$old['networkid']),array('protocoltype'=>$protocoltype));
- }
- }
- if ($old['projectid'] != $data['projectid']) {
- $this->Project_model->add_network_count(array('projectid'=>$data['projectid'],'lampcount'=>0,'faultcount'=>0));
- $this->Project_model->minus_network_count(array('projectid'=>$old['projectid'],'lampcount'=>0,'faultcount'=>0));
- }
- $data['updatetime'] = date('Y-m-d H-i-s',time());
- $id = $this->Weathermonitor_model->update(array('id'=>$where['id']),$data);
-
- $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
- $this->add_operation_log('update',"编辑\"{$projectData['projectname']}\"项目下的环境监控,监控编号\"{$data['number']}\"",0);
- $this->add_operation_log('update',"Update environmental monitor.Monitor number:\"{$data['number']}\".Project name:\"{$projectData['projectname']}\"",0,1);
- }
- exit(json_result('0000',$this->response['0000'],array('id'=>$id)));
- }
- // 删除环境监控(支持批量操作)
- public function del(){
- $videoIds = $this->input->post('id',true);
- $role = $this->get_user_info('role');
- // if ($role == COMPANY_CUSTOMER) {
- // exit(json_result('0011',$this->response['0011'],array()));
- // }
- if (empty($videoIds)) {
- exit(json_result('0007',$this->response['0007'],array()));
- }
- $ids = explode(',', $videoIds);
- foreach ($ids as $v) {
- $old = $this->Weathermonitor_model->get_data_by_filter(array('id'=>$v),'networkid,projectid,number,protocoltype');
- // 删除网络
- if (!empty($old['networkid'])) {
- // $this->Zone_model->delOne($old['networkid']);
- $this->Network_model->delData(array('id'=>$old['networkid']));
- $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$old['networkid'].'}';
- send_cmd($cmdstr,0,0,,$old['protocoltype']);
- }
- // 修改项目下的网络数
- $this->Project_model->minus_network_count(array('projectid'=>$old['projectid'],'lampcount'=>0,'faultcount'=>0));
- $projectData = $this->Project_model->getData(array('id'=>$old['projectid']),'projectname');
- $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目下的环境监控,监控编号\"{$old['number']}\"",0);
- $this->add_operation_log('delete',"Delete environmental monitor.Monitor number:\"{$old['number']}\".Project name:\"{$projectData['projectname']}\"",0,1);
- }
- $data = $this->Weathermonitor_model->get_list_by_filter(array('id'=>$ids),'W.id,W.projectid,W.networkid');
-
- $this->Weathermonitor_model->delData(array('id'=>$ids));
-
- exit(json_result('0000',$this->response['0000'],array()));
- }
- // 环境监控详情
- public function info(){
- $weatherid = $this->input->post('id',true);
- if (empty($weatherid)) {
- exit(json_result('0007',$this->response['0007'],array()));
- }
- $field = "id,
- number,
- projectid,
- address,
- section,
- longitude,
- latitude,
- monitortype,
- batteryah,
- networkid";
- $data = $this->Weathermonitor_model->get_data_by_filter(array('id'=>$weatherid),$field);
- $netData = $this->Network_model->getData(array('id'=>$data['networkid']),'protocoltype');
- $data['protocoltype'] = $netData['protocoltype'];
- exit(json_result('0000',$this->response['0000'],$data));
- }
- // 数据报表
- public function data_list(){
- // 监控id
- $id = $this->input->post('id',true);
- if (empty($id)) {
- exit(json_result('0007',$this->response['0007'],array()));
- }
- $type = $this->input->post('type',true);
- $date_type = $this->input->post('date_type',true);
- $date = $this->input->post('date',true);
- $dataList = $this->data($id,$type,$date_type,$date);
- exit(json_result('0000',$this->response['0000'],$dataList));
- }
- // 数据对比
- public function data_com(){
- $ids = $this->input->post('ids',true);
- $ids = trim($ids,',');
- if (empty($ids)) {
- exit(json_result('0007',$this->response['0007'],array()));
- }
- $type = $this->input->post('type',true);
- $date_type = $this->input->post('date_type',true);
- $date = $this->input->post('date',true);
- $idArr = explode(',', $ids);
- $dataArr = array();
- foreach ($idArr as $v) {
- $dataArr[] = $this->data($v,$type,$date_type,$date);
- }
-
- exit(json_result('0000',$this->response['0000'],array('list'=>$dataArr)));
- }
- // 获取报表数据
- private function data($id,$type,$date_type,$date){
- if (empty($type)) $type = 'temperature';
- if (empty($date_type)) $date_type = 'year';
- if ($date_type == 'year') {
- $date = empty($date) ? date('Y') : date('Y',strtotime($date.'-01'));
- }elseif ($date_type == 'month') {
- $date = empty($date) ? date('Y-m') : date('Y-m',strtotime($date));
- }elseif ($date_type == 'day') {
- // $date = empty($date) ? date('Y-m-d') : date('Y-m-d',strtotime($date));
- }else{
- $date = '';
- }
- if ($date_type == 'day') {
- if (date('Y-m-d',strtotime($date)) == date('Y-m-d',time())) {
- $end = intval(date('H', time()));
- }else{
- $end = 24;
- }
- $dataList = array();
- for($start = 1; $start <= $end; $start++){
- $startStr = $start-1;
- $startStr = $startStr < 10 ? "0".$startStr : $startStr;
- $data = $this->Weathermonitor_model->reportData($id,$type, $date.' '.$startStr.':00:00',$date.' '.$startStr.':59:59');
- $dataList['time'][] = $start < 10 ? "0".$start.':00' : $start.':00';
- // 设置固定大气压强
- if ($type == 'atmospressue') {
- if (!empty($data['max'])) {
- $data['max'] = 10;
- $data['min'] = 10;
- $data['avg'] = 10;
- }
- }
- $dataList['value']['max'][] = round($data['max'],1);
- $dataList['value']['min'][] = round($data['min'],1);
- $dataList['value']['avg'][] = round($data['avg'],1);
- }
- }elseif ($date_type == 'month') {
- $beginDateMonth = date('Y-m-01', strtotime($date.'-01 00:00:00'));
- $comDate = date('Y-m-01 00:00:00', time());
- if (strtotime($beginDateMonth) > strtotime($comDate)) {
- $end = 0;
- }elseif (strtotime($beginDateMonth) == strtotime($comDate)) {
- $end = intval(date('d', time()));
- }else{
- $end = date('d', strtotime("$beginDateMonth +1 month -1 day"));
- }
- $dataList = array();
- for($start = 1; $start <= $end; $start++){
- $startStr = $start;
- if ($start < 10) {
- $startStr = "0".$start;
- }
- $data = $this->Weathermonitor_model->reportData($id,$type, $date.'-'.$startStr,$date.'-'.$startStr.' 23:59:59');
- $dataList['time'][] = $date.'-'.$startStr;
- // 设置固定大气压强
- if ($type == 'atmospressue') {
- if (!empty($data['max'])) {
- $data['max'] = 10;
- $data['min'] = 10;
- $data['avg'] = 10;
- }
- }
- $dataList['value']['max'][] = round($data['max'],1);
- $dataList['value']['min'][] = round($data['min'],1);
- $dataList['value']['avg'][] = round($data['avg'],1);
- }
-
- } elseif ($date_type == 'year') {
-
- $dataList = array();
- $y = intval(date('Y',time()));
- if ($y == $date) {
- $end = intval(date('m',time()));
- }elseif($date > $y){
- $end = 0;
- }else{
- $end = 12;
- }
- for($start = 1; $start <= $end; $start++){
- $startStr = $start;
- if ($start < 10) {
- $startStr = "0".$start;
- }
- $beginDateMonth = date('Y-m-01', strtotime($date."-{$startStr}-01 00:00:00"));
- $m = date('Y-m-d', strtotime("$beginDateMonth +1 month -1 day"));
- $data = $this->Weathermonitor_model->reportData($id,$type, $date."-".$startStr.'-1 00:00:00',$m.' 23:59:59');
- $dataList['time'][] = $date.'-'.$startStr;
- if ($type == 'atmospressue') {
- if (!empty($data['max'])) {
- $data['max'] = 10;
- $data['min'] = 10;
- $data['avg'] = 10;
- }
- }
- $dataList['value']['max'][] = round($data['max'],1);
- $dataList['value']['min'][] = round($data['min'],1);
- $dataList['value']['avg'][] = round($data['avg'],1);
- }
- } else {
- $dataList = array();
- for($start = $this->Weathermonitor_model->getStartYear($id); $start <= intval(date('Y')); $start++){
- $startStr = $start;
- $data = $this->Weathermonitor_model->reportData($id,$type, $startStr);
- $dataList['time'][] = $startStr;
- if ($type == 'atmospressue') {
- if (!empty($data['max'])) {
- $data['max'] = 10;
- $data['min'] = 10;
- $data['avg'] = 10;
- }
- }
- $dataList['value']['max'][] = round($data['max'],1);
- $dataList['value']['min'][] = round($data['min'],1);
- $dataList['value']['avg'][] = round($data['avg'],1);
- }
- }
- $data = $this->Weathermonitor_model->getOne($id,'number');
- $dataList['number'] = $data['number'];
- return $dataList;
- }
- }
- ?>
|