123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 |
- <?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));
- $version = $this->session->userdata('version');
- // 总网络数
- $network_total = $this->Network_model->getTotal(array('projectid'=>$project_id), $role, $companyid, $userid);
- // 告警信息列表
- if (!empty($projectid)) {
- $alarm_list = $this->Alarm_model->getList(array('projectid'=>$project_id,'status'=>0,'type'=>0),$role,$userid,1,HOME_ALARM_COUNT);
- }else{
- $alarm_list = $this->Alarm_model->getList(array('status'=>0,'type'=>0),$role,$userid,1,HOME_ALARM_COUNT);
- }
- // 系统日志列表
- $field = "U.username as username,S.content as content,S.optype as optype,S.time as time";
- // 日志权限控制
- $filter = array('language'=>0);
- if ($role == COMPANY_ADMIN) {
- $filter['companyid'] = $this->get_user_info('companyid');
- }elseif ($role == COMPANY_CUSTOMER) {
- $filter['userid'] = $this->get_user_info('id');
- }
- if (empty($version)) {
- $syslog_list = $this->Syslog_model->getList($filter,1,HOME_SYSLOG_COUNT,$field);
- }else{
- $filter['language'] = 1;
- // 英文版
- foreach ($alarm_list as $k=>$value) {
- $stralarmtype = trim($value['stralarmtype']);
- if (!empty($stralarmtype)) {
- $alarmArr = explode(',', $stralarmtype);
- $temp = array();
- foreach ($alarmArr as $a) {
- $temp[] = alarm_translate($a);
- }
- $alarm_list[$k]['stralarmtype'] = implode('.', $temp);
- }
- }
- $syslog_list = $this->Syslog_model->getList($filter,1,HOME_SYSLOG_COUNT,$field);
- }
- // 获取视屏监控列表
- $video_list = $this->Videomonitor_model->getList($role, $companyid, $project_id, $userid,1,HOME_VIDEO_COUNT);
- foreach ($video_list as &$v) {
- if (!empty($v['image'])) {
- $v['image'] = base_url($v['image']);
- }
- }
- $projectList = $this->Project_model->get_list(array('company'=>2305),'id');
- $proArr = array();
- foreach ($projectList as $p) {
- $proArr[] = $p['id'];
- }
- $this->load->model('User_model');
- $userList = $this->User_model->get_list(array('companyid'=>2305),'id');
- $userArr = array();
- foreach ($userList as $u) {
- $userArr[] = $u['id'];
- }
- // 获取环境监控列表
- $weather_list = $this->Weathermonitor_model->get_list_by_role($role,$userid, $companyid, $project_id,'*',1,HOME_WEATHER_COUNT);
- $arr = $this->Lamp_model->getTotalLampInfo($role, $companyid, $project_id, $userid);
- if (!empty($arr['num'])) {
- $online_arr = $this->Lamp_model->getOnlineCount($role, $companyid, $project_id, $userid, 1);
- $lighting_arr = $this->Lamp_model->getCountByFilter($role, $companyid, $project_id, $userid, array('L.status'=>1));
- $failure = $this->Lamp_model->get_fault_count(array('L.projectid'=>$project_id,'L.isfaulted'=>1), $role, $companyid, $userid);
- // 监控数
- // $video_count = $this->Videomonitor_model->getTotal(array(),$role,$userid,$companyid,$project_id);
- // $weather_count = $this->Weathermonitor_model->getTotal(array(),$role,$userid,$companyid,$project_id);
-
- if (in_array($userid, $userArr) || in_array($project_id, $proArr)) {
- $data['online_rate'] = round($arr['online'], 2);
- $data['lighting_rate'] = round($arr['light'], 2);
- }else{
- $data['online_rate'] = round($online_arr['num']/$arr['num']*100, 2);
- $data['lighting_rate'] = round($lighting_arr['num']/$arr['num']*100, 2);
- }
- $data['failure_rate'] = round($failure/$arr['num']*100, 2);
- $data['failure_count'] = $failure;
- $data['total_power'] = round($arr['power'], 3);
- $data['total_install'] = round($arr['install']/1000, 3);
- $data['total_lamp'] = intval($arr['num']);
- $data['total_network'] = intval($network_total);
- $data['co2_emission'] = round($arr['power']*0.977, 3);
- $data['so2_emission'] = round($arr['power']*0.977/2620*8.5, 3);
- $data['coal_saving'] = round($arr['power']*0.977/2620, 3);
- } else {
- $data['online_rate'] = 0;
- $data['failure_rate'] = 0;
- $data['lighting_rate'] = 0;
- $data['failure_count'] = 0;
- $data['total_power'] = 0;
- $data['total_install'] = 0;
- $data['total_lamp'] = 0;
- $data['total_network'] = intval($network_total);
- $data['co2_emission'] = 0;
- $data['so2_emission'] = 0;
- $data['coal_saving'] = 0;
- }
- $data['syslog_list'] = $syslog_list;
- $data['alarm_list'] = $alarm_list;
- $data['video_list'] = $video_list;
- $data['weather_list'] = $weather_list;
- 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');
- $companyid = $this->get_user_info('companyid');
- $role = $this->get_user_info('role');
- $keyword = $this->input->post('keyword');
- $searchType = $this->input->post('searchType');
- $data = $this->User_model->get_nav($role,$userid,$companyid,$keyword,$searchType);
- 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,
- address,
- createtime as create_time,
- timezone,
- patrolinterval,
- cityid,
- lampcount as install_num';
- $data = $this->Project_model->get_one(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;
- // }
- // }
-
- 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'];
- // 监控数
- // $video_count = $this->Videomonitor_model->getTotal(array(),$role,$userid,$companyid,$project_id);
- // $weather_count = $this->Weathermonitor_model->getTotal(array(),$role,$userid,$companyid,$project_id);
- // $data['monitor_count'] = $video_count + $weather_count;
- $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['province'] = $this->input->post('province_id',true);
- // $data['zone'] = $this->input->post('zone_id',true);
- $data['cityid'] = $this->input->post('pid',true);
- $address = $this->input->post('address',true);
- $data['company'] = $this->input->post('company',true);
- $data['timezone'] = $this->input->post('timezone',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(mb_strlen($data['projectname']) > 20) exit(json_result('0211',$this->response['0211'],array()));
-
- // if(empty($data['zone'])) exit(json_result('0206',$this->response['0206'],array()));
- // if(empty($data['province'])) exit(json_result('0207',$this->response['0207'],array()));
- if(empty($data['timezone'])) exit(json_result('0208',$this->response['0208'],array()));
- // if(empty($data['company'])) exit(json_result('0209',$this->response['0209'],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()));
- // if ($this->Project_model->getDataCount(array('projectid'=>$data['projectid'], 'company'=>$data['company'])) > 0) {
- // exit(json_result('0201', $this->response['0201'], array()));
- // }
- // if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$data['company'])) > 0) {
- // exit(json_result('0203', $this->response['0203'], array()));
- // }
- // 记录区域
- // $data['id'] = $this->Zone_model->insert(array(
- // 'name' => $data['projectname'],
- // 'parent' => '' ,
- // 'level' => 3
- // ));
-
- if (!empty($data['timezone'])) {
- $timezone = $this->Project_model->get_timezone_info(array('id'=>$data['timezone']),'value');
- // 获取当前时区对应的时间
- $data['createtime'] = get_time_by_timezone($timezone['value']);
- }else{
- $data['createtime'] = date('Y-m-d H:i:s');
- }
- $projectId = $this->Project_model->insert($data);
- if ($role != SYSTEM_ADMIN) {
- $zone = $this->get_user_info('zone');
- $userid = $this->get_user_info('id');
- if (empty($zone)) {
- $this->User_model->update(array('id'=>$userid),array('zone'=>$projectId));
- }else{
- $arr = explode(',', $zone);
- $arr[] = $projectId;
- $zoneStr = implode(',', $arr);
- $this->User_model->update(array('id'=>$userid),array('zone'=>$zoneStr));
- }
- if ($role == COMPANY_CUSTOMER) {
- $pid = $this->get_user_info('parentid');
- $pres = $this->User_model->getOne($pid,'zone');
- $pzone = $pres['zone'];
- $userid = $this->get_user_info('id');
- if (empty($pzone)) {
- $this->User_model->update(array('id'=>$pid),array('zone'=>$projectId));
- }else{
- $parr = explode(',', $pzone);
- $parr[] = $projectId;
- $pzoneStr = implode(',', $parr);
- $this->User_model->update(array('id'=>$pid),array('zone'=>$pzoneStr));
- }
- }
- }
-
- // $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('projectid'=>$data['projectid'], 'company'=>$data['company']), $where['id']) > 0) {
- // exit(json_result('0201', $this->response['0201'], array()));
- // }
- 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->getData($where);
- if ($ret['cityid'] != $data['cityid']) {
- $sql = "delete from project_weather where projectId = ".$where['id'];
- $this->db->query($sql);
- }
- if ($ret['timezone'] != $data['timezone']) {
- $data['createtime'] = date_change($ret['createtime'],$ret['timezone'],$data['timezone']);
- }
- // $this->Zone_model->update($where, array('name'=>$data['projectname'], 'parent'=>$data['province']));
- $this->Project_model->update($where,$data);
- $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');
- // if ($role == COMPANY_CUSTOMER) {
- // exit(json_result('0011', $this->response['0011'], array()));
- // }
- $project_id = $this->input->post('project_id', true);
- if(empty($project_id)) {
- exit(json_result('0200', $this->response['0200'], array()));
- }
- $lampListInfo = $this->Lamp_model->get_list_in('P.id',$project_id,'L.address,N.protocoltype');
- foreach ($lampListInfo as $lampinfo) {
- $lampinfo['createtime'] = date('Y-m-d H:i:s',time());
- $this->db->insert('lampinfo_backup', $lampinfo);
- }
- // 删除项目对应的区域跟下级区域
- // $this->Zone_model->delZone($project_id);
- $net_arr = $this->Network_model->get_list_in(array(),array(),'id,protocoltype',array('projectid'=>$project_id));
- // foreach ($net_arr as $v) {
- // $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$v['id'].'}';
- // send_cmd($cmdstr,0,0,$v['protocoltype']);
- // }
- // 删除项目下面的网络
- $this->Network_model->delData(array('projectid'=>$project_id));
- // 删除项目下路灯的告警信息
- $this->Alarm_model->delDataByCondition(array('projectid'=>$project_id));
- // 删除项目下的路灯
- $this->Lamp_model->delData(array('projectid'=>$project_id));
- // 删除项目下的监控
- $this->Videomonitor_model->delData(array('projectid'=>$project_id));
- $this->Weathermonitor_model->delData(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->delOne($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']));
- }
- }
|