Project.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. $role = $this->get_user_info('role');
  25. $project_id = intval($this->input->post('project_id', true));
  26. $type = intval($this->input->post('type', true));
  27. $section = $this->input->post('section', true);
  28. $company = intval($this->input->post('company', true));
  29. $version = $this->session->userdata('version');
  30. $data = array();
  31. // 总网络数
  32. if (empty($project_id)) exit(json_result('0007',$this->response['0007']));
  33. $data['network_total'] = $this->Network_model->get_count(array('projectid'=>$project_id,'type'=>0));
  34. $proData = $this->Project_model->get_one(['id'=>$project_id],'projectname,projectid as number,address,createtime as create_time,cityid');
  35. $data['number'] = $proData['number'];
  36. $data['address'] = $proData['address'];
  37. $data['projectname'] = $proData['projectname'];
  38. $data['create_time'] = $proData['create_time'];
  39. $data['cityid'] = $proData['cityid'];
  40. $where = array();
  41. $where['projectid'] = $project_id;
  42. $where2[] = 'L.projectid = '.$project_id;
  43. if ($section !== '' && $section !== NUll) {
  44. $where['section'] = $section;
  45. $where2[] = 'L.section = "'.$section.'"';
  46. }
  47. if (!empty($company) && !empty($type)) {
  48. if ($type == 2) {
  49. $where['manu'] = $company;
  50. $where2[] = 'L.manu = '.$company;
  51. }elseif ($type == 3) {
  52. $where['supplier'] = $company;
  53. $where2[] = 'L.supplier = '.$company;
  54. }elseif ($type == 4) {
  55. $where['po'] = $company;
  56. $where2[] = 'L.po = '.$company;
  57. }else {
  58. // $where['upazilla'] = $company;
  59. // $where2['L.upazilla'] = $company;
  60. }
  61. }
  62. $data['total_lamp'] = $this->Lamp_model->get_count($where);
  63. $join = [];
  64. $join[] = ['table'=>'(select lampid,max(updatetime) as maxTime from warning_info_log group by lampid) as t1','cond'=>'t1.lampid = L.id','type'=>'inner'];
  65. $join[] = ['table'=>'warning_info_log as AI','cond'=>'AI.lampid = t1.lampid AND t1.maxTime = AI.updatetime','type'=>'inner'];
  66. $where2[] = 'L.netstatus = 1';
  67. $where2[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  68. $where2 = implode(' AND ', $where2);
  69. $res = $this->Lamp_model->get_list_by_multi_join($where2, 'count(*) as total',NULL, NULL, $join, NULL, NUll, 'L', true);
  70. $data['failure_count'] = $res['total'];
  71. $where['netstatus'] = 1;
  72. $data['online_count'] = $this->Lamp_model->get_count($where);
  73. $where['status'] = 1;
  74. $data['lighting_count'] = $this->Lamp_model->get_count($where);
  75. // $where2['N.status'] = 1;
  76. // $join = [];
  77. // $join[] = ['table'=>'network as N','cond'=>'N.id = L.networkid','type'=>'left'];
  78. // $res = $this->Lamp_model->get_list_by_multi_join($where2, 'count(*) as total',NULL, NULL, $join, NULL, NUll, 'L', true);
  79. // $data['online_count'] = $res['total'];
  80. // $where2['L.status'] = 1;
  81. // $res = $this->Lamp_model->get_list_by_multi_join($where2, 'count(*) as total',NULL, NULL, $join, NULL, NUll, 'L', true);
  82. // $data['lighting_count'] = $res['total'];
  83. $data['timezone'] = '';
  84. if (empty($data['cityid'])) {
  85. $data['countryId'] = 0;
  86. $data['provinceId'] = 0;
  87. $data['cityId'] = 0;
  88. $data['areaId'] = 0;
  89. $data['weatherList'] = [];
  90. $data['longitude'] = '0';
  91. $data['latitude'] = '0';
  92. $location_ch = '暂无位置信息,请在编辑项目页面选择位置信息';
  93. $location_en = 'There is no location information, please select location information on the edit project page.';
  94. }else{
  95. $res1 = $this->Global_location_model->get_one(['id'=>$data['cityid']],'level,pid,chinese_name,longitude,latitude,english_name');
  96. $data['longitude'] = $res1['longitude'];
  97. $data['latitude'] = $res1['latitude'];
  98. $res2 = $this->Global_location_model->get_one(['id'=>$res1['pid']],'pid,chinese_name,english_name');
  99. if ($res1['level'] == 4) {
  100. $data['areaId'] = $data['cityid'];
  101. $data['cityId'] = $res1['pid'];
  102. $data['provinceId'] = $res2['pid'];
  103. $res3 = $this->Global_location_model->get_one(['id'=>$res2['pid']],'pid,chinese_name,english_name');
  104. $data['countryId'] = $res3['pid'];
  105. $location_ch = $res3['chinese_name'].','.$res2['chinese_name'].','.$res1['chinese_name'];
  106. $location_en = $res1['english_name'].','.$res2['english_name'].','.$res3['english_name'];
  107. }elseif ($res1['level'] == 3) {
  108. $data['areaId'] = 0;
  109. $data['cityId'] = $data['cityid'];
  110. $data['provinceId'] = $res1['pid'];
  111. $data['countryId'] = $res2['pid'];
  112. $location_ch = $res2['chinese_name'].','.$res1['chinese_name'];
  113. $location_en = $res1['english_name'].','.$res2['english_name'];
  114. }elseif ($res1['level'] == 2) {
  115. $data['areaId'] = 0;
  116. $data['cityId'] = 0;
  117. $data['provinceId'] = $data['cityid'];
  118. $data['countryId'] = $res1['pid'];
  119. $location_ch = $res1['chinese_name'];
  120. $location_en = $res1['english_name'];
  121. }else{
  122. $data['areaId'] = 0;
  123. $data['cityId'] = 0;
  124. $data['provinceId'] = 0;
  125. $data['countryId'] = $data['cityid'];
  126. $location_ch = $res1['chinese_name'];
  127. $location_en = $res1['english_name'];
  128. }
  129. $data['location'] = $location_en;
  130. $project_weather = $this->db->query('select * from project_weather where projectId = '.$project_id)->row_array();
  131. if (empty($project_weather) || time() - strtotime($project_weather['updatetime']) > 3600) {
  132. $url = 'https://way.jd.com/he/freeweather?city='.$res1['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  133. $resData = request_post($url);
  134. $arr = json_decode($resData,true);
  135. if ($arr['code'] == 10000) {
  136. if ($arr['result']['HeWeather5'][0]['status'] == 'unknown location' || $arr['result']['HeWeather5'][0]['status'] != 'ok') {
  137. $url = 'https://free-api.heweather.net/s6/weather/forecast?location='.$res1['longitude'].','.$res1['latitude'].'&key=63dbc994630941fab269ad3c3af4d5d2';
  138. // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  139. $resData = request_post($url);
  140. $arr = json_decode($resData,true);
  141. // var_dump($url);
  142. if ($arr['HeWeather6'][0]['status'] == 'ok') {
  143. $now = $arr['HeWeather6'][0]['daily_forecast'][0];
  144. $daily_forecast = $arr['HeWeather6'][0]['daily_forecast'];
  145. $weatherList = array();
  146. $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']))];
  147. for ($i=1; $i < count($daily_forecast); $i++) {
  148. $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']))];
  149. }
  150. $data['weatherList'] = $weatherList;
  151. if (empty($project_weather)) {
  152. $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
  153. $this->db->query($sql);
  154. }else{
  155. $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
  156. $this->db->query($sql);
  157. }
  158. }else{
  159. $data['weatherList'] = [];
  160. }
  161. // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  162. // $resData = request_post($url);
  163. // $arr = json_decode($resData,true);
  164. }else{
  165. if ($arr['result']['HeWeather5'][0]['status'] == 'ok') {
  166. $now = $arr['result']['HeWeather5'][0]['now'];
  167. $daily_forecast = $arr['result']['HeWeather5'][0]['daily_forecast'];
  168. $weatherList = array();
  169. $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']))];
  170. for ($i=1; $i < count($daily_forecast); $i++) {
  171. $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']))];
  172. }
  173. $data['weatherList'] = $weatherList;
  174. if (empty($project_weather)) {
  175. $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
  176. $this->db->query($sql);
  177. }else{
  178. $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
  179. $this->db->query($sql);
  180. }
  181. }else{
  182. $data['weatherList'] = [];
  183. }
  184. }
  185. }else{
  186. $data['weatherList'] = [];
  187. }
  188. }else{
  189. $data['weatherList'] = json_decode($project_weather['weatherInfo'],true);
  190. // var_dump($data['weatherList']);die;
  191. }
  192. }
  193. if (empty($version)) {
  194. foreach ($data['weatherList'] as $key => $value) {
  195. $data['weatherList'][$key]['weatherType'] = weather_translate_cn($data['weatherList'][$key]['code']);
  196. $data['weatherList'][$key]['dateStr'] = date_translate_cn($data['weatherList'][$key]['dateStr']);
  197. }
  198. }else{
  199. foreach ($data['weatherList'] as $key => $value) {
  200. $data['weatherList'][$key]['weatherType'] = weather_translate_en($data['weatherList'][$key]['code']);
  201. }
  202. }
  203. exit(json_result('0000', $this->response['0000'], $data));
  204. }
  205. public function get_pram_data(){
  206. $projectid = $this->input->post('projectid',true);
  207. $res = $this->Load_model->get_data_by_filter(['relateid'=>$projectid,'cmdtype'=>2]);
  208. if (empty($res)) {
  209. $res = [];
  210. $res['workmode'] = '0';
  211. $res['worktimefirst'] = '4';
  212. $res['workpowerfirst'] = '80';
  213. $res['worktimesencond'] = '1';
  214. $res['workpowersencond'] = '40';
  215. $res['worktimethird'] = '8';
  216. $res['workpowerthird'] = '30';
  217. $res['worktimeforth'] = '0';
  218. $res['workpowerforth'] = '30';
  219. $res['voptically'] = '5';
  220. $res['delaytime'] = '1';
  221. $res['ledloadcurrent'] = '330';
  222. $res['powercmd'] = '1';
  223. $res['switchfeature'] = '1';
  224. }
  225. exit(json_result('0000',$this->response['0000'],$res));
  226. }
  227. // 设置负载参数
  228. public function set_pram_data(){
  229. $projectid = $this->input->post('projectid',true);
  230. $controlType = intval($this->input->post('controlType',true));
  231. if ($controlType == 0) {
  232. $data['workmode'] = $this->input->post('workmode',true);
  233. $data['worktimefirst'] = $this->input->post('worktimefirst',true);
  234. $data['workpowerfirst'] = $this->input->post('workpowerfirst',true);
  235. $data['worktimesencond'] = $this->input->post('worktimesencond',true);
  236. $data['workpowersencond'] = $this->input->post('workpowersencond',true);
  237. $data['worktimethird'] = $this->input->post('worktimethird',true);
  238. $data['workpowerthird'] = $this->input->post('workpowerthird',true);
  239. $data['worktimeforth'] = $this->input->post('worktimeforth',true);
  240. $data['workpowerforth'] = $this->input->post('workpowerforth',true);
  241. $data['voptically'] = $this->input->post('voptically',true);
  242. $data['delaytime'] = $this->input->post('delaytime',true);
  243. $data['ledloadcurrent'] = $this->input->post('ledloadcurrent',true);
  244. $data['powercmd'] = $this->input->post('powercmd',true);
  245. $data['switchfeature'] = $this->input->post('switchfeature',true);
  246. }else{
  247. $data['light_voltage'] = $this->input->post('light_voltage',true);
  248. $data['intell_power'] = $this->input->post('intell_power',true);
  249. $data['load_current'] = $this->input->post('load_current',true);
  250. $data['light_delay'] = $this->input->post('light_delay',true);
  251. $data['induction_delay'] = $this->input->post('induction_delay',true);
  252. $data['first_light_time'] = $this->input->post('first_light_time',true);
  253. $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
  254. $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
  255. $data['second_light_time'] = $this->input->post('second_light_time',true);
  256. $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
  257. $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
  258. $data['third_light_time'] = $this->input->post('third_light_time',true);
  259. $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
  260. $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
  261. $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
  262. $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
  263. $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
  264. $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
  265. $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
  266. $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
  267. $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
  268. $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
  269. $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
  270. $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
  271. $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
  272. $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
  273. $data['eighth_light_time'] = $this->input->post('eighth_light_time',true);
  274. $data['eighth_light_human_power'] = $this->input->post('eighth_light_human_power',true);
  275. $data['eighth_light_unman_power'] = $this->input->post('eighth_light_unman_power',true);
  276. $data['ninth_light_time'] = $this->input->post('ninth_light_time',true);
  277. $data['ninth_light_human_power'] = $this->input->post('ninth_light_human_power',true);
  278. $data['ninth_light_unman_power'] = $this->input->post('ninth_light_unman_power',true);
  279. $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
  280. $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
  281. $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
  282. $data['begin_voltage'] = $this->input->post('begin_voltage',true);
  283. $data['end_voltage'] = $this->input->post('end_voltage',true);
  284. $data['end_current'] = $this->input->post('end_current',true);
  285. }
  286. $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();
  287. foreach ($lampList as $value) {
  288. $data['relateid'] = $value['id'];
  289. $data['cmdtype'] = 0;
  290. $data['updatetime'] = date('Y-m-d H:i:s',time());
  291. if ($controlType == 0) {
  292. if (($value['protocoltype'] == 4 || $value['protocoltype'] == 6) && $value['lampprotocoltype'] == 0) {
  293. $address = '';
  294. if (mb_strlen($value['address']) >= 8) {
  295. $address = mb_substr($value['address'], -8);
  296. }else{
  297. $address = '00000000';
  298. }
  299. $sendData = '10040010';
  300. $sendData .= bitSubStr(base_convert($data['workmode'], 10, 16));
  301. $sendData .= bitSubStr(base_convert($data['worktimefirst'], 10, 16));
  302. $sendData .= bitSubStr(base_convert($data['workpowerfirst'], 10, 16));
  303. $sendData .= bitSubStr(base_convert($data['worktimesencond'], 10, 16));
  304. $sendData .= bitSubStr(base_convert($data['workpowersencond'], 10, 16));
  305. $sendData .= bitSubStr(base_convert($data['worktimethird'], 10, 16));
  306. $sendData .= bitSubStr(base_convert($data['workpowerthird'], 10, 16));
  307. $sendData .= bitSubStr(base_convert($data['worktimeforth'], 10, 16));
  308. $sendData .= bitSubStr(base_convert($data['workpowerforth'], 10, 16));
  309. $sendData .= bitSubStr(base_convert($data['delaytime'], 10, 16));
  310. $sendData .= doubBitSubStr(base_convert(intval($data['voptically']*10), 10, 16));
  311. $sendData .= doubBitSubStr(base_convert(intval($data['ledloadcurrent']), 10, 16));
  312. $sendData .= bitSubStr(base_convert($data['powercmd'], 10, 16));
  313. $sendData .= bitSubStr(base_convert($data['switchfeature'], 10, 16));
  314. $sum = 0;
  315. for ($i=0; $i < mb_strlen($sendData)/2; $i++) {
  316. if ($i == 0) continue;
  317. $t = mb_substr($sendData, $i*2,2);
  318. $sum += base_convert($t, 16, 10);
  319. }
  320. $sum = base_convert($sum, 10, 16);
  321. $sendData .= bitSubStr($sum);
  322. $sendData = $address.$sendData;
  323. if ($value['protocoltype'] == 4) {
  324. $res = lampMqttCmd('/IDCOL/CmdInput/'.$value['address'],'/IDCOL/CmdOutput/'.$value['address'],pack('H*','0001'.$sendData),0);
  325. }else{
  326. device_cmd($value['deviceid'],pack('H*',$sendData));
  327. }
  328. continue;
  329. }
  330. $res = $this->Load_model->get_data_by_filter(['relateid'=>$projectid,'cmdtype'=>2]);
  331. if(empty($res)){
  332. $id = $this->Load_model->insert($data);
  333. } else {
  334. $this->Load_model->update(['id'=>$res['id']],$data);
  335. $id = $res['id'];
  336. }
  337. $cmd = '{"cmd_type":"load_param_cmd","cmd_id":'.$id.',"broadcast":0}';
  338. $cmdret = send_cmd($cmd,0,0,$value['protocoltype']);
  339. }else{
  340. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_load_asy';
  341. $temp = $data;
  342. $temp['username'] = $this->input->post('username',true);
  343. $temp['client_key'] = $this->input->post('client_key',true);
  344. $temp['token'] = $this->input->post('token',true);
  345. $temp['clientid'] = $this->input->post('clientid',true);
  346. $temp['version'] = $this->session->userdata('version');
  347. $temp['lamp_id'] = $value['id'];
  348. doAsyncRequest($url,$temp);
  349. // if (($value['protocoltype'] == 4 || $value['protocoltype'] == 6) && $value['lampprotocoltype'] == 0){
  350. // continue;
  351. // }
  352. // $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$value['id'],'cmdtype'=>0]);
  353. // if(empty($res)){
  354. // $id = $this->Modbus_load_model->insert($data);
  355. // } else {
  356. // $this->Modbus_load_model->update(['id'=>$res['id']],$data);
  357. // $id = $res['id'];
  358. // }
  359. // $cmd = '{"cmd_type":"modbus_load_param_cmd","cmd_id":'.$id.',"broadcast":0}';
  360. // $cmdret = send_cmd($cmd,0,0,$value['protocoltype']);
  361. }
  362. }
  363. exit(json_result('0000',$this->response['0000']));
  364. }
  365. // 项目管理侧边导航栏
  366. public function nav(){
  367. $userid = $this->get_user_info('id');
  368. $company = $this->get_user_info('company');
  369. $role = $this->get_user_info('role');
  370. $keyword = $this->input->post('keyword');
  371. $searchType = $this->input->post('searchType');
  372. $type = $this->input->post('type',true);
  373. $where = array();
  374. if (empty($type)) {
  375. if ($role != SYSTEM_ADMIN) $where['company'] = $company;
  376. $data = $this->Project_model->get_list($where, 'projectname,id',NULL, NULL, 'convert(projectname using gbk) asc', NUll);
  377. if (!empty($data)) {
  378. $temp = array();
  379. foreach ($data as $key => $value) {
  380. $value['sectionList'] = array();
  381. $temp[$value['id']] = $value;
  382. }
  383. $projectIdArr = array_column($data, 'id');
  384. $sectionList = $this->Lamp_model->get_list(['projectid'=>$projectIdArr], 'section as name,projectid',NULL, NULL, 'convert(section using gbk) asc', 'projectid,section');
  385. foreach ($sectionList as $key => $value) {
  386. if (isset($temp[$value['projectid']])) {
  387. unset($value['projectid']);
  388. $temp[$sectionList[$key]['projectid']]['sectionList'][] = $value;
  389. }
  390. }
  391. $data = array_values($temp);
  392. }
  393. exit(json_result('0000',$this->response['0000'],$data));
  394. }else{
  395. if ($role != SYSTEM_ADMIN) $where['P.company'] = $company;
  396. $join = [];
  397. $join[] = ['table'=>'project as P','cond'=>'L.projectid = P.id','type'=>'inner'];
  398. if ($type == 2) {
  399. $sectionWhere = ['manu !='=>0];
  400. $join[] = ['table'=>'company as C','cond'=>'L.manu = C.id','type'=>'inner'];
  401. $group = 'L.manu,L.projectid';
  402. $order = 'convert(C.name using gbk) asc';
  403. $fields = 'C.name as company,P.projectname,P.id,C.id as cId';
  404. }elseif ($type == 3) {
  405. $sectionWhere = ['supplier !='=>0];
  406. $join[] = ['table'=>'company as C','cond'=>'L.supplier = C.id','type'=>'inner'];
  407. $group = 'L.supplier,L.projectid';
  408. $order = 'convert(C.name using gbk) asc';
  409. $fields = 'C.name as company,P.projectname,P.id,C.id as cId';
  410. }elseif ($type == 4) {
  411. $sectionWhere = ['po !='=>0];
  412. $join[] = ['table'=>'company as C','cond'=>'L.po = C.id','type'=>'inner'];
  413. $group = 'L.po,L.projectid';
  414. $order = 'convert(C.name using gbk) asc';
  415. $fields = 'C.name as company,P.projectname,P.id,C.id as cId';
  416. }else {
  417. // $sectionWhere = ['upazilla !='=>0];
  418. $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'left'];
  419. // $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'left'];
  420. $group = 'P.cityid,p.id';
  421. $order = 'convert(G.english_name using gbk) asc';
  422. $fields = 'G.english_name as company,P.projectname,P.id,G.id as cId';
  423. }
  424. $data = $this->Lamp_model->get_list_by_join($where, $fields,NULL, NULL, $join, $order, $group, 'L');
  425. if (!empty($data)) {
  426. $temp = array();
  427. $temp2 = array();
  428. foreach ($data as $key => $value) {
  429. $value['sectionList'] = array();
  430. $temp[$value['id']] = $value;
  431. $temp2[$value['cId']] = array('company'=>$value['company'],'cId'=>$value['cId'],'projectList'=>array());
  432. }
  433. $projectIdArr = array_column($data, 'id');
  434. $sectionWhere['projectid'] = $projectIdArr;
  435. $sectionList = $this->Lamp_model->get_list($sectionWhere, 'section as name,projectid',NULL, NULL, 'convert(section using gbk) asc', NUll);
  436. foreach ($sectionList as $key => $value) {
  437. if (isset($temp[$value['projectid']])) {
  438. unset($value['projectid']);
  439. $temp[$sectionList[$key]['projectid']]['sectionList'][] = $value;
  440. }
  441. }
  442. $temp = array_values($temp);
  443. foreach ($temp as $key => $value) {
  444. if (isset($temp2[$value['cId']])) {
  445. unset($value['cId']);
  446. unset($value['company']);
  447. $temp2[$temp[$key]['cId']]['projectList'][] = $value;
  448. }
  449. }
  450. $data = array_values($temp2);
  451. }
  452. exit(json_result('0000',$this->response['0000'],$data));
  453. }
  454. }
  455. // 获取项目下拉列表
  456. public function get() {
  457. $role = $this->get_user_info('role');
  458. $company = $this->get_user_info('company');
  459. $where = array();
  460. if ($role != SYSTEM_ADMIN) $where['company'] = $company;
  461. $list = $this->Project_model->get_list($where,'projectname,id');
  462. exit(json_result('0000', $this->response['0000'], array('list'=>$list)));
  463. }
  464. private function getLeftTimeByProject($project_id){
  465. $lampid_arr = $this->Lamp_model->getLampIds(array('project' => $project_id));
  466. $time_arr = $this->Patrol_model->get_list_in('id',$lampid_arr,'nextquerytime as time');
  467. if (empty($time_arr)) {
  468. return 0;
  469. }else{
  470. $querytime = max($time_arr)['time'];
  471. $nowtime = time();
  472. return $querytime>$nowtime ? intval(($querytime-$nowtime)/60) : 0;
  473. }
  474. }
  475. // 获取项目详情
  476. public function detail() {
  477. $project_id = $this->input->post('project_id', true);
  478. if(empty($project_id)){
  479. exit(json_result('0200', $this->response['0200'], array()));
  480. }
  481. $role = $this->get_user_info('role');
  482. $userid = $this->get_user_info('id');
  483. $fields = 'projectname as project_name,
  484. projectid as project_no,
  485. address,
  486. company,
  487. createtime as create_time,
  488. patrolinterval,
  489. cityid,
  490. lampcount as install_num';
  491. $data = $this->Project_model->getData(array('id' => $project_id), $fields);
  492. $data['timezone'] = 6;
  493. $version = $this->session->userdata('version');
  494. if(!$data){
  495. exit(json_result('0202', $this->response['0202'], $data));
  496. } else {
  497. if (empty($data['cityid'])) {
  498. $data['countryId'] = 0;
  499. $data['provinceId'] = 0;
  500. $data['cityId'] = 0;
  501. $data['areaId'] = 0;
  502. $data['weatherList'] = [];
  503. $data['longitude'] = '0';
  504. $data['latitude'] = '0';
  505. $location_ch = '暂无位置信息,请在编辑项目页面选择位置信息';
  506. $location_en = 'There is no location information, please select location information on the edit project page.';
  507. }else{
  508. $res1 = $this->Global_location_model->get_one(['id'=>$data['cityid']],'level,pid,chinese_name,longitude,latitude,english_name');
  509. $data['longitude'] = $res1['longitude'];
  510. $data['latitude'] = $res1['latitude'];
  511. $res2 = $this->Global_location_model->get_one(['id'=>$res1['pid']],'pid,chinese_name,english_name');
  512. if ($res1['level'] == 4) {
  513. $data['areaId'] = $data['cityid'];
  514. $data['cityId'] = $res1['pid'];
  515. $data['provinceId'] = $res2['pid'];
  516. $res3 = $this->Global_location_model->get_one(['id'=>$res2['pid']],'pid,chinese_name,english_name');
  517. $data['countryId'] = $res3['pid'];
  518. $location_ch = $res3['chinese_name'].','.$res2['chinese_name'].','.$res1['chinese_name'];
  519. $location_en = $res1['english_name'].','.$res2['english_name'].','.$res3['english_name'];
  520. }elseif ($res1['level'] == 3) {
  521. $data['areaId'] = 0;
  522. $data['cityId'] = $data['cityid'];
  523. $data['provinceId'] = $res1['pid'];
  524. $data['countryId'] = $res2['pid'];
  525. $location_ch = $res2['chinese_name'].','.$res1['chinese_name'];
  526. $location_en = $res1['english_name'].','.$res2['english_name'];
  527. }elseif ($res1['level'] == 2) {
  528. $data['areaId'] = 0;
  529. $data['cityId'] = 0;
  530. $data['provinceId'] = $data['cityid'];
  531. $data['countryId'] = $res1['pid'];
  532. $location_ch = $res1['chinese_name'];
  533. $location_en = $res1['english_name'];
  534. }else{
  535. $data['areaId'] = 0;
  536. $data['cityId'] = 0;
  537. $data['provinceId'] = 0;
  538. $data['countryId'] = $data['cityid'];
  539. $location_ch = $res1['chinese_name'];
  540. $location_en = $res1['english_name'];
  541. }
  542. }
  543. $data['location'] = $location_en;
  544. exit(json_result('0000', $this->response['0000'], $data));
  545. }
  546. }
  547. // 添加编辑项目
  548. public function save() {
  549. $role = $this->get_user_info('role');
  550. $where['id'] = $this->input->post('project_id',true);
  551. $data['projectname'] = trim($this->input->post('project_name',true));
  552. $data['cityid'] = $this->input->post('pid',true);
  553. $address = $this->input->post('address',true);
  554. $patrolinterval = $this->input->post('patrolinterval',true);
  555. if(isset($address)) $data['address'] = $address;
  556. if(isset($patrolinterval)) $data['patrolinterval'] = $patrolinterval;
  557. if(empty($data['projectname'])) exit(json_result('0204',$this->response['0204'],array()));
  558. if (empty($where['id'])) { // 添加项目
  559. $data['projectid'] = $this->input->post('project_no',true);
  560. if(empty($data['projectid'])) exit(json_result('0205',$this->response['0205'],array()));
  561. if(mb_strlen($data['projectid']) > 20) exit(json_result('0210',$this->response['0210'],array()));
  562. if(!preg_match('/^[A-Za-z0-9]+$/', $data['projectid'])) exit(json_result('0212',$this->response['0212'],array()));
  563. $data['userId'] = $this->get_user_info('id');
  564. $data['company'] = $this->get_user_info('company');
  565. $data['createtime'] = date('Y-m-d H:i:s',time() - 8*3600);
  566. $projectId = $this->Project_model->add($data);
  567. // $projectId = $data['id'];
  568. $this->add_operation_log('insert',"添加\"{$data['projectname']}\"项目,项目编号\"{$data['projectid']}\"",0);
  569. $this->add_operation_log('insert',"Add project.Project name:\"{$data['projectname']}\".Project number:\"{$data['projectid']}\"",0,1);
  570. } else { // 编辑项目
  571. if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$data['company']), $where['id']) > 0) {
  572. exit(json_result('0203', $this->response['0203'], array()));
  573. }
  574. $ret = $this->Project_model->get_one($where);
  575. if ($ret['cityid'] != $data['cityid']) {
  576. $sql = "delete from project_weather where projectId = ".$where['id'];
  577. $this->db->query($sql);
  578. }
  579. $this->Project_model->update($data,$where);
  580. $projectId = $where['id'];
  581. // 修改项目下巡检信息
  582. // if (isset($patrolinterval)) {
  583. // if (empty($patrolinterval)) {
  584. // $patrolData['patroltype'] = 0;
  585. // }else{
  586. // $patrolData['patroltype'] = 1;
  587. // $patrolData['patrolinterval'] = intval($patrolinterval);
  588. // $patrolData['updatetime'] = date("Y-m-d H:i:s", time());
  589. // $patrolData['nextquerytime'] = date("Y-m-d H:i:s", time());
  590. // }
  591. // $idArr = $this->Lamp_model->get_list_by_project($projectId,'L.id,N.protocoltype,L.address');
  592. // foreach ($idArr as $v) {
  593. // $res = $this->Patrol_model->getOne($v['id']);
  594. // if(empty($res)){
  595. // $patrolData['id'] = $v['id'];
  596. // $this->Patrol_model->insert($patrolData);
  597. // unset($patrolData['id']);
  598. // } else {
  599. // $this->Patrol_model->update(array('id'=>$v['id']), $patrolData);
  600. // }
  601. // if ($v['protocoltype'] == 4) {
  602. // $sendData = '000101A501';;
  603. // $sendData .= bitSubStr(base_convert(intval($patrolData['patrolinterval']/5), 10, 16));
  604. // lampMqttCmd('/lamp/WriteIn/'.$v['address'],'/lamp/WriteOut/'.$v['address'],pack('H*', $sendData),0);
  605. // continue;
  606. // }
  607. // if (isset($v['protocoltype']) && $v['protocoltype'] != 0) {
  608. // $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$v['id'].',"broadcast":0}';
  609. // $cmdret = send_cmd($cmd, 1,0,$v['protocoltype']);
  610. // }
  611. // }
  612. // }
  613. $this->add_operation_log('update',"修改\"{$data['projectname']}\"项目,项目编号\"{$ret['projectid']}\"",0);
  614. $this->add_operation_log('update',"Update project.Project name:\"{$data['projectname']}\".Project number:\"{$ret['projectid']}\"",0,1);
  615. }
  616. exit(json_result('0000', $this->response['0000'], array("id" => intval($projectId))));
  617. }
  618. // 删除项目
  619. public function del() {
  620. $role = $this->get_user_info('role');
  621. $project_id = $this->input->post('project_id', true);
  622. if(empty($project_id)) exit(json_result('0007', $this->response['0007'], array()));
  623. $lampListInfo = $this->Lamp_model->get_one(['projectid'=>$project_id],'id');
  624. if (!empty($lampListInfo)) exit(json_result('0214',$this->response['0214']));
  625. $net_arr = $this->Network_model->get_one(['projectid'=>$project_id],'id');
  626. if (!empty($net_arr)) exit(json_result('0215',$this->response['0215']));
  627. // 删除项目下的监控
  628. $this->Videomonitor_model->delete(array('projectid'=>$project_id));
  629. $this->Weathermonitor_model->delete(array('projectid'=>$project_id));
  630. $projectData = $this->Project_model->getData(array('id'=>$project_id),'projectname,projectid');
  631. $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目,项目编号\"{$projectData['projectid']}\"",0);
  632. $this->add_operation_log('delete',"Delete project.Project name:\"{$projectData['projectname']}\".Project number:\"{$projectData['projectid']}\"",0,1);
  633. // 删除项目
  634. $this->Project_model->delete(['id'=>$project_id]);
  635. exit(json_result('0000', $this->response['0000'], array()));
  636. }
  637. private function do_setting($id, $data){
  638. $ret = $this->Patrol_model->getOne($id);
  639. if(empty($ret)){
  640. $data['id'] = $id;
  641. $this->Patrol_model->insert($data);
  642. } else {
  643. $this->Patrol_model->update(array('id'=>$id), $data);
  644. }
  645. $res = $this->Lamp_model->getOne($id,'N.protocoltype,L.address,L.protocoltype as lampprotocoltype');
  646. if ($res['protocoltype'] == 4) {
  647. $sendData = '000101A501';;
  648. $sendData .= bitSubStr(base_convert(intval($data['patrolinterval']/5), 10, 16));
  649. lampMqttCmd('/lamp/WriteIn/'.$res['address'],'/lamp/WriteOut/'.$res['address'],pack('H*', $sendData),0);
  650. }
  651. if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
  652. $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$id.',"broadcast":0}';
  653. $cmdret = send_cmd($cmd, 1,0,$res['protocoltype']);
  654. }
  655. }
  656. // 巡检设置
  657. public function patrol(){
  658. $username = $this->input->post('username',true);
  659. $role = $this->input->post('role',true);
  660. $client_key = $this->input->post('client_key',true);
  661. $token = $this->input->post('token',true);
  662. $clientid = $this->input->post('clientid',true);
  663. $version = $this->session->userdata('version');
  664. $lampid = intval($this->input->post('lampid', true));
  665. $interval = $this->input->post('interval',true);
  666. $lampArr = explode(',', $lamp_id);
  667. // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  668. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/project/patrol_asy';
  669. // foreach ($lampArr as $l) {
  670. doAsyncRequest($url,['clientid'=>$clientid,'lampid'=>$lampid,'interval'=>$interval,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'role'=>$role]);
  671. // }
  672. exit();
  673. }
  674. public function patrol_asy() {
  675. $lampid = intval($this->input->post('lampid', true));
  676. $interval = $this->input->post('interval',true);
  677. if (empty($lampid)) exit(json_result('0007',$this->response['0007']));
  678. $lampData = $this->Lamp_model->get_one(['id'=>$lampid],'address');
  679. $address = substr($lampData['address'], 2);
  680. $sendData = '000A';
  681. $interval = base_convert($interval, 10, 16);
  682. $sendData .= substr('0000'.$interval, -4);
  683. $res = $this->send_cmd('16',$address,$senddata);
  684. if (empty($res)) {
  685. exit(json_result('0016',$this->response['0016']));
  686. }else{
  687. $res = unpack('H*', $res['msg'])[1];
  688. if (substr($res, 18,2) != '01') exit(json_result('0010',$this->response['0010']));
  689. }
  690. exit(json_result('0000', $this->response['0000'], array()));
  691. }
  692. // 固件升级
  693. public function upgrade(){
  694. $userid = $this->get_user_info('id');
  695. if ($userid != 263) exit(json_result('0011',$this->response['0011']));
  696. $projectid = intval($this->input->post('projectId',true));
  697. if (empty($projectid)) exit(json_result('0007',$this->response['0007']));
  698. $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';
  699. $lampList = $this->db->query($sql)->result_array();
  700. $url = trim($this->input->post('url',true));
  701. if (empty($url)) exit(json_result('0000',$this->response['0000']));
  702. // $url = 'http://station-iot.com/app/m26mqtt/opencpu_m26mqtt_1_0_13.bin';
  703. $len = base_convert(strlen($url), 10, 16).'';
  704. if (mb_strlen($len) > 2) {
  705. $len = mb_substr($len, -2);
  706. }elseif (mb_strlen($len) <= 1) {
  707. $len = '0'.$len;
  708. }
  709. foreach ($lampList as $lampData) {
  710. lampMqttCmd('/lamp/UpgradeIn/'.$lampData['address'],'/lamp/UpgradeOut/'.$lampData['address'],pack('H*','0001'.$len.'00'.$url),0);
  711. }
  712. $cmd = '{"cmd_type":"app_upgrade_cmd","relateid":'.$projectid.',"senddata":"'.$url.'","cmd_id":"0"}';
  713. $cmdret = send_cmd($cmd,1,0,2);
  714. exit(json_result('0000',$this->response['0000']));
  715. }
  716. }