package com.ruoyi.demo.utils; import cn.hutool.core.date.DateUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.ruoyi.demo.config.MtStarProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; import java.util.HashMap; import java.util.List; @Component public class MtStarUtil { @Autowired MtStarProperties mtStarProperties; public String sign(long l){ String accesSecretMd5 = SecureUtil.md5(mtStarProperties.getSecret()); // long l = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")); //10位 String s = SecureUtil.md5(mtStarProperties.getAuthtoken() + accesSecretMd5 + l); return s; } public String sign2(long l){ return SecureUtil.md5(mtStarProperties.getAuthtoken() + SecureUtil.md5(mtStarProperties.getSecret()) + l); } // 搜索,根据设备编号返回设备cMark public String getCMark(String equipmentCode){ HashMap map = initMap(); map.put("state", 1); map.put("startIndex", 0); map.put("queryCount", 1); map.put("select_name", equipmentCode); String post = HttpUtil.post(mtStarProperties.getUrl() + "dev_get_devs_cmarks", map); JSONObject obj = JSONUtil.parseObj(post); if (obj.get("retCode",Integer.class) == 0){ List list = obj.getBeanList("cMarkList", String.class); if (list != null && !list.isEmpty()){ JSONObject entries = JSONUtil.parseObj(list.get(0)); return entries.get("cMark", String.class); } } return "error"; } public String wakeUpAndDormancy(String equipmentCode, int onoff){ StringBuffer callBack = new StringBuffer(); callBack.append(mtStarProperties.getUrl()).append("sendClientParams"); String cMark = getCMark(equipmentCode); if ("error".equals(cMark)) return "error"; HashMap map = initMap(); map.put("paramType", 17); map.put("cMarks", cMark); HashMap sCmd = new HashMap<>(); sCmd.put("paramType", 17); sCmd.put("onoff", onoff); sCmd.put("subcmd", "ctrl_lcd_onoff_ontime"); map.put("params", sCmd.toString()); map.put("retry", 0); String post = HttpUtil.post(mtStarProperties.getUrl() + "sendClientParams", map); JSONObject obj = JSONUtil.parseObj(post); if (0 == obj.get("retCode", Integer.class)) return "success"; return "error"; } // 唤醒 public String wakeup(String equipmentCode){ if ("success".equals(wakeUpAndDormancy(equipmentCode, 1))) return "成功"; else return "失败"; } // 休眠 public String dormancy(String equipmentCode){ if ("success".equals(wakeUpAndDormancy(equipmentCode, 0))) return "成功"; else return "失败"; } // 初始化请求map public HashMap initMap(){ HashMap map = new HashMap<>(); map.put("access_token", mtStarProperties.getAuthtoken()); long l = DateUtil.currentSeconds(); map.put("timestamp", l); map.put("sign", sign2(l)); return map; } // 例子 public String getDecive(Integer state, String selectName) { HashMap map = new HashMap<>(); map.put("access_token", mtStarProperties.getAuthtoken()); long value = DateUtil.currentSeconds(); map.put("timestamp", value); map.put("sign", SecureUtil.md5(mtStarProperties.getAuthtoken() + SecureUtil.md5(mtStarProperties.getSecret()) + value)); map.put("state", state); map.put("select_name", selectName); // log.info("满天星获取设备列表 请求参数 = {}", map); String post = HttpUtil.post(mtStarProperties.getUrl() + "dev_get_devs_cmarks", map); // log.info("post返回 {}", post); JSONObject jsonObject = JSONUtil.parseObj(post); if ("0".equals(jsonObject.getStr("retCode"))) { return jsonObject.getStr("cMarkList"); } else { return null; } } }