Commit dab157db9b97ebd8b55d0657ec75c05b66f29361

Authored by 杨鸣坤
1 parent 7ee8a0dd

池州经开区设备上报

  1 +package com.iot.scheduler.service.chizhou;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.iot.scheduler.utils.MqttUtils;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import org.springframework.stereotype.Service;
  7 +
  8 +import java.text.MessageFormat;
  9 +import java.text.SimpleDateFormat;
  10 +import java.util.*;
  11 +
  12 +/**
  13 + * 池州经开区数据上传
  14 + */
  15 +@Slf4j
  16 +@Service
  17 +public class CzDeviceReportService {
  18 +
  19 + String broker = "112.30.143.137:5007";
  20 + String username = "admin";
  21 + String password = "ly@666yc";
  22 +
  23 + public void deviceReport() {
  24 + List<String> deviceIdList = Arrays.asList("TCKJ-001", "TCKJ-002", "TCKJ-003", "TCKJ-004", "TCKJ-005", "TCKJ-006",
  25 + "TCKJ-007", "TCKJ-008", "TCKJ-009", "TCKJ-010", "TCKJ-011", "TCKJ-012", "TCKJ-013", "TCKJ-014", "TCKJ-015",
  26 + "TCKJ-016", "TCKJ-017", "TCKJ-018", "TCKJ-019", "TCKJ-020", "TCKJ-021", "TCKJ-022", "TCKJ-023", "TCKJ-024",
  27 + "TCKJ-025", "TCKJ-026", "TCKJ-027", "TCKJ-028", "TCKJ-029", "TCKJ-030", "TCKJ-031", "TCKJ-032", "TCKJ-033",
  28 + "TCKJ-034", "TCKJ-035", "TCKJ-036", "TCKJ-037", "TCKJ-038", "TCKJ-039", "TCKJ-040", "TCKJ-041",
  29 + "vJAzZWk1Q2I8cdq7kx3z", "LmiW3ljI4KP487CeMmol", "ceK7YSNLrTjVSSPaFmn3", "6KePBYyCiEgo10iJXIrF",
  30 + "9vrxIQhbaZ542DvCVne0", "XMLN-001", "XMLN-002", "XMLN-003", "XMLN-004", "XMLN-005", "XMLN-006", "XMLN-007",
  31 + "XMLN-008", "XMLN-009", "XMLN-010", "XMLN-011", "XMLN-012", "XMLN-013", "XMLN-014", "XMLN-015", "XMLN-016",
  32 + "XMLN-017", "XMLN-018", "XMLN-019", "XMLN-020", "XMLN-021", "XMLN-022", "XMLN-023", "XMLN-024", "XMLN-025",
  33 + "XMLN-026", "XMLN-027", "XMLN-028", "XMLN-029", "XMLN-030", "XMLN-031", "XMLN-032", "XMLN-033", "XMLN-034",
  34 + "XMLN-035", "XMLN-036", "XMLN-037", "XMLN-038", "XMLN-039", "XMLN-040", "XMLN-041", "XMLN-042", "XMLN-043",
  35 + "XMLN-044", "XMLN-045", "XMLN-046", "XMLN-047", "XMLN-048", "XMLN-049", "XMLN-050", "XMLN-051", "XMLN-052",
  36 + "GYDPF-014", "GYDPF-006", "KBS-1-1", "KBS-1-2", "KBS-3-1", "KBS-3-5", "KBS-3-3", "CS-001", "uzGf4mjzkOPwGO4aCOf3",
  37 + "GYDPF-001", "GYDPF-010", "KBS-1-5", "KBS-2-2", "KBS-3-7", "CS-003", "LBKXrYxLQN9W7qmmqzp5", "rba7VggnqGdNyPvVNs1n",
  38 + "GYDPF-002", "GYDPF-016", "GYDPF-017", "KBS-1-6", "EqFMlJarb2aDwpwvUi6J", "x2173A2HKOiPqeRsKtWY", "GYDPF-005",
  39 + "KBS-1-3", "KBS-2-1", "KBS-3-4", "1fzR6X57k2aafbbkH4po", "lbqMr5pmkNJLWEPSa0yp", "mYz1kUajPOROg0Ewcfhr",
  40 + "6TLlfKBbTMaUZGgHtvuH", "XTE8PKwLSYp29gE1umfW", "MqYu4y0OycHQkzZhothe", "IFeN0lAXlAVQ1fD7gwk1", "hZb9NsRCqtVBSagFqnPS",
  41 + "r22r53PNgY7TeqCK5y14", "KCGSwfTvGCtBfd0qsH63", "4KzFQCncmwFh7UT4vbHp", "GYDPF-007", "KBS-3-2", "t2kkUTZYqxmKLN4X4VPB",
  42 + "9vWKALGoat3XalEtQaRB", "vYtwLMkppPbM3CtB3U9U", "4vwTfMqi8oEtWxdEUrE8", "vVvK2cTLvjYtZX1dksyd", "GYDPF-003",
  43 + "KBS-3-8", "2E6eUu66qxl5Llr6ib5v", "iWbeBfwg36lIpBRMDnvO", "GYDPF-015", "KBS-2-4", "OSbA7X7FGETF0TXub4OS",
  44 + "GYDPF-008", "KBS-2-3", "KBS-2-7", "pRhGGECbXu6FBshyg2yg", "zrVVNRo8QIbjeYjdoCUq", "fjPj3oHW7cL5m0AE1t3y",
  45 + "8dJsgqjmxIIduP4Sr2rL", "GYDPF-012", "KBS-1-4", "JZ8Tbzye8rKotYTbHUJw", "dwToSYEWr04sPtBUxaOH", "GYDPF-009",
  46 + "oe3OmagW5Wi9yw9LfUr3", "2mDiH94D7uSh4CtD2bi0", "ceGyDxlK7xzMoUacIwCv", "gxECiKjMEsVkYyowKIMd", "irAqF3fmzaM5GIo8ZLAx",
  47 + "GYDPF-013", "KBS-2-5", "KBS-3-6", "dxPdbg2UqqYADPMy9TVe", "cjFUpeVPTOh4Uf6wnVH1", "VR5XgpqhFWGMcwdy8yPQ",
  48 + "GYDPF-004", "KBS-1-7", "KBS-2-8", "HEXmiSz0sXsahRHV2bQm", "0lIIGWP49Xuo91TnTvVx", "C99WsNGFSGQSV4n3YmrW",
  49 + "3paJSiHK32wvVM6f1tO8", "GYDPF-011", "KBS-1-8", "KBS-2-6", "yrk5Cxa6EJncWX2kTywa", "xOxHHscmrEBhLTlmyRrF",
  50 + "e3HzLoIl0p3iEM860EdK");
  51 +
  52 + log.info("总设备数量: {}台", deviceIdList.size());
  53 +
  54 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  55 + String formattedDate = sdf.format(new Date());
  56 + log.info("上报时间统一为: {}", formattedDate);
  57 +
  58 + // 用于统计状态分布
  59 + Map<Integer, Integer> statusCountMap = new HashMap<>();
  60 + statusCountMap.put(1, 0);
  61 + statusCountMap.put(2, 0);
  62 + statusCountMap.put(3, 0);
  63 + statusCountMap.put(4, 0);
  64 +
  65 + // 用于统计上报结果
  66 + int successCount = 0;
  67 + int failCount = 0;
  68 + List<String> failedDevices = new ArrayList<>();
  69 +
  70 + // 用于记录不同clientId的设备数量
  71 + int clientId1Count = 0;
  72 + int clientId2Count = 0;
  73 + int clientId3Count = 0;
  74 +
  75 + for (int index = 0; index < deviceIdList.size(); index++) {
  76 + String deviceId = deviceIdList.get(index);
  77 +
  78 + // 根据index确定clientId
  79 + String clientId = "2020672015207559169";
  80 + if (index > 45 && index <= 97) {
  81 + clientId = "2020672119054331904";
  82 + clientId2Count++;
  83 + } else if (index > 97) {
  84 + clientId = "2020672228886376448";
  85 + clientId3Count++;
  86 + } else {
  87 + clientId1Count++;
  88 + }
  89 +
  90 + String topic = MessageFormat.format("/{0}/{1}/properties/report", clientId, deviceId);
  91 +
  92 + // 获取设备状态
  93 + int deviceState = getDeviceState();
  94 +
  95 + // 统计状态分布
  96 + statusCountMap.put(deviceState, statusCountMap.get(deviceState) + 1);
  97 +
  98 + Map<String, Object> properties = new HashMap<>(5);
  99 + properties.put("type", 1);
  100 + properties.put("state", deviceState);
  101 + properties.put("time", formattedDate);
  102 + properties.put("output", 0); // 当日产量,还未确定,暂定为0
  103 + properties.put("energy", 0); // 当日能耗,还未确定,暂定为0
  104 +
  105 + Map<String, Object> deviceMap = new HashMap<>(2);
  106 + deviceMap.put("deviceId", deviceId);
  107 + deviceMap.put("properties", properties);
  108 +
  109 + // 记录调试信息
  110 +// if (log.isDebugEnabled()) {
  111 +// log.debug("准备上报设备信息: deviceId={}, clientId={}, state={}, topic={}",
  112 +// deviceId, clientId, deviceState, topic);
  113 +// }
  114 +
  115 + try {
  116 + log.debug("开始MQTT发布: deviceId={}", deviceId);
  117 + MqttUtils.quickPublish(broker, topic, username, password, clientId, JSON.toJSONString(deviceMap));
  118 + successCount++;
  119 +
  120 + // 每10个设备记录一次进度
  121 +// if ((index + 1) % 10 == 0) {
  122 +// log.info("已处理 {} 个设备,当前处理: {}", index + 1, deviceId);
  123 +// }
  124 +
  125 + } catch (Exception e) {
  126 + failCount++;
  127 + failedDevices.add(deviceId);
  128 + log.error("设备上报失败: deviceId={}, clientId={}, topic={}, 错误信息: {}",
  129 + deviceId, clientId, topic, e.getMessage());
  130 + log.error("详细异常信息:", e);
  131 + }
  132 +
  133 + // 可选:添加延迟,避免发送过快
  134 + try {
  135 + Thread.sleep(10); // 10毫秒延迟
  136 + } catch (InterruptedException e) {
  137 + log.warn("线程休眠被中断", e);
  138 + Thread.currentThread().interrupt();
  139 + }
  140 + }
  141 +
  142 + // 任务完成,输出统计信息
  143 + log.info("========== 设备状态上报任务完成 ==========");
  144 + log.info("任务完成时间: {}", new Date());
  145 + log.info("设备总数: {}台", deviceIdList.size());
  146 + log.info("成功上报: {}台", successCount);
  147 + log.info("失败上报: {}台", failCount);
  148 +
  149 + if (failCount > 0) {
  150 + log.warn("失败设备列表: {}", failedDevices);
  151 + }
  152 +
  153 + // 输出clientId分布
  154 + log.info("clientId分布统计:");
  155 + log.info(" clientId-2020672015207559169: {}台", clientId1Count);
  156 + log.info(" clientId-2020672119054331904: {}台", clientId2Count);
  157 + log.info(" clientId-2020672228886376448: {}台", clientId3Count);
  158 +
  159 + // 输出状态分布统计
  160 + log.info("设备状态分布统计 (按8:1:0.5:0.5比例):");
  161 + log.info(" 状态1(80%): {}台 (占比: {}%)",
  162 + statusCountMap.get(1),
  163 + String.format("%.1f", statusCountMap.get(1) * 100.0 / deviceIdList.size()));
  164 + log.info(" 状态2(10%): {}台 (占比: {}%)",
  165 + statusCountMap.get(2),
  166 + String.format("%.1f", statusCountMap.get(2) * 100.0 / deviceIdList.size()));
  167 + log.info(" 状态3(5%): {}台 (占比: {}%)",
  168 + statusCountMap.get(3),
  169 + String.format("%.1f", statusCountMap.get(3) * 100.0 / deviceIdList.size()));
  170 + log.info(" 状态4(5%): {}台 (占比: {}%)",
  171 + statusCountMap.get(4),
  172 + String.format("%.1f", statusCountMap.get(4) * 100.0 / deviceIdList.size()));
  173 +
  174 + // 计算实际比例
  175 + double ratio1 = statusCountMap.get(1) / (double) statusCountMap.get(2);
  176 + double ratio3 = statusCountMap.get(3) / (double) statusCountMap.get(2);
  177 + double ratio4 = statusCountMap.get(4) / (double) statusCountMap.get(2);
  178 + log.info("实际比例: {} : 1 : {} : {}",
  179 + String.format("%.1f", ratio1),
  180 + String.format("%.1f", ratio3),
  181 + String.format("%.1f", ratio4));
  182 +
  183 + // 任务执行耗时
  184 + log.info("========== 任务执行结束 ==========");
  185 + }
  186 +
  187 + private int getDeviceState() {
  188 + Random random = new Random();
  189 + double rand = random.nextDouble();
  190 + int status;
  191 +
  192 + if (rand < 0.8) {
  193 + status = 1; // 80%概率
  194 + } else if (rand < 0.9) {
  195 + status = 2; // 10%概率
  196 + } else if (rand < 0.95) {
  197 + status = 3; // 5%概率
  198 + } else {
  199 + status = 4; // 5%概率
  200 + }
  201 +
  202 + // 调试日志
  203 + if (log.isTraceEnabled()) {
  204 + log.trace("生成设备状态: random={}, status={}", rand, status);
  205 + }
  206 +
  207 + return status;
  208 + }
  209 +}
  1 +package com.iot.scheduler.zone;
  2 +
  3 +import com.iot.scheduler.service.chizhou.CzDeviceReportService;
  4 +import com.iot.scheduler.task.AbstractZoneScheduler;
  5 +import jakarta.annotation.Resource;
  6 +import lombok.extern.slf4j.Slf4j;
  7 +import org.springframework.scheduling.annotation.Scheduled;
  8 +import org.springframework.stereotype.Component;
  9 +
  10 +/**
  11 + * 池州经开区设备同步
  12 + */
  13 +@Slf4j
  14 +@Component
  15 +public class ChiZoneScheduler extends AbstractZoneScheduler {
  16 +
  17 + @Resource
  18 + private CzDeviceReportService czDeviceReportService;
  19 +
  20 + @Override
  21 + protected String getZoneName() {
  22 + return "Chizhou (池州经开区)";
  23 + }
  24 +
  25 + @Scheduled(cron = "${scheduler.chizhou.push:0 0/10 * * * ?}")
  26 + public void pushJmDevicesToThirdParty() {
  27 + String taskName = "PJ Devices (IoT -> 3rd Party)";
  28 + logStart(taskName);
  29 + try {
  30 + log.info("[{}] PJ pushing devices...", getZoneName());
  31 + czDeviceReportService.deviceReport();
  32 + Thread.sleep(1000);
  33 + } catch (Exception e) {
  34 + logError(taskName, e);
  35 + } finally {
  36 + logEnd(taskName);
  37 + }
  38 + }
  39 +}
