|
@@ -13,9 +13,11 @@ import com.template.model.Devices;
|
|
|
import com.template.common.result.CommonResult;
|
|
import com.template.common.result.CommonResult;
|
|
|
import com.template.services.DevicesService;
|
|
import com.template.services.DevicesService;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
@@ -34,6 +36,7 @@ import java.util.TimerTask;
|
|
|
* @since 2024-04-25
|
|
* @since 2024-04-25
|
|
|
*/
|
|
*/
|
|
|
@RestController
|
|
@RestController
|
|
|
|
|
+@Slf4j
|
|
|
@RequestMapping("/api/devicePublic")
|
|
@RequestMapping("/api/devicePublic")
|
|
|
public class DevicePublicController {
|
|
public class DevicePublicController {
|
|
|
|
|
|
|
@@ -54,21 +57,37 @@ public class DevicePublicController {
|
|
|
public CommonResult publicDeviceMethod() throws UnsupportedEncodingException {
|
|
public CommonResult publicDeviceMethod() throws UnsupportedEncodingException {
|
|
|
System.out.println("设备开始发布");
|
|
System.out.println("设备开始发布");
|
|
|
List<Devices> devicesList =devicesService.list(new QueryWrapper<>());
|
|
List<Devices> devicesList =devicesService.list(new QueryWrapper<>());
|
|
|
|
|
+ int count =devicesList.size();
|
|
|
if(devicesList != null && devicesList.size() > 0){
|
|
if(devicesList != null && devicesList.size() > 0){
|
|
|
//修改定时任务创建方式
|
|
//修改定时任务创建方式
|
|
|
Timer[] timers=new Timer[devicesList.size()];
|
|
Timer[] timers=new Timer[devicesList.size()];
|
|
|
|
|
+// 任务计数
|
|
|
int i=0;
|
|
int i=0;
|
|
|
|
|
+ //重新发布前先清空timer任务
|
|
|
|
|
+ if (TimerMap.timerList.size()>0){
|
|
|
|
|
+ for(Timer timer:TimerMap.timerList){
|
|
|
|
|
+ timer.cancel();
|
|
|
|
|
+ }
|
|
|
|
|
+ TimerMap.timerList.clear();
|
|
|
|
|
+ }
|
|
|
|
|
+ long delay=1000;
|
|
|
|
|
+ String topicOld="";
|
|
|
|
|
+ String topicNew="";
|
|
|
for (Devices devices:devicesList){
|
|
for (Devices devices:devicesList){
|
|
|
- String command = devices.getDeviceSerial();
|
|
|
|
|
- String topic = devices.getName()+"_GET";
|
|
|
|
|
- String outTopic = devices.getName()+"_PUB";
|
|
|
|
|
|
|
+ String command = devices.getDeviceNo()+"030AF1000E";
|
|
|
|
|
+ topicOld=topicNew;
|
|
|
|
|
+ String topic = devices.getTopic()+"_GET";
|
|
|
|
|
+ topicNew=topic;
|
|
|
|
|
+ String outTopic = devices.getTopic()+"_PUB";
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
- if (topic==null || topic.trim().length()==0){
|
|
|
|
|
- System.out.println("topic不能为空,command:"+command+";topic:"+topic);
|
|
|
|
|
|
|
+ if (!StringUtils.hasText(devices.getTopic())){
|
|
|
|
|
+ log.info("topic不能为空,command:"+devices.getDeviceNo()+";topic:"+devices.getTopic());
|
|
|
|
|
+ count--;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
- if (command==null || command.trim().length()==0){
|
|
|
|
|
- System.out.println("command不能为空,command:"+command+";topic:"+topic);
|
|
|
|
|
|
|
+ if (!StringUtils.hasText(devices.getDeviceNo())){
|
|
|
|
|
+ System.out.println("command不能为空,command:"+devices.getDeviceNo()+";topic:"+topic);
|
|
|
|
|
+ count--;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
// if (command.length()!=12){
|
|
// if (command.length()!=12){
|
|
@@ -79,34 +98,32 @@ public class DevicePublicController {
|
|
|
//可能需要在这里加一次订阅
|
|
//可能需要在这里加一次订阅
|
|
|
//mqttPushClient.subscribe("outTopic",1);//只需要订阅一次 订阅多次会返回多条数据
|
|
//mqttPushClient.subscribe("outTopic",1);//只需要订阅一次 订阅多次会返回多条数据
|
|
|
int[] Qos={1};
|
|
int[] Qos={1};
|
|
|
- command="010310000010";
|
|
|
|
|
String CRC = CRCGenerator.getCRC16_Modbus_Str(command);
|
|
String CRC = CRCGenerator.getCRC16_Modbus_Str(command);
|
|
|
- command = command+" "+CRC;
|
|
|
|
|
|
|
+ command = command+CRC;
|
|
|
//command = HexStringUtils.toHex(command.replaceAll(" ","")).replaceAll(" ", "");
|
|
//command = HexStringUtils.toHex(command.replaceAll(" ","")).replaceAll(" ", "");
|
|
|
byte[] command2 = Hex.hexStr2Str(command.replaceAll(" ",""));
|
|
byte[] command2 = Hex.hexStr2Str(command.replaceAll(" ",""));
|
|
|
- //任务数大于或等于设备数量时清空timer任务
|
|
|
|
|
- if (TimerMap.timerList.size()>=devicesList.size()){
|
|
|
|
|
- for(Timer timer:TimerMap.timerList){
|
|
|
|
|
- timer.cancel();
|
|
|
|
|
- }
|
|
|
|
|
- TimerMap.timerList.clear();
|
|
|
|
|
- }
|
|
|
|
|
TimerMap.timerList.add(new Timer());
|
|
TimerMap.timerList.add(new Timer());
|
|
|
timers[i] = TimerMap.timerList.get(i);
|
|
timers[i] = TimerMap.timerList.get(i);
|
|
|
//消息中心软件默认六分钟 所以不满足现有需求 得做定时:每五秒执行一次任务
|
|
//消息中心软件默认六分钟 所以不满足现有需求 得做定时:每五秒执行一次任务
|
|
|
TimerTask task = new TimerTask() {
|
|
TimerTask task = new TimerTask() {
|
|
|
@Override
|
|
@Override
|
|
|
public void run() {
|
|
public void run() {
|
|
|
- System.out.println(topic+"====="+command2+ TimeExchange.getTime());
|
|
|
|
|
|
|
+ log.info(topic+"====="+command2+ TimeExchange.getTime());
|
|
|
mqttPushClient.publish2(1,true, topic,command2);
|
|
mqttPushClient.publish2(1,true, topic,command2);
|
|
|
mqttPushClient.subscribe(outTopic,1);//只需要订阅一次 订阅多次会返回多条数据
|
|
mqttPushClient.subscribe(outTopic,1);//只需要订阅一次 订阅多次会返回多条数据
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+ //主题相同则设置为延时1s执行
|
|
|
|
|
+ if(!topicOld.equals(topicNew)){
|
|
|
|
|
+ delay=1000;
|
|
|
|
|
+ }
|
|
|
// 每隔5秒执行一次任务
|
|
// 每隔5秒执行一次任务
|
|
|
- timers[i].schedule(task, 1000, Long.parseLong(
|
|
|
|
|
|
|
+ timers[i].schedule(task, delay, Long.parseLong(
|
|
|
pollTime));
|
|
pollTime));
|
|
|
- System.out.println("发布成功,command:"+command+";topic:"+topic);
|
|
|
|
|
- System.out.println("非静态方法被调用");
|
|
|
|
|
|
|
+ //每个任务延时增加2s 执行间隔设置为30s防止并发网关获取部到数据 12*2=24
|
|
|
|
|
+ delay =delay+2000;
|
|
|
|
|
+ log.info("发布成功,command:"+command+";topic:"+topic);
|
|
|
i++;
|
|
i++;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|