Network.php 13 KB

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