@@ -24,10 +24,10 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler { @@ -24,10 +24,10 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler {
24 24
25 @Override 25 @Override
26 protected String getZoneName() { 26 protected String getZoneName() {
27 - return "Chizhou (池州经开区)"; 27 + return "Huaining (怀宁经开区)";
28 } 28 }
29 29
30 - @Scheduled(cron = "${scheduler.chizhou.pull:0 0/10 * * * ?}") 30 + @Scheduled(cron = "${scheduler.huaining.pull:0 0/10 * * * ?}")
31 public void pullDevicesFromThirdParty() { 31 public void pullDevicesFromThirdParty() {
32 String taskName = "Pull Devices (3rd Party -> IoT)"; 32 String taskName = "Pull Devices (3rd Party -> IoT)";
33 logStart(taskName); 33 logStart(taskName);
@@ -46,7 +46,7 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler { @@ -46,7 +46,7 @@ public class ChizhouZoneScheduler extends AbstractZoneScheduler {
46 } 46 }
47 } 47 }
48 48
49 - @Scheduled(cron = "${scheduler.chizhou.push:0 0/15 * * * ?}") 49 + @Scheduled(cron = "${scheduler.huaining.push:0 0/15 * * * ?}")
50 public void pushDevicesToThirdParty() { 50 public void pushDevicesToThirdParty() {
51 String taskName = "Push Devices (IoT -> 3rd Party)"; 51 String taskName = "Push Devices (IoT -> 3rd Party)";
52 logStart(taskName); 52 logStart(taskName);
@@ -24,10 +24,10 @@ logging: @@ -24,10 +24,10 @@ logging:
24 # Scheduler Configuration (Cron Expressions) 24 # Scheduler Configuration (Cron Expressions)
25 # Format: sec min hour day month day-of-week 25 # Format: sec min hour day month day-of-week
26 scheduler: 26 scheduler:
27 - huaining: 27 + chizhou:
28 pull: "0 0/5 * * * ?" # Every 5 minutes 28 pull: "0 0/5 * * * ?" # Every 5 minutes
29 push: "0 0/10 * * * ?" # Every 10 minutes 29 push: "0 0/10 * * * ?" # Every 10 minutes
30 - chizhou: 30 + huaining:
31 pull: "0 0/1 * * * ?" 31 pull: "0 0/1 * * * ?"
32 push: "0 0/1 * * * ?" 32 push: "0 0/1 * * * ?"
33 panji: 33 panji: