package testExport; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.happy.Model.HistoryBm; import com.happy.Model.QueryUrlEnum; import com.happy.Unitil_elc.*; import com.happy.common.wx.WxUtil; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class DataClient { static String token = "c19ac221e0504552b6c2ebed4b2b069b"; public static void main(String[] args) { getToken(); //System.out.println(getPowerLastHistoryCumulant("NCJT210826009061")); //String orderNo = WxUtil.mchOrderNo(); //System.out.println(recharege("HZJH201901220010", orderNo, "1")); System.out.println(getAccountInfo("NCJT210826002156".replace("\n", ""))); //System.out.println(recharege("336211427165", orderNo, "0.1")); // Gson gson = new Gson(); // com.alibaba.fastjson.JSONObject mess = DataClient.recharege("336211427165", orderNo, "0.1"); // HashMap userMap = gson.fromJson(mess.toString(), new TypeToken>() {}.getType()); // String msg = userMap.get("msg"); // if (msg.equals("请求成功")) { // System.out.println("成功"); // } //System.out.println(getAccountInfo("NCJT210826001025")); //System.out.println(getPowerLastHistoryCumulantw("336211429210")); //System.out.println(queryPowerRealTimeData("NCJT210826010145")); //System.out.println(queryDataCenter()); //System.out.println(getHistoryCumulant()); // getUserAccountInfo(); // queryBuildingUsed(); // queryBuilding(); // queryDataCenter(); // customerRegister(); // getAccountInfo(); // customerInfoModify(); // getUserAccountInfo(); // recharege(); // disconnect(); // powerRealData(); // getPowerLastHistoryCumulant(); // getHistoryCumulant(); // getMonthBill(); // getLastHistoryCumulant(); } /* 除获取token外的所有请求都需要带有token数据,demo中简单起见未做token的保存处理,实际使用中请自行实现 */ public static void getToken() { HashMap map = new HashMap<>(); map.put("operatorSecret", Globals.OPERATOR_SECRET); try { JSONObject jsonObject = queryData(QueryUrlEnum.QUERY_TOKEN.getUrl(), map, null); String data = jsonObject.getString("data"); JSONObject dataObject = JSONObject.parseObject(data); token = dataObject.getString("accessToken"); } catch (Exception e) { e.printStackTrace(); } } // 查询建筑日/小时用量数据 public static void queryBuildingUsed() { HashMap map = new HashMap<>(); map.put("buildingCode", "JHEQ-2#");//建筑编码 map.put("type", "1");//数据类型 map.put("dataTime", "2021090915");//数据时间 try { queryData(QueryUrlEnum.QUERY_BUILDING_USED.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } } // 查询建筑信息 public static void queryBuilding() { HashMap map = new HashMap<>(); map.put("buildingCode", "JHEQ-1#");//建筑编码 map.put("dataCenterCode", "000000");//数据中心编码 try { queryData(QueryUrlEnum.QUERY_BUILDING.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } } // 查询数据中心信息 public static JSONObject queryDataCenter() { HashMap map = new HashMap<>(); map.put("dataCenterCode", "000001");//数据中心编码 JSONObject jsonObject = null; try { jsonObject = queryData(QueryUrlEnum.QUERY_DATACENTER.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } // 电费月账单(水费月账单参数相同,只有接口不同,) public static JSONObject getMonthBill(String user_id, String months) { HashMap map = new HashMap<>(); map.put("userId", user_id);//户号"HZJH201901220010" map.put("queryTimes", months);//查询时间点 [yyyy-MM,yyyy-MM……] Object money = null; try { return queryData(QueryUrlEnum.POWER_MONTH_BILL.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); return null; } } // 账户信息 public static JSONObject getAccountInfo(String user_id) { HashMap map = new HashMap<>(); map.put("userId", user_id);//户号 try { return queryData(QueryUrlEnum.ACCOUNT_INFO.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); return null; } } // 用户信息 public static void getUserAccountInfo() { HashMap map = new HashMap<>(); map.put("userId", "HZJH201903190022");//户号 try { queryData(QueryUrlEnum.USER_INFO.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } } // 用户注册 public static void customerRegister() { HashMap map = new HashMap<>(); map.put("userId", "HZJH201903190022");//户号 注意不可重复 map.put("phone", "18888888888");//手机号 map.put("time", "2019-03-06 14:00:00");//注册时间 map.put("name", "李华");//名称 map.put("linkman", "李华");//联系人 try { queryData(QueryUrlEnum.ACCOUNT_REGISTER.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } } // 用户充值 public static JSONObject recharege(String user_id, String orderNo, String money) { HashMap map = new HashMap<>(); map.put("userId", user_id);//户号 注意不可重复 map.put("tradeNo", orderNo);//交易号 一个交易号只能充值一次"2018090101010103" map.put("money", money);//金额 try { return queryData(QueryUrlEnum.ACCOUNT_RECHARGE.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); return null; } } // 用户信息修改 public static void customerInfoModify() { HashMap map = new HashMap<>(); map.put("userId", "HZJH201903190022");//户号 注意不可重复 map.put("phone", "18888888888");//手机号 map.put("linkman", "Li-Hua");//注册时间 map.put("name", "李华");//名称 try { queryData(QueryUrlEnum.ACCOUNT_MODIFY.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } } // 历史数据 public static JSONObject getHistoryCumulant(String pointid, String startTime, String endTime) { JSONObject jsonObject = null; HashMap map = new HashMap<>(); map.put("pointId", pointid);//表计编码 map.put("startTime", startTime);//开始时间 map.put("endTime", endTime);//结束时间 map.put("pointType", "1");//设备类型信息:1电表;2水表 map.put("type", "1");//数据类型 0, 所有(可以理解为,最小采集周期间隔的数据,如小时数据); 1, 按天,采集周期为天的数据 try { jsonObject = queryData(QueryUrlEnum.HISTORY_CUMULANT.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } // 电表最新一条历史数据 public static JSONObject getPowerLastHistoryCumulant(String pointid) { HashMap map = new HashMap<>(); map.put("pointId", pointid);//表计编码 map.put("pointType", "1"); JSONObject jsonObject = null; try { jsonObject = queryData(QueryUrlEnum.LAST_HISTORY_CUMULANT.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } // 水表最新一条历史数据 public static JSONObject getPowerLastHistoryCumulantw(String pointid) { HashMap map = new HashMap<>(); map.put("pointId", pointid);//表计编码 map.put("pointType", "2"); JSONObject jsonObject = null; try { jsonObject = queryData(QueryUrlEnum.LAST_HISTORY_CUMULANT.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } // 最新一条历史数据 public static void getLastHistoryCumulant() { HashMap map = new HashMap<>(); map.put("pointId", "00000090409190");//表计编码 map.put("pointType", "2"); try { queryData(QueryUrlEnum.LAST_HISTORY_CUMULANT.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } } // 拉合闸 public static void disconnect() { HashMap map = new HashMap<>(); map.put("pointId", "000000000001");//表计编号 map.put("operType", "1");//1.拉闸2.合闸 try { queryData(QueryUrlEnum.REMOTE_DISCONNECT.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } } // 电表实时信息 电表实时信息抄读时间较长,注意设置连接的超时时间 public static void powerRealData() { String token = "23fff64f891e4bca8280224d916e789a"; HashMap map = new HashMap<>(); map.put("pointId", "000000000001");//表计编号 try { queryData(QueryUrlEnum.METER_REAL_INFO.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } } // 电表实时信息 电表实时信息抄读时间较长,注意设置连接的超时时间 public static JSONObject queryPowerRealTimeData(String pointid) { HashMap map = new HashMap<>(); map.put("pointId", pointid);//表计编号 try { return queryData(QueryUrlEnum.queryPowerRealTimeData.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); return null; } } // 查询用户信息 public static JSONObject queryChangedUserAccountInfo (String startTime, String endTime, String pageIndex, String pageLimit){ HashMap map = new HashMap<>(); map.put("startTime", startTime); map.put("endTime", endTime); map.put("pageIndex", pageIndex); map.put("pageLimit", pageLimit); JSONObject jsonObject = null; try { jsonObject = queryData(QueryUrlEnum.queryChangedUserAccountInfo.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } // 查询用能单位能耗 public static JSONObject queryEnergyUnitUsed (String energyUnitId, String energyUnitType, String energyType, String circleType, String dataTime){ HashMap map = new HashMap<>(); map.put("energyUnitId", energyUnitId); map.put("energyUnitType", energyUnitType); map.put("energyType", energyType); map.put("circleType", circleType); map.put("dataTime", dataTime); JSONObject jsonObject = null; try { jsonObject = queryData(QueryUrlEnum.queryEnergyUnitUsed.getUrl(), map, token); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } /** * 请求数据 * * @param path 请求接口 * @param dataMap 所需的数据Map */ public static JSONObject queryData(String path, Map dataMap, String token) throws Exception { // 加密data部分 String encode = CodeUtil.dataEncode(JSON.toJSONString(dataMap), Globals.DATA_SECRET, Globals.DATA_SECRET_IV); /*组装必要参数 start */ String seq = "0001"; String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); Map map = new HashMap<>(); map.put("operatorId", Globals.OPERATOR_ID); map.put("data", encode); map.put("timeStamp", timeStamp); map.put("seq", seq); /*组装必要参数 end */ if (encode == null) return null; /*生成签名 start*/ // 为防止+在收发过程中被变成空格,将加密后的密文中的所有空格替换一下 // 需要签名的参数: operatorId + 加密后的data + 时间戳 + 自增序列 String signSrc = Globals.OPERATOR_ID + encode.replaceAll(" ", "+") + timeStamp + seq; String sig = CodeUtil.getSign(signSrc, Globals.SIGN_KEY); //将签名放入参数Map map.put("sig", sig); /*生成签名 start*/ /*执行请求,使用可以附加token的doPost方*/ HttpClientHelper instance = HttpClientHelper.getInstance(); //token也可以放进参数列表 String result = instance.doPost(Globals.URL_PREFIX + (path.startsWith("/") ? path : ("/" + path)), map, token); if (StringUtil.isEmpty(result)) return null; /*解析数据 start*/ 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); } /*解析数据 end*/ // 打印 or 其他操作 // 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))); } }