Project.php 50 KB

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