Project.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. $companyid = $this->input->post('companyid',true);
  437. $role = $this->get_user_info('role');
  438. $userid = $this->get_user_info('id');
  439. $keyword = $this->input->post('keyword', true);
  440. $condition = array();
  441. if (!empty($keyword)) {
  442. $condition['keyword'] = $keyword;
  443. }
  444. if (empty($companyid)) {
  445. $companyid = $this->get_user_info('companyid');
  446. $data = $this->Project_model->getMultiData($condition, 'P.id, P.projectname as name, P.networkcount as network', $role, $companyid, $userid);
  447. }else{
  448. $zone = $this->get_user_info('zone');
  449. $data = $this->Project_model->get_list_by_company('id, projectname as name,company', $role, $companyid, $zone);
  450. }
  451. if (!empty($data)) {
  452. foreach ($data as $key => $value) {
  453. $data[$key]['tag'] = get_first_char($value['name']);
  454. }
  455. }
  456. exit(json_result('0000', $this->response['0000'], array('projects'=>$data)));
  457. }
  458. private function getLeftTimeByProject($project_id){
  459. $lampid_arr = $this->Lamp_model->getLampIds(array('project' => $project_id));
  460. $time_arr = $this->Patrol_model->get_list_in('id',$lampid_arr,'nextquerytime as time');
  461. if (empty($time_arr)) {
  462. return 0;
  463. }else{
  464. $querytime = max($time_arr)['time'];
  465. $nowtime = time();
  466. return $querytime>$nowtime ? intval(($querytime-$nowtime)/60) : 0;
  467. }
  468. }
  469. // 获取项目详情
  470. public function detail() {
  471. $project_id = $this->input->post('project_id', true);
  472. if(empty($project_id)){
  473. exit(json_result('0200', $this->response['0200'], array()));
  474. }
  475. $role = $this->get_user_info('role');
  476. $companyid = $this->get_user_info('companyid');
  477. $userid = $this->get_user_info('id');
  478. $fields = 'projectname as project_name,
  479. projectid as project_no,
  480. guestname as customer,
  481. company as company_id,
  482. province as province_id,
  483. zone as zone_id,
  484. address,
  485. createtime as create_time,
  486. timezone,
  487. patrolinterval,
  488. cityid,
  489. lampcount as install_num';
  490. $data = $this->Project_model->getData(array('id' => $project_id), $fields);
  491. $version = $this->session->userdata('version');
  492. if(!$data){
  493. exit(json_result('0202', $this->response['0202'], $data));
  494. } else {
  495. if (empty($data['cityid'])) {
  496. $data['countryId'] = 0;
  497. $data['provinceId'] = 0;
  498. $data['cityId'] = 0;
  499. $data['areaId'] = 0;
  500. $data['weatherList'] = [];
  501. $data['longitude'] = '0';
  502. $data['latitude'] = '0';
  503. $location_ch = '暂无位置信息,请在编辑项目页面选择位置信息';
  504. $location_en = 'There is no location information, please select location information on the edit project page.';
  505. }else{
  506. $res1 = $this->Global_location_model->getOne($data['cityid'],'level,pid,chinese_name,longitude,latitude,english_name');
  507. $data['longitude'] = $res1['longitude'];
  508. $data['latitude'] = $res1['latitude'];
  509. $res2 = $this->Global_location_model->getOne($res1['pid'],'pid,chinese_name,english_name');
  510. if ($res1['level'] == 4) {
  511. $data['areaId'] = $data['cityid'];
  512. $data['cityId'] = $res1['pid'];
  513. $data['provinceId'] = $res2['pid'];
  514. $res3 = $this->Global_location_model->getOne($res2['pid'],'pid,chinese_name,english_name');
  515. $data['countryId'] = $res3['pid'];
  516. $location_ch = $res3['chinese_name'].','.$res2['chinese_name'].','.$res1['chinese_name'];
  517. $location_en = $res1['english_name'].','.$res2['english_name'].','.$res3['english_name'];
  518. }elseif ($res1['level'] == 3) {
  519. $data['areaId'] = 0;
  520. $data['cityId'] = $data['cityid'];
  521. $data['provinceId'] = $res1['pid'];
  522. $data['countryId'] = $res2['pid'];
  523. $location_ch = $res2['chinese_name'].','.$res1['chinese_name'];
  524. $location_en = $res1['english_name'].','.$res2['english_name'];
  525. }elseif ($res1['level'] == 2) {
  526. $data['areaId'] = 0;
  527. $data['cityId'] = 0;
  528. $data['provinceId'] = $data['cityid'];
  529. $data['countryId'] = $res1['pid'];
  530. $location_ch = $res1['chinese_name'];
  531. $location_en = $res1['english_name'];
  532. }else{
  533. $data['areaId'] = 0;
  534. $data['cityId'] = 0;
  535. $data['provinceId'] = 0;
  536. $data['countryId'] = $data['cityid'];
  537. $location_ch = $res1['chinese_name'];
  538. $location_en = $res1['english_name'];
  539. }
  540. $project_weather = $this->db->query('select * from project_weather where projectId = '.$project_id)->row_array();
  541. if (empty($project_weather) || time() - strtotime($project_weather['updatetime']) > 3600) {
  542. $url = 'https://way.jd.com/he/freeweather?city='.$res1['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  543. $resData = request_post($url);
  544. $arr = json_decode($resData,true);
  545. if ($arr['code'] == 10000) {
  546. if ($arr['result']['HeWeather5'][0]['status'] == 'unknown location') {
  547. $url = 'https://free-api.heweather.net/s6/weather/forecast?location='.$res1['longitude'].','.$res1['latitude'].'&key=63dbc994630941fab269ad3c3af4d5d2';
  548. // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  549. $resData = request_post($url);
  550. $arr = json_decode($resData,true);
  551. // var_dump($url);
  552. if ($arr['HeWeather6'][0]['status'] == 'ok') {
  553. $now = $arr['HeWeather6'][0]['daily_forecast'][0];
  554. $daily_forecast = $arr['HeWeather6'][0]['daily_forecast'];
  555. $weatherList = array();
  556. $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']))];
  557. for ($i=1; $i < count($daily_forecast); $i++) {
  558. $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']))];
  559. }
  560. $data['weatherList'] = $weatherList;
  561. if (empty($project_weather)) {
  562. $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
  563. $this->db->query($sql);
  564. }else{
  565. $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
  566. $this->db->query($sql);
  567. }
  568. }else{
  569. $data['weatherList'] = [];
  570. }
  571. // $url = 'https://way.jd.com/he/freeweather?city='.$res2['chinese_name'].'&appkey=a44fc7f907194098c69bb8e90003bca6';
  572. // $resData = request_post($url);
  573. // $arr = json_decode($resData,true);
  574. }else{
  575. if ($arr['result']['HeWeather5'][0]['status'] == 'ok') {
  576. $now = $arr['result']['HeWeather5'][0]['now'];
  577. $daily_forecast = $arr['result']['HeWeather5'][0]['daily_forecast'];
  578. $weatherList = array();
  579. $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']))];
  580. for ($i=1; $i < count($daily_forecast); $i++) {
  581. $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']))];
  582. }
  583. $data['weatherList'] = $weatherList;
  584. if (empty($project_weather)) {
  585. $sql = "insert into project_weather (weatherInfo,projectId,updatetime) values('".json_encode($weatherList)."',".$project_id.",'".date('Y-m-d H:i:s',time())."')";
  586. $this->db->query($sql);
  587. }else{
  588. $sql = "update project_weather set weatherInfo = '".json_encode($weatherList)."',updatetime = '".date('Y-m-d H:i:s',time())."' where projectId = ".$project_id;
  589. $this->db->query($sql);
  590. }
  591. }else{
  592. $data['weatherList'] = [];
  593. }
  594. }
  595. }else{
  596. $data['weatherList'] = [];
  597. }
  598. }else{
  599. $data['weatherList'] = json_decode($project_weather['weatherInfo'],true);
  600. // var_dump($data['weatherList']);die;
  601. }
  602. }
  603. $data['company'] = $this->Company_model->get_data_by_id($data['company_id'], 'name');
  604. if (empty($version)) {
  605. $data['location'] = $location_ch;
  606. $res = $this->Project_model->get_timezone_info(array('id'=>$data['timezone']),'name');
  607. foreach ($data['weatherList'] as $key => $value) {
  608. $data['weatherList'][$key]['weatherType'] = weather_translate_cn($data['weatherList'][$key]['code']);
  609. $data['weatherList'][$key]['dateStr'] = date_translate_cn($data['weatherList'][$key]['dateStr']);
  610. }
  611. }else{
  612. $data['location'] = $location_en;
  613. foreach ($data['weatherList'] as $key => $value) {
  614. $data['weatherList'][$key]['weatherType'] = weather_translate_en($data['weatherList'][$key]['code']);
  615. }
  616. $res = $this->Project_model->get_timezone_info(array('id'=>$data['timezone']),'enname as name');
  617. }
  618. $data['timezone_name'] = $res['name'];
  619. $data['patrol_time'] = strval($this->getLeftTimeByProject($project_id));
  620. // 获取项目下的网络列表
  621. $data['network_list'] = $this->Network_model->get_list_by_filter(array('projectid'=>$project_id));
  622. $temp = array();
  623. foreach ($data['network_list'] as $key => $value) {
  624. if (isset($value['gatewaytype']) && $value['gatewaytype'] == 'direct') {
  625. continue;
  626. }
  627. $temp[] = $value;
  628. }
  629. $data['network_list'] = $temp;
  630. exit(json_result('0000', $this->response['0000'], $data));
  631. }
  632. }
  633. // 添加编辑项目
  634. public function save() {
  635. $role = $this->get_user_info('role');
  636. $where['id'] = $this->input->post('project_id',true);
  637. $data['projectname'] = trim($this->input->post('project_name',true));
  638. $data['cityid'] = $this->input->post('pid',true);
  639. $address = $this->input->post('address',true);
  640. $patrolinterval = $this->input->post('patrolinterval',true);
  641. if(isset($address)) $data['address'] = $address;
  642. if(isset($patrolinterval)) $data['patrolinterval'] = $patrolinterval;
  643. if(empty($data['projectname'])) exit(json_result('0204',$this->response['0204'],array()));
  644. if (empty($where['id'])) { // 添加项目
  645. $data['projectid'] = $this->input->post('project_no',true);
  646. if(empty($data['projectid'])) exit(json_result('0205',$this->response['0205'],array()));
  647. if(mb_strlen($data['projectid']) > 20) exit(json_result('0210',$this->response['0210'],array()));
  648. if(!preg_match('/^[A-Za-z0-9]+$/', $data['projectid'])) exit(json_result('0212',$this->response['0212'],array()));
  649. $data['userId'] = $this->get_user_info('id');
  650. $data['company'] = $this->get_user_info('company');
  651. $data['createtime'] = date('Y-m-d H:i:s',time() - 8*3600);
  652. $projectId = $this->Project_model->add($data);
  653. // $projectId = $data['id'];
  654. $this->add_operation_log('insert',"添加\"{$data['projectname']}\"项目,项目编号\"{$data['projectid']}\"",0);
  655. $this->add_operation_log('insert',"Add project.Project name:\"{$data['projectname']}\".Project number:\"{$data['projectid']}\"",0,1);
  656. } else { // 编辑项目
  657. if ($this->Project_model->getDataCount(array('projectname'=>$data['projectname'], 'company'=>$data['company']), $where['id']) > 0) {
  658. exit(json_result('0203', $this->response['0203'], array()));
  659. }
  660. $ret = $this->Project_model->get_one($where);
  661. if ($ret['cityid'] != $data['cityid']) {
  662. $sql = "delete from project_weather where projectId = ".$where['id'];
  663. $this->db->query($sql);
  664. }
  665. $this->Project_model->update($data,$where);
  666. $projectId = $where['id'];
  667. // 修改项目下巡检信息
  668. // if (isset($patrolinterval)) {
  669. // if (empty($patrolinterval)) {
  670. // $patrolData['patroltype'] = 0;
  671. // }else{
  672. // $patrolData['patroltype'] = 1;
  673. // $patrolData['patrolinterval'] = intval($patrolinterval);
  674. // $patrolData['updatetime'] = date("Y-m-d H:i:s", time());
  675. // $patrolData['nextquerytime'] = date("Y-m-d H:i:s", time());
  676. // }
  677. // $idArr = $this->Lamp_model->get_list_by_project($projectId,'L.id,N.protocoltype,L.address');
  678. // foreach ($idArr as $v) {
  679. // $res = $this->Patrol_model->getOne($v['id']);
  680. // if(empty($res)){
  681. // $patrolData['id'] = $v['id'];
  682. // $this->Patrol_model->insert($patrolData);
  683. // unset($patrolData['id']);
  684. // } else {
  685. // $this->Patrol_model->update(array('id'=>$v['id']), $patrolData);
  686. // }
  687. // if ($v['protocoltype'] == 4) {
  688. // $sendData = '000101A501';;
  689. // $sendData .= bitSubStr(base_convert(intval($patrolData['patrolinterval']/5), 10, 16));
  690. // lampMqttCmd('/WE/WriteIn/'.$v['address'],'/WE/WriteOut/'.$v['address'],pack('H*', $sendData),0);
  691. // continue;
  692. // }
  693. // if (isset($v['protocoltype']) && $v['protocoltype'] != 0) {
  694. // $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$v['id'].',"broadcast":0}';
  695. // $cmdret = send_cmd($cmd, 1,0,$v['protocoltype']);
  696. // }
  697. // }
  698. // }
  699. $this->add_operation_log('update',"修改\"{$data['projectname']}\"项目,项目编号\"{$ret['projectid']}\"",0);
  700. $this->add_operation_log('update',"Update project.Project name:\"{$data['projectname']}\".Project number:\"{$ret['projectid']}\"",0,1);
  701. }
  702. exit(json_result('0000', $this->response['0000'], array("id" => intval($projectId))));
  703. }
  704. // 删除项目
  705. public function del() {
  706. $role = $this->get_user_info('role');
  707. $project_id = $this->input->post('project_id', true);
  708. if(empty($project_id)) exit(json_result('0007', $this->response['0007'], array()));
  709. $lampListInfo = $this->Lamp_model->get_one(['projectid'=>$project_id],'id');
  710. if (!empty($lampListInfo)) exit(json_result('0214',$this->response['0214']));
  711. $net_arr = $this->Network_model->get_one(['projectid'=>$project_id],'id');
  712. if (!empty($net_arr)) exit(json_result('0215',$this->response['0215']));
  713. // 删除项目下的监控
  714. $this->Videomonitor_model->delete(array('projectid'=>$project_id));
  715. $this->Weathermonitor_model->delete(array('projectid'=>$project_id));
  716. $projectData = $this->Project_model->getData(array('id'=>$project_id),'projectname,projectid');
  717. $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目,项目编号\"{$projectData['projectid']}\"",0);
  718. $this->add_operation_log('delete',"Delete project.Project name:\"{$projectData['projectname']}\".Project number:\"{$projectData['projectid']}\"",0,1);
  719. // 删除项目
  720. $this->Project_model->delete(['id'=>$project_id]);
  721. exit(json_result('0000', $this->response['0000'], array()));
  722. }
  723. private function do_setting($id, $data){
  724. $ret = $this->Patrol_model->getOne($id);
  725. if(empty($ret)){
  726. $data['id'] = $id;
  727. $this->Patrol_model->insert($data);
  728. } else {
  729. $this->Patrol_model->update(array('id'=>$id), $data);
  730. }
  731. $res = $this->Lamp_model->getOne($id,'N.protocoltype,L.address,L.protocoltype as lampprotocoltype');
  732. if ($res['protocoltype'] == 4) {
  733. $sendData = '000101A501';;
  734. $sendData .= bitSubStr(base_convert(intval($data['patrolinterval']/5), 10, 16));
  735. lampMqttCmd('/WE/WriteIn/'.$res['address'],'/WE/WriteOut/'.$res['address'],pack('H*', $sendData),0);
  736. continue;
  737. }
  738. if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
  739. $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$id.',"broadcast":0}';
  740. $cmdret = send_cmd($cmd, 1,0,$res['protocoltype']);
  741. }
  742. }
  743. // 巡检设置
  744. public function patrol() {
  745. $role = $this->get_user_info('role');
  746. // if ($role == COMPANY_CUSTOMER) {
  747. // exit(json_result('0011', $this->response['0011'], array()));
  748. // }
  749. $type = $this->input->post('type', true);
  750. $value = $this->input->post('value', true);
  751. $switch = $this->input->post('switch',true);
  752. $interval = $this->input->post('interval',true);
  753. if(!in_array($type, array('project','network','lamp')) || empty($interval) || empty($value)) {
  754. exit(json_result('0005', $this->response['0005'], array()));
  755. }
  756. $nowtime = time();
  757. $data['patroltype'] = intval($switch);
  758. $data['patrolinterval'] = intval($interval);
  759. $data['updatetime'] = date("Y-m-d H:i:s", $nowtime);
  760. $data['nextquerytime'] = date("Y-m-d H:i:s", time());
  761. if ($type == 'project') {
  762. $idArr = $this->Lamp_model->getLampIds(array('project'=>$value));
  763. }elseif ($type == 'network') {
  764. $idArr = $this->Lamp_model->getLampIds(array('network'=>$value));
  765. }else{
  766. $id = $value;
  767. $ret = $this->Patrol_model->getOne($id);
  768. if(empty($ret)){
  769. $data['id'] = $id;
  770. $this->Patrol_model->insert($data);
  771. } else {
  772. $this->Patrol_model->update(array('id'=>$id), $data);
  773. }
  774. $res = $this->Lamp_model->getOne($id,'N.protocoltype,L.address,L.protocoltype as lampprotocoltype');
  775. if ($res['protocoltype'] == 4) {
  776. $sendData = '000101A501';;
  777. $sendData .= bitSubStr(base_convert(intval($interval/5), 10, 16));
  778. lampMqttCmd('/WE/WriteIn/'.$res['address'],'/WE/WriteOut/'.$res['address'],pack('H*', $sendData),0);
  779. }else{
  780. if (isset($res['protocoltype']) && $res['protocoltype'] != 0) {
  781. $cmd = '{"cmd_type":"set_patrol_cmd","cmd_id":'.$id.',"broadcast":0}';
  782. $cmdret = send_cmd($cmd,1,0,$res['protocoltype']);
  783. // if (empty($cmdret)) exit(json_result('0016',$this->response['0016'], array('number'=>$lampData['number'])));
  784. // if($cmdret === false){
  785. // exit(json_result('0008', $this->response['0008']));
  786. // } else {
  787. // $res = json_decode($cmdret, true);
  788. // }
  789. // if (isset($res['result']) && $res['result'] == true) {
  790. // exit(json_result('0000', $this->response['0000']));
  791. // } else {
  792. // $version = $this->session->userdata('version');
  793. // if (empty($version)) {
  794. // $res['msg'] = empty($res['msg']) ? '未知错误' : transfer_error_tips($res['msg']);
  795. // exit(json_result('0010', $res['msg']));
  796. // }else{
  797. // $res['msg'] = empty($res['msg']) ? 'Unknown error' : $res['msg'];
  798. // exit(json_result('other', $res['msg']));
  799. // }
  800. // }
  801. }
  802. }
  803. }
  804. if (!empty($idArr)) {
  805. foreach ($idArr as $id) {
  806. $this->do_setting($id, $data);
  807. }
  808. }
  809. exit(json_result('0000', $this->response['0000'], array()));
  810. }
  811. // 固件升级
  812. public function upgrade(){
  813. $userid = $this->get_user_info('id');
  814. if ($userid != 263) exit(json_result('0011',$this->response['0011']));
  815. $projectid = intval($this->input->post('projectId',true));
  816. if (empty($projectid)) exit(json_result('0007',$this->response['0007']));
  817. $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';
  818. $lampList = $this->db->query($sql)->result_array();
  819. $url = trim($this->input->post('url',true));
  820. if (empty($url)) exit(json_result('0000',$this->response['0000']));
  821. // $url = 'http://station-iot.com/app/m26mqtt/opencpu_m26mqtt_1_0_13.bin';
  822. $len = base_convert(strlen($url), 10, 16).'';
  823. if (mb_strlen($len) > 2) {
  824. $len = mb_substr($len, -2);
  825. }elseif (mb_strlen($len) <= 1) {
  826. $len = '0'.$len;
  827. }
  828. foreach ($lampList as $lampData) {
  829. lampMqttCmd('/WE/UpgradeIn/'.$lampData['address'],'/WE/UpgradeOut/'.$lampData['address'],pack('H*','0001'.$len.'00'.$url),0);
  830. }
  831. $cmd = '{"cmd_type":"app_upgrade_cmd","relateid":'.$projectid.',"senddata":"'.$url.'","cmd_id":"0"}';
  832. $cmdret = send_cmd($cmd,1,0,2);
  833. exit(json_result('0000',$this->response['0000']));
  834. }
  835. }