|
@@ -0,0 +1,199 @@
|
|
|
+package io.github.pnoker.center.manager.config;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import org.apache.axis.client.Call;
|
|
|
+import org.apache.axis.encoding.XMLType;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.remoting.RemoteAccessException;
|
|
|
+import org.springframework.retry.annotation.Backoff;
|
|
|
+import org.springframework.retry.annotation.Retryable;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import util.ParseMD5;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import javax.xml.namespace.QName;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName XuchangWaterConfig
|
|
|
+ * @Description: TODO 许昌800只户表数据查询
|
|
|
+ * @Author LX
|
|
|
+ * @Date 2024/1/30
|
|
|
+ * @Version V1.0
|
|
|
+ **/
|
|
|
+@Configuration
|
|
|
+public class XuchangWaterConfig {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ParseMD5.class);
|
|
|
+
|
|
|
+ //TODO 初始化方法
|
|
|
+ //TODO 远程WebService查询智能水表接口
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ String user = "zzsfy";
|
|
|
+ String pwdMd5 = "D41D8CD98F00B204E980";
|
|
|
+ creatPostAndTransData(user,pwdMd5,1,"",1,20);
|
|
|
+ getFreezeDayDosage(user,pwdMd5,"2024-01-01","2024-01-30","",1,20);
|
|
|
+ }
|
|
|
+
|
|
|
+ private volatile static String url = "http://42.227.69.38:8004/WebServiceOfNBIoT.asmx";
|
|
|
+
|
|
|
+ //TODO 获取表当前数据
|
|
|
+ @Retryable(value = {RemoteAccessException.class}, maxAttempts = 3, backoff = @Backoff(delay = 200L, multiplier = 1))
|
|
|
+ public static JSONArray creatPostAndTransData(String A,String B,int C,String D,int page,int pageSize) {
|
|
|
+ //todo 可改为从配置文件获取,或者每次都从参数中传过来,因为我的项目中url不变,所以写死在这里
|
|
|
+ String line = "";
|
|
|
+ // 用来存储接收到的返回值,铁汁们可以考虑下为什么用StringBuffer而不是普通的String ^.^
|
|
|
+ StringBuffer resultStringBuffer = new StringBuffer();
|
|
|
+ OutputStreamWriter out = null;
|
|
|
+ try {
|
|
|
+ // 根据url连接接口
|
|
|
+ URL realUrl = new URL(url);
|
|
|
+ URLConnection urlConnection = realUrl.openConnection();
|
|
|
+ // 根据需要,将传来的接口参数组装为xml文本
|
|
|
+ String xmlInfo = getGetReadNowDataXmlInfo(A,B,C,D,page,pageSize);
|
|
|
+ byte[] xmlInfoBytes = xmlInfo.getBytes();
|
|
|
+ urlConnection.setDoInput(true);
|
|
|
+ urlConnection.setDoOutput(true);
|
|
|
+ urlConnection.setUseCaches(false);
|
|
|
+ urlConnection.setRequestProperty("Content-Type","text/xml; charset=utf-8");
|
|
|
+ urlConnection.setRequestProperty("Content-length",String.valueOf(xmlInfoBytes.length));
|
|
|
+ out = new OutputStreamWriter(urlConnection.getOutputStream());
|
|
|
+ out.write(new String(xmlInfo.getBytes(StandardCharsets.ISO_8859_1)));
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ // 开始接收返回值
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
|
|
|
+ for (line = bufferedReader.readLine(); line != null;line = bufferedReader.readLine()) {
|
|
|
+ resultStringBuffer.append(line);
|
|
|
+ }
|
|
|
+ // 从返回的文本中,截取出我们需要的内容
|
|
|
+ int beginIndex = resultStringBuffer.indexOf("<NBIoT_GetReadNowDataResult>") + 28;
|
|
|
+ int endIndex = resultStringBuffer.lastIndexOf("</NBIoT_GetReadNowDataResult>");
|
|
|
+ String jsonString = resultStringBuffer.toString().substring(beginIndex,endIndex);
|
|
|
+ JSONObject result = JSONObject.parseObject(jsonString);
|
|
|
+ String outResult = result.getString("outResult");
|
|
|
+ JSONArray jsonArray = null;
|
|
|
+ if(!StringUtils.isEmpty(outResult)&&"1".equals(outResult)){
|
|
|
+ jsonArray = result.getJSONArray("outData");
|
|
|
+ }else{
|
|
|
+ log.error("waterErrorCode:"+outResult+";waterErrorMsg:"+result.getString("outMsg"));
|
|
|
+ }
|
|
|
+ // List<Map> list = JSONObject.parseArray(jsonArray.toString(),Map.class);
|
|
|
+ return jsonArray;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //TODO TODO 获取表日用量
|
|
|
+ @Retryable(value = {RemoteAccessException.class}, maxAttempts = 3, backoff = @Backoff(delay = 200L, multiplier = 1))
|
|
|
+ public static JSONArray getFreezeDayDosage(String A,String B,String C,String D,String F,int page,int pageSize) {
|
|
|
+ //todo 可改为从配置文件获取,或者每次都从参数中传过来,因为我的项目中url不变,所以写死在这里
|
|
|
+ String line = "";
|
|
|
+ // 用来存储接收到的返回值,铁汁们可以考虑下为什么用StringBuffer而不是普通的String ^.^
|
|
|
+ StringBuffer resultStringBuffer = new StringBuffer();
|
|
|
+ OutputStreamWriter out = null;
|
|
|
+ try {
|
|
|
+ // 根据url连接接口
|
|
|
+ URL realUrl = new URL(url);
|
|
|
+ URLConnection urlConnection = realUrl.openConnection();
|
|
|
+ // 根据需要,将传来的接口参数组装为xml文本
|
|
|
+ String xmlInfo = getGetFreezeDayDosageXmlInfo(A,B,C,D,F,page,pageSize);
|
|
|
+ byte[] xmlInfoBytes = xmlInfo.getBytes();
|
|
|
+ urlConnection.setDoInput(true);
|
|
|
+ urlConnection.setDoOutput(true);
|
|
|
+ urlConnection.setUseCaches(false);
|
|
|
+ urlConnection.setRequestProperty("Content-Type","text/xml; charset=utf-8");
|
|
|
+ urlConnection.setRequestProperty("Content-length",String.valueOf(xmlInfoBytes.length));
|
|
|
+ out = new OutputStreamWriter(urlConnection.getOutputStream());
|
|
|
+ out.write(new String(xmlInfo.getBytes(StandardCharsets.ISO_8859_1)));
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ // 开始接收返回值
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
|
|
|
+ for (line = bufferedReader.readLine(); line != null;line = bufferedReader.readLine()) {
|
|
|
+ resultStringBuffer.append(line);
|
|
|
+ }
|
|
|
+ // 从返回的文本中,截取出我们需要的内容
|
|
|
+ int beginIndex = resultStringBuffer.indexOf("<NBIoT_GetFreezeDayDosageResult>") + 32;
|
|
|
+ int endIndex = resultStringBuffer.lastIndexOf("</NBIoT_GetFreezeDayDosageResult>");
|
|
|
+ String jsonString = resultStringBuffer.toString().substring(beginIndex,endIndex);
|
|
|
+ JSONObject result = JSONObject.parseObject(jsonString);
|
|
|
+ String outResult = result.getString("outResult");
|
|
|
+ JSONArray jsonArray = null;
|
|
|
+ if(!StringUtils.isEmpty(outResult)&&"1".equals(outResult)){
|
|
|
+ jsonArray = result.getJSONArray("outData");
|
|
|
+ }else{
|
|
|
+ log.error("waterErrorCode:"+outResult+";waterErrorMsg:"+result.getString("outMsg"));
|
|
|
+ }
|
|
|
+ // List<Map> list = JSONObject.parseArray(jsonArray.toString(),Map.class);
|
|
|
+ return jsonArray;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getGetReadNowDataXmlInfo(String A,String B,int C,String D,int page,int pageSize) {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ stringBuilder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
|
|
+ stringBuilder.append("<soap:Envelope xmlns:xsi=\"http:/www.w3.org/201/XMLschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLschema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
|
|
|
+ stringBuilder.append(" <soap:Body>");
|
|
|
+ stringBuilder.append(" <NBIoT_GetReadNowData xmlns=\"http://www.suntront.com/\">");
|
|
|
+ stringBuilder.append(" <user>"+A+"</user>");
|
|
|
+ stringBuilder.append(" <pwd>"+B+"</pwd>");
|
|
|
+ stringBuilder.append(" <pageSize>"+pageSize+"</pageSize>");
|
|
|
+ stringBuilder.append(" <currentPageIndex>"+page+"</currentPageIndex>");
|
|
|
+ stringBuilder.append(" <OrderType>"+C+"</OrderType>");
|
|
|
+ stringBuilder.append(" <meterAddr>"+D+"</meterAddr>");
|
|
|
+ stringBuilder.append(" </NBIoT_GetReadNowData>");
|
|
|
+ stringBuilder.append(" </soap:Body>");
|
|
|
+ stringBuilder.append("</soap:Envelope>");
|
|
|
+ return stringBuilder.toString();
|
|
|
+ }
|
|
|
+ private static String getGetFreezeDayDosageXmlInfo(String A,String B,String C,String D,String F,int page,int pageSize) {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ stringBuilder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
|
|
+ stringBuilder.append("<soap:Envelope xmlns:xsi=\"http:/www.w3.org/201/XMLschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLschema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
|
|
|
+ stringBuilder.append(" <soap:Body>");
|
|
|
+ stringBuilder.append(" <NBIoT_GetFreezeDayDosage xmlns=\"http://www.suntront.com/\">");
|
|
|
+ stringBuilder.append(" <user>"+A+"</user>");
|
|
|
+ stringBuilder.append(" <pwd>"+B+"</pwd>");
|
|
|
+ stringBuilder.append(" <pageSize>"+pageSize+"</pageSize>");
|
|
|
+ stringBuilder.append(" <currentPageIndex>"+page+"</currentPageIndex>");
|
|
|
+ stringBuilder.append(" <meterAddr>"+F+"</meterAddr>");
|
|
|
+ stringBuilder.append(" <beginDate>"+C+"</beginDate>");
|
|
|
+ stringBuilder.append(" <endDate>"+D+"</endDate>");
|
|
|
+ stringBuilder.append(" </NBIoT_GetFreezeDayDosage>");
|
|
|
+ stringBuilder.append(" </soap:Body>");
|
|
|
+ stringBuilder.append("</soap:Envelope>");
|
|
|
+ return stringBuilder.toString();
|
|
|
+ }
|
|
|
+}
|