MQTT设备模拟器(全部代码)

本文介绍了一个使用JavaScript编写的MQTT设备模拟器,该模拟器能够动态修改模拟数据并发送到MQTT broker,以模拟物联网设备的行为。关键功能包括start和stop方法,用于启动和停止模拟器,且在使用前需要配置网络连接。
摘要由CSDN通过智能技术生成

        该模拟器是基于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&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值