diff --git a/jsowell-admin/src/main/resources/application.yml b/jsowell-admin/src/main/resources/application.yml index b2ce30400..410109cf5 100644 --- a/jsowell-admin/src/main/resources/application.yml +++ b/jsowell-admin/src/main/resources/application.yml @@ -130,4 +130,5 @@ wulian: #高德地图相关 aMap: merchantPrivateKey: - aMapPublicKey: \ No newline at end of file + aMapPublicKey: + appId: \ No newline at end of file diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/dto/amap/ChargeDeviceDynamicsDTO.java b/jsowell-pile/src/main/java/com/jsowell/pile/dto/amap/ChargeDeviceDynamicsDTO.java new file mode 100644 index 000000000..5aacbc978 --- /dev/null +++ b/jsowell-pile/src/main/java/com/jsowell/pile/dto/amap/ChargeDeviceDynamicsDTO.java @@ -0,0 +1,13 @@ +package com.jsowell.pile.dto.amap; + +import lombok.Data; + +/** + * TODO + * + * @author Lemon + * @Date 2023/6/15 15:54 + */ +@Data +public class ChargeDeviceDynamicsDTO { +} diff --git a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/domain/AMapConnectorStatusInfo.java b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/domain/AMapConnectorStatusInfo.java new file mode 100644 index 000000000..91cac5254 --- /dev/null +++ b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/domain/AMapConnectorStatusInfo.java @@ -0,0 +1,35 @@ +package com.jsowell.thirdparty.amap.domain; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +/** + * 高德地图充电枪状态 + * + * @author Lemon + * @Date 2023/6/15 16:00 + */ +@Data +public class AMapConnectorStatusInfo { + + //充电设备接口编码,同一运营商内唯一 + @JSONField(name = "ConnectorID") + private String connectorID; + + // 充电枪所属的桩设备ID + @JSONField(name = "EquipmentID") + private String equipmentID; + + /** + * 状态 + * 0:离网 + * 1:空闲 + * 2:占用(未充电) + * 3:占用(充电中) + * 4:占用(预约锁定) + * 255:故障 + */ + @JSONField(name = "Status") + private String status; + +} diff --git a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/AMapService.java b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/AMapService.java index 9d95e4c37..92f489375 100644 --- a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/AMapService.java +++ b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/AMapService.java @@ -1,5 +1,6 @@ package com.jsowell.thirdparty.amap.service; +import com.jsowell.pile.dto.amap.ChargeDeviceDynamicsDTO; import com.jsowell.pile.dto.amap.GetStationInfoDTO; import com.jsowell.thirdparty.amap.domain.AMapStationInfo; @@ -19,4 +20,12 @@ public interface AMapService { * @return */ List getStationInfos(GetStationInfoDTO dto); + + /** + * 商家推送充电设备动态数据 + * @param pileConnectorCode + * @return + * @throws Exception + */ + String pushChargingDeviceDynamics(String pileConnectorCode) throws Exception; } diff --git a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/impl/AMapServiceImpl.java b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/impl/AMapServiceImpl.java index c4e3c2a3f..65fbb5567 100644 --- a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/impl/AMapServiceImpl.java +++ b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/impl/AMapServiceImpl.java @@ -1,20 +1,22 @@ package com.jsowell.thirdparty.amap.service.impl; +import com.alibaba.fastjson.JSONObject; import com.jsowell.common.constant.Constants; +import com.jsowell.common.exception.BusinessException; import com.jsowell.common.util.PageUtils; import com.jsowell.common.util.StringUtils; import com.jsowell.pile.domain.PileBasicInfo; import com.jsowell.pile.domain.PileStationInfo; +import com.jsowell.pile.dto.amap.ChargeDeviceDynamicsDTO; import com.jsowell.pile.dto.amap.GetStationInfoDTO; import com.jsowell.pile.service.*; import com.jsowell.pile.vo.base.ConnectorInfoVO; import com.jsowell.pile.vo.uniapp.BillingPriceVO; +import com.jsowell.pile.vo.uniapp.PileConnectorDetailVO; import com.jsowell.pile.vo.web.PileModelInfoVO; -import com.jsowell.thirdparty.amap.domain.AMapConnectorInfo; -import com.jsowell.thirdparty.amap.domain.AMapEquipmentInfo; -import com.jsowell.thirdparty.amap.domain.AMapPriceChargingInfo; -import com.jsowell.thirdparty.amap.domain.AMapStationInfo; +import com.jsowell.thirdparty.amap.domain.*; import com.jsowell.thirdparty.amap.service.AMapService; +import com.jsowell.thirdparty.amap.util.AMapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -102,12 +104,10 @@ public class AMapServiceImpl implements AMapService { aMapInfo.setAMapPriceChargingInfo(priceList); Map pileNumMap = getPileNum(stationInfo.getId()); - aMapInfo.setFastEquipmentNum(pileNumMap.get("fastTotal")); aMapInfo.setSlowEquipmentNum(pileNumMap.get("slowTotal")); List aMapEquipmentInfos = getPileListByStationId(String.valueOf(stationInfo.getId())); - aMapInfo.setAMapEquipmentInfos(aMapEquipmentInfos); resultList.add(aMapInfo); @@ -116,6 +116,47 @@ public class AMapServiceImpl implements AMapService { return resultList; } + /** + * 商家推送充电设备动态数据 + * @param pileConnectorCode + * @return + * @throws Exception + */ + @Override + public String pushChargingDeviceDynamics(String pileConnectorCode) throws Exception { + // 根据枪口号查出桩信息 + PileConnectorDetailVO pileConnectorDetailVO = pileBasicInfoService.queryPileConnectorDetail(pileConnectorCode); + if (pileConnectorDetailVO == null) { + throw new BusinessException("", ""); + } + String stationId = pileConnectorDetailVO.getStationId(); + Map pileNumMap = getPileNum(Long.parseLong(stationId)); + + AMapConnectorStatusInfo info = new AMapConnectorStatusInfo(); + info.setConnectorID(pileConnectorCode); + info.setEquipmentID(pileConnectorDetailVO.getPileSn()); + info.setStatus(pileConnectorDetailVO.getConnectorStatus()); + + // 拼装业务参数 + JSONObject json = new JSONObject(); + json.put("stationID", stationId); + json.put("fast_free", pileNumMap.get("fastFree")); + json.put("fast_total", pileNumMap.get("fastTotal")); + json.put("slow_free", pileNumMap.get("slowFree")); + json.put("slow_total", pileNumMap.get("slowTotal")); + json.put("connectorStatusInfo", info); + + String SPI = "amap.charging.pushStationStatus"; + + Map map = AMapUtils.generateParamMap(json, SPI); + + // 调用高德接口 + return AMapUtils.sendPost(map); + } + + + + /** * 根据站点id查询快、慢充设备数量 * @param stationId @@ -123,20 +164,30 @@ public class AMapServiceImpl implements AMapService { */ private Map getPileNum(Long stationId) { int fastTotal = 0; + int fastFree = 0; int slowTotal = 0; + int slowFree = 0; List connectorList = pileConnectorInfoService.getUniAppConnectorList(stationId); for (ConnectorInfoVO connectorVO : connectorList) { if (StringUtils.equals(connectorVO.getChargingType(), Constants.ONE)) { // 快充 fastTotal += 1; + if (StringUtils.equals(connectorVO.getConnectorStatus(), Constants.ONE)) { + fastFree += 1; + } } else { // 慢充 slowTotal += 1; + if (StringUtils.equals(connectorVO.getConnectorStatus(), Constants.ONE)) { + slowFree += 1; + } } } Map map = new LinkedHashMap<>(); map.put("fastTotal", fastTotal); + map.put("fastFree", fastFree); map.put("slowTotal", slowTotal); + map.put("slowFree", slowFree); return map; } diff --git a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/util/AMapUtils.java b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/util/AMapUtils.java index 04f071ec5..c392c67a6 100644 --- a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/util/AMapUtils.java +++ b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/util/AMapUtils.java @@ -1,6 +1,14 @@ package com.jsowell.thirdparty.amap.util; +import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; import java.io.*; import java.security.*; @@ -19,6 +27,9 @@ public class AMapUtils { @Value("${aMap.aMapPublicKey}") private static String AMAP_PUBLIC_KEY; + @Value("${aMap.appId}") + private static String APP_ID; + /** * 使用商家私钥生成签名 * @@ -32,6 +43,69 @@ public class AMapUtils { return getSign(signContent, MERCHANT_PRIVATE_KEY); } + /** + * 构造参数map + * @param bizContent 业务参数 + * @param SPI 高德文档中每个接口的SPI + * @return 构造好可直接发送请求的map参数 + */ + public static Map generateParamMap(JSONObject bizContent, String SPI) throws Exception { + Map map = new LinkedHashMap<>(); + map.put("biz_content", JSONObject.toJSONString(bizContent)); + map.put("app_id", APP_ID); + map.put("utc_timestamp", String.valueOf(System.currentTimeMillis())); + map.put("version", "1.0"); + map.put("charset", "UTF-8"); + map.put("method", SPI); + // 生成签名 + String sign = AMapUtils.generateSign(map); + map.put("sign", sign); + map.put("sign_type", "RSA2"); + + return map; + } + + /** + * 向高德地图发送请求 + * @param paramMap 请求参数 + * @return 高德返回结果 + */ + public static String sendPost(Map paramMap) { + // 定义Content-type、初始化HttpEntity + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + MultiValueMap formBody = convertToMultiValueMap(paramMap); + HttpEntity> entity = new HttpEntity<>(formBody, headers); + + // 调用高德接口 + // 线下联调环境:https://restapi.amap.com/rest/openmp/devgw?key=高德云店秘钥key + // 线上环境:https://restapi.amap.com/rest/openmp/gw?key=高德云店秘钥key + // 高德云店秘钥key请登录高德云店「接入准备及配置」页面查看 + String url = "https://restapi.amap.com/rest/openmp/devgw?key=77ff5350723d29901fa859515f553644"; + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity resp = restTemplate.postForEntity(url, entity, String.class); + if (resp.getBody() != null && !"".equals(resp.getBody())) { + JSONObject respObj = JSONObject.parseObject(resp.getBody()); + System.out.println("高德返回结果:" + respObj); + return JSONObject.toJSONString(respObj); + } + return null; + } + + /** + * 将Map转为MultiValueMap + * (MultiValueMap 可一个 key 对应多个 value) + * @param paramMap 业务参数map + * @return MultiValueMap + */ + private static MultiValueMap convertToMultiValueMap(Map paramMap) { + MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); + for (Map.Entry entry : paramMap.entrySet()) { + multiValueMap.add(entry.getKey(), entry.getValue()); + } + return multiValueMap; + } + /** * 参数转换为待加签字符串 *