| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816 | 
							- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
- include_once(FCPATH . 'application/controllers/Base_Controller.php');
 
- class Project extends Base_Controller {
 
-     public function __construct() {
 
-         parent::__construct();
 
-         $this->load->model('Company_model');
 
-         $this->load->model('Project_model');
 
-         $this->load->model('Network_model');
 
-         $this->load->model('Lamp_model');
 
-         // $this->load->model('Zone_model');
 
-         $this->load->model('Patrol_model');
 
-         $this->load->model('Alarm_model');
 
-         $this->load->model('Syslog_model');
 
-         $this->load->model('Videomonitor_model');
 
-         $this->load->model('Weathermonitor_model');
 
-         $this->load->model('User_model');
 
-         $this->load->model('Load_model');
 
-         $this->load->model('Modbus_load_model');
 
-         $this->load->model('Global_location_model');
 
-     }
 
-     // 项目管理首页
 
-     public function data() {
 
-         $userid = $this->get_user_info('id');
 
-         $role = $this->get_user_info('role');
 
-         $project_id = intval($this->input->post('project_id', true));
 
-         $type = intval($this->input->post('type', true));
 
-         $section = $this->input->post('section', true);
 
-         $company = intval($this->input->post('company', true));
 
-         $version = $this->session->userdata('version');
 
-         $data = array();
 
-         // 总网络数
 
-         if (empty($project_id)) exit(json_result('0007',$this->response['0007']));
 
-         $data['network_total'] = $this->Network_model->get_count(array('projectid'=>$project_id,'type'=>0));
 
-         $proData = $this->Project_model->get_one(['id'=>$project_id],'projectname,projectid as number,address,createtime as create_time,cityid');
 
-         $data['number'] = $proData['number'];
 
-         $data['address'] = $proData['address'];
 
-         $data['projectname'] = $proData['projectname'];
 
-         $data['create_time'] = $proData['create_time'];
 
-         $data['cityid'] = $proData['cityid'];
 
-         $where = array();
 
-         $where['projectid'] = $project_id;
 
-         $where2[] = 'L.projectid = '.$project_id;
 
-         if ($section !== '' && $section !== NUll) {
 
-             $where['section'] = $section;
 
-             $where2[] = 'L.section = "'.$section.'"';
 
-         }
 
-         if (!empty($company) && !empty($type)) {
 
-             if ($type == 2) {
 
-                 $where['manu'] = $company;
 
-                 $where2[] = 'L.manu = '.$company;
 
-             }elseif ($type == 3) {
 
-                 $where['supplier'] = $company;
 
-                 $where2[] = 'L.supplier = '.$company;
 
-             }elseif ($type == 4) {
 
-                 $where['po'] = $company;
 
-                 $where2[] = 'L.po = '.$company;
 
-             }else {
 
-                 // $where['upazilla'] = $company;
 
-                 // $where2['L.upazilla'] = $company;
 
-             }
 
-         }
 
-         $data['total_lamp'] = $this->Lamp_model->get_count($where);
 
-         $join = [];
 
-         $join[] = ['table'=>'(select lampid,max(updatetime) as maxTime from warning_info_log group by lampid) as t1','cond'=>'t1.lampid = L.id','type'=>'inner'];
 
-         $join[] = ['table'=>'warning_info_log as AI','cond'=>'AI.lampid = t1.lampid AND t1.maxTime = AI.updatetime','type'=>'inner'];
 
-         $where2[] = 'L.netstatus = 1';
 
-         $where2[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
 
-         $where2 = implode(' AND ', $where2);
 
-         $res = $this->Lamp_model->get_list_by_multi_join($where2, 'count(*) as total',NULL, NULL, $join, NULL, NUll, 'L', true);
 
-         $data['failure_count'] = $res['total'];
 
-         $where['netstatus'] = 1;
 
-         $data['online_count'] = $this->Lamp_model->get_count($where);
 
-         $where['status'] = 1;
 
-         $data['lighting_count'] = $this->Lamp_model->get_count($where);
 
-         // $where2['N.status'] = 1;
 
-         // $join = [];
 
-         // $join[] = ['table'=>'network as N','cond'=>'N.id = L.networkid','type'=>'left'];
 
-         // $res = $this->Lamp_model->get_list_by_multi_join($where2, 'count(*) as total',NULL, NULL, $join, NULL, NUll, 'L', true);
 
-         // $data['online_count'] = $res['total'];
 
-         // $where2['L.status'] = 1;
 
-         // $res = $this->Lamp_model->get_list_by_multi_join($where2, 'count(*) as total',NULL, NULL, $join, NULL, NUll, 'L', true);
 
-         // $data['lighting_count'] = $res['total'];
 
-         $timeData = $this->Global_location_model->get_one(['id'=>$data['cityid']],'timezone');
 
-         if ($timeData['timezone'] < 0) {
 
-             $timezone = substr('00'.abs($timeData['timezone']), -2).':00';
 
-             $data['timezone'] = 'UTC/GMT -'.$timezone;
 
-         }else{
 
-             $timezone = substr('00'.abs($timeData['timezone']), -2).':00';
 
-             $data['timezone'] = 'UTC/GMT +'.$timezone;
 
-         }
 
-         if (empty($data['cityid'])) {
 
-             $data['countryId'] = 0;
 
-             $data['provinceId'] = 0;
 
-             $data['cityId'] = 0;
 
-             $data['areaId'] = 0;
 
-             $data['weatherList'] = [];
 
-             $data['longitude'] = '0';
 
-             $data['latitude'] = '0';
 
-             $location_ch = '暂无位置信息,请在编辑项目页面选择位置信息';
 
-             $location_en = 'There is no location information, please select location information on the edit project page.';
 
-         }else{
 
-             $res1 = $this->Global_location_model->get_one(['id'=>$data['cityid']],'level,pid,chinese_name,longitude,latitude,english_name');
 
-             $data['longitude'] = $res1['longitude'];
 
-             $data['latitude'] = $res1['latitude'];
 
-             $res2 = $this->Global_location_model->get_one(['id'=>$res1['pid']],'pid,chinese_name,english_name');
 
-             if ($res1['level'] == 4) {
 
-                 $data['areaId'] = $data['cityid'];
 
-                 $data['cityId'] = $res1['pid'];
 
-                 $data['provinceId'] = $res2['pid'];
 
-                 $res3 = $this->Global_location_model->get_one(['id'=>$res2['pid']],'pid,chinese_name,english_name');
 
-                 $data['countryId'] = $res3['pid'];
 
-                 $location_ch = $res3['chinese_name'].','.$res2['chinese_name'].','.$res1['chinese_name'];
 
-                 $location_en = $res1['english_name'].','.$res2['english_name'].','.$res3['english_name'];
 
-             }elseif ($res1['level'] == 3) {
 
-                 $data['areaId'] = 0;
 
-                 $data['cityId'] = $data['cityid'];
 
-                 $data['provinceId'] = $res1['pid'];
 
-                 $data['countryId'] = $res2['pid'];
 
-                 $location_ch = $res2['chinese_name'].','.$res1['chinese_name'];
 
-                 $location_en = $res1['english_name'].','.$res2['english_name'];
 
-             }elseif ($res1['level'] == 2) {
 
-                 $data['areaId'] = 0;
 
-                 $data['cityId'] = 0;
 
-                 $data['provinceId'] = $data['cityid'];
 
-                 $data['countryId'] = $res1['pid'];
 
-                 $location_ch = $res1['chinese_name'];
 
-                 $location_en = $res1['english_name'];
 
-             }else{
 
-                 $data['areaId'] = 0;
 
-                 $data['cityId'] = 0;
 
-                 $data['provinceId'] = 0;
 
-                 $data['countryId'] = $data['cityid'];
 
-                 $location_ch = $res1['chinese_name'];
 
-                 $location_en = $res1['english_name'];
 
-             }
 
-             $data['location'] = $location_en;
 
-             $project_weather = $this->db->query('select * from project_weather where projectId = '.$project_id)->row_array();
 
-             if (empty($project_weather) || time() - strtotime($project_weather['updatetime']) > 3600) {
 
-                 $url = 'https://way.jd.com/he/freeweather?city='.$res1['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
 
-                 $resData = request_post($url);
 
-                 $arr = json_decode($resData,true);
 
-                 if ($arr['code'] == 10000) {
 
-                     if ($arr['result']['HeWeather5'][0]['status'] == 'unknown location' || $arr['result']['HeWeather5'][0]['status'] != 'ok') {
 
-                         $url = 'https://free-api.heweather.net/s6/weather/forecast?location='.$res1['longitude'].','.$res1['latitude'].'&key=63dbc994630941fab269ad3c3af4d5d2';
 
-                         // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
 
-                         $resData = request_post($url);
 
-                         $arr = json_decode($resData,true);
 
-                         // var_dump($url);
 
-                         if ($arr['HeWeather6'][0]['status'] == 'ok') {
 
-                             $now = $arr['HeWeather6'][0]['daily_forecast'][0];
 
-                             $daily_forecast = $arr['HeWeather6'][0]['daily_forecast'];
 
-                             $weatherList = array();
 
-                             $weatherList[] = ['weatherType'=>$now['cond_txt_d'],'code'=>$now['cond_code_d'],'tmp'=>intval(($now['tmp_min'] + $now['tmp_max'])/2),'sr'=>$now['sr'],'ss'=>$now['ss'],'dateStr'=>date('D',strtotime($now['date'])),'date'=>date('m-d',strtotime($now['date']))];
 
-                             for ($i=1; $i < count($daily_forecast); $i++) { 
 
-                                 $weatherList[] = ['weatherType'=>$daily_forecast[$i]['cond_txt_d'],'code'=>$daily_forecast[$i]['cond_code_d'],'maxTmp'=>$daily_forecast[$i]['tmp_max'],'minTmp'=>$daily_forecast[$i]['tmp_min'],'sr'=>$daily_forecast[$i]['sr'],'ss'=>$daily_forecast[$i]['ss'],'dateStr'=>date('D',strtotime($daily_forecast[$i]['date'])),'date'=>date('m-d',strtotime($daily_forecast[$i]['date']))];
 
-                             }
 
-                             $data['weatherList'] = $weatherList;
 
-                             if (empty($project_weather)) {
 
-                                 $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
 
-                                 $this->db->query($sql);
 
-                             }else{
 
-                                 $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
 
-                                 $this->db->query($sql);
 
-                             }
 
-                         }else{
 
-                             $data['weatherList'] = [];
 
-                         }
 
-                         // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
 
-                         // $resData = request_post($url);
 
-                         // $arr = json_decode($resData,true);
 
-                     }else{
 
-                         if ($arr['result']['HeWeather5'][0]['status'] == 'ok') {
 
-                             $now = $arr['result']['HeWeather5'][0]['now'];
 
-                             $daily_forecast = $arr['result']['HeWeather5'][0]['daily_forecast'];
 
-                             $weatherList = array();
 
-                             $weatherList[] = ['weatherType'=>$now['cond']['txt'],'code'=>$now['cond']['code'],'tmp'=>$now['tmp'],'sr'=>$daily_forecast[0]['astro']['sr'],'ss'=>$daily_forecast[0]['astro']['ss'],'dateStr'=>date('D',strtotime($daily_forecast[0]['date'])),'date'=>date('m-d',strtotime($daily_forecast[0]['date']))];
 
-                             for ($i=1; $i < count($daily_forecast); $i++) { 
 
-                                 $weatherList[] = ['weatherType'=>$daily_forecast[$i]['cond']['txt_d'],'code'=>$daily_forecast[$i]['cond']['code_d'],'maxTmp'=>$daily_forecast[$i]['tmp']['max'],'minTmp'=>$daily_forecast[$i]['tmp']['min'],'sr'=>$daily_forecast[$i]['astro']['sr'],'ss'=>$daily_forecast[$i]['astro']['ss'],'dateStr'=>date('D',strtotime($daily_forecast[$i]['date'])),'date'=>date('m-d',strtotime($daily_forecast[$i]['date']))];
 
-                             }
 
-                             $data['weatherList'] = $weatherList;
 
-                             if (empty($project_weather)) {
 
-                                 $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
 
-                                 $this->db->query($sql);
 
-                             }else{
 
-                                 $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
 
-                                 $this->db->query($sql);
 
-                             }
 
-                         }else{
 
-                             $data['weatherList'] = [];
 
-                         }
 
-                     }
 
-                 }else{
 
-                     $data['weatherList'] = [];
 
-                 }
 
-             }else{
 
-                 $data['weatherList'] = json_decode($project_weather['weatherInfo'],true);
 
-                 // var_dump($data['weatherList']);die;
 
-             }
 
-         }
 
-         if (empty($version)) {
 
-             foreach ($data['weatherList'] as $key => $value) {
 
-                 $data['weatherList'][$key]['weatherType'] = weather_translate_cn($data['weatherList'][$key]['code']);
 
-                 $data['weatherList'][$key]['dateStr'] = date_translate_cn($data['weatherList'][$key]['dateStr']);
 
-             }
 
-         }else{
 
-             foreach ($data['weatherList'] as $key => $value) {
 
-                 $data['weatherList'][$key]['weatherType'] = weather_translate_en($data['weatherList'][$key]['code']);
 
-             }
 
-         }
 
-         exit(json_result('0000', $this->response['0000'], $data));
 
-     }
 
-     public function get_pram_data(){
 
-         $projectid = $this->input->post('projectid',true);
 
-         $res = $this->Load_model->get_data_by_filter(['relateid'=>$projectid,'cmdtype'=>2]);
 
-         if (empty($res)) {
 
-             $res = [];
 
-             $res['workmode']         = '0';
 
-             $res['worktimefirst']    = '4';
 
-             $res['workpowerfirst']   = '80';
 
-             $res['worktimesencond']  = '1';
 
-             $res['workpowersencond'] = '40';
 
-             $res['worktimethird']    = '8';
 
-             $res['workpowerthird']   = '30';
 
-             $res['worktimeforth']    = '0';
 
-             $res['workpowerforth']   = '30';
 
-             $res['voptically']       = '5';
 
-             $res['delaytime']        = '1';
 
-             $res['ledloadcurrent']   = '330';
 
-             $res['powercmd']         = '1';
 
-             $res['switchfeature']    = '1';
 
-         }
 
-         exit(json_result('0000',$this->response['0000'],$res));
 
-     }
 
-     // 设置负载参数
 
-     public function set_pram_data(){
 
-         $projectid = $this->input->post('projectid',true);
 
-         $controlType = intval($this->input->post('controlType',true));
 
-         if ($controlType == 0) {
 
-             $data['workmode'] = $this->input->post('workmode',true);
 
-             $data['worktimefirst'] = $this->input->post('worktimefirst',true);
 
-             $data['workpowerfirst'] = $this->input->post('workpowerfirst',true);
 
-             $data['worktimesencond'] = $this->input->post('worktimesencond',true);
 
-             $data['workpowersencond'] = $this->input->post('workpowersencond',true);
 
-             $data['worktimethird'] = $this->input->post('worktimethird',true);
 
-             $data['workpowerthird'] = $this->input->post('workpowerthird',true);
 
-             $data['worktimeforth'] = $this->input->post('worktimeforth',true);
 
-             $data['workpowerforth'] = $this->input->post('workpowerforth',true);
 
-             $data['voptically'] = $this->input->post('voptically',true);
 
-             $data['delaytime'] = $this->input->post('delaytime',true);
 
-             $data['ledloadcurrent'] = $this->input->post('ledloadcurrent',true);
 
-             $data['powercmd'] = $this->input->post('powercmd',true);
 
-             $data['switchfeature'] = $this->input->post('switchfeature',true);
 
-         }else{
 
-             $data['light_voltage']         = $this->input->post('light_voltage',true);
 
-             $data['intell_power']    = $this->input->post('intell_power',true);
 
-             $data['load_current']   = $this->input->post('load_current',true);
 
-             $data['light_delay']  = $this->input->post('light_delay',true);
 
-             $data['induction_delay'] = $this->input->post('induction_delay',true);
 
-             $data['first_light_time']    = $this->input->post('first_light_time',true);
 
-             $data['first_light_human_power']   = $this->input->post('first_light_human_power',true);
 
-             $data['first_light_unman_power']    = $this->input->post('first_light_unman_power',true);
 
-             $data['second_light_time']   = $this->input->post('second_light_time',true);
 
-             $data['second_light_human_power']       = $this->input->post('second_light_human_power',true);
 
-             $data['second_light_unman_power']        = $this->input->post('second_light_unman_power',true);
 
-             $data['third_light_time']   = $this->input->post('third_light_time',true);
 
-             $data['third_light_human_power']         = $this->input->post('third_light_human_power',true);
 
-             $data['third_light_unman_power']    = $this->input->post('third_light_unman_power',true);
 
-             $data['fourth_light_time']    = $this->input->post('fourth_light_time',true);
 
-             $data['fourth_light_human_power']    = $this->input->post('fourth_light_human_power',true);
 
-             $data['fourth_light_unman_power']    = $this->input->post('fourth_light_unman_power',true);
 
-             $data['fifth_light_time']    = $this->input->post('fifth_light_time',true);
 
-             $data['fifth_light_human_power']    = $this->input->post('fifth_light_human_power',true);
 
-             $data['fifth_light_unman_power']    = $this->input->post('fifth_light_unman_power',true);
 
-             $data['sixth_light_time']    = $this->input->post('sixth_light_time',true);
 
-             $data['sixth_light_human_power']    = $this->input->post('sixth_light_human_power',true);
 
-             $data['sixth_light_unman_power']    = $this->input->post('sixth_light_unman_power',true);
 
-             $data['seventh_light_time']    = $this->input->post('seventh_light_time',true);
 
-             $data['seventh_light_human_power']    = $this->input->post('seventh_light_human_power',true);
 
-             $data['seventh_light_unman_power']    = $this->input->post('seventh_light_unman_power',true);
 
-             $data['eighth_light_time']    = $this->input->post('eighth_light_time',true);
 
-             $data['eighth_light_human_power']    = $this->input->post('eighth_light_human_power',true);
 
-             $data['eighth_light_unman_power']    = $this->input->post('eighth_light_unman_power',true);
 
-             $data['ninth_light_time']    = $this->input->post('ninth_light_time',true);
 
-             $data['ninth_light_human_power']    = $this->input->post('ninth_light_human_power',true);
 
-             $data['ninth_light_unman_power']    = $this->input->post('ninth_light_unman_power',true);
 
-             $data['tenth_light_time']    = $this->input->post('tenth_light_time',true);
 
-             $data['tenth_light_human_power']    = $this->input->post('tenth_light_human_power',true);
 
-             $data['tenth_light_unman_power']    = $this->input->post('tenth_light_unman_power',true);
 
-             $data['begin_voltage']    = $this->input->post('begin_voltage',true);
 
-             $data['end_voltage']    = $this->input->post('end_voltage',true);
 
-             $data['end_current']    = $this->input->post('end_current',true);
 
-         }
 
-         $lampList = $this->db->query('select L.id,N.protocoltype,L.protocoltype as lampprotocoltype,L.address,N.deviceid from lampinfo AS L left join network AS N on L.networkid = N.id where L.projectid = '.$projectid)->result_array();
 
-         foreach ($lampList as $value) {
 
-             $data['relateid'] = $value['id'];
 
-             $data['cmdtype'] = 0;
 
-             $data['updatetime'] = date('Y-m-d H:i:s',time());
 
-             if ($controlType == 0) {
 
-                 if (($value['protocoltype'] == 4 || $value['protocoltype'] == 6) && $value['lampprotocoltype'] == 0) {
 
-                     $address = '';
 
-                     if (mb_strlen($value['address']) >= 8) {
 
-                         $address = mb_substr($value['address'], -8);
 
-                     }else{
 
-                         $address = '00000000';
 
-                     }
 
-                     $sendData = '10040010';
 
-                     $sendData .= bitSubStr(base_convert($data['workmode'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['worktimefirst'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['workpowerfirst'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['worktimesencond'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['workpowersencond'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['worktimethird'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['workpowerthird'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['worktimeforth'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['workpowerforth'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['delaytime'], 10, 16));
 
-                     $sendData .= doubBitSubStr(base_convert(intval($data['voptically']*10), 10, 16));
 
-                     $sendData .= doubBitSubStr(base_convert(intval($data['ledloadcurrent']), 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['powercmd'], 10, 16));
 
-                     $sendData .= bitSubStr(base_convert($data['switchfeature'], 10, 16));
 
-                     $sum = 0;
 
-                     for ($i=0; $i < mb_strlen($sendData)/2; $i++) { 
 
-                         if ($i == 0) continue;
 
-                         $t = mb_substr($sendData, $i*2,2);
 
-                         $sum += base_convert($t, 16, 10);
 
-                     }
 
-                     $sum = base_convert($sum, 10, 16);
 
-                     $sendData .= bitSubStr($sum);
 
-                     $sendData = $address.$sendData;
 
-                     
 
-                     if ($value['protocoltype'] == 4) {
 
-                         $res = lampMqttCmd('/IDCOL/CmdInput/'.$value['address'],'/IDCOL/CmdOutput/'.$value['address'],pack('H*','0001'.$sendData),0);
 
-                     }else{
 
-                         device_cmd($value['deviceid'],pack('H*',$sendData));
 
-                     }
 
-                     continue;
 
-                 }
 
-                 $res = $this->Load_model->get_data_by_filter(['relateid'=>$projectid,'cmdtype'=>2]);
 
-                 if(empty($res)){
 
-                     $id = $this->Load_model->insert($data);
 
-                 } else {
 
-                     $this->Load_model->update(['id'=>$res['id']],$data);
 
-                     $id = $res['id'];
 
-                 }
 
-                 $cmd = '{"cmd_type":"load_param_cmd","cmd_id":'.$id.',"broadcast":0}';
 
-                 $cmdret = send_cmd($cmd,0,0,$value['protocoltype']);
 
-             }else{
 
-                 $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_load_asy';
 
-                 $temp = $data;
 
-                 $temp['username'] = $this->input->post('username',true);
 
-                 $temp['client_key'] = $this->input->post('client_key',true);
 
-                 $temp['token'] = $this->input->post('token',true);
 
-                 $temp['clientid'] = $this->input->post('clientid',true);
 
-                 $temp['version'] = $this->session->userdata('version');
 
-                 $temp['lamp_id'] = $value['id'];
 
-                 doAsyncRequest($url,$temp);
 
-                 // if (($value['protocoltype'] == 4 || $value['protocoltype'] == 6) && $value['lampprotocoltype'] == 0){
 
-                 //     continue;
 
-                 // }
 
-                 
 
-                 // $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$value['id'],'cmdtype'=>0]);
 
-                 // if(empty($res)){
 
-                 //     $id = $this->Modbus_load_model->insert($data);
 
-                 // } else {
 
-                 //     $this->Modbus_load_model->update(['id'=>$res['id']],$data);
 
-                 //     $id = $res['id'];
 
-                 // }
 
-                 // $cmd = '{"cmd_type":"modbus_load_param_cmd","cmd_id":'.$id.',"broadcast":0}';
 
-                 // $cmdret = send_cmd($cmd,0,0,$value['protocoltype']);
 
-             }
 
-             
 
-         }
 
-         exit(json_result('0000',$this->response['0000']));
 
-         
 
-     }
 
-     // 项目管理侧边导航栏
 
-     public function nav(){
 
-         $userid = $this->get_user_info('id');
 
-         $company = $this->get_user_info('company');
 
-         $role = $this->get_user_info('role');
 
-         $keyword = $this->input->post('keyword');
 
-         $searchType = $this->input->post('searchType');
 
-         $type = $this->input->post('type',true);
 
-         $where = array();
 
-         if (empty($type)) {
 
-             if ($role != SYSTEM_ADMIN) $where['company'] = $company;
 
-             $data = $this->Project_model->get_list($where, 'projectname,id,lampcount',NULL, NULL, 'convert(projectname using gbk) asc', NUll);
 
-             if (!empty($data)) {
 
-                 $temp = array();
 
-                 foreach ($data as $key => $value) {
 
-                     $value['sectionList'] = array();
 
-                     $temp[$value['id']] = $value;
 
-                 }
 
-                 $projectIdArr = array_column($data, 'id');
 
-                 $sectionList = $this->Lamp_model->get_list(['projectid'=>$projectIdArr], 'section as name,projectid,count(*) as lampcount',NULL, NULL, 'convert(section using gbk) asc', 'projectid,section');
 
-                 foreach ($sectionList as $key => $value) {
 
-                     if (isset($temp[$value['projectid']])) {
 
-                         unset($value['projectid']);
 
-                         $temp[$sectionList[$key]['projectid']]['sectionList'][] = $value;
 
-                     }
 
-                 }
 
-                 $data = array_values($temp);
 
-             }
 
-             exit(json_result('0000',$this->response['0000'],$data));
 
-         }else{
 
-             if ($role != SYSTEM_ADMIN) $where['P.company'] = $company;
 
-             $join = [];
 
-             $join[] = ['table'=>'project as P','cond'=>'L.projectid = P.id','type'=>'inner'];
 
-             if ($type == 2) {
 
-                 $sectionWhere = ['manu !='=>0];
 
-                 $join[] = ['table'=>'company as C','cond'=>'L.manu = C.id','type'=>'inner'];
 
-                 $group = 'L.manu,L.projectid';
 
-                 $order = 'convert(C.name using gbk) asc';
 
-                 $fields = 'C.name as company,C.no,P.projectname,P.id,C.id as cId,count(*) as lampcount';
 
-             }elseif ($type == 3) {
 
-                 $sectionWhere = ['supplier !='=>0];
 
-                 $join[] = ['table'=>'company as C','cond'=>'L.supplier = C.id','type'=>'inner'];
 
-                 $group = 'L.supplier,L.projectid';
 
-                 $order = 'convert(C.name using gbk) asc';
 
-                 $fields = 'C.name as company,C.no,P.projectname,P.id,C.id as cId,count(*) as lampcount';
 
-             }elseif ($type == 4) {
 
-                 $sectionWhere = ['po !='=>0];
 
-                 $join[] = ['table'=>'company as C','cond'=>'L.po = C.id','type'=>'inner'];
 
-                 $group = 'L.po,L.projectid';
 
-                 $order = 'convert(C.name using gbk) asc';
 
-                 $fields = 'C.name as company,C.no,P.projectname,P.id,C.id as cId,count(*) as lampcount';
 
-             }else {
 
-                 // $sectionWhere = ['upazilla !='=>0];
 
-                 $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'left'];
 
-                 // $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'left'];
 
-                 $group = 'P.cityid,p.id';
 
-                 $order = 'convert(G.english_name using gbk) asc';
 
-                 $fields = 'G.english_name as company,P.projectname,P.id,G.id as cId,count(*) as lampcount';
 
-             }
 
-             $data = $this->Lamp_model->get_list_by_join($where, $fields,NULL, NULL, $join, $order, $group, 'L');
 
-             if (!empty($data)) {
 
-                 $temp = array();
 
-                 $temp2 = array();
 
-                 foreach ($data as $key => $value) {
 
-                     $value['sectionList'] = array();
 
-                     $temp[$value['id']] = $value;
 
-                     if (isset($value['no']) && $value['no'] !== '') $value['company'] = $value['no'];
 
-                     $temp2[$value['cId']] = array('company'=>$value['company'],'lampcount'=>$value['lampcount'],'cId'=>$value['cId'],'projectList'=>array());
 
-                 }
 
-                 $projectIdArr = array_column($data, 'id');
 
-                 $sectionWhere['projectid'] = $projectIdArr;
 
-                 $sectionList = $this->Lamp_model->get_list($sectionWhere, 'section as name,projectid,count(*) as lampcount',NULL, NULL, 'convert(section using gbk) asc', 'projectid,section');
 
-                 foreach ($sectionList as $key => $value) {
 
-                     if (isset($temp[$value['projectid']])) {
 
-                         unset($value['projectid']);
 
-                         $temp[$sectionList[$key]['projectid']]['sectionList'][] = $value;
 
-                     }
 
-                 }
 
-                 $temp = array_values($temp);
 
-                 foreach ($temp as $key => $value) {
 
-                     if (isset($temp2[$value['cId']])) {
 
-                         unset($value['cId']);
 
-                         unset($value['company']);
 
-                         $temp2[$temp[$key]['cId']]['projectList'][] = $value;
 
-                     }
 
-                 }
 
-                 $data = array_values($temp2);
 
-             }
 
-             exit(json_result('0000',$this->response['0000'],$data));
 
-         }
 
-     }
 
-     // 获取项目下拉列表
 
-     public function get() {
 
-         $role = $this->get_user_info('role');
 
-         $company = $this->get_user_info('company');
 
-         $where = array();
 
-         if ($role != SYSTEM_ADMIN) $where['company'] = $company;
 
-         $list = $this->Project_model->get_list($where,'projectname,id');
 
-         exit(json_result('0000', $this->response['0000'], array('list'=>$list)));
 
-     }
 
-     private function getLeftTimeByProject($project_id){
 
-         $lampid_arr = $this->Lamp_model->getLampIds(array('project' => $project_id));
 
-         $time_arr = $this->Patrol_model->get_list_in('id',$lampid_arr,'nextquerytime as time');
 
-         if (empty($time_arr)) {
 
-             return 0;
 
-         }else{
 
-             $querytime = max($time_arr)['time'];
 
-             $nowtime = time();
 
-             return $querytime>$nowtime ? intval(($querytime-$nowtime)/60) : 0; 
 
-         }
 
-     }
 
-     // 获取项目详情
 
-     public function detail() {
 
-         $project_id = $this->input->post('project_id', true);
 
-         if(empty($project_id)){
 
-             exit(json_result('0200', $this->response['0200'], array()));
 
-         }
 
-         $role = $this->get_user_info('role');
 
-         $userid = $this->get_user_info('id');
 
-         $fields = 'projectname as project_name,
 
-                    projectid as project_no,
 
-                    address,
 
-                    company,
 
-                    createtime as create_time,
 
-                    patrolinterval,
 
-                    cityid,
 
-                    lampcount as install_num';
 
-         $data = $this->Project_model->getData(array('id' => $project_id), $fields);
 
-         $data['timezone'] = 6;
 
-         $version = $this->session->userdata('version');
 
-         if(!$data){
 
-             exit(json_result('0202', $this->response['0202'], $data));
 
-         } else {
 
-             if (empty($data['cityid'])) {
 
-                 $data['countryId'] = 0;
 
-                 $data['provinceId'] = 0;
 
-                 $data['cityId'] = 0;
 
-                 $data['areaId'] = 0;
 
-                 $data['weatherList'] = [];
 
-                 $data['longitude'] = '0';
 
-                 $data['latitude'] = '0';
 
-                 $location_ch = '暂无位置信息,请在编辑项目页面选择位置信息';
 
-                 $location_en = 'There is no location information, please select location information on the edit project page.';
 
-             }else{
 
-                 $res1 = $this->Global_location_model->get_one(['id'=>$data['cityid']],'level,pid,chinese_name,longitude,latitude,english_name');
 
-                 $data['longitude'] = $res1['longitude'];
 
-                 $data['latitude'] = $res1['latitude'];
 
-                 $res2 = $this->Global_location_model->get_one(['id'=>$res1['pid']],'pid,chinese_name,english_name');
 
-                 if ($res1['level'] == 4) {
 
-                     $data['areaId'] = $data['cityid'];
 
-                     $data['cityId'] = $res1['pid'];
 
-                     $data['provinceId'] = $res2['pid'];
 
-                     $res3 = $this->Global_location_model->get_one(['id'=>$res2['pid']],'pid,chinese_name,english_name');
 
-                     $data['countryId'] = $res3['pid'];
 
-                     $location_ch = $res3['chinese_name'].','.$res2['chinese_name'].','.$res1['chinese_name'];
 
-                     $location_en = $res1['english_name'].','.$res2['english_name'].','.$res3['english_name'];
 
-                 }elseif ($res1['level'] == 3) {
 
-                     $data['areaId'] = 0;
 
-                     $data['cityId'] = $data['cityid'];
 
-                     $data['provinceId'] = $res1['pid'];
 
-                     $data['countryId'] = $res2['pid'];
 
-                     $location_ch = $res2['chinese_name'].','.$res1['chinese_name'];
 
-                     $location_en = $res1['english_name'].','.$res2['english_name'];
 
-                 }elseif ($res1['level'] == 2) {
 
-                     $data['areaId'] = 0;
 
-                     $data['cityId'] = 0;
 
-                     $data['provinceId'] = $data['cityid'];
 
-                     $data['countryId'] = $res1['pid'];
 
-                     $location_ch = $res1['chinese_name'];
 
-                     $location_en = $res1['english_name'];
 
-                 }else{
 
-                     $data['areaId'] = 0;
 
-                     $data['cityId'] = 0;
 
-                     $data['provinceId'] = 0;
 
-                     $data['countryId'] = $data['cityid'];
 
-                     $location_ch = $res1['chinese_name'];
 
-                     $location_en = $res1['english_name'];
 
-                 }
 
-             }
 
-             $data['location'] = $location_en;
 
-             exit(json_result('0000', $this->response['0000'], $data));
 
-         }
 
-     }
 
-     // 添加编辑项目
 
-     public function save() {
 
-         $role = $this->get_user_info('role');
 
-         $where['id']         = $this->input->post('project_id',true);
 
-         $data['projectname'] = trim($this->input->post('project_name',true));
 
-         
 
-         $data['cityid']    = $this->input->post('pid',true);
 
-         $address     = $this->input->post('address',true);
 
-         $patrolinterval = $this->input->post('patrolinterval',true);
 
-         if(isset($address)) $data['address'] = $address;
 
-         if(isset($patrolinterval)) $data['patrolinterval'] = $patrolinterval;
 
-         if(empty($data['projectname'])) exit(json_result('0204',$this->response['0204'],array()));
 
-         
 
-         if (empty($where['id'])) {  // 添加项目
 
-             $data['userId'] = $this->get_user_info('id');
 
-             $data['company'] = $this->get_user_info('company');
 
-             if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$data['company'])) > 0) {
 
-                 exit(json_result('0203', $this->response['0203'], array()));
 
-             }
 
-             $data['projectid']   = $this->input->post('project_no',true);
 
-             if(empty($data['projectid'])) exit(json_result('0205',$this->response['0205'],array()));
 
-             if(mb_strlen($data['projectid']) > 20) exit(json_result('0210',$this->response['0210'],array()));
 
-             if(!preg_match('/^[A-Za-z0-9]+$/', $data['projectid'])) exit(json_result('0212',$this->response['0212'],array()));
 
-             
 
-             
 
-             $data['createtime']  = date('Y-m-d H:i:s',time() - 8*3600);
 
-             $projectId = $this->Project_model->add($data);
 
-             
 
-             // $projectId = $data['id'];
 
-             $this->add_operation_log('insert',"添加\"{$data['projectname']}\"项目,项目编号\"{$data['projectid']}\"",0);
 
-             $this->add_operation_log('insert',"Add project.Project name:\"{$data['projectname']}\".Project number:\"{$data['projectid']}\"",0,1);
 
-         } else {  // 编辑项目
 
-             $company = $this->get_user_info('company');
 
-             if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$company), $where['id']) > 0) {
 
-                 exit(json_result('0203', $this->response['0203'], array()));
 
-             }
 
-             $ret = $this->Project_model->get_one($where);
 
-             if ($ret['cityid'] != $data['cityid']) {
 
-                 $sql = "delete from project_weather where projectId = ".$where['id'];
 
-                 $this->db->query($sql);
 
-             }
 
-             $this->Project_model->update($data,$where);
 
-             $projectId = $where['id'];
 
-             // 修改项目下巡检信息
 
-             // if (isset($patrolinterval)) {
 
-             //     if (empty($patrolinterval)) {
 
-             //         $patrolData['patroltype']     = 0;
 
-             //     }else{
 
-             //         $patrolData['patroltype']     = 1;
 
-             //         $patrolData['patrolinterval'] = intval($patrolinterval);
 
-             //         $patrolData['updatetime']     = date("Y-m-d H:i:s", time());
 
-             //         $patrolData['nextquerytime']  = date("Y-m-d H:i:s", time());
 
-             //     }
 
-             //     $idArr = $this->Lamp_model->get_list_by_project($projectId,'L.id,N.protocoltype,L.address');
 
-             //     foreach ($idArr as $v) {
 
-             //         $res = $this->Patrol_model->getOne($v['id']);
 
-             //         if(empty($res)){
 
-             //             $patrolData['id'] = $v['id'];
 
-             //             $this->Patrol_model->insert($patrolData);
 
-             //             unset($patrolData['id']);
 
-             //         } else {
 
-             //             $this->Patrol_model->update(array('id'=>$v['id']), $patrolData);
 
-             //         }
 
-             //         if ($v['protocoltype'] == 4) {
 
-             //             $sendData = '000101A501';;
 
-             //             $sendData .= bitSubStr(base_convert(intval($patrolData['patrolinterval']/5), 10, 16));
 
-             //             lampMqttCmd('/lamp/WriteIn/'.$v['address'],'/lamp/WriteOut/'.$v['address'],pack('H*', $sendData),0);
 
-             //             continue;
 
-             //         }
 
-             //         if (isset($v['protocoltype']) && $v['protocoltype'] != 0) {
 
-             //             $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$v['id'].',"broadcast":0}';
 
-             //             $cmdret = send_cmd($cmd, 1,0,$v['protocoltype']);
 
-             //         }
 
-             //     }
 
-             // }
 
-             $this->add_operation_log('update',"修改\"{$data['projectname']}\"项目,项目编号\"{$ret['projectid']}\"",0);
 
-             $this->add_operation_log('update',"Update project.Project name:\"{$data['projectname']}\".Project number:\"{$ret['projectid']}\"",0,1);
 
-         }
 
-         exit(json_result('0000', $this->response['0000'], array("id" => intval($projectId))));
 
-     }
 
-     // 删除项目
 
-     public function del() {
 
-         $role = $this->get_user_info('role');
 
-         $project_id = $this->input->post('project_id', true);
 
-         if(empty($project_id)) exit(json_result('0007', $this->response['0007'], array()));
 
-         $lampListInfo = $this->Lamp_model->get_one(['projectid'=>$project_id],'id');
 
-         
 
-         if (!empty($lampListInfo)) exit(json_result('0214',$this->response['0214']));
 
-         $net_arr = $this->Network_model->get_one(['projectid'=>$project_id],'id');
 
-         if (!empty($net_arr)) exit(json_result('0215',$this->response['0215']));
 
-         // 删除项目下的监控
 
-         $this->Videomonitor_model->delete(array('projectid'=>$project_id));
 
-         $this->Weathermonitor_model->delete(array('projectid'=>$project_id));
 
-         $projectData = $this->Project_model->getData(array('id'=>$project_id),'projectname,projectid');
 
-         $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目,项目编号\"{$projectData['projectid']}\"",0);
 
-         $this->add_operation_log('delete',"Delete project.Project name:\"{$projectData['projectname']}\".Project number:\"{$projectData['projectid']}\"",0,1);
 
-         // 删除项目
 
-         $this->Project_model->delete(['id'=>$project_id]);
 
-         exit(json_result('0000', $this->response['0000'], array()));
 
-     }
 
-     private function do_setting($id, $data){
 
-         $ret = $this->Patrol_model->getOne($id);
 
-         if(empty($ret)){
 
-             $data['id'] = $id;
 
-             $this->Patrol_model->insert($data);
 
-         } else {
 
-             $this->Patrol_model->update(array('id'=>$id), $data);
 
-         }
 
-         $res = $this->Lamp_model->getOne($id,'N.protocoltype,L.address,L.protocoltype as lampprotocoltype');
 
-         if ($res['protocoltype'] == 4) {
 
-             $sendData = '000101A501';;
 
-             $sendData .= bitSubStr(base_convert(intval($data['patrolinterval']/5), 10, 16));
 
-             lampMqttCmd('/lamp/WriteIn/'.$res['address'],'/lamp/WriteOut/'.$res['address'],pack('H*', $sendData),0);
 
-         }
 
-         if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
 
-             $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$id.',"broadcast":0}';
 
-             $cmdret = send_cmd($cmd, 1,0,$res['protocoltype']);
 
-         }
 
-     }
 
-     // 巡检设置
 
-     public function patrol(){
 
-         $username = $this->input->post('username',true);
 
-         $role = $this->input->post('role',true);
 
-         $client_key = $this->input->post('client_key',true);
 
-         $token = $this->input->post('token',true);
 
-         $clientid = $this->input->post('clientid',true);
 
-         $version = $this->session->userdata('version');
 
-         $lampid = intval($this->input->post('lampid', true));
 
-         $interval = $this->input->post('interval',true);
 
-         $lampArr = explode(',', $lamp_id);
 
-         // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
 
-         $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/project/patrol_asy';
 
-         
 
-         // foreach ($lampArr as $l) {
 
-             doAsyncRequest($url,['clientid'=>$clientid,'lampid'=>$lampid,'interval'=>$interval,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'role'=>$role]);
 
-         // }
 
-         
 
-         exit();
 
-     }
 
-     public function patrol_asy() {
 
-         $lampid = intval($this->input->post('lampid', true));
 
-         $interval = $this->input->post('interval',true);
 
-         if (empty($lampid)) exit(json_result('0007',$this->response['0007']));
 
-         $lampData = $this->Lamp_model->get_one(['id'=>$lampid],'address');
 
-         $address = substr($lampData['address'], 2);
 
-         $sendData = '000A';
 
-         $interval = base_convert($interval, 10, 16);
 
-         $sendData .= substr('0000'.$interval, -4);
 
-         $res = $this->send_cmd('16',$address,$senddata);
 
-         if (empty($res)) {
 
-             exit(json_result('0016',$this->response['0016']));
 
-         }else{
 
-             $res = unpack('H*', $res['msg'])[1];
 
-             if (substr($res, 18,2) != '01') exit(json_result('0010',$this->response['0010']));
 
-         }
 
-         exit(json_result('0000', $this->response['0000'], array()));
 
-     }
 
-     // 固件升级
 
-     public function upgrade(){
 
-         $userid = $this->get_user_info('id');
 
-         if ($userid != 263) exit(json_result('0011',$this->response['0011']));
 
-         $projectid = intval($this->input->post('projectId',true));
 
-         if (empty($projectid)) exit(json_result('0007',$this->response['0007']));
 
-         $sql = 'select L.address from lampinfo AS L left join network AS N ON L.networkid = N.id where L.projectid = '.$projectid.' AND N.protocoltype = 4';
 
-         $lampList = $this->db->query($sql)->result_array();
 
-         $url = trim($this->input->post('url',true));
 
-         if (empty($url)) exit(json_result('0000',$this->response['0000']));
 
-         // $url = 'http://station-iot.com/app/m26mqtt/opencpu_m26mqtt_1_0_13.bin';
 
-         $len = base_convert(strlen($url), 10, 16).'';
 
-         if (mb_strlen($len) > 2) {
 
-             $len = mb_substr($len, -2);
 
-         }elseif (mb_strlen($len) <= 1) {
 
-             $len = '0'.$len;
 
-         }
 
-         foreach ($lampList as $lampData) {
 
-             lampMqttCmd('/lamp/UpgradeIn/'.$lampData['address'],'/lamp/UpgradeOut/'.$lampData['address'],pack('H*','0001'.$len.'00'.$url),0);
 
-         }
 
-         $cmd = '{"cmd_type":"app_upgrade_cmd","relateid":'.$projectid.',"senddata":"'.$url.'","cmd_id":"0"}';
 
-         $cmdret = send_cmd($cmd,1,0,2);
 
-         exit(json_result('0000',$this->response['0000']));
 
-     }
 
- }
 
 
  |