Project.php 44 KB

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