123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917 |
- <?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');
- $companyid = $this->get_user_info('companyid');
- $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 = $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 as location,createtime as create_time,cityid');
- $data['number'] = $proData['number'];
- $data['location'] = $proData['location'];
- $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 ($company !== '' && $company !== NUll) {
- if ($type == 1) {
- $where['manu'] = $company;
- $where2['L.manu'] = $company;
- }elseif ($type == 2) {
- $where['supplier'] = $company;
- $where2['L.supplier'] = $company;
- }elseif ($type == 3) {
- $where['po'] = $company;
- $where2['L.po'] = $company;
- }else {
- $where['upazilla'] = $company;
- $where2['L.upazilla'] = $company;
- }
- }
- $data['total_lamp'] = $this->Lamp_model->get_count($where);
- $where['isfaulted'] = 1;
- $data['failure_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'];
- $data['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->getOne($data['cityid'],'level,pid,chinese_name,longitude,latitude,english_name');
- $data['longitude'] = $res1['longitude'];
- $data['latitude'] = $res1['latitude'];
- $res2 = $this->Global_location_model->getOne($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->getOne($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'];
- }
- $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') {
- $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('/WE/TransIn/'.$value['address'],'/WE/TransOut/'.$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',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',NULL, NULL, 'convert(section using gbk) asc', 'projectid,section');
- foreach ($sectionList as $key => $value) {
- if (isset($temp[$value['projectid']])) {
- unset($value['projectid']);
- $temp[$value['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 == 1) {
- $group = 'L.manu,L.projectid';
- $order = 'convert(L.manu using gbk) asc';
- $fields = 'L.manu as company,P.projectname,P.id';
- }elseif ($type == 2) {
- $group = 'L.supplier,L.projectid';
- $order = 'convert(L.supplier using gbk) asc';
- $fields = 'L.supplier as company,P.projectname,P.id';
- }elseif ($type == 3) {
- $group = 'L.po,L.projectid';
- $order = 'convert(L.po using gbk) asc';
- $fields = 'L.po as company,P.projectname,P.id';
- }else {
- $group = 'L.upazilla,L.projectid';
- $order = 'convert(L.upazilla using gbk) asc';
- $fields = 'L.upazilla as company,P.projectname,P.id';
- }
- $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;
- $temp2[$value['company']] = array('company'=>$value['company'],'projectList'=>array());
- }
- $projectIdArr = array_column($data, 'id');
- $sectionList = $this->Lamp_model->get_list(['projectid'=>$projectIdArr], 'section as name,projectid',NULL, NULL, 'convert(section using gbk) asc', 'projectid,section');
- foreach ($sectionList as $key => $value) {
- if (isset($temp[$value['projectid']])) {
- unset($value['projectid']);
- $temp[$value['projectid']]['sectionList'][] = $value;
- }
- }
- $temp = array_values($temp);
- foreach ($temp as $key => $value) {
- if (isset($temp2[$value['company']])) {
- unset($value['company']);
- $temp[$value['company']]['projectList'][] = $value;
- }
- }
- $data = array_values($temp2);
- }
- exit(json_result('0000',$this->response['0000'],$data));
- }
- }
- // 获取项目下拉列表
- public function get() {
- $companyid = $this->input->post('companyid',true);
- $role = $this->get_user_info('role');
- $userid = $this->get_user_info('id');
- $keyword = $this->input->post('keyword', true);
- $condition = array();
- if (!empty($keyword)) {
- $condition['keyword'] = $keyword;
- }
- if (empty($companyid)) {
- $companyid = $this->get_user_info('companyid');
- $data = $this->Project_model->getMultiData($condition, 'P.id, P.projectname as name, P.networkcount as network', $role, $companyid, $userid);
- }else{
- $zone = $this->get_user_info('zone');
- $data = $this->Project_model->get_list_by_company('id, projectname as name,company', $role, $companyid, $zone);
- }
-
- if (!empty($data)) {
- foreach ($data as $key => $value) {
- $data[$key]['tag'] = get_first_char($value['name']);
- }
- }
- exit(json_result('0000', $this->response['0000'], array('projects'=>$data)));
- }
- 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');
- $companyid = $this->get_user_info('companyid');
- $userid = $this->get_user_info('id');
- $fields = 'projectname as project_name,
- projectid as project_no,
- guestname as customer,
- company as company_id,
- province as province_id,
- zone as zone_id,
- address,
- createtime as create_time,
- timezone,
- patrolinterval,
- cityid,
- lampcount as install_num';
- $data = $this->Project_model->getData(array('id' => $project_id), $fields);
- $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->getOne($data['cityid'],'level,pid,chinese_name,longitude,latitude,english_name');
- $data['longitude'] = $res1['longitude'];
- $data['latitude'] = $res1['latitude'];
- $res2 = $this->Global_location_model->getOne($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->getOne($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'];
- }
- $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') {
- $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;
- }
- }
- $data['company'] = $this->Company_model->get_data_by_id($data['company_id'], 'name');
- if (empty($version)) {
- $data['location'] = $location_ch;
- $res = $this->Project_model->get_timezone_info(array('id'=>$data['timezone']),'name');
- 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{
- $data['location'] = $location_en;
- foreach ($data['weatherList'] as $key => $value) {
- $data['weatherList'][$key]['weatherType'] = weather_translate_en($data['weatherList'][$key]['code']);
- }
- $res = $this->Project_model->get_timezone_info(array('id'=>$data['timezone']),'enname as name');
- }
- $data['timezone_name'] = $res['name'];
- $data['patrol_time'] = strval($this->getLeftTimeByProject($project_id));
- // 获取项目下的网络列表
- $data['network_list'] = $this->Network_model->get_list_by_filter(array('projectid'=>$project_id));
- $temp = array();
- foreach ($data['network_list'] as $key => $value) {
- if (isset($value['gatewaytype']) && $value['gatewaytype'] == 'direct') {
- continue;
- }
- $temp[] = $value;
- }
- $data['network_list'] = $temp;
- 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['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['userId'] = $this->get_user_info('id');
- $data['company'] = $this->get_user_info('company');
-
- $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 { // 编辑项目
- if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$data['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('/WE/WriteIn/'.$v['address'],'/WE/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('/WE/WriteIn/'.$res['address'],'/WE/WriteOut/'.$res['address'],pack('H*', $sendData),0);
- continue;
- }
- 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() {
- $role = $this->get_user_info('role');
- // if ($role == COMPANY_CUSTOMER) {
- // exit(json_result('0011', $this->response['0011'], array()));
- // }
- $type = $this->input->post('type', true);
- $value = $this->input->post('value', true);
- $switch = $this->input->post('switch',true);
- $interval = $this->input->post('interval',true);
- if(!in_array($type, array('project','network','lamp')) || empty($interval) || empty($value)) {
- exit(json_result('0005', $this->response['0005'], array()));
- }
- $nowtime = time();
- $data['patroltype'] = intval($switch);
- $data['patrolinterval'] = intval($interval);
- $data['updatetime'] = date("Y-m-d H:i:s", $nowtime);
- $data['nextquerytime'] = date("Y-m-d H:i:s", time());
- if ($type == 'project') {
- $idArr = $this->Lamp_model->getLampIds(array('project'=>$value));
- }elseif ($type == 'network') {
- $idArr = $this->Lamp_model->getLampIds(array('network'=>$value));
- }else{
- $id = $value;
- $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($interval/5), 10, 16));
- lampMqttCmd('/WE/WriteIn/'.$res['address'],'/WE/WriteOut/'.$res['address'],pack('H*', $sendData),0);
- }else{
- 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']);
- // if (empty($cmdret)) exit(json_result('0016',$this->response['0016'], array('number'=>$lampData['number'])));
- // if($cmdret === false){
- // exit(json_result('0008', $this->response['0008']));
- // } else {
- // $res = json_decode($cmdret, true);
- // }
- // if (isset($res['result']) && $res['result'] == true) {
- // exit(json_result('0000', $this->response['0000']));
- // } else {
- // $version = $this->session->userdata('version');
- // if (empty($version)) {
- // $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
- // exit(json_result('0010', $res['msg']));
- // }else{
- // $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
- // exit(json_result('other', $res['msg']));
- // }
- // }
- }
- }
-
- }
-
- if (!empty($idArr)) {
- foreach ($idArr as $id) {
- $this->do_setting($id, $data);
- }
- }
- 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('/WE/UpgradeIn/'.$lampData['address'],'/WE/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']));
- }
- }
|