|
@@ -0,0 +1,141 @@
|
|
|
+package com.usky.demo.controller.web;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.usky.demo.Globals;
|
|
|
+import com.usky.demo.service.vo.EnergyRequestVO;
|
|
|
+import com.usky.demo.service.vo.MqttDataInfoVO;
|
|
|
+import com.usky.demo.util.CodeUtil;
|
|
|
+import com.usky.demo.util.HttpClientHelper;
|
|
|
+import com.usky.demo.util.JsonUtil;
|
|
|
+import com.usky.demo.util.StringUtil;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/dataEnergy")
|
|
|
+public class DataEnergyController {
|
|
|
+
|
|
|
+ static String token = "c19ac221e0504552b6c2ebed4b2b069b";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/queryEnergyUnitUsed")
|
|
|
+ public String QueryEnergyElectricUsed(@RequestParam(value = "energyUnitId") String energyUnitId,
|
|
|
+ @RequestParam(value = "energyUnitType") String energyUnitType,
|
|
|
+ @RequestParam(value = "energyType") String energyType,
|
|
|
+ @RequestParam(value = "circleType") String circleType,
|
|
|
+ @RequestParam(value = "dataTime") String dataTime) {
|
|
|
+ String result = "";
|
|
|
+
|
|
|
+ getToken();
|
|
|
+
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ map.put("energyUnitId", energyUnitId);
|
|
|
+ map.put("energyUnitType",energyUnitType);
|
|
|
+ map.put("energyType",energyType);
|
|
|
+ map.put("circleType",circleType);
|
|
|
+ map.put("dataTime",dataTime);
|
|
|
+ try {
|
|
|
+ JSONObject electricObject = queryData("queryEnergyUnitUsed", map, token);
|
|
|
+ String electricData = electricObject.getString("data");
|
|
|
+ JSONArray array = JSON.parseArray(electricData);
|
|
|
+ JSONObject dataObj = JSONObject.parseObject(array.get(0).toString());
|
|
|
+ result = dataObj.toString();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/helloWorld")
|
|
|
+ public String helloWorld() {
|
|
|
+ return "hello world";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void getToken() {
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ map.put("operatorSecret", Globals.OPERATOR_SECRET);
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = queryData("queryToken", map, null);
|
|
|
+ String data = jsonObject.getString("data");
|
|
|
+ JSONObject dataObject = JSONObject.parseObject(data);
|
|
|
+ token = dataObject.getString("accessToken");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 请求数据
|
|
|
+ *
|
|
|
+ * @param path 请求接口
|
|
|
+ * @param dataMap 所需的数据Map
|
|
|
+ */
|
|
|
+ public static JSONObject queryData(String path, Map<String, String> dataMap, String token) throws Exception {
|
|
|
+
|
|
|
+ String encode = CodeUtil.dataEncode(JSON.toJSONString(dataMap), Globals.DATA_SECRET, Globals.DATA_SECRET_IV);
|
|
|
+
|
|
|
+ String seq = "0001";
|
|
|
+ String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("operatorId", Globals.OPERATOR_ID);
|
|
|
+ map.put("data", encode);
|
|
|
+ map.put("timeStamp", timeStamp);
|
|
|
+ map.put("seq", seq);
|
|
|
+
|
|
|
+ if (encode == null) return null;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ String signSrc = Globals.OPERATOR_ID + encode.replaceAll(" ", "+") + timeStamp + seq;
|
|
|
+ String sig = CodeUtil.getSign(signSrc, Globals.SIGN_KEY);
|
|
|
+
|
|
|
+ map.put("sig", sig);
|
|
|
+
|
|
|
+
|
|
|
+ HttpClientHelper instance = HttpClientHelper.getInstance();
|
|
|
+
|
|
|
+ String result = instance.doPost(Globals.URL_PREFIX + (path.startsWith("/") ? path : ("/" + path)), map, token);
|
|
|
+ if (StringUtil.isEmpty(result)) return null;
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject jsonObject = JSON.parseObject(result);
|
|
|
+
|
|
|
+ if (!"0".equals(jsonObject.getString("ret"))) {
|
|
|
+ printJsonMessage("send Error:", jsonObject);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String resultSigStr = jsonObject.getString("operatorId") + jsonObject.getString("data").replaceAll(" ", "+")
|
|
|
+ + jsonObject.getString("msg") + jsonObject.getString("ret");
|
|
|
+ String resultSig = CodeUtil.getSign(resultSigStr, Globals.SIGN_KEY);
|
|
|
+ if (StringUtil.isNotEmpty(jsonObject.getString("sig")) && jsonObject.getString("sig").equals(resultSig)) {
|
|
|
+
|
|
|
+ String data = CodeUtil.dataDecode(jsonObject.getString("data"), Globals.DATA_SECRET, Globals.DATA_SECRET_IV);
|
|
|
+ jsonObject.put("data", data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ printJsonMessage("Send Message:", map);
|
|
|
+ printJsonMessage("Receive Message:", result);
|
|
|
+ printJsonMessage("Decode Message:", jsonObject);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void printJsonMessage(String prefix, Object obj) {
|
|
|
+ System.out.println(prefix + JsonUtil.JsonFormart(JSON.toJSONString(obj)));
|
|
|
+ }
|
|
|
+}
|