| | |
| | | import org.springframework.http.client.ClientHttpRequestFactory; |
| | | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.text.ParseException; |
| | |
| | | * @return |
| | | */ |
| | | public List<String> createFence(String id, String points){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(new Date()); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 35); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String format = simpleDateFormat.format(calendar.getTime()); |
| | | String ak = key; |
| | | List<String> result = new ArrayList<>(); |
| | | |
| | | |
| | | //开始调用高德接口创建围栏 |
| | | // points格式:lng,lat;lng,lat;... 或 lng,lat;radius(圆形) |
| | | String[] s = points.split("_"); |
| | | List<String> resoult = new ArrayList<>(); |
| | | for(int i = 0; i < s.length; i++){ |
| | | String[] split = s[i].split(";"); |
| | | JSONObject json = new JSONObject(); |
| | | json.put("name", "location_" + id); |
| | | if(split.length == 2){ |
| | | json.put("center", split[0]); |
| | | json.put("radius", split[1]); |
| | | }else{ |
| | | json.put("points", s[i]); |
| | | for (String item : s) { |
| | | String url; |
| | | MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
| | | params.add("ak", ak); |
| | | params.add("service_id", "241187"); |
| | | params.add("name", "location_" + id); |
| | | |
| | | String[] split = item.split(";"); |
| | | if (split.length == 2 && split[1].matches("\\d+")) { |
| | | // 圆形围栏 |
| | | params.add("center", split[0]); |
| | | params.add("radius", split[1]); |
| | | url = "http://yingyan.baidu.com/api/v3/fence/createcircle"; |
| | | } else { |
| | | // 多边形围栏 |
| | | params.add("vertexes", item); |
| | | url = "https://yingyan.baidu.com/api/v3/fence/createpolygonfence"; |
| | | } |
| | | json.put("enable", "true"); |
| | | json.put("valid_time", format); |
| | | json.put("repeat", "Mon,Tues,Wed,Thur,Fri,Sat,Sun"); |
| | | json.put("alert_condition", "enter;leave"); |
| | | json.put("desc", id); |
| | | |
| | | params.add("coord_type", "bd09ll"); // 百度经纬度 |
| | | params.add("denoise", "10"); // 去噪 |
| | | params.add("alarm_condition", "enter;leave"); // 进出报警 |
| | | |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); |
| | | headers.setContentType(type); |
| | | headers.add("Accept", MediaType.APPLICATION_JSON.toString()); |
| | | HttpEntity<String> formEntity = new HttpEntity<>(json.toString(), headers); |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers); |
| | | |
| | | String url = "https://restapi.amap.com/v4/geofence/meta?key=" + key; |
| | | String js = restTemplate.postForObject(url, formEntity, String.class); |
| | | JSONObject jsonObject = JSON.parseObject(js); |
| | | JSONObject data = jsonObject.getJSONObject("data"); |
| | | String status = data.getString("status"); |
| | | |
| | | gdInterfaceService.saveData("https://restapi.amap.com/v4/geofence/meta", "创建电子围栏"); |
| | | |
| | | if(status.equals("0")){//创建成功 |
| | | resoult.add(data.getString("gid")); |
| | | String response = restTemplate.postForObject(url, request, String.class); |
| | | JSONObject json = JSON.parseObject(response); |
| | | if (json.getIntValue("status") == 0) { |
| | | result.add(json.getString("fence_id")); |
| | | }else{ |
| | | result.add("-1"); |
| | | } |
| | | } |
| | | return resoult; |
| | | return result; |
| | | } |
| | | |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public boolean deleteElectricFenc(String gid){ |
| | | restTemplate.delete("https://restapi.amap.com/v4/geofence/meta?key={1}&gid={2}", key, gid); |
| | | System.out.println("删除:" ); |
| | | MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
| | | params.add("ak", key); |
| | | params.add("service_id", "241187"); |
| | | params.add("fence_ids", gid); |
| | | System.out.println("删除:"); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers); |
| | | |
| | | gdInterfaceService.saveData("https://restapi.amap.com/v4/geofence/meta", "删除电子围栏"); |
| | | String response = restTemplate.postForObject("https://yingyan.baidu.com/api/v3/fence/delete", request, String.class); |
| | | System.err.println(response); |
| | | gdInterfaceService.saveData("https://yingyan.baidu.com/api/v3/fence/delete", "删除电子围栏"); |
| | | |
| | | return true; |
| | | } |