Merge remote-tracking branch 'orgin/igds-api-gateway' into igds-api-gateway
# Conflicts:
# src/main/java/com/fzzy/gateway/view/GatewayConf.view.xml
已删除5个文件
已修改20个文件
已添加20个文件
| | |
| | | DEVICE_IDCARD_HTTP("DEVICE_IDCARD_HTTP", "身份è¯-HTTPåè®®"), |
| | | DEVICE_LED_HTTP("DEVICE_LED_HTTP", "LED-HTTPåè®®"), |
| | | DEVICE_LPR_SDK_HK("DEVICE_LPR_SDK_HK", "车çè¯å«-SDK海康"), |
| | | DEVICE_TEST("DEVICE_TEST", "空åè®®"); |
| | | DEVICE_TEST("DEVICE_TEST", "æµè¯åè®®"); |
| | | |
| | | |
| | | private String code; |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.api.utils; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.text.DecimalFormat; |
| | | |
| | | /** |
| | | * æ°åæ ¼å¼åå·¥å
·ç±» |
| | | */ |
| | | public class NumberUtil { |
| | | |
| | | /** |
| | | * 对doubleç±»åçæ°å¼ä¿çæå®ä½æ°çå°æ°ã<br> |
| | | * è¯¥æ¹æ³èå
¥æ¨¡å¼ï¼åâææ¥è¿çâæ°åèå
¥ï¼å¦æä¸ä¸¤ä¸ªç¸é»æ°åçè·ç¦»ç¸çï¼å为åä¸èå
¥çèå
¥æ¨¡å¼ã<br> |
| | | * <b>注æï¼</b>å¦æç²¾åº¦è¦æ±æ¯è¾ç²¾ç¡®è¯·ä½¿ç¨ keepPrecision(String number, int precision)æ¹æ³ |
| | | * @param number è¦ä¿çå°æ°çæ°å |
| | | * @param precision å°æ°ä½æ° |
| | | * @return double 妿æ°å¼è¾å¤§ï¼å使ç¨ç§å¦è®¡æ°æ³è¡¨ç¤º |
| | | */ |
| | | public static double keepPrecision(Double number, int precision) { |
| | | if(null == number || 0.0 == number ) return 0.0; |
| | | BigDecimal bg = new BigDecimal(number); |
| | | return bg.setScale(precision, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | } |
| | | |
| | | /** |
| | | * 对floatç±»åçæ°å¼ä¿çæå®ä½æ°çå°æ°ã<br> |
| | | * è¯¥æ¹æ³èå
¥æ¨¡å¼ï¼åâææ¥è¿çâæ°åèå
¥ï¼å¦æä¸ä¸¤ä¸ªç¸é»æ°åçè·ç¦»ç¸çï¼å为åä¸èå
¥çèå
¥æ¨¡å¼ã<br> |
| | | * <b>注æï¼</b>å¦æç²¾åº¦è¦æ±æ¯è¾ç²¾ç¡®è¯·ä½¿ç¨ keepPrecision(String number, int precision)æ¹æ³ |
| | | * @param number è¦ä¿çå°æ°çæ°å |
| | | * @param precision å°æ°ä½æ° |
| | | * @return float 妿æ°å¼è¾å¤§ï¼å使ç¨ç§å¦è®¡æ°æ³è¡¨ç¤º |
| | | */ |
| | | public static float keepPrecision(Float number, int precision) { |
| | | if(null == number) return 0f; |
| | | BigDecimal bg = new BigDecimal(number); |
| | | return bg.setScale(precision, BigDecimal.ROUND_HALF_UP).floatValue(); |
| | | } |
| | | /** |
| | | * double转å符串ï¼é¿å
åºç°ç§å¦è®¡æ°æ³ |
| | | * @param d |
| | | * @return |
| | | */ |
| | | public static String doubleToStr(Double d) { |
| | | if(null == d) return ""; |
| | | DecimalFormat df = new DecimalFormat("0.0"); |
| | | return df.format(d); |
| | | } |
| | | |
| | | } |
| | |
| | | private ApiInitService apiInitService; |
| | | @Autowired |
| | | private MqttPublishService mqttPublishService; |
| | | |
| | | @Autowired |
| | | private GatewayTimerScheduled scheduled; |
| | | |
| | | @Override |
| | | public void run(String... args) throws Exception { |
| | | mqttPublishService.init(); |
| | | //æ§è¡åå§åæ¹æ¡ |
| | | apiInitService.init(); |
| | | |
| | | |
| | | //è·åæ°è±¡ä¿¡æ¯ |
| | | scheduled.doWeatherExe(); |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | package com.fzzy.gateway; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.fzzy.api.data.ApiParam; |
| | | import com.fzzy.api.entity.ApiConfs; |
| | | import com.fzzy.api.service.*; |
| | | import com.fzzy.api.utils.ContextUtil; |
| | | import com.fzzy.api.utils.RedisUtil; |
| | | import com.fzzy.gateway.api.GatewayRemoteManager; |
| | | import com.fzzy.gateway.data.WeatherWebDto; |
| | | import com.fzzy.gateway.entity.GatewayConf; |
| | | import com.fzzy.gateway.service.GatewayConfService; |
| | | import com.fzzy.gateway.util.GatewayHttpUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.apache.commons.lang.time.DateUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | |
| | | @Slf4j |
| | | @Component(GatewayTimerScheduled.BEAN_ID) |
| | | public class GatewayTimerScheduled { |
| | | |
| | | public static String DEFAULT_URL = "https://v0.yiketianqi.com/api?unescape=1&version=v61&appid={appId}&appsecret={appsecret}&cityid={cityid}"; |
| | | public static String DEFAULT_APP_ID = "49421971"; |
| | | public static String DEFAULT_APP_SECRET = "JmJE48Fv"; |
| | | public static String DEFAULT_CITYID = "101270101";//æé½ |
| | | |
| | | |
| | | public static final String BEAN_ID = "gateway.timerScheduled"; |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * æ¯é´é30åéæ§è¡ä¸æ¬¡ |
| | | */ |
| | | @Scheduled(cron = "0 0/30 * * * ?") |
| | | public void scheduled30() { |
| | | |
| | | //宿¶è·åæ°è±¡ä¿¡æ¯ |
| | | doWeatherExe(); |
| | | } |
| | | |
| | | public void doWeatherExe() { |
| | | |
| | | try { |
| | | String url = DEFAULT_URL; |
| | | url = url.replace("{appId}", DEFAULT_APP_ID).replace("{appsecret}", DEFAULT_APP_SECRET).replace("{cityid}", DEFAULT_CITYID); |
| | | |
| | | String result = GatewayHttpUtil.doGet(url, null); |
| | | |
| | | if (null == result) { |
| | | log.error("å½åå¤ç½è·åæ°è±¡ä¿¡æ¯å¤±è´¥â¦â¦"); |
| | | return; |
| | | } |
| | | WeatherWebDto dto = JSON.parseObject(result, WeatherWebDto.class); |
| | | if (StringUtils.isNotEmpty(dto.getErrcode())) { |
| | | log.error("å½åå¤ç½è·åæ°è±¡ä¿¡æ¯å¼å¸¸:{}", dto.getErrmsg()); |
| | | return; |
| | | } |
| | | |
| | | WeatherWebDto.contextMap.put("default", dto); |
| | | |
| | | |
| | | log.info("===========================ç³»ç»å®æ¶è·è·åæ°è±¡ä¿¡æ¯===={}==================",dto); |
| | | |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ§è¡ç½å
³å¿è·³ |
| | |
| | | package com.fzzy.gateway.api; |
| | | |
| | | |
| | | import com.fzzy.gateway.data.BaseResp; |
| | | import com.fzzy.gateway.hx2023.data.KafaGrainData; |
| | | import com.fzzy.gateway.hx2023.data.*; |
| | | |
| | |
| | | public KafaGrainData syncGrain(SyncReqData reqData); |
| | | |
| | | |
| | | /** |
| | | * åæ¥ç²®æ
ä¿¡æ¯è¿åJSONæ¥æ |
| | | * |
| | | * @param reqData |
| | | * @return |
| | | */ |
| | | public BaseResp syncGrain2(SyncReqData reqData); |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | | public class BaseResp { |
| | | |
| | | private int code = 200; |
| | | |
| | | private String msg = "æå"; |
| | | |
| | | |
| | | private String data; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * |
| | | * @author: andy.jia |
| | | * @description: å¤ç½æ°è±¡è§£æä½¿ç¨DTO |
| | | * @version: |
| | | * @data:2019å¹´12æ7æ¥ |
| | | * |
| | | */ |
| | | @Data |
| | | public class WeatherWebDto implements Serializable { |
| | | |
| | | |
| | | public static Map<String,WeatherWebDto> contextMap = new HashMap<>(); |
| | | |
| | | /** |
| | | * |
| | | */ |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | |
| | | private String errcode;//é误ç¼ç |
| | | private String errmsg;//éè¯¯ä¿¡æ¯ |
| | | |
| | | private String cityid;// å½ååå¸ID |
| | | private String city;//åå¸åç§° |
| | | private String update_time;// æ°è±¡å°æ´æ°æ¶é´ |
| | | private String date;// æ¥æ |
| | | private String week;// ææ |
| | | private String wea;// å¤©æ°æ
åµ |
| | | private String wea_img;// 天æ°å¯¹åºå¾æ (xue, lei, shachen, wu, bingbao, yun, yu,yin, qing) |
| | | private String tem;// å½å温度 |
| | | private String air;// 空æ°è´¨é |
| | | private String air_pm25;// PM2.5 |
| | | private String air_level;// 空æ°è´¨éç级 |
| | | private String air_tips;// 空æ°è´¨éæè¿° |
| | | private String humidity;// 湿度 |
| | | private String visibility;// è½è§åº¦ |
| | | private String pressure;// æ°åhPa |
| | | private String win;// é£å |
| | | private String win_speed;// é£éç级 |
| | | private String win_meter;// é£é å¦: 12km/h |
| | | private String alarm;// é¢è¦ä¿¡æ¯ |
| | | } |
| | |
| | | @PropertyDef(label = "åç§°") |
| | | private String deviceName; |
| | | |
| | | @Column(name = "PRODUCT_ID_", length = 50) |
| | | @PropertyDef(label = "设å¤ç±»åKEY") |
| | | private String productId; |
| | | |
| | | @Column(name = "TYPE_", length = 10) |
| | | @PropertyDef(label = "设å¤ç±»å") |
| | | private String type; |
| | |
| | | @PropertyDef(label = "夿³¨", description = "夿³¨ä¿¡æ¯") |
| | | private String remark; |
| | | |
| | | |
| | | @Column(name = "CABLE_RULE_", length = 20) |
| | | @PropertyDef(label = "å¸çº¿è§å", description = "å¹³æ¹ä»è¡¨ç¤ºå±è¡åï¼çä»è¡¨ç¤ºæ¯åçåæ°") |
| | | private String cableRule; |
| | | |
| | | @Column(name = "CABLE_CIR_", length = 20) |
| | | @PropertyDef(label = "çä»å±è§å", description = "é对çä»") |
| | | private String cableCir; |
| | | |
| | | } |
| | |
| | | |
| | | public static String MESSAGE_TYPE_REPORT_PROPERTY = "REPORT_PROPERTY"; |
| | | |
| | | public static String MESSAGE_TYPE_INVOKE_FUNCTION = "INVOKE_FUNCTION"; |
| | | |
| | | /** |
| | | * ç²®æ
éæä»¤ |
| | | */ |
| | | public static String FUNCTION_getTAndRHInfo = "getTAndRHInfo"; |
| | | |
| | | |
| | | public static String getMessageId() { |
| | | return System.currentTimeMillis() + RandomUtils.nextInt(1000) + ""; |
| | |
| | | import com.fzzy.api.utils.ContextUtil; |
| | | import com.fzzy.api.utils.RedisConst; |
| | | import com.fzzy.api.utils.RedisUtil; |
| | | import com.fzzy.gateway.entity.GatewayConf; |
| | | import com.fzzy.gateway.hx2023.data.GatewayAuthData; |
| | | import com.fzzy.gateway.service.GatewayConfService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * |
| | |
| | | |
| | | @Resource |
| | | private RedisUtil redisUtil; |
| | | @Resource |
| | | private GatewayConfService confService; |
| | | |
| | | |
| | | /** |
| | | * é´ææ¥å£ |
| | |
| | | public @ResponseBody |
| | | JSONObject authorize(@RequestBody GatewayAuthData data) { |
| | | |
| | | log.debug("============é´æ==========={}--{}", data.getUsername(), data.getPassword()); |
| | | |
| | | List<GatewayConf> list = confService.getCacheConfList(); |
| | | |
| | | JSONObject json = new JSONObject(); |
| | | json.put("timestamp", System.currentTimeMillis()); |
| | | if (null == list || list.isEmpty()) { |
| | | json.put("code", 500); |
| | | json.put("message", "æªè·åç½å
³ä¿¡æ¯"); |
| | | return json; |
| | | } |
| | | |
| | | String gatewayId = null; |
| | | for (GatewayConf conf : list) { |
| | | if (data.getUsername().equals(conf.getGatewayUsername()) && data.getPassword().equals(conf.getGatewayPassword())) { |
| | | gatewayId = conf.getGatewayId(); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (null == gatewayId) { |
| | | json.put("code", 500); |
| | | json.put("message", "æªå¹é
å°ç¨æ·ååå¯ç "); |
| | | return json; |
| | | } |
| | | |
| | | |
| | | //TODO éªè¯ç¨æ·ååå¯ç |
| | | String token = "fzzy-" + gatewayId; |
| | | |
| | | |
| | | String token = ContextUtil.getUUID(); |
| | | log.debug("============é´æ==========={}--{}--{}", data.getUsername(), data.getPassword(), token); |
| | | |
| | | this.updateGatewayToken(token, data.getUsername()); |
| | | |
| | | JSONObject json = new JSONObject(); |
| | | |
| | | JSONObject result = new JSONObject(); |
| | | |
| | | result.put("token", token); |
| | | |
| | | json.put("result", result); |
| | | json.put("message", "æå"); |
| | | json.put("status", 0); |
| | | json.put("code", 200); |
| | | json.put("timestamp", System.currentTimeMillis()); |
| | | |
| | | return json; |
| | | } |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | | public class ClientHeaders { |
| | | |
| | | private String productId; |
| | | private String deviceName; |
| | | private String orgId; |
| | | private String msgId; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | | public class CloudHeaders { |
| | | |
| | | private String productId; |
| | | private String deviceName; |
| | | private String orgId; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * äºç«¯åå¨çæ¥æ |
| | | */ |
| | | @Data |
| | | public class CloudSendData { |
| | | |
| | | private CloudHeaders headers; |
| | | |
| | | private String functionId; |
| | | |
| | | private String messageType; |
| | | |
| | | private String messageId; |
| | | |
| | | private String deviceId; |
| | | |
| | | private String timestamp; |
| | | |
| | | private boolean success; |
| | | |
| | | private List<InputData> inputs; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | |
| | | /** |
| | | * ç²®æ
ä¿¡æ¯ |
| | | */ |
| | | @Data |
| | | public class GrainData { |
| | | |
| | | //设å¤ç¼ç |
| | | private String deviceId; |
| | | private ClientHeaders headers; |
| | | |
| | | //æ¶æ¯ ID |
| | | private String messageId; |
| | | |
| | | private String messageType = "INVOKE_FUNCTION_REPLY"; |
| | | |
| | | private boolean success = true; |
| | | |
| | | private String timestamp; |
| | | |
| | | private GrainOutPut outPut; |
| | | |
| | | private GrainWeather weatherStation; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import lombok.Data; |
| | | |
| | | import java.util.List; |
| | | |
| | | |
| | | /** |
| | | * ç²®æ
ä¿¡æ¯ |
| | | */ |
| | | @Data |
| | | public class GrainOutPut { |
| | | |
| | | //模åè¯å«ç -常éï¼åºå®ä¼ apiTemperature |
| | | private String apISource = "apiTemperature"; |
| | | |
| | | //æ´ä»å¹³å温度 |
| | | private String avgTemperature; |
| | | |
| | | //æ´ä»æé«æ¸©åº¦ |
| | | private String maxTemperature; |
| | | |
| | | //æ´ä»æä½æ¸©åº¦ |
| | | private String minTemperature; |
| | | |
| | | private String minX = "0"; |
| | | |
| | | private String minY = "0"; |
| | | |
| | | private String minZ = "0"; |
| | | |
| | | private String maxX = "0"; |
| | | |
| | | private String maxY = "0"; |
| | | |
| | | private String maxZ = "0"; |
| | | |
| | | |
| | | private List<GrainTemp> temperature; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * ç²®æ
ä¿¡æ¯è¯¦ç» |
| | | */ |
| | | @Data |
| | | public class GrainTemp { |
| | | |
| | | //æ ¹å· |
| | | private String cableNum; |
| | | |
| | | //å±å· |
| | | private String layerNumber; |
| | | |
| | | //æ¸©åº¦å¼ |
| | | private String temperature; |
| | | |
| | | //ç´¢å¼ï¼ä»0å¼å§ |
| | | private String position; |
| | | |
| | | // //è¯¥æ¸©åº¦ç¹æå¨çå,å¹³æ¹ä»ãå°ä¸ä»å¿
å¡« |
| | | // private String linex; |
| | | // |
| | | // //è¯¥æ¸©åº¦ç¹æå¨çè¡,å¹³æ¹ä»ãå°ä¸ä»å¿
å¡« |
| | | // private String rowy; |
| | | |
| | | //æµ
åä»ãçä»å¿
å¡«ï¼ç¤ºä¾ï¼{\"totalCircle\":3,\"smallCircle\":\"4,10,16\"}ï¼totalCircleï¼æ»åæ°ï¼smallCircleï¼æ¯åæå æ ¹ç¼ |
| | | private String total_circle; |
| | | |
| | | |
| | | //å
·ä½åæ°--æµ
åä»ãçä»å¿
å¡« |
| | | private String circle; |
| | | |
| | | public GrainTemp() { |
| | | } |
| | | |
| | | public GrainTemp(String cableNum, String layerNumber, String temperature, String position) { |
| | | this.cableNum = cableNum; |
| | | this.layerNumber = layerNumber; |
| | | this.temperature = temperature; |
| | | this.position = position; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | | public class GrainWeather { |
| | | |
| | | private String airPressure; |
| | | |
| | | private String humidity; |
| | | |
| | | private String id; |
| | | |
| | | private String messageId; |
| | | |
| | | private String pm; |
| | | |
| | | private String radiation; |
| | | |
| | | private String rainfallAmount; |
| | | |
| | | private String temperature; |
| | | |
| | | private String windAngle; |
| | | |
| | | private String windDirection; |
| | | |
| | | private String windPower; |
| | | |
| | | private String windSpeed; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | | public class InputData { |
| | | |
| | | private String name; |
| | | private String value; |
| | | } |
| | |
| | | package com.fzzy.gateway.hx2023.data; |
| | | |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | |
| | | private String functionId; |
| | | |
| | | private String jsonData; |
| | | |
| | | private GatewayDevice device; |
| | | |
| | | private boolean autoReplay; |
| | | } |
| | |
| | | package com.fzzy.gateway.hx2023.service; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.fzzy.api.data.GatewayDeviceProtocol; |
| | | import com.fzzy.api.utils.NumberUtil; |
| | | import com.fzzy.gateway.api.GatewaySyncGranService; |
| | | import com.fzzy.gateway.hx2023.data.KafaGrainData; |
| | | import com.fzzy.gateway.hx2023.data.SyncReqData; |
| | | import com.fzzy.gateway.data.BaseResp; |
| | | import com.fzzy.gateway.data.WeatherWebDto; |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import com.fzzy.gateway.hx2023.ScConstant; |
| | | import com.fzzy.gateway.hx2023.data.*; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * ç½å
³ä¸ç²®æ
åæºé讯åè§£æ |
| | |
| | | |
| | | @Override |
| | | public String getGrainProtocol() { |
| | | return null; |
| | | return GatewayDeviceProtocol.DEVICE_TEST.getCode(); |
| | | } |
| | | |
| | | @Override |
| | | public KafaGrainData syncGrain(SyncReqData reqData) { |
| | | |
| | | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public BaseResp syncGrain2(SyncReqData reqData) { |
| | | |
| | | GatewayDevice device = reqData.getDevice(); |
| | | |
| | | |
| | | if (StringUtils.isEmpty(device.getCableCir())) { |
| | | return getGrainTest1(reqData, device); |
| | | } |
| | | |
| | | |
| | | BaseResp resp = new BaseResp(); |
| | | resp.setCode(500); |
| | | resp.setMsg("没æå¹é
å°è§å"); |
| | | return resp; |
| | | } |
| | | |
| | | private BaseResp getGrainTest1(SyncReqData reqData, GatewayDevice device) { |
| | | |
| | | String[] cableRule = device.getCableRule().split("-"); |
| | | |
| | | int cableZ = Integer.valueOf(cableRule[0]); |
| | | int cableY = Integer.valueOf(cableRule[1]); |
| | | int cableX = Integer.valueOf(cableRule[2]); |
| | | int sumNum = cableZ * cableY * cableX; |
| | | |
| | | |
| | | WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); |
| | | double tMIn = 20, tMax = 25; |
| | | if (null != weather) { |
| | | double tOut = Double.valueOf(weather.getTem()); |
| | | tMIn = tOut - 2; |
| | | tMax = tOut + 3; |
| | | } |
| | | |
| | | |
| | | //æ°æ®å°è£
|
| | | GrainData grain = new GrainData(); |
| | | grain.setMessageId(ScConstant.getMessageId()); |
| | | grain.setDeviceId(device.getDeviceId()); |
| | | grain.setTimestamp(System.currentTimeMillis() + ""); |
| | | |
| | | ClientHeaders headers = new ClientHeaders(); |
| | | headers.setDeviceName(device.getDeviceName()); |
| | | headers.setProductId(device.getProductId()); |
| | | headers.setOrgId(device.getOrgId()); |
| | | headers.setMsgId(ScConstant.getMessageId()); |
| | | grain.setHeaders(headers); |
| | | |
| | | GrainOutPut outPut = new GrainOutPut(); |
| | | |
| | | outPut.setAvgTemperature(NumberUtil.keepPrecision((tMax + tMIn) / 2, 1) + ""); |
| | | outPut.setMinTemperature(tMax + ""); |
| | | outPut.setMaxTemperature(tMIn + ""); |
| | | |
| | | |
| | | List<GrainTemp> temperature = new ArrayList<>(); |
| | | //æ ¹å· |
| | | int cableNum = 1, position = 0; |
| | | |
| | | double curTemp = tMIn; |
| | | double randomNumber = tMIn; |
| | | int x = 0, y = 0, z = 0; |
| | | for (int i = 0; i < sumNum; i++) { |
| | | randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn; |
| | | curTemp = NumberUtil.keepPrecision(randomNumber, 1); |
| | | position = i; |
| | | z = i % cableZ + 1; |
| | | x = i / (cableZ * cableY); |
| | | y = x * (cableZ * cableY); |
| | | y = (i - y) / cableZ; |
| | | |
| | | //æ ¹å· |
| | | cableNum = (i / cableZ) + 1; |
| | | |
| | | |
| | | temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + "")); |
| | | } |
| | | |
| | | outPut.setTemperature(temperature); |
| | | |
| | | grain.setOutPut(outPut); |
| | | |
| | | |
| | | BaseResp resp = new BaseResp(); |
| | | resp.setData(JSONObject.toJSONString(grain)); |
| | | |
| | | return resp; |
| | | } |
| | | } |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.service; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | | import com.fzzy.gateway.api.GatewayRemoteManager; |
| | | import com.fzzy.gateway.hx2023.data.CloudSendData; |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import com.fzzy.gateway.hx2023.ScConstant; |
| | | import com.fzzy.gateway.data.BaseResp; |
| | | import com.fzzy.gateway.hx2023.data.SyncReqData; |
| | | import com.fzzy.mqtt.MqttProviderConfig; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | /** |
| | | * æ ¹æ®æ¥åå°çMQTTä¿¡æ¯æ§è¡ |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class OnReceiveMqttService { |
| | | |
| | | @Resource |
| | | private GatewayRemoteManager gatewayRemoteManager; |
| | | @Resource |
| | | private MqttProviderConfig providerClient; |
| | | |
| | | |
| | | /** |
| | | * å½åæ¥æ¶å°äºç«¯åéä¿¡æ¯ |
| | | * |
| | | * @param message |
| | | */ |
| | | public void onReceiveMessage(String message) { |
| | | |
| | | try { |
| | | CloudSendData cloudSendData = JSONObject.parseObject(message, CloudSendData.class); |
| | | |
| | | |
| | | String functionId = cloudSendData.getFunctionId(); |
| | | |
| | | //ç²®æ
éé |
| | | if (ScConstant.FUNCTION_getTAndRHInfo.equals(functionId)) { |
| | | getTAndRHInfo(cloudSendData); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | private void getTAndRHInfo(CloudSendData cloudSendData) { |
| | | |
| | | String deviceId = cloudSendData.getDeviceId(); |
| | | |
| | | GatewayDevice device = GatewayUtils.getCacheByDeviceId(deviceId); |
| | | |
| | | SyncReqData syncReqData = new SyncReqData(); |
| | | syncReqData.setDeviceId(deviceId); |
| | | syncReqData.setMessageId(cloudSendData.getMessageId()); |
| | | syncReqData.setMessageType(cloudSendData.getMessageType()); |
| | | syncReqData.setFunctionId(cloudSendData.getFunctionId()); |
| | | syncReqData.setAutoReplay(true); |
| | | syncReqData.setDevice(device); |
| | | |
| | | BaseResp resp = gatewayRemoteManager.getSyncGrainService(device.getSyncProtocol()).syncGrain2(syncReqData); |
| | | |
| | | //èªå¨æ¨é |
| | | if (200 == resp.getCode() && syncReqData.isAutoReplay()) { |
| | | String topic = "/${productId}/${deviceId}/properties/report"; |
| | | |
| | | topic = topic.replace("${productId}", device.getProductId()).replace("${deviceId}", device.getDeviceId()); |
| | | |
| | | providerClient.publish(topic, resp.getData()); |
| | | |
| | | log.info("=======ç²®æ
æ¨é==========={}", resp.getData()); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.websocket; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.PathParam; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | |
| | | |
| | | /** |
| | | * ä¸åè°ç¨ç²®æ¸©æ£æµæ¥å£æä»¤ topic |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | @ServerEndpoint(value = "/{productId}/{deviceId}/properties/report") |
| | | public class WebSockDeviceMessageReport { |
| | | |
| | | @OnOpen |
| | | public void onOpen(Session session, |
| | | @PathParam("productId") String productId, |
| | | @PathParam("deviceId") String deviceId |
| | | ) throws Exception { |
| | | |
| | | log.info("--------ä¸åè°ç¨ç²®æ¸©æ£æµæ¥å£æä»¤ topic------"); |
| | | } |
| | | |
| | | @OnClose |
| | | public void onClose() { |
| | | |
| | | log.info("WebSocketè¿æ¥å
³é={}"); |
| | | } |
| | | |
| | | /** |
| | | * æ¶å°å端åéçä¿¡æ¯ |
| | | * |
| | | * @param message |
| | | * @param session |
| | | */ |
| | | @OnMessage |
| | | public void onMessage(String message, Session session) { |
| | | |
| | | log.info("æ¥èªå端çä¿¡æ¯:\n" + message); |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Session session, Throwable error) { |
| | | log.error("åçé误"); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.hx2023.websocket; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.PathParam; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | |
| | | |
| | | /** |
| | | * ä¸åè°ç¨ç²®æ¸©æ£æµæ¥å£æä»¤ topic |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | @ServerEndpoint(value = "/device-message-sender/{productId}/{deviceId}") |
| | | public class WebSockDeviceMessageSender { |
| | | |
| | | @OnOpen |
| | | public void onOpen(Session session, |
| | | @PathParam("productId") String productId, |
| | | @PathParam("deviceId") String deviceId |
| | | ) throws Exception { |
| | | |
| | | log.info("--------ä¸åè°ç¨ç²®æ¸©æ£æµæ¥å£æä»¤ topic------"); |
| | | } |
| | | |
| | | @OnClose |
| | | public void onClose() { |
| | | |
| | | log.info("WebSocketè¿æ¥å
³é={}"); |
| | | } |
| | | |
| | | /** |
| | | * æ¶å°å端åéçä¿¡æ¯ |
| | | * |
| | | * @param message |
| | | * @param session |
| | | */ |
| | | @OnMessage |
| | | public void onMessage(String message, Session session) { |
| | | |
| | | log.info("æ¥èªå端çä¿¡æ¯:\n" + message); |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Session session, Throwable error) { |
| | | log.error("åçé误"); |
| | | } |
| | | } |
| | |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import com.fzzy.gateway.hx2023.ScConstant; |
| | | import com.fzzy.gateway.hx2023.data.*; |
| | | import com.fzzy.gateway.hx2023.websocket.WebSocketDeviceReport; |
| | | import com.fzzy.gateway.service.repository.GatewayDeviceRep; |
| | | |
| | | import com.fzzy.mqtt.MqttPublishService; |
| | |
| | | /** |
| | | * gatewayDeviceService#updateSave |
| | | * |
| | | * @param entity |
| | | * @param data |
| | | */ |
| | | @DataResolver |
| | | public void updateSave(GatewayDevice entity) { |
| | | GatewayDevice data = new GatewayDevice(); |
| | | BeanUtils.copyProperties(entity, data); |
| | | public void updateSave(GatewayDevice data) { |
| | | GatewayDevice data2 = new GatewayDevice(); |
| | | BeanUtils.copyProperties(data, data2); |
| | | |
| | | if (null == data.getId()) { |
| | | data.setId(ContextUtil.getUUID()); |
| | | } |
| | | |
| | | |
| | | if (null == data.getDeviceSn()) { |
| | | if (null != entity.getIp()) { |
| | | data.setDeviceSn(entity.getIp()); |
| | | if (null == data2.getDeviceSn()) { |
| | | if (null != data2.getIp()) { |
| | | data.setDeviceSn(data2.getIp()); |
| | | } else { |
| | | data.setDeviceSn(data.getDeviceId()); |
| | | data.setDeviceSn(data2.getDeviceId()); |
| | | } |
| | | } |
| | | |
| | | gatewayDeviceRep.save(data); |
| | | |
| | | if (null == data2.getId()) { |
| | | data2.setId(ContextUtil.getUUID()); |
| | | gatewayDeviceRep.save(data2); |
| | | }else{ |
| | | gatewayDeviceRep.save(data2); |
| | | } |
| | | flushCache(); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * gatewayDeviceService#flushCache |
| | | */ |
| | | @Expose |
| | | public void flushCache() { |
| | | List<GatewayDevice> list = listAll(); |
| | | if (null == list || list.isEmpty()) return; |
| | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * gatewayDeviceService#ajaxTestWeight |
| | | * å°ç£
æ¨éæµè¯ |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.gateway.service; |
| | | |
| | | import com.bstek.dorado.annotation.DataResolver; |
| | | import com.fzzy.api.utils.ContextUtil; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import com.fzzy.gateway.service.repository.GatewayDeviceRep; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.data.domain.Sort; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | |
| | | @Component |
| | | public class GatewayDeviceService2 { |
| | | |
| | | @Resource |
| | | private GatewayDeviceRep gatewayDeviceRep; |
| | | |
| | | public List<GatewayDevice> listAll() { |
| | | Sort sort = new Sort(Sort.Direction.ASC, "deviceId"); |
| | | return gatewayDeviceRep.findAll(sort); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * gatewayDeviceService2#updateSave |
| | | * |
| | | * @param data |
| | | */ |
| | | @DataResolver |
| | | public void updateSave(GatewayDevice data) { |
| | | GatewayDevice data2 = new GatewayDevice(); |
| | | BeanUtils.copyProperties(data, data2); |
| | | |
| | | if (null == data2.getDeviceSn()) { |
| | | if (null != data2.getIp()) { |
| | | data.setDeviceSn(data2.getIp()); |
| | | } else { |
| | | data.setDeviceSn(data2.getDeviceId()); |
| | | } |
| | | } |
| | | |
| | | if (null == data2.getId()) { |
| | | data2.setId(ContextUtil.getUUID()); |
| | | gatewayDeviceRep.save(data2); |
| | | } else { |
| | | gatewayDeviceRep.save(data2); |
| | | } |
| | | flushCache(); |
| | | } |
| | | |
| | | public void flushCache() { |
| | | List<GatewayDevice> list = listAll(); |
| | | if (null == list || list.isEmpty()) return; |
| | | for (GatewayDevice device : list) { |
| | | GatewayUtils.add2Cache(device); |
| | | } |
| | | } |
| | | |
| | | } |
| | |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.util.EntityUtils; |
| | | |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.net.URLEncoder; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * ç½å
³ä¸ç¨HTTP请æ±å·¥å
·ç±» |
| | |
| | | package com.fzzy.gateway.util; |
| | | |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.nio.charset.StandardCharsets; |
| | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | public class GatewayRSAUtils { |
| | | /** |
| | | * RSAæå¤§å 坿æå¤§å° 2048/8-11 |
| | |
| | | <Property name="exClassName">toolbar-button-push</Property> |
| | | </ToolBarButton> |
| | | <ToolBarButton> |
| | | <ClientEvent name="onClick">view.get("#dialogLpr").show();</ClientEvent> |
| | | <Property name="caption">è½¦çæ¨éæµè¯</Property> |
| | | <Property name="iconClass">fa fa-search</Property> |
| | | <Property name="exClassName">toolbar-button-push</Property> |
| | | </ToolBarButton> |
| | | <ToolBarButton> |
| | | <Property name="caption">ç½å
³åå§å</Property> |
| | | <Property name="iconClass">fa fa-search</Property> |
| | | <Property name="exClassName">toolbar-button-push</Property> |
| | |
| | | <Buttons> |
| | | <Button> |
| | | <ClientEvent name="onClick">var data = view.get("#dsQuery.data");
 |
| | | view.get("#ajaxTestGrain").set("parameter",data).execute(function(result){
 |
| | | view.get("#ajaxTestGrain").set("parameter",data.toJSON()).execute(function(result){
 |
| | | self.get("parent").hide();
 |
| | | $alert(result);
 |
| | | });</ClientEvent> |
| | |
| | | <Buttons> |
| | | <Button> |
| | | <ClientEvent name="onClick">var data = view.get("#dsQuery.data");
 |
| | | view.get("#ajaxTestWeight").set("parameter",data).execute(function(result){
 |
| | | view.get("#ajaxTestWeight").set("parameter",data.get("weight")).execute(function(result){
 |
| | | self.get("parent").hide();
 |
| | | $alert(result);
 |
| | | });</ClientEvent> |
| | |
| | | <Property name="label">设å¤å¯ç </Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="depotIdSys"> |
| | | <Property></Property> |
| | | <Property name="label">èªå®ä¹ä»åºç¼ç </Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="productId"> |
| | | <Property></Property> |
| | | <Property name="label">设å¤ç±»åKEY</Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="cableRule"> |
| | | <Property/> |
| | | <Property name="label">åºåºç³»ç»ä»åºç¼ç </Property> |
| | | <Property name="label">å¸çº¿è§å</Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="cableCir"> |
| | | <Property/> |
| | | <Property name="label">çä»å±è§å</Property> |
| | | </PropertyDef> |
| | | </DataType> |
| | | </Model> |
| | |
| | | <Property name="property">type</Property> |
| | | <Property name="align">center</Property> |
| | | </DataColumn> |
| | | <DataColumn name="productId"> |
| | | <Property name="property">productId</Property> |
| | | </DataColumn> |
| | | <DataColumn name="deviceId"> |
| | | <Property name="property">deviceId</Property> |
| | | <Property name="align">center</Property> |
| | |
| | | <Property name="closeable">false</Property> |
| | | <Buttons> |
| | | <Button> |
| | | <ClientEvent name="onClick">var cur = view.get("#dgMain").getCurrentItem();
 |
| | | view.get("#updateSave").execute(function(){
 |
| | | <ClientEvent name="onClick">view.get("#updateSave").execute(function(){
 |
| | | self.get("parent").hide();
 |
| | | });</ClientEvent> |
| | | <Property name="caption">ä¿åä¿®æ¹</Property> |
| | |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement> |
| | | <Property name="name">productId</Property> |
| | | <Property name="property">productId</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement> |
| | | <Property name="name">orgId</Property> |
| | | <Property name="property">orgId</Property> |
| | | <Editor/> |
| | |
| | | <AutoFormElement> |
| | | <Property name="name">depotId</Property> |
| | | <Property name="property">depotId</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement> |
| | | <Property name="name">depotIdSys</Property> |
| | | <Property name="property">depotIdSys</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement> |
| | |
| | | <Property name="property">httpUrl</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement> |
| | | <Property name="name">depotIdSys</Property> |
| | | <Property name="property">depotIdSys</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement layoutConstraint="colSpan:3"> |
| | | <Property name="name">remark</Property> |
| | | <Property name="property">remark</Property> |
| | | <Property name="editorType">TextArea</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement> |
| | | <Property name="name">cableRule</Property> |
| | | <Property name="property">cableRule</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement> |
| | | <Property name="name">cableCir</Property> |
| | | <Property name="property">cableCir</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | </AutoForm> |
| | |
| | | <Tools/> |
| | | </Dialog> |
| | | <UpdateAction id="updateSave"> |
| | | <Property name="dataResolver">gatewayDeviceService#updateSave</Property> |
| | | <Property name="dataResolver">gatewayDeviceService2#updateSave</Property> |
| | | <UpdateItem> |
| | | <Property name="dataPath">[#current]</Property> |
| | | <Property name="dataSet">dsMain</Property> |
| | | <Property name="alias">data</Property> |
| | | </UpdateItem> |
| | | </UpdateAction> |
| | | <AjaxAction id="ajaxDel"> |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.mqtt; |
| | | |
| | | import com.fzzy.gateway.hx2023.service.OnReceiveMqttService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; |
| | | import org.eclipse.paho.client.mqttv3.MqttCallback; |
| | | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class MqttConsumerCallBack implements MqttCallback { |
| | | |
| | | |
| | | @Autowired |
| | | private OnReceiveMqttService onReceiveMqttService; |
| | | |
| | | /** |
| | | * 客æ·ç«¯æå¼è¿æ¥çåè° |
| | | */ |
| | | @Override |
| | | public void connectionLost(Throwable throwable) { |
| | | log.info("䏿å¡å¨æå¼è¿æ¥ï¼å¯éè¿"); |
| | | } |
| | | |
| | | /** |
| | | * æ¶æ¯å°è¾¾çåè° |
| | | */ |
| | | @Override |
| | | public void messageArrived(String topic, MqttMessage message) throws Exception { |
| | | |
| | | |
| | | String messageStr = new String(message.getPayload()); |
| | | |
| | | |
| | | log.info(String.format("æ¥æ¶æ¶æ¯ä¸»é¢ : %s", topic)); |
| | | log.info(String.format("æ¥æ¶æ¶æ¯Qos : %d", message.getQos())); |
| | | log.info(String.format("æ¥æ¶æ¶æ¯å
容 : %s", messageStr)); |
| | | |
| | | log.info(String.format("æ¥æ¶æ¶æ¯retained : %b", message.isRetained())); |
| | | |
| | | onReceiveMqttService.onReceiveMessage(messageStr); |
| | | } |
| | | |
| | | /** |
| | | * æ¶æ¯å叿åçåè° |
| | | */ |
| | | @Override |
| | | public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { |
| | | log.info(String.format("æ¥æ¶æ¶æ¯æå")); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.mqtt; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.eclipse.paho.client.mqttv3.*; |
| | | import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | |
| | | @Configuration |
| | | @Slf4j |
| | | public class MqttConsumerConfig { |
| | | |
| | | @Autowired |
| | | private MqttProperties mqttProperties; |
| | | @Autowired |
| | | private MqttConsumerCallBack mqttConsumerCallBack; |
| | | |
| | | /** |
| | | * 客æ·ç«¯å¯¹è±¡ |
| | | */ |
| | | private MqttClient client; |
| | | |
| | | /** |
| | | * å¨beanåå§ååè¿æ¥å°æå¡å¨ |
| | | */ |
| | | @PostConstruct |
| | | public void init() { |
| | | connect(); |
| | | } |
| | | |
| | | /** |
| | | * 客æ·ç«¯è¿æ¥æå¡ç«¯ |
| | | */ |
| | | public void connect() { |
| | | try { |
| | | //å建MQTT客æ·ç«¯å¯¹è±¡ |
| | | client = new MqttClient(mqttProperties.getHost(), mqttProperties.getClientInId(), new MemoryPersistence()); |
| | | //è¿æ¥è®¾ç½® |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | //æ¯å¦æ¸
空sessionï¼è®¾ç½®ä¸ºfalse表示æå¡å¨ä¼ä¿ç客æ·ç«¯çè¿æ¥è®°å½ï¼å®¢æ·ç«¯éè¿ä¹åè½è·åå°æå¡å¨å¨å®¢æ·ç«¯æå¼è¿æ¥æé´æ¨éçæ¶æ¯ |
| | | //设置为trueè¡¨ç¤ºæ¯æ¬¡è¿æ¥å°æå¡ç«¯é½æ¯ä»¥æ°ç身份 |
| | | options.setCleanSession(true); |
| | | //è®¾ç½®è¿æ¥ç¨æ·å |
| | | options.setUserName(mqttProperties.getUsername()); |
| | | //è®¾ç½®è¿æ¥å¯ç |
| | | options.setPassword(mqttProperties.getPassword().toCharArray()); |
| | | //设置è¶
æ¶æ¶é´ï¼åä½ä¸ºç§ |
| | | options.setConnectionTimeout(10); |
| | | //设置å¿è·³æ¶é´ åä½ä¸ºç§ï¼è¡¨ç¤ºæå¡å¨æ¯é1.5*20ç§çæ¶é´å客æ·ç«¯åéå¿è·³å¤æå®¢æ·ç«¯æ¯å¦å¨çº¿ |
| | | options.setKeepAliveInterval(20); |
| | | //设置é屿¶æ¯çè¯é¢ï¼è¥å®¢æ·ç«¯åæå¡å¨ä¹é´çè¿æ¥æå¤æå¼ï¼æå¡å¨å°åå¸å®¢æ·ç«¯çéå±ä¿¡æ¯ |
| | | options.setWill("willTopic", (mqttProperties.getClientInId() + "䏿å¡å¨æå¼è¿æ¥").getBytes(), 0, false); |
| | | //设置åè° |
| | | // client.setCallback(new MqttConsumerCallBack()); |
| | | client.setCallback(mqttConsumerCallBack); |
| | | client.connect(options); |
| | | //订é
ä¸»é¢ |
| | | //æ¶æ¯ç级ï¼å䏻颿°ç»ä¸ä¸å¯¹åºï¼æå¡ç«¯å°æç
§æå®ç级ç»è®¢é
äºä¸»é¢ç客æ·ç«¯æ¨éæ¶æ¯ |
| | | int[] qos = {1, 1}; |
| | | //ä¸»é¢ |
| | | String[] topics = mqttProperties.getTopics().split(","); |
| | | //订é
ä¸»é¢ |
| | | client.subscribe(topics, qos); |
| | | } catch (MqttException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æå¼è¿æ¥ |
| | | */ |
| | | public void disConnect() { |
| | | try { |
| | | client.disconnect(); |
| | | } catch (MqttException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 订é
ä¸»é¢ |
| | | */ |
| | | public void subscribe(String topic, int qos) { |
| | | try { |
| | | client.subscribe(topic, qos); |
| | | } catch (MqttException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.mqtt; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | |
| | | @Controller |
| | | public class MqttController { |
| | | @Autowired |
| | | private MqttConsumerConfig client; |
| | | |
| | | @Autowired |
| | | private MqttProperties mqttProperties; |
| | | |
| | | @RequestMapping("/connect") |
| | | public @ResponseBody |
| | | String connect() { |
| | | client.connect(); |
| | | return mqttProperties.getClientOutId() + "è¿æ¥å°æå¡å¨"; |
| | | } |
| | | |
| | | @RequestMapping("/disConnect") |
| | | @ResponseBody |
| | | public String disConnect() { |
| | | client.disConnect(); |
| | | return mqttProperties.getClientOutId() + "䏿å¡å¨æå¼è¿æ¥"; |
| | | } |
| | | |
| | | } |
| | |
| | | package com.fzzy.mqtt; |
| | | |
| | | |
| | | import org.springframework.integration.annotation.MessagingGateway; |
| | | import org.springframework.integration.mqtt.support.MqttHeaders; |
| | | import org.springframework.messaging.handler.annotation.Header; |
| | |
| | | /** |
| | | * ç¨æ·å |
| | | */ |
| | | private String clientUsername; |
| | | private String username; |
| | | |
| | | /** |
| | | * å¯ç |
| | | */ |
| | | private String clientPassword; |
| | | private String password; |
| | | |
| | | |
| | | /** |
| | | * 客æ·ç«¯Idï¼åä¸å°æå¡å¨ä¸ï¼ä¸å
许åºç°éå¤ç客æ·ç«¯id |
| | | * 客æ·ç«¯Id-åå¸è
Id |
| | | */ |
| | | private String clientId; |
| | | private String clientOutId; |
| | | |
| | | /** |
| | | * 客æ·ç«¯Id-被订é
è
Id |
| | | */ |
| | | private String clientInId; |
| | | |
| | | |
| | | /** |
| | | * è¶
æ¶æ¶é´ |
| | | */ |
| | | private int clientTimeout = 5000; |
| | | private int timout = 5000; |
| | | |
| | | /** |
| | | * 设置ä¼è¯å¿è·³æ¶é´ åä½ä¸ºç§ æå¡å¨ä¼æ¯é1.5*20ç§çæ¶é´å客æ·ç«¯ |
| | | * åéä¸ªæ¶æ¯å¤æå®¢æ·ç«¯æ¯å¦å¨çº¿ï¼ä½è¿ä¸ªæ¹æ³å¹¶æ²¡æéè¿çæºå¶ |
| | | */ |
| | | private int clientAliveTime = 30000; |
| | | private int keepAliveInterval = 20; |
| | | |
| | | private int clientMaxConnectTime; |
| | | private int maxConnectTimes = 5; |
| | | |
| | | private String clientTopics; |
| | | private String topics; |
| | | |
| | | /** |
| | | * è¿æ¥æ¹å¼ |
| | | */ |
| | | private Integer clientQos = 0; |
| | | private Integer qos = 0; |
| | | |
| | | /** |
| | | * é»è®¤è¿æ¥ä¸»é¢ï¼ä»¥/#ç»å°¾è¡¨ç¤ºè®¢é
ææä»¥testå¼å¤´çä¸»é¢ |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.mqtt; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; |
| | | import org.eclipse.paho.client.mqttv3.MqttCallback; |
| | | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| | | |
| | | @Slf4j |
| | | public class MqttProviderCallBack implements MqttCallback { |
| | | |
| | | /** |
| | | * 客æ·ç«¯æå¼è¿æ¥çåè° |
| | | */ |
| | | @Override |
| | | public void connectionLost(Throwable throwable) { |
| | | System.out.println("䏿å¡å¨æå¼è¿æ¥ï¼å¯éè¿"); |
| | | } |
| | | |
| | | /** |
| | | * æ¶æ¯å°è¾¾çåè° |
| | | */ |
| | | @Override |
| | | public void messageArrived(String topic, MqttMessage message) throws Exception { |
| | | System.out.println(String.format("æ¥æ¶æ¶æ¯ä¸»é¢ : %s", topic)); |
| | | System.out.println(String.format("æ¥æ¶æ¶æ¯Qos : %d", message.getQos())); |
| | | System.out.println(String.format("æ¥æ¶æ¶æ¯å
容 : %s", new String(message.getPayload()))); |
| | | System.out.println(String.format("æ¥æ¶æ¶æ¯retained : %b", message.isRetained())); |
| | | } |
| | | |
| | | /** |
| | | * æ¶æ¯å叿åçåè° |
| | | */ |
| | | @Override |
| | | public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { |
| | | System.out.println(String.format("æ¥æ¶æ¶æ¯æå")); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.mqtt; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.eclipse.paho.client.mqttv3.*; |
| | | import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | |
| | | @Configuration |
| | | @Slf4j |
| | | public class MqttProviderConfig { |
| | | |
| | | @Autowired |
| | | private MqttProperties mqttProperties; |
| | | /** |
| | | * 客æ·ç«¯å¯¹è±¡ |
| | | */ |
| | | private MqttClient client; |
| | | |
| | | /** |
| | | * |
| | | * å¨beanåå§ååè¿æ¥å°æå¡å¨ |
| | | */ |
| | | @PostConstruct |
| | | public void init(){ |
| | | connect(); |
| | | } |
| | | |
| | | /** |
| | | * 客æ·ç«¯è¿æ¥æå¡ç«¯ |
| | | */ |
| | | public void connect(){ |
| | | try{ |
| | | //å建MQTT客æ·ç«¯å¯¹è±¡ |
| | | client = new MqttClient(mqttProperties.getHost(),mqttProperties.getClientOutId(),new MemoryPersistence()); |
| | | //è¿æ¥è®¾ç½® |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | //æ¯å¦æ¸
空sessionï¼è®¾ç½®false表示æå¡å¨ä¼ä¿ç客æ·ç«¯çè¿æ¥è®°å½ï¼è®¢é
主é¢ï¼qosï¼,客æ·ç«¯éè¿ä¹åè½è·åå°æå¡å¨å¨å®¢æ·ç«¯æå¼è¿æ¥æé´æ¨éçæ¶æ¯ |
| | | //设置为trueè¡¨ç¤ºæ¯æ¬¡è¿æ¥æå¡å¨é½æ¯ä»¥æ°ç身份 |
| | | options.setCleanSession(true); |
| | | //è®¾ç½®è¿æ¥ç¨æ·å |
| | | options.setUserName(mqttProperties.getUsername()); |
| | | //è®¾ç½®è¿æ¥å¯ç |
| | | options.setPassword(mqttProperties.getPassword().toCharArray()); |
| | | //设置è¶
æ¶æ¶é´ï¼åä½ä¸ºç§ |
| | | options.setConnectionTimeout(100); |
| | | //设置å¿è·³æ¶é´ åä½ä¸ºç§ï¼è¡¨ç¤ºæå¡å¨æ¯é 1.5*20ç§çæ¶é´å客æ·ç«¯åéå¿è·³å¤æå®¢æ·ç«¯æ¯å¦å¨çº¿ |
| | | options.setKeepAliveInterval(20); |
| | | //设置é屿¶æ¯çè¯é¢ï¼è¥å®¢æ·ç«¯åæå¡å¨ä¹é´çè¿æ¥æå¤æå¼ï¼æå¡å¨å°åå¸å®¢æ·ç«¯çéå±ä¿¡æ¯ |
| | | options.setWill("willTopic",(mqttProperties.getClientOutId() + "䏿å¡å¨æå¼è¿æ¥").getBytes(),0,false); |
| | | //设置åè° |
| | | client.setCallback(new MqttProviderCallBack()); |
| | | client.connect(options); |
| | | } catch(MqttException e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public void publish(String topic,String message){ |
| | | MqttMessage mqttMessage = new MqttMessage(); |
| | | mqttMessage.setQos(mqttProperties.getQos()); |
| | | mqttMessage.setRetained(true); |
| | | mqttMessage.setPayload(message.getBytes()); |
| | | //主é¢çç®çå°ï¼ç¨äºåå¸/订é
ä¿¡æ¯ |
| | | MqttTopic mqttTopic = client.getTopic(topic); |
| | | //æä¾ä¸ç§æºå¶æ¥è·è¸ªæ¶æ¯çä¼ éè¿åº¦ |
| | | //ç¨äºå¨ä»¥éé»å¡æ¹å¼ï¼å¨åå°è¿è¡ï¼æ§è¡å叿¯è·è¸ªæ¶æ¯çä¼ éè¿åº¦ |
| | | MqttDeliveryToken token; |
| | | try { |
| | | //尿宿¶æ¯åå¸å°ä¸»é¢ï¼ä½ä¸çå¾
æ¶æ¯ä¼ é宿ï¼è¿åçtokenå¯ç¨äºè·è¸ªæ¶æ¯çä¼ éç¶æ |
| | | //䏿¦æ¤æ¹æ³å¹²åå°è¿åï¼æ¶æ¯å°±å·²è¢«å®¢æ·ç«¯æ¥ååå¸ï¼å½è¿æ¥å¯ç¨ï¼å°å¨åå°å®ææ¶æ¯ä¼ éã |
| | | token = mqttTopic.publish(mqttMessage); |
| | | token.waitForCompletion(); |
| | | } catch (MqttException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public void publish(int qos,boolean retained,String topic,String message){ |
| | | MqttMessage mqttMessage = new MqttMessage(); |
| | | mqttMessage.setQos(qos); |
| | | mqttMessage.setRetained(retained); |
| | | mqttMessage.setPayload(message.getBytes()); |
| | | //主é¢çç®çå°ï¼ç¨äºåå¸/订é
ä¿¡æ¯ |
| | | MqttTopic mqttTopic = client.getTopic(topic); |
| | | //æä¾ä¸ç§æºå¶æ¥è·è¸ªæ¶æ¯çä¼ éè¿åº¦ |
| | | //ç¨äºå¨ä»¥éé»å¡æ¹å¼ï¼å¨åå°è¿è¡ï¼æ§è¡å叿¯è·è¸ªæ¶æ¯çä¼ éè¿åº¦ |
| | | MqttDeliveryToken token; |
| | | try { |
| | | //尿宿¶æ¯åå¸å°ä¸»é¢ï¼ä½ä¸çå¾
æ¶æ¯ä¼ é宿ï¼è¿åçtokenå¯ç¨äºè·è¸ªæ¶æ¯çä¼ éç¶æ |
| | | //䏿¦æ¤æ¹æ³å¹²åå°è¿åï¼æ¶æ¯å°±å·²è¢«å®¢æ·ç«¯æ¥ååå¸ï¼å½è¿æ¥å¯ç¨ï¼å°å¨åå°å®ææ¶æ¯ä¼ éã |
| | | token = mqttTopic.publish(mqttMessage); |
| | | token.waitForCompletion(); |
| | | } catch (MqttException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | } |
| | |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | @RestController |
| | | public class MqttPubController { |
| | | |
| | | @Autowired |
| | | private MqttGatewayService gatewayService; |
| | | private MqttProviderConfig providerClient; |
| | | |
| | | |
| | | @RequestMapping("/hello") |
| | | public String hello() { |
| | | return "hello!"; |
| | | @RequestMapping("/sendMessage") |
| | | public @ResponseBody |
| | | String sendMessage(String topic, String message) { |
| | | try { |
| | | providerClient.publish(topic, message); |
| | | return "åéæå"; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return "åé失败"; |
| | | } |
| | | |
| | | @RequestMapping("/sendMqtt") |
| | | public String sendMqtt(String sendData) { |
| | | System.out.println(sendData); |
| | | System.out.println("è¿å
¥sendMqtt-------" + sendData); |
| | | gatewayService.sendToMqtt("topic01", (String) sendData); |
| | | return "Test is OK"; |
| | | } |
| | | |
| | | @RequestMapping("/sendMqttTopic") |
| | | public String sendMqtt(String sendData, String topic) { |
| | | //System.out.println(sendData+" "+topic); |
| | | //System.out.println("è¿å
¥inboundåéï¼"+sendData); |
| | | gatewayService.sendToMqtt(topic, (String) sendData); |
| | | return "Test is OK"; |
| | | } |
| | | |
| | | } |
| | |
| | | |
| | | @Service |
| | | public class MqttPublishService { |
| | | |
| | | private static MqttClient client ; |
| | | |
| | | |
| | |
| | | auto-offset-reset: latest |
| | | key-deserializer: org.apache.kafka.common.serialization.StringDeserializer |
| | | value-deserializer: org.apache.kafka.common.serialization.StringDeserializer |
| | | |
| | | mqtt: |
| | | host: tcp://10.13.4.84:11883 |
| | | client-id: |
| | | client-username: |
| | | client-password: |
| | | client-timeout: 10 |
| | | client-alive-time: 20 |
| | | client-max-connect-times: 5 |
| | | client-topics: |
| | | client-qos: 0 |
| | | isOpen: false |
| | |
| | | value-deserializer: org.apache.kafka.common.serialization.StringDeserializer |
| | | |
| | | mqtt: |
| | | host: tcp://10.13.4.84:11883 |
| | | client-id: |
| | | client-username: admin |
| | | client-password: 123456 |
| | | client-timeout: 10 |
| | | client-alive-time: 20 |
| | | client-max-connect-times: 5 |
| | | client-topics: "/device/${productId}/${deviceId}/message/property/report,/device-message-sender/${productId}/${deviceId}" |
| | | client-qos: 0 |
| | | host: tcp://127.0.0.1:1883 |
| | | username: admin |
| | | password: pwdmqtt.. |
| | | client-outId: fzzy-customer-id |
| | | client-inId: fzzy-6e3d92ff71b911eea5e50250f2000002 |
| | | timeout: 10 |
| | | keep-alive-interval: 20 |
| | | max-connect-times: 5 |
| | | qos: 0 |
| | | isOpen: false |
| | | default: |
| | | topic: testTopic |
| | | topics: "/+/+/properties/report,/device-message-sender/+/+" |