Project.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. $timeData = $this->Global_location_model->get_one(['id'=>$data['cityid']],'timezone');
  84. if ($timeData['timezone'] < 0) {
  85. $timezone = substr('00'.abs($timeData['timezone']), -2).':00';
  86. $data['timezone'] = 'UTC/GMT -'.$timezone;
  87. }else{
  88. $timezone = substr('00'.abs($timeData['timezone']), -2).':00';
  89. $data['timezone'] = 'UTC/GMT +'.$timezone;
  90. }
  91. if (empty($data['cityid'])) {
  92. $data['countryId'] = 0;
  93. $data['provinceId'] = 0;
  94. $data['cityId'] = 0;
  95. $data['areaId'] = 0;
  96. $data['weatherList'] = [];
  97. $data['longitude'] = '0';
  98. $data['latitude'] = '0';
  99. $location_ch = '暂无位置信息,请在编辑项目页面选择位置信息';
  100. $location_en = 'There is no location information, please select location information on the edit project page.';
  101. }else{
  102. $res1 = $this->Global_location_model->get_one(['id'=>$data['cityid']],'level,pid,chinese_name,longitude,latitude,english_name');
  103. $data['area'] = $res1['english_name'];
  104. $data['longitude'] = $res1['longitude'];
  105. $data['latitude'] = $res1['latitude'];
  106. $res2 = $this->Global_location_model->get_one(['id'=>$res1['pid']],'pid,chinese_name,english_name');
  107. $data['city'] = $res2['english_name'];
  108. // if ($res1['level'] == 4) {
  109. $data['areaId'] = $data['cityid'];
  110. $data['cityId'] = $res1['pid'];
  111. $data['provinceId'] = $res2['pid'];
  112. $res3 = $this->Global_location_model->get_one(['id'=>$res2['pid']],'pid,chinese_name,english_name');
  113. $data['province'] = $res3['english_name'];
  114. $data['countryId'] = $res3['pid'];
  115. $location_ch = $res3['chinese_name'].','.$res2['chinese_name'].','.$res1['chinese_name'];
  116. $location_en = $res1['english_name'].','.$res2['english_name'].','.$res3['english_name'];
  117. // }elseif ($res1['level'] == 3) {
  118. // $data['areaId'] = 0;
  119. // $data['cityId'] = $data['cityid'];
  120. // $data['provinceId'] = $res1['pid'];
  121. // $data['countryId'] = $res2['pid'];
  122. // $location_ch = $res2['chinese_name'].','.$res1['chinese_name'];
  123. // $location_en = $res1['english_name'].','.$res2['english_name'];
  124. // }elseif ($res1['level'] == 2) {
  125. // $data['areaId'] = 0;
  126. // $data['cityId'] = 0;
  127. // $data['provinceId'] = $data['cityid'];
  128. // $data['countryId'] = $res1['pid'];
  129. // $location_ch = $res1['chinese_name'];
  130. // $location_en = $res1['english_name'];
  131. // }else{
  132. // $data['areaId'] = 0;
  133. // $data['cityId'] = 0;
  134. // $data['provinceId'] = 0;
  135. // $data['countryId'] = $data['cityid'];
  136. // $location_ch = $res1['chinese_name'];
  137. // $location_en = $res1['english_name'];
  138. // }
  139. $data['location'] = $location_en;
  140. $project_weather = $this->db->query('select * from project_weather where projectId = '.$project_id)->row_array();
  141. if (empty($project_weather) || time() - strtotime($project_weather['updatetime']) > 3600) {
  142. $url = 'https://way.jd.com/he/freeweather?city='.$res1['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  143. $resData = request_post($url);
  144. $arr = json_decode($resData,true);
  145. if ($arr['code'] == 10000) {
  146. if ($arr['result']['HeWeather5'][0]['status'] == 'unknown location' || $arr['result']['HeWeather5'][0]['status'] != 'ok') {
  147. $url = 'https://free-api.heweather.net/s6/weather/forecast?location='.$res1['longitude'].','.$res1['latitude'].'&key=63dbc994630941fab269ad3c3af4d5d2';
  148. // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  149. $resData = request_post($url);
  150. $arr = json_decode($resData,true);
  151. // var_dump($url);
  152. if ($arr['HeWeather6'][0]['status'] == 'ok') {
  153. $now = $arr['HeWeather6'][0]['daily_forecast'][0];
  154. $daily_forecast = $arr['HeWeather6'][0]['daily_forecast'];
  155. $weatherList = array();
  156. $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']))];
  157. for ($i=1; $i < count($daily_forecast); $i++) {
  158. $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']))];
  159. }
  160. $data['weatherList'] = $weatherList;
  161. if (empty($project_weather)) {
  162. $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
  163. $this->db->query($sql);
  164. }else{
  165. $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
  166. $this->db->query($sql);
  167. }
  168. }else{
  169. $data['weatherList'] = [];
  170. }
  171. // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  172. // $resData = request_post($url);
  173. // $arr = json_decode($resData,true);
  174. }else{
  175. if ($arr['result']['HeWeather5'][0]['status'] == 'ok') {
  176. $now = $arr['result']['HeWeather5'][0]['now'];
  177. $daily_forecast = $arr['result']['HeWeather5'][0]['daily_forecast'];
  178. $weatherList = array();
  179. $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']))];
  180. for ($i=1; $i < count($daily_forecast); $i++) {
  181. $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']))];
  182. }
  183. $data['weatherList'] = $weatherList;
  184. if (empty($project_weather)) {
  185. $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
  186. $this->db->query($sql);
  187. }else{
  188. $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
  189. $this->db->query($sql);
  190. }
  191. }else{
  192. $data['weatherList'] = [];
  193. }
  194. }
  195. }else{
  196. $data['weatherList'] = [];
  197. }
  198. }else{
  199. $data['weatherList'] = json_decode($project_weather['weatherInfo'],true);
  200. // var_dump($data['weatherList']);die;
  201. }
  202. }
  203. if (empty($version)) {
  204. foreach ($data['weatherList'] as $key => $value) {
  205. $data['weatherList'][$key]['weatherType'] = weather_translate_cn($data['weatherList'][$key]['code']);
  206. $data['weatherList'][$key]['dateStr'] = date_translate_cn($data['weatherList'][$key]['dateStr']);
  207. }
  208. }else{
  209. foreach ($data['weatherList'] as $key => $value) {
  210. $data['weatherList'][$key]['weatherType'] = weather_translate_en($data['weatherList'][$key]['code']);
  211. }
  212. }
  213. exit(json_result('0000', $this->response['0000'], $data));
  214. }
  215. public function get_pram_data(){
  216. $projectid = $this->input->post('projectid',true);
  217. $res = $this->Load_model->get_data_by_filter(['relateid'=>$projectid,'cmdtype'=>2]);
  218. if (empty($res)) {
  219. $res = [];
  220. $res['workmode'] = '0';
  221. $res['worktimefirst'] = '4';
  222. $res['workpowerfirst'] = '80';
  223. $res['worktimesencond'] = '1';
  224. $res['workpowersencond'] = '40';
  225. $res['worktimethird'] = '8';
  226. $res['workpowerthird'] = '30';
  227. $res['worktimeforth'] = '0';
  228. $res['workpowerforth'] = '30';
  229. $res['voptically'] = '5';
  230. $res['delaytime'] = '1';
  231. $res['ledloadcurrent'] = '330';
  232. $res['powercmd'] = '1';
  233. $res['switchfeature'] = '1';
  234. }
  235. exit(json_result('0000',$this->response['0000'],$res));
  236. }
  237. // 设置负载参数
  238. public function set_pram_data(){
  239. $projectid = $this->input->post('projectid',true);
  240. $controlType = intval($this->input->post('controlType',true));
  241. if ($controlType == 0) {
  242. $data['workmode'] = $this->input->post('workmode',true);
  243. $data['worktimefirst'] = $this->input->post('worktimefirst',true);
  244. $data['workpowerfirst'] = $this->input->post('workpowerfirst',true);
  245. $data['worktimesencond'] = $this->input->post('worktimesencond',true);
  246. $data['workpowersencond'] = $this->input->post('workpowersencond',true);
  247. $data['worktimethird'] = $this->input->post('worktimethird',true);
  248. $data['workpowerthird'] = $this->input->post('workpowerthird',true);
  249. $data['worktimeforth'] = $this->input->post('worktimeforth',true);
  250. $data['workpowerforth'] = $this->input->post('workpowerforth',true);
  251. $data['voptically'] = $this->input->post('voptically',true);
  252. $data['delaytime'] = $this->input->post('delaytime',true);
  253. $data['ledloadcurrent'] = $this->input->post('ledloadcurrent',true);
  254. $data['powercmd'] = $this->input->post('powercmd',true);
  255. $data['switchfeature'] = $this->input->post('switchfeature',true);
  256. }else{
  257. $data['light_voltage'] = $this->input->post('light_voltage',true);
  258. $data['intell_power'] = $this->input->post('intell_power',true);
  259. $data['load_current'] = $this->input->post('load_current',true);
  260. $data['light_delay'] = $this->input->post('light_delay',true);
  261. $data['induction_delay'] = $this->input->post('induction_delay',true);
  262. $data['first_light_time'] = $this->input->post('first_light_time',true);
  263. $data['first_light_human_power'] = $this->input->post('first_light_human_power',true);
  264. $data['first_light_unman_power'] = $this->input->post('first_light_unman_power',true);
  265. $data['second_light_time'] = $this->input->post('second_light_time',true);
  266. $data['second_light_human_power'] = $this->input->post('second_light_human_power',true);
  267. $data['second_light_unman_power'] = $this->input->post('second_light_unman_power',true);
  268. $data['third_light_time'] = $this->input->post('third_light_time',true);
  269. $data['third_light_human_power'] = $this->input->post('third_light_human_power',true);
  270. $data['third_light_unman_power'] = $this->input->post('third_light_unman_power',true);
  271. $data['fourth_light_time'] = $this->input->post('fourth_light_time',true);
  272. $data['fourth_light_human_power'] = $this->input->post('fourth_light_human_power',true);
  273. $data['fourth_light_unman_power'] = $this->input->post('fourth_light_unman_power',true);
  274. $data['fifth_light_time'] = $this->input->post('fifth_light_time',true);
  275. $data['fifth_light_human_power'] = $this->input->post('fifth_light_human_power',true);
  276. $data['fifth_light_unman_power'] = $this->input->post('fifth_light_unman_power',true);
  277. $data['sixth_light_time'] = $this->input->post('sixth_light_time',true);
  278. $data['sixth_light_human_power'] = $this->input->post('sixth_light_human_power',true);
  279. $data['sixth_light_unman_power'] = $this->input->post('sixth_light_unman_power',true);
  280. $data['seventh_light_time'] = $this->input->post('seventh_light_time',true);
  281. $data['seventh_light_human_power'] = $this->input->post('seventh_light_human_power',true);
  282. $data['seventh_light_unman_power'] = $this->input->post('seventh_light_unman_power',true);
  283. $data['eighth_light_time'] = $this->input->post('eighth_light_time',true);
  284. $data['eighth_light_human_power'] = $this->input->post('eighth_light_human_power',true);
  285. $data['eighth_light_unman_power'] = $this->input->post('eighth_light_unman_power',true);
  286. $data['ninth_light_time'] = $this->input->post('ninth_light_time',true);
  287. $data['ninth_light_human_power'] = $this->input->post('ninth_light_human_power',true);
  288. $data['ninth_light_unman_power'] = $this->input->post('ninth_light_unman_power',true);
  289. $data['tenth_light_time'] = $this->input->post('tenth_light_time',true);
  290. $data['tenth_light_human_power'] = $this->input->post('tenth_light_human_power',true);
  291. $data['tenth_light_unman_power'] = $this->input->post('tenth_light_unman_power',true);
  292. $data['begin_voltage'] = $this->input->post('begin_voltage',true);
  293. $data['end_voltage'] = $this->input->post('end_voltage',true);
  294. $data['end_current'] = $this->input->post('end_current',true);
  295. }
  296. $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();
  297. foreach ($lampList as $value) {
  298. $data['relateid'] = $value['id'];
  299. $data['cmdtype'] = 0;
  300. $data['updatetime'] = date('Y-m-d H:i:s',time());
  301. if ($controlType == 0) {
  302. if (($value['protocoltype'] == 4 || $value['protocoltype'] == 6) && $value['lampprotocoltype'] == 0) {
  303. $address = '';
  304. if (mb_strlen($value['address']) >= 8) {
  305. $address = mb_substr($value['address'], -8);
  306. }else{
  307. $address = '00000000';
  308. }
  309. $sendData = '10040010';
  310. $sendData .= bitSubStr(base_convert($data['workmode'], 10, 16));
  311. $sendData .= bitSubStr(base_convert($data['worktimefirst'], 10, 16));
  312. $sendData .= bitSubStr(base_convert($data['workpowerfirst'], 10, 16));
  313. $sendData .= bitSubStr(base_convert($data['worktimesencond'], 10, 16));
  314. $sendData .= bitSubStr(base_convert($data['workpowersencond'], 10, 16));
  315. $sendData .= bitSubStr(base_convert($data['worktimethird'], 10, 16));
  316. $sendData .= bitSubStr(base_convert($data['workpowerthird'], 10, 16));
  317. $sendData .= bitSubStr(base_convert($data['worktimeforth'], 10, 16));
  318. $sendData .= bitSubStr(base_convert($data['workpowerforth'], 10, 16));
  319. $sendData .= bitSubStr(base_convert($data['delaytime'], 10, 16));
  320. $sendData .= doubBitSubStr(base_convert(intval($data['voptically']*10), 10, 16));
  321. $sendData .= doubBitSubStr(base_convert(intval($data['ledloadcurrent']), 10, 16));
  322. $sendData .= bitSubStr(base_convert($data['powercmd'], 10, 16));
  323. $sendData .= bitSubStr(base_convert($data['switchfeature'], 10, 16));
  324. $sum = 0;
  325. for ($i=0; $i < mb_strlen($sendData)/2; $i++) {
  326. if ($i == 0) continue;
  327. $t = mb_substr($sendData, $i*2,2);
  328. $sum += base_convert($t, 16, 10);
  329. }
  330. $sum = base_convert($sum, 10, 16);
  331. $sendData .= bitSubStr($sum);
  332. $sendData = $address.$sendData;
  333. if ($value['protocoltype'] == 4) {
  334. $res = lampMqttCmd('/IDCOL/CmdInput/'.$value['address'],'/IDCOL/CmdOutput/'.$value['address'],pack('H*','0001'.$sendData),0);
  335. }else{
  336. device_cmd($value['deviceid'],pack('H*',$sendData));
  337. }
  338. continue;
  339. }
  340. $res = $this->Load_model->get_data_by_filter(['relateid'=>$projectid,'cmdtype'=>2]);
  341. if(empty($res)){
  342. $id = $this->Load_model->insert($data);
  343. } else {
  344. $this->Load_model->update(['id'=>$res['id']],$data);
  345. $id = $res['id'];
  346. }
  347. $cmd = '{"cmd_type":"load_param_cmd","cmd_id":'.$id.',"broadcast":0}';
  348. $cmdret = send_cmd($cmd,0,0,$value['protocoltype']);
  349. }else{
  350. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/device/lampcontrol/modbus_set_load_asy';
  351. $temp = $data;
  352. $temp['username'] = $this->input->post('username',true);
  353. $temp['client_key'] = $this->input->post('client_key',true);
  354. $temp['token'] = $this->input->post('token',true);
  355. $temp['clientid'] = $this->input->post('clientid',true);
  356. $temp['version'] = $this->session->userdata('version');
  357. $temp['lamp_id'] = $value['id'];
  358. doAsyncRequest($url,$temp);
  359. // if (($value['protocoltype'] == 4 || $value['protocoltype'] == 6) && $value['lampprotocoltype'] == 0){
  360. // continue;
  361. // }
  362. // $res = $this->Modbus_load_model->get_data_by_filter(['relateid'=>$value['id'],'cmdtype'=>0]);
  363. // if(empty($res)){
  364. // $id = $this->Modbus_load_model->insert($data);
  365. // } else {
  366. // $this->Modbus_load_model->update(['id'=>$res['id']],$data);
  367. // $id = $res['id'];
  368. // }
  369. // $cmd = '{"cmd_type":"modbus_load_param_cmd","cmd_id":'.$id.',"broadcast":0}';
  370. // $cmdret = send_cmd($cmd,0,0,$value['protocoltype']);
  371. }
  372. }
  373. exit(json_result('0000',$this->response['0000']));
  374. }
  375. // 项目管理侧边导航栏
  376. public function nav(){
  377. $userid = $this->get_user_info('id');
  378. $company = $this->get_user_info('company');
  379. $role = $this->get_user_info('role');
  380. $keyword = $this->input->post('keyword');
  381. $searchType = $this->input->post('searchType');
  382. $type = $this->input->post('type',true);
  383. $where = array();
  384. if (empty($type)) {
  385. if ($role != SYSTEM_ADMIN) {
  386. if ($role == 2) {
  387. // $filter .= ' AND L.manu = '.$company;
  388. $where['P.company = '] = $company;
  389. $sectionWhere['P.company'] = $company;
  390. // $countWhere[] = 'manu = '.$company;
  391. }elseif ($role == 3) {
  392. // $filter .= ' AND L.supplier = '.$company;
  393. // $countWhere[] = 'supplier = '.$company;
  394. // $where[''] = $company;
  395. $where = '(L.supplier = '.$company.' or P.company = '.$company.')';
  396. $sectionWhere['L.supplier'] = $company;
  397. }elseif ($role == 4) {
  398. // $filter .= ' AND L.po = '.$company;
  399. // $countWhere[] = 'po = '.$company;
  400. // $where['L.po = '] = $company;
  401. $where = '(L.po = '.$company.' or P.company = '.$company.')';
  402. $sectionWhere['L.po'] = $company;
  403. }else {
  404. // $filter .= ' AND P.cityid = '.$company;
  405. // $where['P.cityid = '] = $company;
  406. $where = '(P.cityid = '.$company.' or P.company = '.$company.')';
  407. $sectionWhere['P.cityid'] = $company;
  408. }
  409. }
  410. // $data = $this->Project_model->get_list($where, 'projectname,id,lampcount',NULL, NULL, 'convert(projectname using gbk) asc', NUll);
  411. $join = array();
  412. $join = ['table'=>'lampinfo as L','cond'=>'L.projectid = P.id','type'=>'left'];
  413. $data = $this->Project_model->get_list_by_join($where, 'P.projectname,P.id,count(L.id) as lampcount',NULL, NULL, $join, 'convert(projectname using gbk) asc', 'P.id', 'P');
  414. if (!empty($data)) {
  415. $temp = array();
  416. foreach ($data as $key => $value) {
  417. $value['sectionList'] = array();
  418. $temp[$value['id']] = $value;
  419. }
  420. $projectIdArr = array_column($data, 'id');
  421. $sectionWhere['L.projectid'] = $projectIdArr;
  422. $sectionWhere['L.section !='] = '';
  423. $join = array();
  424. $join = ['table'=>'project as P','cond'=>'L.projectid = P.id','type'=>'left'];
  425. $sectionList = $this->Lamp_model->get_list_by_join($sectionWhere, 'L.section as name,L.projectid,count(*) as lampcount',NULL, NULL,$join, 'convert(L.section using gbk) asc', 'L.projectid,L.section','L');
  426. foreach ($sectionList as $key => $value) {
  427. if (isset($temp[$value['projectid']])) {
  428. unset($value['projectid']);
  429. $temp[$sectionList[$key]['projectid']]['sectionList'][] = $value;
  430. }
  431. }
  432. $data = array_values($temp);
  433. }
  434. if ($this->os == 2) {
  435. exit(json_result('0000',$this->response['0000'],['list'=>$data]));
  436. }else{
  437. exit(json_result('0000',$this->response['0000'],$data));
  438. }
  439. }else{
  440. // if ($role != SYSTEM_ADMIN) $where['P.company'] = $company;
  441. $join = [];
  442. $join[] = ['table'=>'project as P','cond'=>'L.projectid = P.id','type'=>'inner'];
  443. if ($type == 2) {
  444. if ($role != SYSTEM_ADMIN) {
  445. if ($role == 2) {
  446. // $filter .= ' AND L.manu = '.$company;
  447. $where['L.manu = '] = $company;
  448. $sectionWhere['L.manu'] = $company;
  449. // $countWhere[] = 'manu = '.$company;
  450. }elseif ($role == 3) {
  451. // $filter .= ' AND L.supplier = '.$company;
  452. // $countWhere[] = 'supplier = '.$company;
  453. $where['L.supplier = '] = $company;
  454. $sectionWhere['L.supplier'] = $company;
  455. }elseif ($role == 4) {
  456. // $filter .= ' AND L.po = '.$company;
  457. // $countWhere[] = 'po = '.$company;
  458. $where['L.po = '] = $company;
  459. $sectionWhere['L.po'] = $company;
  460. }else {
  461. // $filter .= ' AND P.cityid = '.$company;
  462. $where['P.cityid = '] = $company;
  463. $sectionWhere['P.cityid'] = $company;
  464. }
  465. }
  466. // $sectionWhere = ['manu !='=>0];
  467. $join[] = ['table'=>'company as C','cond'=>'L.manu = C.id','type'=>'inner'];
  468. $group = 'L.manu,L.projectid';
  469. $order = 'convert(C.name using gbk) asc';
  470. $fields = 'C.name as company,C.no,P.projectname,P.id,C.id as cId,count(*) as lampcount';
  471. $fields2 = 'L.manu as cId';
  472. }elseif ($type == 3) {
  473. if ($role != SYSTEM_ADMIN) {
  474. if ($role == 2) {
  475. // $filter .= ' AND L.manu = '.$company;
  476. $where['L.manu = '] = $company;
  477. $sectionWhere['L.manu'] = $company;
  478. // $countWhere[] = 'manu = '.$company;
  479. }elseif ($role == 3) {
  480. // $filter .= ' AND L.supplier = '.$company;
  481. // $countWhere[] = 'supplier = '.$company;
  482. $where['L.supplier = '] = $company;
  483. $sectionWhere['L.supplier'] = $company;
  484. }elseif ($role == 4) {
  485. // $filter .= ' AND L.po = '.$company;
  486. // $countWhere[] = 'po = '.$company;
  487. $where['L.po = '] = $company;
  488. $where['L.supplier !='] = 0;
  489. $sectionWhere['L.po'] = $company;
  490. $sectionWhere['supplier !='] = 0;
  491. // $sectionWhere['supplier !='] = '';
  492. }else {
  493. // $filter .= ' AND P.cityid = '.$company;
  494. $where['P.cityid = '] = $company;
  495. $sectionWhere['P.cityid'] = $company;
  496. }
  497. }
  498. $join[] = ['table'=>'company as C','cond'=>'L.supplier = C.id','type'=>'inner'];
  499. $group = 'L.supplier,L.projectid';
  500. $order = 'convert(C.name using gbk) asc';
  501. $fields = 'C.name as company,C.no,P.projectname,P.id,C.id as cId,count(*) as lampcount';
  502. $fields2 = 'L.supplier as cId';
  503. }elseif ($type == 4) {
  504. // $sectionWhere = ['po'=>$company];
  505. if ($role != SYSTEM_ADMIN) {
  506. if ($role == 2) {
  507. // $filter .= ' AND L.manu = '.$company;
  508. $where['L.manu = '] = $company;
  509. $sectionWhere['L.manu'] = $company;
  510. // $countWhere[] = 'manu = '.$company;
  511. }elseif ($role == 3) {
  512. // $filter .= ' AND L.supplier = '.$company;
  513. // $countWhere[] = 'supplier = '.$company;
  514. $where['L.supplier = '] = $company;
  515. $sectionWhere['L.supplier'] = $company;
  516. }elseif ($role == 4) {
  517. // $filter .= ' AND L.po = '.$company;
  518. // $countWhere[] = 'po = '.$company;
  519. $where['L.po = '] = $company;
  520. $sectionWhere['L.po'] = $company;
  521. }else {
  522. // $filter .= ' AND P.cityid = '.$company;
  523. $where['P.cityid = '] = $company;
  524. $sectionWhere['P.cityid'] = $company;
  525. }
  526. }
  527. $join[] = ['table'=>'company as C','cond'=>'L.po = C.id','type'=>'inner'];
  528. $group = 'L.po,L.projectid';
  529. $order = 'convert(C.name using gbk) asc';
  530. $fields = 'C.name as company,C.no,P.projectname,P.id,C.id as cId,count(*) as lampcount';
  531. $fields2 = 'L.po as cId';
  532. }else {
  533. // $sectionWhere = ['upazilla !='=>0];
  534. if ($role != SYSTEM_ADMIN) {
  535. if ($role == 2) {
  536. // $filter .= ' AND L.manu = '.$company;
  537. $where['L.manu = '] = $company;
  538. $sectionWhere['L.manu'] = $company;
  539. // $countWhere[] = 'manu = '.$company;
  540. }elseif ($role == 3) {
  541. // $filter .= ' AND L.supplier = '.$company;
  542. // $countWhere[] = 'supplier = '.$company;
  543. $where['L.supplier = '] = $company;
  544. $sectionWhere['L.supplier'] = $company;
  545. }elseif ($role == 4) {
  546. // $filter .= ' AND L.po = '.$company;
  547. // $countWhere[] = 'po = '.$company;
  548. $where['L.po = '] = $company;
  549. $sectionWhere['L.po'] = $company;
  550. }else {
  551. // $filter .= ' AND P.cityid = '.$company;
  552. $where['P.cityid = '] = $company;
  553. $sectionWhere['P.cityid'] = $company;
  554. }
  555. }
  556. $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'left'];
  557. // $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'left'];
  558. $group = 'P.cityid,p.id';
  559. $order = 'convert(G.english_name using gbk) asc';
  560. $fields = 'G.english_name as company,P.projectname,P.id,G.id as cId,count(*) as lampcount';
  561. $fields2 = 'P.cityid as cId';
  562. }
  563. // var_dump($where);die;
  564. $data = $this->Lamp_model->get_list_by_join($where, $fields,NULL, NULL, $join, $order, $group, 'L');
  565. // var_dump($data);die;
  566. if (!empty($data)) {
  567. $temp = array();
  568. $temp2 = array();
  569. foreach ($data as $key => $value) {
  570. $value['sectionList'] = array();
  571. $temp[$value['id']] = $value;
  572. if (isset($value['no']) && $value['no'] !== '') $value['company'] = $value['no'];
  573. if (isset($temp2[$value['cId']])) {
  574. $temp2[$value['cId']]['lampcount'] += $value['lampcount'];
  575. }else{
  576. $temp2[$value['cId']] = array('company'=>$value['company'],'lampcount'=>$value['lampcount'],'cId'=>$value['cId'],'projectList'=>array());
  577. }
  578. }
  579. $projectIdArr = array_column($data, 'id');
  580. $sectionWhere['L.projectid'] = $projectIdArr;
  581. // $sectionWhere['L.section !='] = '';
  582. // var_dump($sectionWhere);die;
  583. // array(2) { ["L.po"]=> string(3) "144" ["L.projectid"]=> array(8) { [0]=> string(2) "20" [1]=> string(2) "28" [2]=> string(2) "43" [3]=> string(2) "20" [4]=> string(2) "39" [5]=> string(2) "28" [6]=> string(2) "42" [7]=> string(2) "51" } }
  584. // "projectid = 28 AND supplier = 116 AND po = 144 AND section = "1234""
  585. // $sectionList = $this->Lamp_model->get_list($sectionWhere, 'section as name,projectid,count(*) as lampcount',NULL, NULL, 'convert(section using gbk) asc', 'projectid,section');
  586. $join = array();
  587. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'left'];
  588. $sectionList = $this->Lamp_model->get_list_by_join($sectionWhere, 'L.section as name,L.projectid,count(*) as lampcount,'.$fields2,NULL, NULL,$join, 'convert(L.section using gbk) asc', $group.',L.section','L');
  589. foreach ($sectionList as $key => $value) {
  590. if (isset($temp2[$value['cId']])) {
  591. if (!isset($temp2[$value['cId']]['projectList'][$value['projectid']]['sectionList'])) {
  592. $temp2[$value['cId']]['projectList'][$value['projectid']]['sectionList'] = array();
  593. }
  594. $temp2[$value['cId']]['projectList'][$value['projectid']]['sectionList'][] = $value;
  595. // var_dump($value);
  596. }
  597. // if (isset($temp[$value['projectid']])) {
  598. // unset($value['projectid']);
  599. // $temp[$sectionList[$key]['projectid']]['sectionList'][] = $value;
  600. // }
  601. }
  602. // $temp = array_values($temp);
  603. // foreach ($temp as $key => $value) {
  604. // if (isset($temp2[$value['cId']])) {
  605. // unset($value['cId']);
  606. // unset($value['company']);
  607. // $temp2[$temp[$key]['cId']]['projectList'][] = $value;
  608. // }
  609. // }
  610. foreach ($temp2 as $key => $value) {
  611. foreach ($value['projectList'] as $key2 => $value2) {
  612. foreach ($value2['sectionList'] as $k => $sec) {
  613. if (empty($sec['name'])) {
  614. unset($value2['sectionList'][$k]);
  615. }
  616. }
  617. $temp2[$key]['projectList'][$key2]['sectionList'] = array_values($value2['sectionList']);
  618. if (isset($temp[$key2])) {
  619. $temp2[$key]['projectList'][$key2]['projectname'] = $temp[$key2]['projectname'];
  620. $temp2[$key]['projectList'][$key2]['id'] = $temp[$key2]['id'];
  621. $temp2[$key]['projectList'][$key2]['lampcount'] = $temp[$key2]['lampcount'];
  622. }
  623. }
  624. }
  625. foreach ($temp2 as $key => $value) {
  626. $temp2[$key]['projectList'] = array_values($value['projectList']);
  627. }
  628. $data = array_values($temp2);
  629. }
  630. if ($this->os == 2) {
  631. exit(json_result('0000',$this->response['0000'],['list'=>$data]));
  632. }else{
  633. exit(json_result('0000',$this->response['0000'],$data));
  634. }
  635. }
  636. }
  637. // 获取项目下拉列表
  638. public function get() {
  639. $role = $this->get_user_info('role');
  640. $company = $this->get_user_info('company');
  641. $where = array();
  642. if ($role != SYSTEM_ADMIN) $where['company'] = $company;
  643. $list = $this->Project_model->get_list($where,'projectname,id');
  644. exit(json_result('0000', $this->response['0000'], array('list'=>$list)));
  645. }
  646. private function getLeftTimeByProject($project_id){
  647. $lampid_arr = $this->Lamp_model->getLampIds(array('project' => $project_id));
  648. $time_arr = $this->Patrol_model->get_list_in('id',$lampid_arr,'nextquerytime as time');
  649. if (empty($time_arr)) {
  650. return 0;
  651. }else{
  652. $querytime = max($time_arr)['time'];
  653. $nowtime = time();
  654. return $querytime>$nowtime ? intval(($querytime-$nowtime)/60) : 0;
  655. }
  656. }
  657. // 获取项目详情
  658. public function detail() {
  659. $project_id = $this->input->post('project_id', true);
  660. if(empty($project_id)){
  661. exit(json_result('0200', $this->response['0200'], array()));
  662. }
  663. $role = $this->get_user_info('role');
  664. $userid = $this->get_user_info('id');
  665. $fields = 'projectname as project_name,
  666. projectid as project_no,
  667. address,
  668. company,
  669. createtime as create_time,
  670. patrolinterval,
  671. cityid,
  672. lampcount as install_num';
  673. $data = $this->Project_model->getData(array('id' => $project_id), $fields);
  674. $data['timezone'] = 6;
  675. $version = $this->session->userdata('version');
  676. if(!$data){
  677. exit(json_result('0202', $this->response['0202'], $data));
  678. } else {
  679. if (empty($data['cityid'])) {
  680. $data['countryId'] = 0;
  681. $data['provinceId'] = 0;
  682. $data['cityId'] = 0;
  683. $data['areaId'] = 0;
  684. $data['weatherList'] = [];
  685. $data['longitude'] = '0';
  686. $data['latitude'] = '0';
  687. $location_ch = '暂无位置信息,请在编辑项目页面选择位置信息';
  688. $location_en = 'There is no location information, please select location information on the edit project page.';
  689. }else{
  690. $res1 = $this->Global_location_model->get_one(['id'=>$data['cityid']],'level,pid,chinese_name,longitude,latitude,english_name');
  691. $data['area'] = $res1['english_name'];
  692. $data['longitude'] = $res1['longitude'];
  693. $data['latitude'] = $res1['latitude'];
  694. $res2 = $this->Global_location_model->get_one(['id'=>$res1['pid']],'pid,chinese_name,english_name');
  695. $data['city'] = $res2['english_name'];
  696. // if ($res1['level'] == 4) {
  697. $data['areaId'] = $data['cityid'];
  698. $data['cityId'] = $res1['pid'];
  699. $data['provinceId'] = $res2['pid'];
  700. $res3 = $this->Global_location_model->get_one(['id'=>$res2['pid']],'pid,chinese_name,english_name');
  701. $data['province'] = $res3['english_name'];
  702. $data['countryId'] = $res3['pid'];
  703. $location_ch = $res3['chinese_name'].','.$res2['chinese_name'].','.$res1['chinese_name'];
  704. $location_en = $res1['english_name'].','.$res2['english_name'].','.$res3['english_name'];
  705. // }elseif ($res1['level'] == 3) {
  706. // $data['areaId'] = 0;
  707. // $data['cityId'] = $data['cityid'];
  708. // $data['provinceId'] = $res1['pid'];
  709. // $data['countryId'] = $res2['pid'];
  710. // $location_ch = $res2['chinese_name'].','.$res1['chinese_name'];
  711. // $location_en = $res1['english_name'].','.$res2['english_name'];
  712. // }elseif ($res1['level'] == 2) {
  713. // $data['areaId'] = 0;
  714. // $data['cityId'] = 0;
  715. // $data['provinceId'] = $data['cityid'];
  716. // $data['countryId'] = $res1['pid'];
  717. // $location_ch = $res1['chinese_name'];
  718. // $location_en = $res1['english_name'];
  719. // }else{
  720. // $data['areaId'] = 0;
  721. // $data['cityId'] = 0;
  722. // $data['provinceId'] = 0;
  723. // $data['countryId'] = $data['cityid'];
  724. // $location_ch = $res1['chinese_name'];
  725. // $location_en = $res1['english_name'];
  726. // }
  727. }
  728. $data['location'] = $location_en;
  729. exit(json_result('0000', $this->response['0000'], $data));
  730. }
  731. }
  732. // 添加编辑项目
  733. public function save() {
  734. $role = $this->get_user_info('role');
  735. $where['id'] = $this->input->post('project_id',true);
  736. $data['projectname'] = trim($this->input->post('project_name',true));
  737. $data['cityid'] = $this->input->post('pid',true);
  738. $address = $this->input->post('address',true);
  739. $patrolinterval = $this->input->post('patrolinterval',true);
  740. if(isset($address)) $data['address'] = $address;
  741. if(isset($patrolinterval)) $data['patrolinterval'] = $patrolinterval;
  742. if(empty($data['projectname'])) exit(json_result('0204',$this->response['0204'],array()));
  743. if (empty($where['id'])) { // 添加项目
  744. $data['userId'] = $this->get_user_info('id');
  745. $data['company'] = $this->get_user_info('company');
  746. if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$data['company'])) > 0) {
  747. exit(json_result('0203', $this->response['0203'], array()));
  748. }
  749. $data['projectid'] = $this->input->post('project_no',true);
  750. if(empty($data['projectid'])) exit(json_result('0205',$this->response['0205'],array()));
  751. if(mb_strlen($data['projectid']) > 20) exit(json_result('0210',$this->response['0210'],array()));
  752. if(!preg_match('/^[A-Za-z0-9]+$/', $data['projectid'])) exit(json_result('0212',$this->response['0212'],array()));
  753. $data['createtime'] = date('Y-m-d H:i:s',time() - 8*3600);
  754. $projectId = $this->Project_model->add($data);
  755. // $projectId = $data['id'];
  756. $this->add_operation_log('insert',"添加\"{$data['projectname']}\"项目,项目编号\"{$data['projectid']}\"",0);
  757. $this->add_operation_log('insert',"Add project.Project name:\"{$data['projectname']}\".Project number:\"{$data['projectid']}\"",0,1);
  758. } else { // 编辑项目
  759. $company = $this->get_user_info('company');
  760. if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$company), $where['id']) > 0) {
  761. exit(json_result('0203', $this->response['0203'], array()));
  762. }
  763. $ret = $this->Project_model->get_one($where);
  764. if ($ret['cityid'] != $data['cityid']) {
  765. $sql = "delete from project_weather where projectId = ".$where['id'];
  766. $this->db->query($sql);
  767. }
  768. $this->Project_model->update($data,$where);
  769. $projectId = $where['id'];
  770. // 修改项目下巡检信息
  771. // if (isset($patrolinterval)) {
  772. // if (empty($patrolinterval)) {
  773. // $patrolData['patroltype'] = 0;
  774. // }else{
  775. // $patrolData['patroltype'] = 1;
  776. // $patrolData['patrolinterval'] = intval($patrolinterval);
  777. // $patrolData['updatetime'] = date("Y-m-d H:i:s", time());
  778. // $patrolData['nextquerytime'] = date("Y-m-d H:i:s", time());
  779. // }
  780. // $idArr = $this->Lamp_model->get_list_by_project($projectId,'L.id,N.protocoltype,L.address');
  781. // foreach ($idArr as $v) {
  782. // $res = $this->Patrol_model->getOne($v['id']);
  783. // if(empty($res)){
  784. // $patrolData['id'] = $v['id'];
  785. // $this->Patrol_model->insert($patrolData);
  786. // unset($patrolData['id']);
  787. // } else {
  788. // $this->Patrol_model->update(array('id'=>$v['id']), $patrolData);
  789. // }
  790. // if ($v['protocoltype'] == 4) {
  791. // $sendData = '000101A501';;
  792. // $sendData .= bitSubStr(base_convert(intval($patrolData['patrolinterval']/5), 10, 16));
  793. // lampMqttCmd('/lamp/WriteIn/'.$v['address'],'/lamp/WriteOut/'.$v['address'],pack('H*', $sendData),0);
  794. // continue;
  795. // }
  796. // if (isset($v['protocoltype']) && $v['protocoltype'] != 0) {
  797. // $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$v['id'].',"broadcast":0}';
  798. // $cmdret = send_cmd($cmd, 1,0,$v['protocoltype']);
  799. // }
  800. // }
  801. // }
  802. $this->add_operation_log('update',"修改\"{$data['projectname']}\"项目,项目编号\"{$ret['projectid']}\"",0);
  803. $this->add_operation_log('update',"Update project.Project name:\"{$data['projectname']}\".Project number:\"{$ret['projectid']}\"",0,1);
  804. }
  805. exit(json_result('0000', $this->response['0000'], array("id" => intval($projectId))));
  806. }
  807. // 删除项目
  808. public function del() {
  809. $role = $this->get_user_info('role');
  810. $project_id = $this->input->post('project_id', true);
  811. if(empty($project_id)) exit(json_result('0007', $this->response['0007'], array()));
  812. $lampListInfo = $this->Lamp_model->get_one(['projectid'=>$project_id],'id');
  813. if (!empty($lampListInfo)) exit(json_result('0214',$this->response['0214']));
  814. // $net_arr = $this->Network_model->get_one(['projectid'=>$project_id],'id');
  815. // if (!empty($net_arr)) exit(json_result('0215',$this->response['0215']));
  816. // 删除项目下的监控
  817. // $this->Videomonitor_model->delete(array('projectid'=>$project_id));
  818. // $this->Weathermonitor_model->delete(array('projectid'=>$project_id));
  819. $projectData = $this->Project_model->getData(array('id'=>$project_id),'projectname,projectid');
  820. $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目,项目编号\"{$projectData['projectid']}\"",0);
  821. $this->add_operation_log('delete',"Delete project.Project name:\"{$projectData['projectname']}\".Project number:\"{$projectData['projectid']}\"",0,1);
  822. // 删除项目
  823. $this->Project_model->delete(['id'=>$project_id]);
  824. exit(json_result('0000', $this->response['0000'], array()));
  825. }
  826. private function do_setting($id, $data){
  827. $ret = $this->Patrol_model->getOne($id);
  828. if(empty($ret)){
  829. $data['id'] = $id;
  830. $this->Patrol_model->insert($data);
  831. } else {
  832. $this->Patrol_model->update(array('id'=>$id), $data);
  833. }
  834. $res = $this->Lamp_model->getOne($id,'N.protocoltype,L.address,L.protocoltype as lampprotocoltype');
  835. if ($res['protocoltype'] == 4) {
  836. $sendData = '000101A501';;
  837. $sendData .= bitSubStr(base_convert(intval($data['patrolinterval']/5), 10, 16));
  838. lampMqttCmd('/lamp/WriteIn/'.$res['address'],'/lamp/WriteOut/'.$res['address'],pack('H*', $sendData),0);
  839. }
  840. if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
  841. $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$id.',"broadcast":0}';
  842. $cmdret = send_cmd($cmd, 1,0,$res['protocoltype']);
  843. }
  844. }
  845. // 巡检设置
  846. public function patrol(){
  847. $username = $this->input->post('username',true);
  848. $role = $this->input->post('role',true);
  849. $client_key = $this->input->post('client_key',true);
  850. $token = $this->input->post('token',true);
  851. $clientid = $this->input->post('clientid',true);
  852. $version = $this->session->userdata('version');
  853. $lampid = intval($this->input->post('lampid', true));
  854. $interval = $this->input->post('interval',true);
  855. $lampArr = explode(',', $lamp_id);
  856. // $lampList = $this->Lamp_model->get_list_in('L.id', $lampArr, 'L.id,N.protocoltype', array());
  857. $url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/api/project/patrol_asy';
  858. // foreach ($lampArr as $l) {
  859. doAsyncRequest($url,['clientid'=>$clientid,'lampid'=>$lampid,'interval'=>$interval,'username'=>$username,'client_key'=>$client_key,'token'=>$token,'version'=>$version,'role'=>$role]);
  860. // }
  861. exit();
  862. }
  863. public function patrol_asy() {
  864. $lampid = intval($this->input->post('lampid', true));
  865. $interval = $this->input->post('interval',true);
  866. if (empty($lampid)) exit(json_result('0007',$this->response['0007']));
  867. $lampData = $this->Lamp_model->get_one(['id'=>$lampid],'address');
  868. $address = substr($lampData['address'], 2);
  869. $sendData = '000A';
  870. $interval = base_convert($interval, 10, 16);
  871. $sendData .= substr('0000'.$interval, -4);
  872. $res = $this->send_cmd('16',$address,$senddata);
  873. if (empty($res)) {
  874. exit(json_result('0016',$this->response['0016']));
  875. }else{
  876. $res = unpack('H*', $res['msg'])[1];
  877. if (substr($res, 18,2) != '01') exit(json_result('0010',$this->response['0010']));
  878. }
  879. exit(json_result('0000', $this->response['0000'], array()));
  880. }
  881. // 固件升级
  882. public function upgrade(){
  883. $userid = $this->get_user_info('id');
  884. if ($userid != 263) exit(json_result('0011',$this->response['0011']));
  885. $projectid = intval($this->input->post('projectId',true));
  886. if (empty($projectid)) exit(json_result('0007',$this->response['0007']));
  887. $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';
  888. $lampList = $this->db->query($sql)->result_array();
  889. $url = trim($this->input->post('url',true));
  890. if (empty($url)) exit(json_result('0000',$this->response['0000']));
  891. // $url = 'http://station-iot.com/app/m26mqtt/opencpu_m26mqtt_1_0_13.bin';
  892. $len = base_convert(strlen($url), 10, 16).'';
  893. if (mb_strlen($len) > 2) {
  894. $len = mb_substr($len, -2);
  895. }elseif (mb_strlen($len) <= 1) {
  896. $len = '0'.$len;
  897. }
  898. foreach ($lampList as $lampData) {
  899. lampMqttCmd('/lamp/UpgradeIn/'.$lampData['address'],'/lamp/UpgradeOut/'.$lampData['address'],pack('H*','0001'.$len.'00'.$url),0);
  900. }
  901. $cmd = '{"cmd_type":"app_upgrade_cmd","relateid":'.$projectid.',"senddata":"'.$url.'","cmd_id":"0"}';
  902. $cmdret = send_cmd($cmd,1,0,2);
  903. exit(json_result('0000',$this->response['0000']));
  904. }
  905. }