Project.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  3. class Project extends Base_Controller {
  4. public function __construct() {
  5. parent::__construct();
  6. $this->load->model('Company_model');
  7. $this->load->model('Project_model');
  8. $this->load->model('Network_model');
  9. $this->load->model('Lamp_model');
  10. // $this->load->model('Zone_model');
  11. $this->load->model('Patrol_model');
  12. $this->load->model('Alarm_model');
  13. $this->load->model('Syslog_model');
  14. $this->load->model('Videomonitor_model');
  15. $this->load->model('Weathermonitor_model');
  16. $this->load->model('User_model');
  17. $this->load->model('Load_model');
  18. $this->load->model('Modbus_load_model');
  19. $this->load->model('Global_location_model');
  20. }
  21. // 项目管理首页
  22. public function data() {
  23. $userid = $this->get_user_info('id');
  24. $companyid = $this->get_user_info('companyid');
  25. $role = $this->get_user_info('role');
  26. $project_id = intval($this->input->post('project_id', true));
  27. $version = $this->session->userdata('version');
  28. // 总网络数
  29. $network_total = $this->Network_model->getTotal(array('projectid'=>$project_id), $role, $companyid, $userid);
  30. // 告警信息列表
  31. if (!empty($projectid)) {
  32. $alarm_list = $this->Alarm_model->getList(array('projectid'=>$project_id,'status'=>0,'type'=>0),$role,$userid,1,HOME_ALARM_COUNT);
  33. }else{
  34. $alarm_list = $this->Alarm_model->getList(array('status'=>0,'type'=>0),$role,$userid,1,HOME_ALARM_COUNT);
  35. }
  36. // 系统日志列表
  37. $field = "U.username as username,S.content as content,S.optype as optype,S.time as time";
  38. // 日志权限控制
  39. $filter = array('language'=>0);
  40. if ($role == COMPANY_ADMIN) {
  41. $filter['companyid'] = $this->get_user_info('companyid');
  42. }elseif ($role == COMPANY_CUSTOMER) {
  43. $filter['userid'] = $this->get_user_info('id');
  44. }
  45. if (empty($version)) {
  46. $syslog_list = $this->Syslog_model->getList($filter,1,HOME_SYSLOG_COUNT,$field);
  47. }else{
  48. $filter['language'] = 1;
  49. // 英文版
  50. foreach ($alarm_list as $k=>$value) {
  51. $stralarmtype = trim($value['stralarmtype']);
  52. if (!empty($stralarmtype)) {
  53. $alarmArr = explode(',', $stralarmtype);
  54. $temp = array();
  55. foreach ($alarmArr as $a) {
  56. $temp[] = alarm_translate($a);
  57. }
  58. $alarm_list[$k]['stralarmtype'] = implode('.', $temp);
  59. }
  60. }
  61. $syslog_list = $this->Syslog_model->getList($filter,1,HOME_SYSLOG_COUNT,$field);
  62. }
  63. // 获取视屏监控列表
  64. $video_list = $this->Videomonitor_model->getList($role, $companyid, $project_id, $userid,1,HOME_VIDEO_COUNT);
  65. foreach ($video_list as &$v) {
  66. if (!empty($v['image'])) {
  67. $v['image'] = base_url($v['image']);
  68. }
  69. }
  70. $projectList = $this->Project_model->get_list(array('company'=>2305),'id');
  71. $proArr = array();
  72. foreach ($projectList as $p) {
  73. $proArr[] = $p['id'];
  74. }
  75. $this->load->model('User_model');
  76. $userList = $this->User_model->get_list(array('companyid'=>2305),'id');
  77. $userArr = array();
  78. foreach ($userList as $u) {
  79. $userArr[] = $u['id'];
  80. }
  81. // 获取环境监控列表
  82. $weather_list = $this->Weathermonitor_model->get_list_by_role($role,$userid, $companyid, $project_id,'*',1,HOME_WEATHER_COUNT);
  83. $arr = $this->Lamp_model->getTotalLampInfo($role, $companyid, $project_id, $userid);
  84. if (!empty($arr['num'])) {
  85. $online_arr = $this->Lamp_model->getOnlineCount($role, $companyid, $project_id, $userid, 1);
  86. $lighting_arr = $this->Lamp_model->getCountByFilter($role, $companyid, $project_id, $userid, array('L.status'=>1));
  87. $failure = $this->Lamp_model->get_fault_count(array('L.projectid'=>$project_id,'L.isfaulted'=>1), $role, $companyid, $userid);
  88. // 监控数
  89. // $video_count = $this->Videomonitor_model->getTotal(array(),$role,$userid,$companyid,$project_id);
  90. // $weather_count = $this->Weathermonitor_model->getTotal(array(),$role,$userid,$companyid,$project_id);
  91. if (in_array($userid, $userArr) || in_array($project_id, $proArr)) {
  92. $data['online_rate'] = round($arr['online'], 2);
  93. $data['lighting_rate'] = round($arr['light'], 2);
  94. }else{
  95. $data['online_rate'] = round($online_arr['num']/$arr['num']*100, 2);
  96. $data['lighting_rate'] = round($lighting_arr['num']/$arr['num']*100, 2);
  97. }
  98. $data['failure_rate'] = round($failure/$arr['num']*100, 2);
  99. $data['failure_count'] = $failure;
  100. $data['total_power'] = round($arr['power'], 3);
  101. $data['total_install'] = round($arr['install']/1000, 3);
  102. $data['total_lamp'] = intval($arr['num']);
  103. $data['total_network'] = intval($network_total);
  104. $data['co2_emission'] = round($arr['power']*0.977, 3);
  105. $data['so2_emission'] = round($arr['power']*0.977/2620*8.5, 3);
  106. $data['coal_saving'] = round($arr['power']*0.977/2620, 3);
  107. } else {
  108. $data['online_rate'] = 0;
  109. $data['failure_rate'] = 0;
  110. $data['lighting_rate'] = 0;
  111. $data['failure_count'] = 0;
  112. $data['total_power'] = 0;
  113. $data['total_install'] = 0;
  114. $data['total_lamp'] = 0;
  115. $data['total_network'] = intval($network_total);
  116. $data['co2_emission'] = 0;
  117. $data['so2_emission'] = 0;
  118. $data['coal_saving'] = 0;
  119. }
  120. $data['syslog_list'] = $syslog_list;
  121. $data['alarm_list'] = $alarm_list;
  122. $data['video_list'] = $video_list;
  123. $data['weather_list'] = $weather_list;
  124. exit(json_result('0000', $this->response['0000'], $data));
  125. }
  126. public function get_pram_data(){
  127. $projectid = $this->input->post('projectid',true);
  128. $res = $this->Load_model->get_data_by_filter(['relateid'=>$projectid,'cmdtype'=>2]);
  129. if (empty($res)) {
  130. $res = [];
  131. $res['workmode'] = '0';
  132. $res['worktimefirst'] = '4';
  133. $res['workpowerfirst'] = '80';
  134. $res['worktimesencond'] = '1';
  135. $res['workpowersencond'] = '40';
  136. $res['worktimethird'] = '8';
  137. $res['workpowerthird'] = '30';
  138. $res['worktimeforth'] = '0';
  139. $res['workpowerforth'] = '30';
  140. $res['voptically'] = '5';
  141. $res['delaytime'] = '1';
  142. $res['ledloadcurrent'] = '330';
  143. $res['powercmd'] = '1';
  144. $res['switchfeature'] = '1';
  145. }
  146. exit(json_result('0000',$this->response['0000'],$res));
  147. }
  148. // 设置负载参数
  149. public function set_pram_data(){
  150. $projectid = $this->input->post('projectid',true);
  151. $controlType = intval($this->input->post('controlType',true));
  152. if ($controlType == 0) {
  153. $data['workmode'] = $this->input->post('workmode',true);
  154. $data['worktimefirst'] = $this->input->post('worktimefirst',true);
  155. $data['workpowerfirst'] = $this->input->post('workpowerfirst',true);
  156. $data['worktimesencond'] = $this->input->post('worktimesencond',true);
  157. $data['workpowersencond'] = $this->input->post('workpowersencond',true);
  158. $data['worktimethird'] = $this->input->post('worktimethird',true);
  159. $data['workpowerthird'] = $this->input->post('workpowerthird',true);
  160. $data['worktimeforth'] = $this->input->post('worktimeforth',true);
  161. $data['workpowerforth'] = $this->input->post('workpowerforth',true);
  162. $data['voptically'] = $this->input->post('voptically',true);
  163. $data['delaytime'] = $this->input->post('delaytime',true);
  164. $data['ledloadcurrent'] = $this->input->post('ledloadcurrent',true);
  165. $data['powercmd'] = $this->input->post('powercmd',true);
  166. $data['switchfeature'] = $this->input->post('switchfeature',true);
  167. }else{
  168. $data['light_voltage'] = $this->input->post('light_voltage',true);
  169. $data['intell_power'] = $this->input->post('intell_power',true);
  170. $data['load_current'] = $this->input->post('load_current',true);
  171. $data['light_delay'] = $this->input->post('light_delay',true);
  172. $data['induction_delay'] = $this->input->post('induction_delay',true);
  173. $data['first_light_time'] = $this->input->post('first_light_time',true);
  174. $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
  175. $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
  176. $data['second_light_time'] = $this->input->post('second_light_time',true);
  177. $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
  178. $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
  179. $data['third_light_time'] = $this->input->post('third_light_time',true);
  180. $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
  181. $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
  182. $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
  183. $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
  184. $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
  185. $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
  186. $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
  187. $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
  188. $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
  189. $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
  190. $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
  191. $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
  192. $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
  193. $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
  194. $data['eighth_light_time'] = $this->input->post('eighth_light_time',true);
  195. $data['eighth_light_human_power'] = $this->input->post('eighth_light_human_power',true);
  196. $data['eighth_light_unman_power'] = $this->input->post('eighth_light_unman_power',true);
  197. $data['ninth_light_time'] = $this->input->post('ninth_light_time',true);
  198. $data['ninth_light_human_power'] = $this->input->post('ninth_light_human_power',true);
  199. $data['ninth_light_unman_power'] = $this->input->post('ninth_light_unman_power',true);
  200. $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
  201. $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
  202. $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
  203. $data['begin_voltage'] = $this->input->post('begin_voltage',true);
  204. $data['end_voltage'] = $this->input->post('end_voltage',true);
  205. $data['end_current'] = $this->input->post('end_current',true);
  206. }
  207. $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();
  208. foreach ($lampList as $value) {
  209. $data['relateid'] = $value['id'];
  210. $data['cmdtype'] = 0;
  211. $data['updatetime'] = date('Y-m-d H:i:s',time());
  212. if ($controlType == 0) {
  213. if (($value['protocoltype'] == 4 || $value['protocoltype'] == 6) && $value['lampprotocoltype'] == 0) {
  214. $address = '';
  215. if (mb_strlen($value['address']) >= 8) {
  216. $address = mb_substr($value['address'], -8);
  217. }else{
  218. $address = '00000000';
  219. }
  220. $sendData = '10040010';
  221. $sendData .= bitSubStr(base_convert($data['workmode'], 10, 16));
  222. $sendData .= bitSubStr(base_convert($data['worktimefirst'], 10, 16));
  223. $sendData .= bitSubStr(base_convert($data['workpowerfirst'], 10, 16));
  224. $sendData .= bitSubStr(base_convert($data['worktimesencond'], 10, 16));
  225. $sendData .= bitSubStr(base_convert($data['workpowersencond'], 10, 16));
  226. $sendData .= bitSubStr(base_convert($data['worktimethird'], 10, 16));
  227. $sendData .= bitSubStr(base_convert($data['workpowerthird'], 10, 16));
  228. $sendData .= bitSubStr(base_convert($data['worktimeforth'], 10, 16));
  229. $sendData .= bitSubStr(base_convert($data['workpowerforth'], 10, 16));
  230. $sendData .= bitSubStr(base_convert($data['delaytime'], 10, 16));
  231. $sendData .= doubBitSubStr(base_convert(intval($data['voptically']*10), 10, 16));
  232. $sendData .= doubBitSubStr(base_convert(intval($data['ledloadcurrent']), 10, 16));
  233. $sendData .= bitSubStr(base_convert($data['powercmd'], 10, 16));
  234. $sendData .= bitSubStr(base_convert($data['switchfeature'], 10, 16));
  235. $sum = 0;
  236. for ($i=0; $i < mb_strlen($sendData)/2; $i++) {
  237. if ($i == 0) continue;
  238. $t = mb_substr($sendData, $i*2,2);
  239. $sum += base_convert($t, 16, 10);
  240. }
  241. $sum = base_convert($sum, 10, 16);
  242. $sendData .= bitSubStr($sum);
  243. $sendData = $address.$sendData;
  244. if ($value['protocoltype'] == 4) {
  245. $res = lampMqttCmd('/WE/TransIn/'.$value['address'],'/WE/TransOut/'.$value['address'],pack('H*','0001'.$sendData),0);
  246. }else{
  247. device_cmd($value['deviceid'],pack('H*',$sendData));
  248. }
  249. continue;
  250. }
  251. $res = $this->Load_model->get_data_by_filter(['relateid'=>$projectid,'cmdtype'=>2]);
  252. if(empty($res)){
  253. $id = $this->Load_model->insert($data);
  254. } else {
  255. $this->Load_model->update(['id'=>$res['id']],$data);
  256. $id = $res['id'];
  257. }
  258. $cmd = '{"cmd_type":"load_param_cmd","cmd_id":'.$id.',"broadcast":0}';
  259. $cmdret = send_cmd($cmd,0,0,$value['protocoltype']);
  260. }else{
  261. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_load_asy';
  262. $temp = $data;
  263. $temp['username'] = $this->input->post('username',true);
  264. $temp['client_key'] = $this->input->post('client_key',true);
  265. $temp['token'] = $this->input->post('token',true);
  266. $temp['clientid'] = $this->input->post('clientid',true);
  267. $temp['version'] = $this->session->userdata('version');
  268. $temp['lamp_id'] = $value['id'];
  269. doAsyncRequest($url,$temp);
  270. // if (($value['protocoltype'] == 4 || $value['protocoltype'] == 6) && $value['lampprotocoltype'] == 0){
  271. // continue;
  272. // }
  273. // $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$value['id'],'cmdtype'=>0]);
  274. // if(empty($res)){
  275. // $id = $this->Modbus_load_model->insert($data);
  276. // } else {
  277. // $this->Modbus_load_model->update(['id'=>$res['id']],$data);
  278. // $id = $res['id'];
  279. // }
  280. // $cmd = '{"cmd_type":"modbus_load_param_cmd","cmd_id":'.$id.',"broadcast":0}';
  281. // $cmdret = send_cmd($cmd,0,0,$value['protocoltype']);
  282. }
  283. }
  284. exit(json_result('0000',$this->response['0000']));
  285. }
  286. // 项目管理侧边导航栏
  287. public function nav(){
  288. $userid = $this->get_user_info('id');
  289. $companyid = $this->get_user_info('companyid');
  290. $role = $this->get_user_info('role');
  291. $keyword = $this->input->post('keyword');
  292. $searchType = $this->input->post('searchType');
  293. $data = $this->User_model->get_nav($role,$userid,$companyid,$keyword,$searchType);
  294. json_result('0000',$this->response['0000'],$data);
  295. }
  296. // 获取项目下拉列表
  297. public function get() {
  298. $companyid = $this->input->post('companyid',true);
  299. $role = $this->get_user_info('role');
  300. $userid = $this->get_user_info('id');
  301. $keyword = $this->input->post('keyword', true);
  302. $condition = array();
  303. if (!empty($keyword)) {
  304. $condition['keyword'] = $keyword;
  305. }
  306. if (empty($companyid)) {
  307. $companyid = $this->get_user_info('companyid');
  308. $data = $this->Project_model->getMultiData($condition, 'P.id, P.projectname as name, P.networkcount as network', $role, $companyid, $userid);
  309. }else{
  310. $zone = $this->get_user_info('zone');
  311. $data = $this->Project_model->get_list_by_company('id, projectname as name,company', $role, $companyid, $zone);
  312. }
  313. if (!empty($data)) {
  314. foreach ($data as $key => $value) {
  315. $data[$key]['tag'] = get_first_char($value['name']);
  316. }
  317. }
  318. exit(json_result('0000', $this->response['0000'], array('projects'=>$data)));
  319. }
  320. private function getLeftTimeByProject($project_id){
  321. $lampid_arr = $this->Lamp_model->getLampIds(array('project' => $project_id));
  322. $time_arr = $this->Patrol_model->get_list_in('id',$lampid_arr,'nextquerytime as time');
  323. if (empty($time_arr)) {
  324. return 0;
  325. }else{
  326. $querytime = max($time_arr)['time'];
  327. $nowtime = time();
  328. return $querytime>$nowtime ? intval(($querytime-$nowtime)/60) : 0;
  329. }
  330. }
  331. // 获取项目详情
  332. public function detail() {
  333. $project_id = $this->input->post('project_id', true);
  334. if(empty($project_id)){
  335. exit(json_result('0200', $this->response['0200'], array()));
  336. }
  337. $role = $this->get_user_info('role');
  338. $companyid = $this->get_user_info('companyid');
  339. $userid = $this->get_user_info('id');
  340. $fields = 'projectname as project_name,
  341. projectid as project_no,
  342. guestname as customer,
  343. company as company_id,
  344. province as province_id,
  345. address,
  346. createtime as create_time,
  347. timezone,
  348. patrolinterval,
  349. cityid,
  350. lampcount as install_num';
  351. $data = $this->Project_model->get_one(array('id' => $project_id), $fields);
  352. $version = $this->session->userdata('version');
  353. if(!$data){
  354. exit(json_result('0202', $this->response['0202'], $data));
  355. } else {
  356. // if (empty($data['cityid'])) {
  357. // $data['countryId'] = 0;
  358. // $data['provinceId'] = 0;
  359. // $data['cityId'] = 0;
  360. // $data['areaId'] = 0;
  361. // $data['weatherList'] = [];
  362. // $data['longitude'] = '0';
  363. // $data['latitude'] = '0';
  364. // $location_ch = '暂无位置信息,请在编辑项目页面选择位置信息';
  365. // $location_en = 'There is no location information, please select location information on the edit project page.';
  366. // }else{
  367. // $res1 = $this->Global_location_model->getOne($data['cityid'],'level,pid,chinese_name,longitude,latitude,english_name');
  368. // $data['longitude'] = $res1['longitude'];
  369. // $data['latitude'] = $res1['latitude'];
  370. // $res2 = $this->Global_location_model->getOne($res1['pid'],'pid,chinese_name,english_name');
  371. // if ($res1['level'] == 4) {
  372. // $data['areaId'] = $data['cityid'];
  373. // $data['cityId'] = $res1['pid'];
  374. // $data['provinceId'] = $res2['pid'];
  375. // $res3 = $this->Global_location_model->getOne($res2['pid'],'pid,chinese_name,english_name');
  376. // $data['countryId'] = $res3['pid'];
  377. // $location_ch = $res3['chinese_name'].','.$res2['chinese_name'].','.$res1['chinese_name'];
  378. // $location_en = $res1['english_name'].','.$res2['english_name'].','.$res3['english_name'];
  379. // }elseif ($res1['level'] == 3) {
  380. // $data['areaId'] = 0;
  381. // $data['cityId'] = $data['cityid'];
  382. // $data['provinceId'] = $res1['pid'];
  383. // $data['countryId'] = $res2['pid'];
  384. // $location_ch = $res2['chinese_name'].','.$res1['chinese_name'];
  385. // $location_en = $res1['english_name'].','.$res2['english_name'];
  386. // }elseif ($res1['level'] == 2) {
  387. // $data['areaId'] = 0;
  388. // $data['cityId'] = 0;
  389. // $data['provinceId'] = $data['cityid'];
  390. // $data['countryId'] = $res1['pid'];
  391. // $location_ch = $res1['chinese_name'];
  392. // $location_en = $res1['english_name'];
  393. // }else{
  394. // $data['areaId'] = 0;
  395. // $data['cityId'] = 0;
  396. // $data['provinceId'] = 0;
  397. // $data['countryId'] = $data['cityid'];
  398. // $location_ch = $res1['chinese_name'];
  399. // $location_en = $res1['english_name'];
  400. // }
  401. // $project_weather = $this->db->query('select * from project_weather where projectId = '.$project_id)->row_array();
  402. // if (empty($project_weather) || time() - strtotime($project_weather['updatetime']) > 3600) {
  403. // $url = 'https://way.jd.com/he/freeweather?city='.$res1['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  404. // $resData = request_post($url);
  405. // $arr = json_decode($resData,true);
  406. // if ($arr['code'] == 10000) {
  407. // if ($arr['result']['HeWeather5'][0]['status'] == 'unknown location') {
  408. // $url = 'https://free-api.heweather.net/s6/weather/forecast?location='.$res1['longitude'].','.$res1['latitude'].'&key=63dbc994630941fab269ad3c3af4d5d2';
  409. // // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  410. // $resData = request_post($url);
  411. // $arr = json_decode($resData,true);
  412. // // var_dump($url);
  413. // if ($arr['HeWeather6'][0]['status'] == 'ok') {
  414. // $now = $arr['HeWeather6'][0]['daily_forecast'][0];
  415. // $daily_forecast = $arr['HeWeather6'][0]['daily_forecast'];
  416. // $weatherList = array();
  417. // $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']))];
  418. // for ($i=1; $i < count($daily_forecast); $i++) {
  419. // $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']))];
  420. // }
  421. // $data['weatherList'] = $weatherList;
  422. // if (empty($project_weather)) {
  423. // $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
  424. // $this->db->query($sql);
  425. // }else{
  426. // $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
  427. // $this->db->query($sql);
  428. // }
  429. // }else{
  430. // $data['weatherList'] = [];
  431. // }
  432. // // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  433. // // $resData = request_post($url);
  434. // // $arr = json_decode($resData,true);
  435. // }else{
  436. // if ($arr['result']['HeWeather5'][0]['status'] == 'ok') {
  437. // $now = $arr['result']['HeWeather5'][0]['now'];
  438. // $daily_forecast = $arr['result']['HeWeather5'][0]['daily_forecast'];
  439. // $weatherList = array();
  440. // $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']))];
  441. // for ($i=1; $i < count($daily_forecast); $i++) {
  442. // $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']))];
  443. // }
  444. // $data['weatherList'] = $weatherList;
  445. // if (empty($project_weather)) {
  446. // $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
  447. // $this->db->query($sql);
  448. // }else{
  449. // $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
  450. // $this->db->query($sql);
  451. // }
  452. // }else{
  453. // $data['weatherList'] = [];
  454. // }
  455. // }
  456. // }else{
  457. // $data['weatherList'] = [];
  458. // }
  459. // }else{
  460. // $data['weatherList'] = json_decode($project_weather['weatherInfo'],true);
  461. // // var_dump($data['weatherList']);die;
  462. // }
  463. // }
  464. if (empty($version)) {
  465. $data['location'] = $location_ch;
  466. $res = $this->Project_model->get_timezone_info(array('id'=>$data['timezone']),'name');
  467. foreach ($data['weatherList'] as $key => $value) {
  468. $data['weatherList'][$key]['weatherType'] = weather_translate_cn($data['weatherList'][$key]['code']);
  469. $data['weatherList'][$key]['dateStr'] = date_translate_cn($data['weatherList'][$key]['dateStr']);
  470. }
  471. }else{
  472. $data['location'] = $location_en;
  473. foreach ($data['weatherList'] as $key => $value) {
  474. $data['weatherList'][$key]['weatherType'] = weather_translate_en($data['weatherList'][$key]['code']);
  475. }
  476. $res = $this->Project_model->get_timezone_info(array('id'=>$data['timezone']),'enname as name');
  477. }
  478. $data['timezone_name'] = $res['name'];
  479. // 监控数
  480. // $video_count = $this->Videomonitor_model->getTotal(array(),$role,$userid,$companyid,$project_id);
  481. // $weather_count = $this->Weathermonitor_model->getTotal(array(),$role,$userid,$companyid,$project_id);
  482. // $data['monitor_count'] = $video_count + $weather_count;
  483. $data['patrol_time'] = strval($this->getLeftTimeByProject($project_id));
  484. // 获取项目下的网络列表
  485. $data['network_list'] = $this->Network_model->get_list_by_filter(array('projectid'=>$project_id));
  486. $temp = array();
  487. foreach ($data['network_list'] as $key => $value) {
  488. if (isset($value['gatewaytype']) && $value['gatewaytype'] == 'direct') {
  489. continue;
  490. }
  491. $temp[] = $value;
  492. }
  493. $data['network_list'] = $temp;
  494. exit(json_result('0000', $this->response['0000'], $data));
  495. }
  496. }
  497. // 添加编辑项目
  498. public function save() {
  499. $role = $this->get_user_info('role');
  500. $where['id'] = $this->input->post('project_id',true);
  501. $data['projectname'] = trim($this->input->post('project_name',true));
  502. // $data['province'] = $this->input->post('province_id',true);
  503. // $data['zone'] = $this->input->post('zone_id',true);
  504. $data['cityid'] = $this->input->post('pid',true);
  505. $address = $this->input->post('address',true);
  506. $data['company'] = $this->input->post('company',true);
  507. $data['timezone'] = $this->input->post('timezone',true);
  508. $patrolinterval = $this->input->post('patrolinterval',true);
  509. if(isset($address)) $data['address'] = $address;
  510. if(isset($patrolinterval)) $data['patrolinterval'] = $patrolinterval;
  511. if(empty($data['projectname'])) exit(json_result('0204',$this->response['0204'],array()));
  512. // if(mb_strlen($data['projectname']) > 20) exit(json_result('0211',$this->response['0211'],array()));
  513. // if(empty($data['zone'])) exit(json_result('0206',$this->response['0206'],array()));
  514. // if(empty($data['province'])) exit(json_result('0207',$this->response['0207'],array()));
  515. if(empty($data['timezone'])) exit(json_result('0208',$this->response['0208'],array()));
  516. // if(empty($data['company'])) exit(json_result('0209',$this->response['0209'],array()));
  517. if (empty($where['id'])) { // 添加项目
  518. $data['projectid'] = $this->input->post('project_no',true);
  519. if(empty($data['projectid'])) exit(json_result('0205',$this->response['0205'],array()));
  520. if(mb_strlen($data['projectid']) > 20) exit(json_result('0210',$this->response['0210'],array()));
  521. if(!preg_match('/^[A-Za-z0-9]+$/', $data['projectid'])) exit(json_result('0212',$this->response['0212'],array()));
  522. // if ($this->Project_model->getDataCount(array('projectid'=>$data['projectid'], 'company'=>$data['company'])) > 0) {
  523. // exit(json_result('0201', $this->response['0201'], array()));
  524. // }
  525. // if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$data['company'])) > 0) {
  526. // exit(json_result('0203', $this->response['0203'], array()));
  527. // }
  528. // 记录区域
  529. // $data['id'] = $this->Zone_model->insert(array(
  530. // 'name' => $data['projectname'],
  531. // 'parent' => '' ,
  532. // 'level' => 3
  533. // ));
  534. if (!empty($data['timezone'])) {
  535. $timezone = $this->Project_model->get_timezone_info(array('id'=>$data['timezone']),'value');
  536. // 获取当前时区对应的时间
  537. $data['createtime'] = get_time_by_timezone($timezone['value']);
  538. }else{
  539. $data['createtime'] = date('Y-m-d H:i:s');
  540. }
  541. $projectId = $this->Project_model->insert($data);
  542. if ($role != SYSTEM_ADMIN) {
  543. $zone = $this->get_user_info('zone');
  544. $userid = $this->get_user_info('id');
  545. if (empty($zone)) {
  546. $this->User_model->update(array('id'=>$userid),array('zone'=>$projectId));
  547. }else{
  548. $arr = explode(',', $zone);
  549. $arr[] = $projectId;
  550. $zoneStr = implode(',', $arr);
  551. $this->User_model->update(array('id'=>$userid),array('zone'=>$zoneStr));
  552. }
  553. if ($role == COMPANY_CUSTOMER) {
  554. $pid = $this->get_user_info('parentid');
  555. $pres = $this->User_model->getOne($pid,'zone');
  556. $pzone = $pres['zone'];
  557. $userid = $this->get_user_info('id');
  558. if (empty($pzone)) {
  559. $this->User_model->update(array('id'=>$pid),array('zone'=>$projectId));
  560. }else{
  561. $parr = explode(',', $pzone);
  562. $parr[] = $projectId;
  563. $pzoneStr = implode(',', $parr);
  564. $this->User_model->update(array('id'=>$pid),array('zone'=>$pzoneStr));
  565. }
  566. }
  567. }
  568. // $projectId = $data['id'];
  569. $this->add_operation_log('insert',"添加\"{$data['projectname']}\"项目,项目编号\"{$data['projectid']}\"",0);
  570. $this->add_operation_log('insert',"Add project.Project name:\"{$data['projectname']}\".Project number:\"{$data['projectid']}\"",0,1);
  571. } else { // 编辑项目
  572. // if ($this->Project_model->getDataCount(array('projectid'=>$data['projectid'], 'company'=>$data['company']), $where['id']) > 0) {
  573. // exit(json_result('0201', $this->response['0201'], array()));
  574. // }
  575. if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$data['company']), $where['id']) > 0) {
  576. exit(json_result('0203', $this->response['0203'], array()));
  577. }
  578. $ret = $this->Project_model->getData($where);
  579. if ($ret['cityid'] != $data['cityid']) {
  580. $sql = "delete from project_weather where projectId = ".$where['id'];
  581. $this->db->query($sql);
  582. }
  583. if ($ret['timezone'] != $data['timezone']) {
  584. $data['createtime'] = date_change($ret['createtime'],$ret['timezone'],$data['timezone']);
  585. }
  586. // $this->Zone_model->update($where, array('name'=>$data['projectname'], 'parent'=>$data['province']));
  587. $this->Project_model->update($where,$data);
  588. $projectId = $where['id'];
  589. // 修改项目下巡检信息
  590. // if (isset($patrolinterval)) {
  591. // if (empty($patrolinterval)) {
  592. // $patrolData['patroltype'] = 0;
  593. // }else{
  594. // $patrolData['patroltype'] = 1;
  595. // $patrolData['patrolinterval'] = intval($patrolinterval);
  596. // $patrolData['updatetime'] = date("Y-m-d H:i:s", time());
  597. // $patrolData['nextquerytime'] = date("Y-m-d H:i:s", time());
  598. // }
  599. // $idArr = $this->Lamp_model->get_list_by_project($projectId,'L.id,N.protocoltype,L.address');
  600. // foreach ($idArr as $v) {
  601. // $res = $this->Patrol_model->getOne($v['id']);
  602. // if(empty($res)){
  603. // $patrolData['id'] = $v['id'];
  604. // $this->Patrol_model->insert($patrolData);
  605. // unset($patrolData['id']);
  606. // } else {
  607. // $this->Patrol_model->update(array('id'=>$v['id']), $patrolData);
  608. // }
  609. // if ($v['protocoltype'] == 4) {
  610. // $sendData = '000101A501';;
  611. // $sendData .= bitSubStr(base_convert(intval($patrolData['patrolinterval']/5), 10, 16));
  612. // lampMqttCmd('/WE/WriteIn/'.$v['address'],'/WE/WriteOut/'.$v['address'],pack('H*', $sendData),0);
  613. // continue;
  614. // }
  615. // if (isset($v['protocoltype']) && $v['protocoltype'] != 0) {
  616. // $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$v['id'].',"broadcast":0}';
  617. // $cmdret = send_cmd($cmd, 1,0,$v['protocoltype']);
  618. // }
  619. // }
  620. // }
  621. $this->add_operation_log('update',"修改\"{$data['projectname']}\"项目,项目编号\"{$ret['projectid']}\"",0);
  622. $this->add_operation_log('update',"Update project.Project name:\"{$data['projectname']}\".Project number:\"{$ret['projectid']}\"",0,1);
  623. }
  624. exit(json_result('0000', $this->response['0000'], array("id" => intval($projectId))));
  625. }
  626. // 删除项目
  627. public function del() {
  628. $role = $this->get_user_info('role');
  629. // if ($role == COMPANY_CUSTOMER) {
  630. // exit(json_result('0011', $this->response['0011'], array()));
  631. // }
  632. $project_id = $this->input->post('project_id', true);
  633. if(empty($project_id)) {
  634. exit(json_result('0200', $this->response['0200'], array()));
  635. }
  636. $lampListInfo = $this->Lamp_model->get_list_in('P.id',$project_id,'L.address,N.protocoltype');
  637. foreach ($lampListInfo as $lampinfo) {
  638. $lampinfo['createtime'] = date('Y-m-d H:i:s',time());
  639. $this->db->insert('lampinfo_backup', $lampinfo);
  640. }
  641. // 删除项目对应的区域跟下级区域
  642. // $this->Zone_model->delZone($project_id);
  643. $net_arr = $this->Network_model->get_list_in(array(),array(),'id,protocoltype',array('projectid'=>$project_id));
  644. // foreach ($net_arr as $v) {
  645. // $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$v['id'].'}';
  646. // send_cmd($cmdstr,0,0,$v['protocoltype']);
  647. // }
  648. // 删除项目下面的网络
  649. $this->Network_model->delData(array('projectid'=>$project_id));
  650. // 删除项目下路灯的告警信息
  651. $this->Alarm_model->delDataByCondition(array('projectid'=>$project_id));
  652. // 删除项目下的路灯
  653. $this->Lamp_model->delData(array('projectid'=>$project_id));
  654. // 删除项目下的监控
  655. $this->Videomonitor_model->delData(array('projectid'=>$project_id));
  656. $this->Weathermonitor_model->delData(array('projectid'=>$project_id));
  657. $projectData = $this->Project_model->getData(array('id'=>$project_id),'projectname,projectid');
  658. $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目,项目编号\"{$projectData['projectid']}\"",0);
  659. $this->add_operation_log('delete',"Delete project.Project name:\"{$projectData['projectname']}\".Project number:\"{$projectData['projectid']}\"",0,1);
  660. // 删除项目
  661. $this->Project_model->delOne($project_id);
  662. exit(json_result('0000', $this->response['0000'], array()));
  663. }
  664. private function do_setting($id, $data){
  665. $ret = $this->Patrol_model->getOne($id);
  666. if(empty($ret)){
  667. $data['id'] = $id;
  668. $this->Patrol_model->insert($data);
  669. } else {
  670. $this->Patrol_model->update(array('id'=>$id), $data);
  671. }
  672. $res = $this->Lamp_model->getOne($id,'N.protocoltype,L.address,L.protocoltype as lampprotocoltype');
  673. if ($res['protocoltype'] == 4) {
  674. $sendData = '000101A501';;
  675. $sendData .= bitSubStr(base_convert(intval($data['patrolinterval']/5), 10, 16));
  676. lampMqttCmd('/WE/WriteIn/'.$res['address'],'/WE/WriteOut/'.$res['address'],pack('H*', $sendData),0);
  677. continue;
  678. }
  679. if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
  680. $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$id.',"broadcast":0}';
  681. $cmdret = send_cmd($cmd, 1,0,$res['protocoltype']);
  682. }
  683. }
  684. // 巡检设置
  685. public function patrol() {
  686. $role = $this->get_user_info('role');
  687. // if ($role == COMPANY_CUSTOMER) {
  688. // exit(json_result('0011', $this->response['0011'], array()));
  689. // }
  690. $type = $this->input->post('type', true);
  691. $value = $this->input->post('value', true);
  692. $switch = $this->input->post('switch',true);
  693. $interval = $this->input->post('interval',true);
  694. if(!in_array($type, array('project','network','lamp')) || empty($interval) || empty($value)) {
  695. exit(json_result('0005', $this->response['0005'], array()));
  696. }
  697. $nowtime = time();
  698. $data['patroltype'] = intval($switch);
  699. $data['patrolinterval'] = intval($interval);
  700. $data['updatetime'] = date("Y-m-d H:i:s", $nowtime);
  701. $data['nextquerytime'] = date("Y-m-d H:i:s", time());
  702. if ($type == 'project') {
  703. $idArr = $this->Lamp_model->getLampIds(array('project'=>$value));
  704. }elseif ($type == 'network') {
  705. $idArr = $this->Lamp_model->getLampIds(array('network'=>$value));
  706. }else{
  707. $id = $value;
  708. $ret = $this->Patrol_model->getOne($id);
  709. if(empty($ret)){
  710. $data['id'] = $id;
  711. $this->Patrol_model->insert($data);
  712. } else {
  713. $this->Patrol_model->update(array('id'=>$id), $data);
  714. }
  715. $res = $this->Lamp_model->getOne($id,'N.protocoltype,L.address,L.protocoltype as lampprotocoltype');
  716. if ($res['protocoltype'] == 4) {
  717. $sendData = '000101A501';;
  718. $sendData .= bitSubStr(base_convert(intval($interval/5), 10, 16));
  719. lampMqttCmd('/WE/WriteIn/'.$res['address'],'/WE/WriteOut/'.$res['address'],pack('H*', $sendData),0);
  720. }else{
  721. if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
  722. $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$id.',"broadcast":0}';
  723. $cmdret = send_cmd($cmd,1,0,$res['protocoltype']);
  724. // if (empty($cmdret)) exit(json_result('0016',$this->response['0016'], array('number'=>$lampData['number'])));
  725. // if($cmdret === false){
  726. // exit(json_result('0008', $this->response['0008']));
  727. // } else {
  728. // $res = json_decode($cmdret, true);
  729. // }
  730. // if (isset($res['result']) && $res['result'] == true) {
  731. // exit(json_result('0000', $this->response['0000']));
  732. // } else {
  733. // $version = $this->session->userdata('version');
  734. // if (empty($version)) {
  735. // $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
  736. // exit(json_result('0010', $res['msg']));
  737. // }else{
  738. // $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
  739. // exit(json_result('other', $res['msg']));
  740. // }
  741. // }
  742. }
  743. }
  744. }
  745. if (!empty($idArr)) {
  746. foreach ($idArr as $id) {
  747. $this->do_setting($id, $data);
  748. }
  749. }
  750. exit(json_result('0000', $this->response['0000'], array()));
  751. }
  752. // 固件升级
  753. public function upgrade(){
  754. $userid = $this->get_user_info('id');
  755. if ($userid != 263) exit(json_result('0011',$this->response['0011']));
  756. $projectid = intval($this->input->post('projectId',true));
  757. if (empty($projectid)) exit(json_result('0007',$this->response['0007']));
  758. $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';
  759. $lampList = $this->db->query($sql)->result_array();
  760. $url = trim($this->input->post('url',true));
  761. if (empty($url)) exit(json_result('0000',$this->response['0000']));
  762. // $url = 'http://station-iot.com/app/m26mqtt/opencpu_m26mqtt_1_0_13.bin';
  763. $len = base_convert(strlen($url), 10, 16).'';
  764. if (mb_strlen($len) > 2) {
  765. $len = mb_substr($len, -2);
  766. }elseif (mb_strlen($len) <= 1) {
  767. $len = '0'.$len;
  768. }
  769. foreach ($lampList as $lampData) {
  770. lampMqttCmd('/WE/UpgradeIn/'.$lampData['address'],'/WE/UpgradeOut/'.$lampData['address'],pack('H*','0001'.$len.'00'.$url),0);
  771. }
  772. $cmd = '{"cmd_type":"app_upgrade_cmd","relateid":'.$projectid.',"senddata":"'.$url.'","cmd_id":"0"}';
  773. $cmdret = send_cmd($cmd,1,0,2);
  774. exit(json_result('0000',$this->response['0000']));
  775. }
  776. }