作者 shenhailong

电表实施数据查询 水表请求接口返回二维码

package com.sunyo.energy.location.controller;
import com.sunyo.energy.location.controller.response.ResultJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/electricity")
public class ElectricityMeterController {
//
// public ResultJson electricityPay(){
//
// }
}
package com.sunyo.energy.location.controller;
import com.sunyo.energy.location.controller.response.ResultJson;
import com.sunyo.energy.location.model.ElectricityBalanceThree;
import com.sunyo.energy.location.model.WaterElectricityParameter;
import com.sunyo.energy.location.service.ElectricityMeterService;
import com.sunyo.energy.location.service.WaterElectricityParameterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -14,11 +16,16 @@ import java.util.Date;
@RequestMapping("/electricity_meter")
@Controller
public class WaterElectricityParameterController {
public class ElectricityParameterController {
@Autowired
private WaterElectricityParameterService waterElectricityParameterService;
/**
* 电表处理远程访问 返回值信息
*/
@Autowired
ElectricityMeterService electricityMeterService;
@ApiOperation(value = "电表参数配置")
... ... @@ -84,4 +91,22 @@ public class WaterElectricityParameterController {
}
}
/**
* 获取电表实施数据
* @param eeId
* @return
*/
@RequestMapping(value = "/getEnergyInfoForRealTime")
@ResponseBody
public ElectricityBalanceThree getEnergyInfoForRealTime(@RequestParam(value = "eeId", required = false) String eeId){
try {
ElectricityBalanceThree energyInfoForRealTime = electricityMeterService.getEnergyInfoForRealTime(eeId);
return energyInfoForRealTime;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
... ...
package com.sunyo.energy.location.service;
import com.sunyo.energy.location.controller.response.ResultJson;
import com.sunyo.energy.location.model.ElectricityBalanceThree;
import java.util.Map;
/**
* @author shenhailong
* <p>
* 2020/5/25/16:12
*/
public interface ElectricityMeterService {
/**
* 获取电表实时数据
*
* @param deviceId
* @return
*/
ElectricityBalanceThree getEnergyInfoForRealTime(String deviceId) throws Exception;
}
... ...
... ... @@ -16,7 +16,9 @@ public interface WaterMeterService {
ResultJson waterMeterAll(String wmId);
// 请求返回二维码
/**
* 请求返回二维码
*/
ResultJson qrCode(String payFees, String wmId, String userId, String realName) throws UnsupportedEncodingException;
... ...
package com.sunyo.energy.location.service.imp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sunyo.energy.location.controller.response.ResultJson;
import com.sunyo.energy.location.model.*;
import com.sunyo.energy.location.service.ElectricityMeterService;
import com.sunyo.energy.location.utils.HttpsUtils;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ElectricityMeterServiceImp {
@Service
public class ElectricityMeterServiceImp implements ElectricityMeterService {
// 获取房间与设备信息
/**
* 获取房间与设备信息
*/
private final static String electricityUrl = "/api/emcs/getRoomAndDevice";
// 获取所有电表实时通断电状态
/**
* 获取所有电表实时通断电状态
*/
private final static String electricityStatus = "/api/emcs/getAllDeviceControlStatus";
// 获取电表实时数据/余额
private final static String electricityBanlance = " /api/emcs/getEnergyInfoForRealTime";
/**
* 获取电表实时数据/余额
*/
private final static String electricityBanlance = "http://192.168.1.2:18080/api/emcs/getEnergyInfoForRealTime";
/**
* 电费查询
... ... @@ -89,9 +101,12 @@ public class ElectricityMeterServiceImp {
public Number balance(String deviceId){
// 获取电表余额
String s1 = HttpsUtils.sendPostHttpRequest(electricityBanlance, "");
String s1 = HttpsUtils.sendPostHttpRequest(electricityBanlance, "1");
ElectricityBalanceOne electricityBalanceOne = JSON.parseObject(s1, ElectricityBalanceOne.class);
ElectricityBalanceTwo data1 = electricityBalanceOne.getData();
List<ElectricityBalanceThree> electricityBalanceThreeList = data1.getDatas();
for (ElectricityBalanceThree electricityBalanceThree: electricityBalanceThreeList){
if (deviceId.equals(electricityBalanceThree.getDeviceId())){
... ... @@ -101,4 +116,26 @@ public class ElectricityMeterServiceImp {
return 0;
}
@Override
public ElectricityBalanceThree getEnergyInfoForRealTime(String deviceId){
Map<String, Object> datas = new HashMap<>();
datas.put("deviceId", "1");
try {
String infoForRealTime = HttpsUtils.sendPost(electricityBanlance, datas);
ElectricityBalanceOne electricityBalanceOne = JSON.parseObject(infoForRealTime, ElectricityBalanceOne.class);
List<ElectricityBalanceThree> infoForRealTimeList = electricityBalanceOne.getData().getDatas();
for (ElectricityBalanceThree electricityBalanceThree: infoForRealTimeList){
if (electricityBalanceThree != null){
return electricityBalanceThree;
}
}
return null;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
... ...
... ... @@ -2,8 +2,6 @@ package com.sunyo.energy.location.service.imp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.xml.internal.ws.message.stream.PayloadStreamReaderMessage;
import com.sunyo.energy.location.controller.response.ResultJson;
import com.sunyo.energy.location.dao.LocationMapper;
import com.sunyo.energy.location.dao.PayRecordsMapper;
... ... @@ -14,13 +12,9 @@ import com.sunyo.energy.location.model.WaterMeter;
import com.sunyo.energy.location.service.WaterMeterService;
import com.sunyo.energy.location.utils.AllUtils;
import com.sunyo.energy.location.utils.HttpsUtils;
import com.sunyo.energy.location.utils.PropertiesLoader;
import org.apache.catalina.loader.ParallelWebappClassLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
... ... @@ -40,7 +34,9 @@ public class WaterMeterServiceImp implements WaterMeterService {
@Autowired
private LocationMapper locationMapper;
// 查询所有水表
/**
* 查询所有水表
*/
private static final String selectUrl = "http://123.56.159.203:8023/nowwmrd/getSelectNowwmrdbyMtId";
private static final String payUrl = "http://123.56.159.203:8023/mtfmset/allWMadd";
... ...
package com.sunyo.energy.location.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EncodingUtils;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Map;
@Slf4j
public class HttpsUtils {
/**
... ... @@ -60,7 +74,7 @@ public class HttpsUtils {
* @return
*/
@SuppressWarnings("rawtypes")
public static String urlencode(Map<String, Object> data) {
public static String urlencode(Map<String, Object> data) throws UnsupportedEncodingException {
// 将map里的参数变成像 showapi_appid=###&showapi_sign=###&的样子
StringBuilder sb = new StringBuilder();
for (Map.Entry i : data.entrySet()) {
... ... @@ -68,6 +82,7 @@ public class HttpsUtils {
sb.append(i.getKey()).append("=")
.append(URLEncoder.encode(i.getValue() + "", "UTF-8"))
.append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
... ... @@ -133,46 +148,80 @@ public class HttpsUtils {
return result;
}
// 测试是否有效
// public static void main(String[] args) {
// // 接口地址
// String requestUrl = "http://localhost:8080/manifest/f5f5669bbdecefd3dacfaba194647c35";
// try {
//
// Map<String, Object> datas = new HashMap<>();
// datas.put("de_size", "dcdf38d9a5d6411985e49155481882f5");
// datas.put("waybillnomaster", "66611111111");
// datas.put("de_type", "001");
// datas.put("delivery_station", "001");
// datas.put("flightno", "CA9999");
// datas.put("flightdate", "2019-9-21 00:00:00");
// datas.put("originatingstation", "CGO");
// datas.put("destinationstation", "CGO");
// datas.put("totalpiece", "19");
// datas.put("totalweight", "19");
// datas.put("paymode", "PP");
// datas.put("customsstatus", "001");
// datas.put("customscode", "4604");
// datas.put("productname", "SHOUJI");
// datas.put("co_company", "测试");
// datas.put("co_address", "测试");
// datas.put("co_telephone", "00000000");
// datas.put("co_country", "CN");
// datas.put("sh_company", "测试");
// datas.put("sh_address", "测试");
// datas.put("sh_country", "CN");
// datas.put("sh_telephone", "0000000");
// datas.put("sh_city", "测试");
// datas.put("USER_ID", "1");
// datas.put("reach_station", "CGO");
// datas.put("de_volume", "000100003_MMM");
// datas.put("de_trstation", "APL,111");
//
// String param ="waybillnomaster=666-11111111&de_volume=66611111111_MMM&de_type=001&delivery_station=001&flightno=CV9765&flightdate=2019-12-20&originatingstation=CGO&destinationstation=ORD&totalpiece=1&totalweight=136.00&paymode=PP&customsstatus=001&customscode=4604&productname=CONSOL&co_company=DSV+AIR+%26+SEA+CO.%2c+LTD&co_address=8%269F%2c+RAFFLES+CITY+CHANGNING+OFFI.+TOWER+3%2c+NO.+1193+CHANGNING+RDSHANG&co_zipcode=200051&shpcusid=&shpaeo=&co_telephone=%2b86+21+33259800&co_country=CN&sh_company=ORD-DSV+AIR++SEA+INC&sh_address=1300+N+ARLINGTON+HEIGHTS+ROAD+SUITE+US&sh_zipcode=60143&cnecusid=&cneaeo=&sh_country=US&sh_telephone=8479568855&sh_fax=&sh_city=CHICAGO&USER_ID=1640&reach_station=ORD&de_trstation=BDX%2c111&de_packing=BDX%2c222&de_remarks=BDX%2c333&de_size=dcdf38d9a5d6411985e49155481882f5";
// String ss = HttpsUtils.sendPostHttpRequest(requestUrl, param);
// JSONArray jsonArray = JSON.parseArray(ss);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
/**
* 发送POST请求
*
* @param url
* 目的地址
* @param parameters
* 请求参数,Map类型。
* @return 远程响应结果
*/
public static String sendPost(String url, Map<String, Object> parameters) {
String result = "";// 返回的结果
BufferedReader in = null;// 读取响应输入流
PrintWriter out = null;
StringBuffer sb = new StringBuffer();// 处理请求参数
String params = "";// 编码之后的参数
try {
// 编码请求参数
if (parameters.size() == 1) {
for (String name : parameters.keySet()) {
sb.append(name).append("=").append(
java.net.URLEncoder.encode(parameters.get(name).toString(),
"UTF-8"));
}
params = sb.toString();
} else {
for (String name : parameters.keySet()) {
sb.append(name).append("=").append(
java.net.URLEncoder.encode(parameters.get(name).toString(),
"UTF-8")).append("&");
}
String temp_params = sb.toString();
params = temp_params.substring(0, temp_params.length() - 1);
}
// 创建URL对象
java.net.URL connURL = new java.net.URL(url);
// 打开URL连接
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL
.openConnection();
// 设置通用属性
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
// 设置POST方式
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
// 获取HttpURLConnection对象对应的输出流
out = new PrintWriter(httpConn.getOutputStream());
// 发送请求参数
out.write(params);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
in = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), "UTF-8"));
String line;
// 读取返回的内容
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
... ...