20260328:数据推送:优先推送红->黄->绿

This commit is contained in:
George 2026-03-28 16:28:33 +08:00
parent eb45296a57
commit 518107ac04
4 changed files with 52 additions and 23 deletions

View File

@ -48,6 +48,7 @@ public class RedisKeyBuilder {
/**
* 物模型值命名缓存key
* Key:TSLV:{productId}_{deviceNumber} HKey:{identity#V/identity#S/identity#M/identity#N}
* TSLV:209_508349495930
*/
public static String buildTSLVCacheKey(Long productId, String serialNumber) {
return FastBeeConstant.REDIS.DEVICE_PRE_KEY + productId + "_" + serialNumber.toUpperCase();

View File

@ -1,5 +1,7 @@
package com.fastbee.common.enums;
import org.apache.commons.lang3.StringUtils;
/**
* 物模型数据等级枚举
* 绿色=正常,黄色=告警,红色=警报
@ -77,8 +79,8 @@ public enum DataLevelEnum {
if (min == null && max == null) {
return false;
}
boolean lowerOk = (min == null) || v >= Double.parseDouble(min);
boolean upperOk = (max == null) || v <= Double.parseDouble(max);
boolean lowerOk = StringUtils.isBlank(min) || v >= Double.parseDouble(min);
boolean upperOk = StringUtils.isBlank(max) || v <= Double.parseDouble(max);
return lowerOk && upperOk;
}
}

View File

@ -34,6 +34,7 @@ import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -51,6 +52,8 @@ public class ThingsModelPushHandler {
private static final RestTemplate REST_TEMPLATE = new RestTemplate();
private static final Pattern VAR_PATTERN = Pattern.compile("\\{\\{([^}]+)}}");
private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Resource
private IThingsModelPushConfigService pushConfigService;
@ -156,25 +159,38 @@ public class ThingsModelPushHandler {
} catch (Exception e) {
value = cacheValue;
}
// 没有值 直接跳过
if (StringUtils.isEmpty(value)) {
log.warn("[Push] configId={} sn={} item={} value={}为空,跳过",
config.getId(), serialNumber, item.getIdentifier(), value);
continue;
}
DataLevelEnum level = DataLevelEnum.evaluate(
value, item.getGreenMin(), item.getGreenMax(),
item.getYellowMin(), item.getYellowMax());
String displayName = StringUtils.isNotEmpty(item.getModelName())
? item.getModelName() : item.getIdentifier();
String text = displayName + "=" + value
+ (StringUtils.isNotEmpty(item.getUnit()) ? item.getUnit() : "");
String text = item.getIdentifier() + "=" + value;
//+ (StringUtils.isNotEmpty(item.getUnit()) ? item.getUnit() : "");
levelTextMap.get(level).add(text);
}
for (Map.Entry<DataLevelEnum, List<String>> entry : levelTextMap.entrySet()) {
if (CollectionUtils.isEmpty(entry.getValue())) {
continue;
// 优先发送 红 ——>> 黄 ——>? 蓝
DataLevelEnum chosen = levelTextMap.entrySet().stream()
.filter(e -> CollectionUtils.isNotEmpty(e.getValue()))
.map(Map.Entry::getKey)
.max(Comparator.comparingInt(DataLevelEnum::getCode))
.orElse(null);
if (chosen == null) {
log.debug("[Push] configId={} sn={} 无有效分级数据,跳过发送",
config.getId(), serialNumber);
return;
}
DataLevelEnum level = entry.getKey();
String dataText = String.join(";", entry.getValue());
sendTemplateMsg(config, msgParams, serialNumber, level, dataText, pushUrl, openIdMap);
String dataText = String.join(";", levelTextMap.get(chosen));
if (dataText.length() > 32) {
dataText = dataText.substring(0, 29) + "...";
}
sendTemplateMsg(config, msgParams, serialNumber, chosen, dataText, pushUrl, openIdMap);
}
/**
@ -187,26 +203,24 @@ public class ThingsModelPushHandler {
String pushUrl, Map<Long, String> openIdMap) {
List<String> varNames = extractVarNames(msgParams.getContent());
LinkedHashMap<String, String> map = new LinkedHashMap<>();
String deviceDisplay = StringUtils.isNotEmpty(config.getDeviceName())
? config.getDeviceName() : serialNumber;
Map<String, String> map = new LinkedHashMap<>();
for (int i = 0; i < varNames.size(); i++) {
String varName = varNames.get(i);
if (i == 0) {
map.put(varName, deviceDisplay);
map.put(varName, serialNumber);
} else if (i == 1) {
map.put(varName, level.getLabel());
} else if (i == 2) {
map.put(varName, dataText);
} else {
map.put(varName, "");
map.put(varName, simpleDateFormat.format(new Date()));
}
}
// 与微信模板消息要求保持一致的 map 迭代方式
Map<String, Object> sendMap = new LinkedHashMap<>();
for (Map.Entry<String, String> m : map.entrySet()) {
sendMap.put(m.getKey(), new TemplateDataVo(m.getValue(), level.getColor()));
sendMap.put(m.getKey(), new TemplateDataVo(m.getValue()));
}
for (Map.Entry<Long, String> userEntry : openIdMap.entrySet()) {
@ -216,7 +230,7 @@ public class ThingsModelPushHandler {
vo.setTemplateId(msgParams.getTemplateId());
vo.setData(sendMap);
if (StringUtils.isNotEmpty(msgParams.getRedirectUrl())) {
vo.setUrl(msgParams.getRedirectUrl());
vo.setUrl(msgParams.getRedirectUrl() + "pagesA/home/device/index?deviceId=" + config.getDeviceId());
}
try {
NotifySendResponse resp = doPost(JSON.toJSONString(vo), pushUrl);
@ -252,10 +266,22 @@ public class ThingsModelPushHandler {
String body = result.getBody();
resp.setResultContent(body);
try {
resp.setStatus("0".equals(JSONObject.parseObject(body).get("errcode").toString()) ? 1 : 0);
JSONObject responseJson = JSONObject.parseObject(body);
boolean errcode = "0".equals(responseJson.get("errcode").toString());
if (!errcode) {
log.warn("[Call Wechat Request] json={}", JSON.toJSONString(json));
log.warn("[Call Wechat Response] body={}", responseJson);
}
resp.setStatus(errcode ? 1 : 0);
} catch (Exception e) {
resp.setStatus(0);
}
return resp;
}
public static void main(String[] args) {
String value = "WaterT=1.76;WaterVol=-6.34;T=20";
String value1 = "[修正]参术儿康糖浆10ml*10支1盒装儿童小...";
System.out.println("value.length() = " + value.length());
}
}

View File

@ -53,7 +53,7 @@ public class ThingsModelPushJob {
* push_interval=0 时不自动推送(仅手动)
*/
private boolean isDue(ThingsModelPushConfig config, long nowMs) {
if (config.getPushInterval() == null || config.getPushInterval() <= 0) {
if (config.getPushInterval() == null || config.getPushInterval() <= 0 || config.getStatus() == 0) {
return false;
}
Date lastPush = config.getLastPushTime();