Network.php 13 KB

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