From f7debb464a2d49b6e66b014cf2beef2a9c4bf98d Mon Sep 17 00:00:00 2001
From: Van333 <van666666@foxmail.com>
Date: 星期三, 26 十月 2022 15:41:46 +0800
Subject: [PATCH] 参数调整
---
src/main/java/api/service/PoleService.java | 25 +++
src/main/java/api/utils/CommonHeader.java | 31 +++
src/main/java/api/utils/HttpUtil.java | 217 +++++++++++++++++++++++++++
src/main/java/api/controller/PoleController.java | 141 +++++++++++++++++
src/main/java/api/bean/PoleLightBatchEntity.java | 31 +++
5 files changed, 445 insertions(+), 0 deletions(-)
diff --git a/src/main/java/api/bean/PoleLightBatchEntity.java b/src/main/java/api/bean/PoleLightBatchEntity.java
new file mode 100644
index 0000000..7e6f04a
--- /dev/null
+++ b/src/main/java/api/bean/PoleLightBatchEntity.java
@@ -0,0 +1,31 @@
+package api.bean;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @author van
+ * @version 1.0
+ * msg锛�
+ * @date 2022/6/6 14:37
+ */
+@Data
+public class PoleLightBatchEntity implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private Long[] streetlightIds;
+
+ private Integer operate;
+
+ private Long programId;
+
+ private Integer scheduleId;
+
+ private Integer lightLevel;
+
+ private Integer lightNumber;
+
+ private Long[] adIds;
+}
diff --git a/src/main/java/api/controller/PoleController.java b/src/main/java/api/controller/PoleController.java
new file mode 100644
index 0000000..e3e6bcc
--- /dev/null
+++ b/src/main/java/api/controller/PoleController.java
@@ -0,0 +1,141 @@
+package api.controller;
+
+import api.bean.PoleLightBatchEntity;
+import api.bean.ReqParams;
+import api.result.Msg;
+import api.service.AccessService;
+import api.service.PoleService;
+import api.utils.HttpUtil;
+import com.alibaba.fastjson.JSON;
+import com.google.common.util.concurrent.RateLimiter;
+import com.google.gson.Gson;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author van
+ * @version 1.0
+ * msg锛氬崟鐏帶鍒跺眰
+ * @date 2021/11/2 15:04
+ */
+@RestController
+@RequestMapping("/pole")
+public class PoleController {
+
+ @Autowired
+ private AccessService accessService;
+
+ RateLimiter rateLimiter = RateLimiter.create(10.0);
+
+ @RequestMapping(value = "polestreetlight/listWithNotStatus",method = RequestMethod.POST)
+ public Msg listWithNotStatus(@RequestBody ReqParams reqParams){
+ rateLimiter.acquire(1);
+ if (reqParams.getLimit()>50){
+ return Msg.error("limit exception!!!");
+ }
+ Long userId = accessService.getUserId(reqParams.getAccessToken());
+
+
+ if(userId == null){
+ return Msg.error("accessToken exception!!!");
+ }
+
+ reqParams.setApiKey(reqParams.getApiKey());
+ reqParams.setUserId(userId);
+
+ String result = HttpUtil.Post(
+ "http://127.0.0.1:8888/machine-fast/serv/api/pole/polestreetlight/listWithNotStatus",
+ new Gson().toJson(reqParams)
+ );
+
+ Map msg = JSON.parseObject(result, HashMap.class);
+
+ if (msg.get("msg").equals("success")){
+
+ return Msg.ok().put("data",msg.get("data"));
+ }
+ else {
+ return Msg.error(msg.get("msg").toString());
+ }
+
+
+ }
+
+ @RequestMapping(value = "polestreetlight/getStatusById",method = RequestMethod.POST)
+ public Msg getStatusById(@RequestBody ReqParams reqParams){
+ rateLimiter.acquire(1);
+ Long userId = accessService.getUserId(reqParams.getAccessToken());
+
+
+ if(userId == null){
+ return Msg.error("accessToken exception!!!");
+ }
+
+ reqParams.setApiKey(reqParams.getApiKey());
+ reqParams.setUserId(userId);
+
+ String result = HttpUtil.Post(
+ "http://127.0.0.1:8888/machine-fast/serv/api/pole/polestreetlight/getStatusById",
+ new Gson().toJson(reqParams)
+ );
+
+ Map msg = JSON.parseObject(result, HashMap.class);
+
+ if (msg.get("msg").equals("success")){
+
+ return Msg.ok().put("data",msg.get("data"));
+ }
+ else {
+ return Msg.error(msg.get("msg").toString());
+ }
+
+
+ }
+ @RequestMapping(value = "polestreetlight/lightBatch",method = RequestMethod.POST)
+ public Msg list(@RequestBody ReqParams reqParams){
+ rateLimiter.acquire(1);
+
+ Long userId = accessService.getUserId(reqParams.getAccessToken());
+
+ if(userId == null){
+ return Msg.error("accessToken exception!!!");
+ }
+
+ reqParams.setApiKey(reqParams.getApiKey());
+ reqParams.setUserId(userId);
+ PoleLightBatchEntity poleLightBatch = new PoleLightBatchEntity();
+ poleLightBatch.setAdIds(reqParams.getPoleLightBatch().getStreetlightIds());
+ poleLightBatch.setLightNumber(reqParams.getPoleLightBatch().getLightNumber());
+ poleLightBatch.setLightLevel(reqParams.getPoleLightBatch().getLightLevel());
+
+ reqParams.setPoleLightBatch(poleLightBatch);
+
+
+ String result = HttpUtil.Post(
+ "http://127.0.0.1:8888/machine-fast/serv/api/pole/polestreetlight/lightBatch",
+ new Gson().toJson(reqParams)
+ );
+
+ Map msg = JSON.parseObject(result, HashMap.class);
+
+
+ assert msg != null;
+ if (msg.get("msg").equals("success")){
+
+ return Msg.ok().put("data",msg.get("data"));
+ }
+ else {
+ return Msg.error(msg.get("msg").toString());
+ }
+
+
+ }
+
+
+}
diff --git a/src/main/java/api/service/PoleService.java b/src/main/java/api/service/PoleService.java
new file mode 100644
index 0000000..932cc1b
--- /dev/null
+++ b/src/main/java/api/service/PoleService.java
@@ -0,0 +1,25 @@
+package api.service;
+
+import api.bean.LampEntity;
+import api.bean.ReqParams;
+import api.bean.WeatherEntity;
+import api.dao.LampDao;
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * @author van
+ * @version 1.0
+ * msg锛氬崟鐏帶鍒�
+ * @date 2021/11/2 10:24
+ */
+@Service("lampService")
+public class PoleService {
+ @Autowired
+ private AccessService accessService;
+
+}
diff --git a/src/main/java/api/utils/CommonHeader.java b/src/main/java/api/utils/CommonHeader.java
new file mode 100644
index 0000000..8ce6163
--- /dev/null
+++ b/src/main/java/api/utils/CommonHeader.java
@@ -0,0 +1,31 @@
+package api.utils;
+
+import lombok.Data;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+
+/**
+ * @author van
+ * @version 1.0
+ * msg锛�
+ * @date 2022/6/6 15:01
+ */
+@Data
+public class CommonHeader extends HashMap<String, String> {
+
+ private static Logger logger = LoggerFactory.getLogger(CommonHeader.class);
+
+ public static final String FORM = "form";
+ public static final String JSON = "json";
+
+ public CommonHeader(String type,boolean hasToken){
+ if(type.equals(FORM)) {
+ super.put("Content-Type","application/x-www-form-urlencoded");
+ } else if(type.equals(JSON)) {
+ super.put("Content-Type","application/json");
+ }
+ }
+
+}
diff --git a/src/main/java/api/utils/HttpUtil.java b/src/main/java/api/utils/HttpUtil.java
new file mode 100644
index 0000000..d9fa51c
--- /dev/null
+++ b/src/main/java/api/utils/HttpUtil.java
@@ -0,0 +1,217 @@
+package api.utils;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.methods.*;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.client.utils.URIBuilder;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.params.CoreConnectionPNames;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLEncoder;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author van
+ * @date 2022/6/6 11:59
+ * @version 1.0
+ * msg锛�
+ */public class HttpUtil {
+
+
+ private static final String UTF8 = "utf-8";
+
+
+ public static String get(String url){
+ CloseableHttpClient client = HttpClients.createDefault();
+ HttpGet get = new HttpGet(url);
+ String result = null;
+ try {
+ HttpResponse httpResponse = client.execute(get);
+ InputStream inStream = httpResponse.getEntity().getContent();
+
+ result = streamToString(inStream,UTF8);
+
+ inStream.close();
+ } catch (Exception e) {
+ return null;
+ }
+ return result;
+ }
+
+ public static String GetWithHeader (String url, List<NameValuePair> urlParameters, Map<String, String> header) throws URISyntaxException {
+
+
+ URI finalURI = new URIBuilder(url).setParameters(urlParameters).build();
+ CloseableHttpClient client = HttpClients.createDefault();
+ HttpGet get = new HttpGet(finalURI);
+ for(String key: header.keySet()) {
+ get.setHeader(key, header.get(key));
+ }
+
+ String result = null;
+ try {
+ HttpResponse httpResponse = client.execute(get);
+ InputStream inStream = httpResponse.getEntity().getContent();
+
+ result = streamToString(inStream, UTF8);
+
+ inStream.close();
+ } catch (Exception e) {
+ return null;
+ }
+ return result;
+ }
+
+
+
+
+ public static String PostWithHeader (String URL, String json, Map<String, String> header) {
+ CloseableHttpClient client = HttpClients.createDefault();
+ HttpPost post = new HttpPost(URL);
+ for(String key: header.keySet()) {
+ post.setHeader(key, header.get(key));
+ }
+
+ String result = null;
+ try {
+ StringEntity s = new StringEntity(json, UTF8);
+ s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
+ "application/json"));
+ post.setEntity(s);
+ HttpResponse httpResponse = client.execute(post);
+ InputStream inStream = httpResponse.getEntity().getContent();
+
+ result = streamToString(inStream,UTF8);
+
+ inStream.close();
+ } catch (Exception e) {
+ return null;
+ }
+ return result;
+ }
+ public static String Post (String URL, String json) {
+ CloseableHttpClient client = HttpClients.createDefault();
+ HttpPost post = new HttpPost(URL);
+
+ String result = null;
+ try {
+ StringEntity s = new StringEntity(json, UTF8);
+ s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
+ "application/json"));
+ s.setContentType("application/json");
+ post.setEntity(s);
+ HttpResponse httpResponse = client.execute(post);
+ InputStream inStream = httpResponse.getEntity().getContent();
+
+ result = streamToString(inStream,UTF8);
+
+ inStream.close();
+ } catch (Exception e) {
+ return null;
+ }
+ return result;
+ }
+
+
+
+ public static String streamToString(InputStream in, String encoding){
+ // 灏嗘祦杞崲涓哄瓧绗︿覆
+ try {
+ StringBuffer sb = new StringBuffer();
+ byte[] b = new byte[1024];
+ for (int n; (n = in.read(b)) != -1;) {
+ sb.append(new String(b, 0, n, encoding));
+ }
+ return sb.toString();
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new RuntimeException("鎻愬彇 requestBody 寮傚父", e);
+ }
+ }
+
+ /**
+ * <pre>
+ * 鏂规硶浣撹鏄庯細鍚戣繙绋嬫帴鍙e彂璧疯姹傦紝杩斿洖瀛楃涓茬被鍨嬬粨鏋�
+ * @param url 鎺ュ彛鍦板潃
+ * @param requestMethod 璇锋眰绫诲瀷
+ * @param params 浼犻�掑弬鏁�
+ * @return String 杩斿洖缁撴灉
+ * </pre>
+ */
+ public static String httpRequestToString(String url, String requestMethod, Map<String, String> params){
+ //鎺ュ彛杩斿洖缁撴灉
+ String methodResult = null;
+ try {
+ String parameters = "";
+ boolean hasParams = false;
+ //灏嗗弬鏁伴泦鍚堟嫾鎺ユ垚鐗瑰畾鏍煎紡锛屽name=zhangsan&age=24
+ for(String key : params.keySet()){
+ String value = URLEncoder.encode(params.get(key), "UTF-8");
+ parameters += key +"="+ value +"&";
+ hasParams = true;
+ }
+ if(hasParams){
+ parameters = parameters.substring(0, parameters.length()-1);
+ }
+ //鏄惁涓篏ET鏂瑰紡璇锋眰
+ boolean isGet = "get".equalsIgnoreCase(requestMethod);
+ boolean isPost = "post".equalsIgnoreCase(requestMethod);
+ boolean isPut = "put".equalsIgnoreCase(requestMethod);
+ boolean isDelete = "delete".equalsIgnoreCase(requestMethod);
+
+ //鍒涘缓HttpClient杩炴帴瀵硅薄
+ DefaultHttpClient client = new DefaultHttpClient();
+ HttpRequestBase method = null;
+ if(isGet){
+ url += "?" + parameters;
+ method = new HttpGet(url);
+ }else if(isPost){
+ method = new HttpPost(url);
+ HttpPost postMethod = (HttpPost) method;
+ StringEntity entity = new StringEntity(parameters);
+ postMethod.setEntity(entity);
+ }else if(isPut){
+ method = new HttpPut(url);
+ HttpPut putMethod = (HttpPut) method;
+ StringEntity entity = new StringEntity(parameters);
+ putMethod.setEntity(entity);
+ }else if(isDelete){
+ url += "?" + parameters;
+ method = new HttpDelete(url);
+ }
+ method.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
+ //璁剧疆鍙傛暟鍐呭绫诲瀷
+ method.addHeader("Content-Type","application/x-www-form-urlencoded");
+ //httpClient鏈湴涓婁笅鏂�
+ HttpClientContext context = null;
+ context = HttpClientContext.create();
+ //璁块棶鎺ュ彛锛岃繑鍥炵姸鎬佺爜
+ HttpResponse response = client.execute(method, context);
+ //杩斿洖鐘舵�佺爜200锛屽垯璁块棶鎺ュ彛鎴愬姛
+ if(response.getStatusLine().getStatusCode()==200){
+ methodResult = EntityUtils.toString(response.getEntity());
+ }
+ client.close();
+ }catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }catch (IOException e) {
+ e.printStackTrace();
+ }
+ return methodResult;
+ }
+
+
+}
--
Gitblit v1.9.3