该模拟器是基于js脚本进行动态修改模拟数据的。向mqtt broker中发送模拟消息,以达到设备模拟的效果。
js代码:
topic中填入有效主题。模拟器数据则是发送的模拟消息,根据实际情况而定。
//属性上报
simulator.onProperty(function (index, session) {
session.sendMessage("topic",JSON.stringify({
模拟器数据,
}))
});
//事件上报
simulator.onEvent(function (index, session) {
session.sendMessage("topic",JSON.stringify({
模拟数据,
}))
});
simulator.onConnect(function (session){
});
模拟器主体代码:
start方法为启动模拟器,stop方法为停止模拟器。使用前需要预先配置网卡。
package com.xiot.simulator.mqttSimulator;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.xiot.simulator.entry.MQTTAuth;
import com.xiot.simulator.message.MessageHandler;
import com.xiot.simulator.result.Result;
import com.xiot.simulator.web.domain.dto.StartDto;
import com.xiot.simulator.web.domain.entity.Simulator;
import com.xiot.simulator.web.mapper.SimulatorMapper;
import com.xiot.simulator.web.networkCardResource.ResourceManage;
import io.netty.buffer.Unpooled;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.kqueue.KQueue;
import io.netty.channel.kqueue.KQueueEventLoopGroup;
import io.netty.channel.kqueue.KQueueSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.mqtt.MqttConnectReturnCode;
import io.netty.handler.codec.mqtt.MqttVersion;
import io.netty.util.concurrent.Future;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.expands.script.engine.DynamicScriptEngine;
import org.hswebframework.expands.script.engine.DynamicScriptEngineFactory;
import org.jetlinks.mqtt.client.MqttClient;
import org.jetlinks.mqtt.client.MqttClientCallback;
import org.jetlinks.mqtt.client.MqttClientConfig;
import org.jetlinks.mqtt.client.MqttConnectResult;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@Service("MqttSimulator")
@Scope("prototype")
@Slf4j
public class MqttSimulator {
private BlockingQueue<Runnable> createMqttJob;
private ScheduledExecutorService executorService;
@Resource
private SimulatorMapper simulatorMapper;
@Autowired
private StartDto startDto;
@Autowired
private ResourceManage resourceManage;
/* public MqttSimulator(BlockingQueue<Runnable> createMqttJob, ScheduledExecutorService executorService) {
this.createMqttJob = createMqttJob;
this.executorService = executorService;
}*/
public MqttSimulator(StartDto startDto){
this.startDto=startDto;
}
private Integer EventLimit=1000;
private Integer PropertyLimit=1000;
//每1000毫秒上报
int eventRate=1000 ;
//批量连接数,每次并发连接,完成后进入下一批
private int threadSize=Runtime.getRuntime().availableProcessors()*2;
// private String scriptFile="./js/handler.js";
Map<String, ClientSession> clientMap=new ConcurrentHashMap<>();
BiConsumer<Integer, ClientSession> eventDataSuppliers;
BiConsumer<Integer, ClientSession> propertyDataSuppliers;
Map<String, MessageHandler> messageHandlerMap = new HashMap<>();
private Map<String, AtomicInteger> portCounter = new ConcurrentHashMap<>();
private Consumer<ClientSession> onConnect;
// ArrayList<ScheduledExecutorService> scheduledExecutorServices=new ArrayList<>();
transient EventLoopGroup eventLoopGroup;
transient Class channelClass;
private static Map&