Network.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  3. class Network extends Base_Controller {
  4. public function __construct() {
  5. parent::__construct();
  6. $this->load->model('Project_model');
  7. $this->load->model('Network_model');
  8. $this->load->model('Lamp_model');
  9. // $this->load->model('Zone_model');
  10. $this->load->model('Alarm_model');
  11. $this->load->model('Videomonitor_model');
  12. $this->load->model('Weathermonitor_model');
  13. }
  14. // 获取网络下拉列表
  15. public function get() {
  16. $userid = $this->get_user_info('id');
  17. $project_id = $this->input->post('project_id', true);
  18. if (empty($project_id)) {
  19. exit(json_result('0308',$this->response['0308'],array()));
  20. }
  21. $project_id = intval($project_id);
  22. $data = !empty($project_id) ? $this->Network_model->get_list_in('projectid', array($project_id), 'id, networkname as name,lampcount as lamp,status,gatewaytype') : array();
  23. $temp = array();
  24. foreach ($data as $key => $value) {
  25. if ($value['gatewaytype'] == 'direct') {
  26. continue;
  27. }
  28. $temp[] = $value;
  29. }
  30. exit(json_result('0000', $this->response['0000'], array('networks'=>$temp)));
  31. }
  32. // 网络路段筛选列表
  33. public function network_section_list() {
  34. $userid = $this->get_user_info('id');
  35. $project_id = $this->input->post('project_id', true);
  36. if (empty($project_id)) {
  37. exit(json_result('0308',$this->response['0308'],array()));
  38. }
  39. $project_id = intval($project_id);
  40. $data = !empty($project_id) ? $this->Network_model->get_list_in('projectid', array($project_id), 'id, networkname as name,gatewaytype') : array();
  41. $temp = array();
  42. foreach ($data as $key => $value) {
  43. $value['type'] = 0;
  44. if ($value['gatewaytype'] == 'direct') {
  45. continue;
  46. }
  47. $temp[] = $value;
  48. }
  49. if (empty($temp)) {
  50. $sectionList = $this->Lamp_model->get_list(array('projectid'=>$project_id,'devicetype'=>1), 'section,count(*) as sum',NULL, NULL, 'section ASC,id DESC', 'section');
  51. foreach ($sectionList as $s) {
  52. if ($s['section'] == '') continue;
  53. $temp[] = array(
  54. 'name' => $s['section'],
  55. 'id' => $s['section'],
  56. 'lamp' => $s['sum'],
  57. 'type' => 1,
  58. );
  59. }
  60. }
  61. exit(json_result('0000', $this->response['0000'], array('networks'=>$temp)));
  62. }
  63. // 删除网络(支持批量)
  64. public function del() {
  65. $role = $this->get_user_info('role');
  66. // if ($role == COMPANY_CUSTOMER) {
  67. // exit(json_result('0011', $this->response['0011'], array()));
  68. // }
  69. $network_ids = $this->input->post('network_ids', true);
  70. if(empty($network_ids)) {
  71. exit(json_result('0300', $this->response['0300'], array()));
  72. }
  73. $ids = explode(",", $network_ids);
  74. // 删除网络对应区域
  75. // $this->Zone_model->delZone($ids);
  76. // 修改项目中网络数
  77. $dataArr = $this->Network_model->getBatch($ids);
  78. foreach ($dataArr as $data) {
  79. $this->Project_model->minus_network_count($data);
  80. }
  81. // 删除网络下路灯的该井告警信息
  82. $this->Alarm_model->delBatchByCondition($ids, 'networkid');
  83. // 删除网络下的路灯
  84. $this->Lamp_model->delBatch($ids, 'networkid');
  85. // 删除网络下的监控
  86. // $this->Videomonitor_model->delData(array('networkid'=>$ids));
  87. $this->Weathermonitor_model->delData(array('networkid'=>$ids));
  88. foreach ($ids as $v) {
  89. $networkData = $this->Network_model->getData(array('id'=>$v),'projectid,networkid,networkname,protocoltype');
  90. $projectData = $this->Project_model->getData(array('id'=>$networkData['projectid']),'projectname');
  91. $this->add_operation_log('delete',"删除\"{$projectData['projectname']}\"项目下的\"{$networkData['networkname']}\"网络,网络编号\"{$networkData['networkid']}\"",0);
  92. $this->add_operation_log('delete',"Delete network.Network number:\"{$networkData['networkid']}\".Network name:\"{$networkData['networkname']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  93. $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$v.'}';
  94. send_cmd($cmdstr,0,0,$v['protocoltype']);
  95. }
  96. // 删除网络
  97. $this->Network_model->delBatch($ids);
  98. // foreach ($ids as $v) {
  99. // $cmdstr = '{"cmd_type":"delete_network_cmd","cmd_id":'.$v.'}';
  100. // send_cmd($cmdstr,0,0);
  101. // }
  102. exit(json_result('0000', $this->response['0000'], array()));
  103. }
  104. // 获取网络详情
  105. public function detail() {
  106. $network_id = $this->input->post('network_id', true);
  107. if(empty($network_id)){
  108. exit(json_result('0300', $this->response['0300'], array()));
  109. }
  110. $fields = 'networkid as network_no,
  111. networkname as network_name,
  112. status,
  113. section,
  114. simcard,
  115. regpack,
  116. longitude,
  117. latitude,
  118. lampcount,
  119. createtime,
  120. gatewaytype,
  121. faultcount,
  122. protocoltype,
  123. ip,
  124. port,
  125. hbpack,
  126. hbcycle,
  127. devicesn as sn,
  128. channel';
  129. $data = $this->Network_model->getData(array('id' => $network_id), $fields);
  130. $data['gatewaytype'] = $data['gatewaytype'] == 'direct' ? '1' : '0';
  131. $data['lampcount'] = $this->Lamp_model->get_total('','','*',array('networkid'=>$network_id));
  132. if(!$data){
  133. exit(json_result('0302', $this->response['0202'], $data));
  134. } else {
  135. exit(json_result('0000', $this->response['0000'], $data));
  136. }
  137. }
  138. // 添加/编辑网络
  139. public function save() {
  140. $role = $this->get_user_info('role');
  141. // if ($role == COMPANY_CUSTOMER) {
  142. // exit(json_result('0011', $this->response['0011'], array()));
  143. // }
  144. $where['id'] = $this->input->post('network_id',true);
  145. $data['projectid'] = $this->input->post('project_id',true);
  146. $data['networkid'] = $this->input->post('network_no',true);
  147. if (empty($data['networkid'])) exit(json_result('0305',$this->response['0305'],array()));
  148. if(!preg_match('/^[A-Za-z0-9]+$/', $data['networkid'])) exit(json_result('0309',$this->response['0309'],array()));
  149. $data['networkname'] = $this->input->post('network_name',true);
  150. $section = $this->input->post('section',true);
  151. $simcard = $this->input->post('simcard',true);
  152. $longitude = doubleval($this->input->post('longitude',true));
  153. $latitude = doubleval($this->input->post('latitude',true));
  154. $data['status'] = $this->input->post('status',true);
  155. // $gatewaytype = $this->input->post('gatewaytype',true);
  156. $data['ip'] = $this->input->post('ip',true);
  157. $data['regpack'] = $this->input->post('regpack',true);
  158. $data['hbpack'] = $this->input->post('hbpack',true);
  159. $devicesn = $this->input->post('sn',true);
  160. $data['lampcount'] = $this->input->post('lampcount',true);
  161. $data['port'] = $this->input->post('port',true);
  162. $protocoltype = $this->input->post('protocoltype',true);
  163. $data['hbcycle'] = $this->input->post('hbcycle',true);
  164. $data['channel'] = intval($this->input->post('channel',true));
  165. // $data['gatewaytype'] = empty($gatewaytype) ? '' : $gatewaytype;
  166. if(isset($longitude)) $data['longitude'] = $longitude;
  167. if(isset($latitude)) $data['latitude'] = $latitude;
  168. if(isset($simcard)) $data['simcard'] = $simcard;
  169. if(isset($section)) $data['section'] = $section;
  170. if(isset($devicesn)) $data['devicesn'] = $devicesn;
  171. if(isset($protocoltype)) $data['protocoltype'] = $protocoltype;
  172. // 网络设备类型
  173. $devicetype = $this->input->post('devicetype',true);
  174. if (!empty($devicetype)) $data['devicetype'] = $devicetype;
  175. $data['lampcount'] = empty($data['lampcount']) ? 0 : $data['lampcount'];
  176. if(empty($data['networkname'])) exit(json_result('0306',$this->response['0306'],array()));
  177. if(empty($data['projectid'])) exit(json_result('0307',$this->response['0307'],array()));
  178. if (!empty($data['protocoltype'])) {
  179. if (!is_numeric($data['protocoltype']) || $data['protocoltype'] < 0 || $data['protocoltype'] > 3) {
  180. exit(json_result('0304', $this->response['0304'], array()));
  181. }
  182. }
  183. $data = remove_null_params($data);
  184. if (empty($where['id'])) {
  185. if ($this->Network_model->getDataCount(array('networkid'=>$data['networkid'], 'projectid'=>$data['projectid'])) > 0) {
  186. exit(json_result('0301', $this->response['0301'], array()));
  187. }
  188. if ($this->Network_model->getDataCount(array('networkname'=>$data['networkname'], 'projectid'=>$data['projectid'])) > 0) {
  189. exit(json_result('0303', $this->response['0303'], array()));
  190. }
  191. // $data['id'] = $this->Zone_model->insert(array(
  192. // 'name' => $data['networkname'],
  193. // 'parent' => $data['projectid'] ,
  194. // 'level' => 4
  195. // ));
  196. $timezone = $this->Project_model->get_timezone_by_projectid($data['projectid']);
  197. $data['createtime'] = get_time_by_timezone($timezone['value']);
  198. $networkId = $this->Network_model->insert($data);
  199. $data['faultcount'] = 0;
  200. $this->Project_model->add_network_count($data);
  201. // $networkId = $data['id'];
  202. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  203. $this->add_operation_log('insert',"在\"{$projectData['projectname']}\"项目下添加\"{$data['networkname']}\"网络,网络编号\"{$data['networkid']}\"",0);
  204. $this->add_operation_log('insert',"Add network.Network number:\"{$data['networkid']}\".Network name:\"{$data['networkname']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  205. } else {
  206. if ($this->Network_model->getDataCount(array('networkid'=>$data['networkid'], 'projectid'=>$data['projectid']), $where['id']) > 0) {
  207. exit(json_result('0301', $this->response['0301'], array()));
  208. }
  209. if ($this->Network_model->getDataCount(array('networkname'=>$data['networkname'], 'projectid'=>$data['projectid']), $where['id']) > 0) {
  210. exit(json_result('0303', $this->response['0303'], array()));
  211. }
  212. // $this->Zone_model->update($where, array('name'=>$data['networkname'], 'parent'=>$data['projectid']));
  213. $old = $this->Network_model->getData($where);
  214. if ($old['projectid'] != $data['projectid']) {
  215. $new['projectid'] = $data['projectid'];
  216. $new['lampcount'] = $old['lampcount'];
  217. $new['faultcount'] = $old['faultcount'];
  218. $new['count'] = $old['monitorcount'];
  219. $old['count'] = $old['monitorcount'];
  220. $this->Project_model->add_network_count($new);
  221. $this->Project_model->minus_network_count($old);
  222. $this->Project_model->add_monitor_count($new);
  223. $this->Project_model->minus_monitor_count($old);
  224. // 修改路灯所属项目
  225. $this->Lamp_model->update(['networkid'=>$where['id']], ['projectid'=>$data['projectid']]);
  226. }
  227. $this->Network_model->update($where, $data);
  228. $networkId = $where['id'];
  229. $projectData = $this->Project_model->getData(array('id'=>$data['projectid']),'projectname');
  230. $this->add_operation_log('update',"修改\"{$projectData['projectname']}\"项目下的\"{$data['networkname']}\"网络,网络编号\"{$old['networkid']}\"",0);
  231. $this->add_operation_log('update',"Update network.Network number:\"{$old['networkid']}\".Network name:\"{$data['networkname']}\".Project name:\"{$projectData['projectname']}\"",0,1);
  232. }
  233. exit(json_result('0000', $this->response['0000'], array("id" => intval($networkId))));
  234. }
  235. }