1 Star 0 Fork 564

吕俊杰 / iot

forked from 飞码 / iot 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

iot网络中间件(2.0)

使用java语言且基于netty, spring boot, redis等开源项目开发来的物联网网络中间件, 支持udp, tcp底层协议和http, mqtt, modbus等上层协议. 主打工业物联网底层网络交互、设备管理、数据存储、大数据处理

主要特性

  • 支持服务端启动监听多个端口, 统一所有协议可使用的api接口
  • 包含一套代理客户端通信协议,支持调用:客户端 -> 服务端 -> 设备 -> 服务端 -> 客户端
  • 支持设备协议对象和其业务对象进行分离(支持默认业务处理器【spring单例注入】和自定义业务处理器)
  • 支持redis, 可以方便的将设备上报的数据进行缓存到redis和从redis获取数据
  • 支持对数据的生产采集和数据的消费进行分离
  • 支持同步和异步调用设备, 支持应用程序代理客户端和设备服务端和设备三端之间的同步和异步调用
  • 服务端支持设备上线/下线/异常的事件通知, 支持自定义心跳事件, 客户端支持断线重连
  • 丰富的日志打印功能,包括设备上线,下线提示, 一个协议的生命周期(请求或者请求+响应)等
  • 支持本地和服务端的并发测试

支持的协议

  1. tcp(固定长度解码, 长度字段解码, 换行符解码,自定义分隔符解码,自定义字节到报文解码)[已完成/v2.0.0]
  2. mqtt协议客户端,支持连接标准mqtt broker服务器[已完成/v2.1.0]
  3. 提供modbus支持[已完成/v2.2.0]
  4. 新增plc支持(开发中...)

更新日志

  1. 完成和测试通过modbus tcp客户端协议实现(2021/9/11)
  2. 完成和测试通过mqtt协议的客户端实现(2021/9/7)

模拟工具

  1. QtSwissArmyKnife 支持udp、tcp、modbus、websocket、串口等调试
  2. IotClient 支持plc(西门子,欧姆龙,三菱),modbus,串口,mqtt,tcp, udp等模拟和调试

合作方式(qq: 97235681)

  1. 设备对接(不包括业务, 只负责设备协议部分,按设备协议数量收费)[500+]
  2. 物联网系统开发(包括设备和业务完整系统)[价格详谈]
  3. 为厂家提供专门的设备交互sdk(java)[价格详谈]

使用教程

首先创建一个springboot应用

服务端教程

引入服务端jar包
//在maven文件里面引入服务端jar包
<dependency>
    <version>version</version>
    <groupId>com.iteaj</groupId>
    <artifactId>iot-server</artifactId>
</dependency>
创建报文解码组件
  1. 创建固定长度解码器组件(以固定长度解码器为例)
package com.iteaj.iot.test.server.fixed;

import com.iteaj.iot.AbstractProtocol;
import com.iteaj.iot.config.ConnectProperties;
import com.iteaj.iot.server.component.FixedLengthFrameDecoderComponent;
import com.iteaj.iot.server.protocol.HeartProtocol;
import com.iteaj.iot.test.TestProtocolType;

/**
 * 固定长度解码器组件测试
 *  _注意:此组件必须交由spring容器管理_ 
 *  _注意:此处必须指定此组件使用的报文类型<FixedLengthServerMessage>_ 
 */
@Component
public class TestFixedLengthDecoderComponent extends FixedLengthFrameDecoderComponent<FixedLengthServerMessage> {

    // 构造函数指定要监听的端口 8080
    // 指定固定长度28
    public TestFixedLengthDecoderComponent() {
        super(new ConnectProperties(8080), 28);
    }

    @Override
    public String getDesc() {
        return "用于测试服务端固定长度字段解码器[FixedLengthFrameDecoder]";
    }

    /**
     * 通过报文头获取对应的协议
     */
    @Override
    public AbstractProtocol getProtocol(FixedLengthServerMessage message) {
        TestProtocolType protocolType = message.getHead().getType();
        // 心跳类型返回心跳协议
        if(protocolType == TestProtocolType.Heart) {
            return HeartProtocol.getInstance(message);
            
        // 对于客户端主动请求服务器的协议类型 - new一个协议对象    
        } else if(protocolType == TestProtocolType.CIReq) {
            return new FixedLengthClientRequestProtocol(message);
        } else {
            // 对于客户端响应服务器的协议类型 - 必须移除保存在服务端的协议
            return remove(message.getHead().getMessageId());
        }

    }

    @Override
    public String getName() {
        return "固定长度字段解码组件";
    }
}
  1. 创建固定长度解码报文类(一个组件对应一个报文)
package com.iteaj.iot.test.server.fixed;

import com.iteaj.iot.server.ServerMessage;
import com.iteaj.iot.test.MessageCreator;

/**
 * 固定长度报文 28
 *    8         8        4       8
 * 设备编号 + messageId + type + 递增值
 *  _注:服务端报文必须继承父类:ServerMessage _ 
 */
public class FixedLengthServerMessage extends ServerMessage {

    // 注意:此构造函数一定要存在
    public FixedLengthServerMessage(byte[] message) {
        super(message);
    }

    public FixedLengthServerMessage(MessageHead head) {
        super(head);
    }

    public FixedLengthServerMessage(MessageHead head, MessageBody body) {
        super(head, body);
    }

    // 解析出客户端请求报文的报文头
    @Override
    protected MessageHead doBuild(byte[] message) {
        return MessageCreator.buildFixedMessageHead(message);
    }
}
创建和设备交互的协议
// 交互协议包含两种
// 1. 客户端主动请求服务端(继承 ClientInitiativeProtocol) 比如:[设备获取服务器的当前时间(请求) -> 服务器响应当前时间给客户端(响应)]
package com.iteaj.iot.test.server.fixed;

import com.iteaj.iot.Message;
import com.iteaj.iot.ProtocolType;
import com.iteaj.iot.server.manager.DevicePipelineManager;
import com.iteaj.iot.server.protocol.ClientInitiativeProtocol;
import com.iteaj.iot.test.MessageCreator;
import com.iteaj.iot.test.TestProtocolType;

public class FixedLengthClientRequestProtocol extends ClientInitiativeProtocol<FixedLengthServerMessage> {

    public FixedLengthClientRequestProtocol(FixedLengthServerMessage requestMessage) {
        super(requestMessage);
    }

    // 构建要响应给客户端的报文
    @Override
    protected FixedLengthServerMessage doBuildResponseMessage() {
        Message.MessageHead head = requestMessage().getHead();
        String equipCode = head.getEquipCode();
        String messageId = head.getMessageId();
        DevicePipelineManager instance = DevicePipelineManager.getInstance();
        return MessageCreator.buildFixedLengthServerMessage(equipCode, messageId, instance.size(), head.getType());
    }
    // 解析出客户端请求的报文内容
    @Override
    protected void doBuildRequestMessage(FixedLengthServerMessage requestMessage) {

    }

    @Override
    public ProtocolType protocolType() {
        return TestProtocolType.CIReq;
    }
}
// 2. 服务端主动请求客户端(继承 ServerInitiativeProtocol)比如:[服务端下发开指令到客户端(请求) -> 客户端响应服务端操作是否成功的状态(响应)]
常用的解码组件

说明:以下是已经适配好的常用的解码器组件

  1. DelimiterBasedFrameDecoderComponent 自定义分隔符解码器组件
  2. FixedLengthFrameDecoderComponent 固定长度解码器组件
  3. LengthFieldBasedFrameDecoderComponent 长度字段解码器组件
  4. LineBasedFrameDecoderComponent 换行符解码器组件
  5. ByteToMessageDecoderComponent 自定义字节到报文解码器组件

客户端教程

引入客户端jar包
//在maven文件里面引入iot-client jar包
<dependency>
    <version>version</version>
    <groupId>com.iteaj</groupId>
    <artifactId>iot-client</artifactId>
</dependency>
使用mqtt客户端组件
//在maven文件里面引入iot-mqtt jar包
<dependency>
    <version>version</version>
    <groupId>com.iteaj</groupId>
    <artifactId>iot-mqtt</artifactId>
</dependency>
使用modbus客户端组件

2. 架构图

iot架构图

  1. 设备端和设备管理端同步和异步如线3、4所示
    • 平台主动向设备发起请求4->3:如果是同步调用则线程将等待设备返回在往下执行业务, 如果设备由于某些原因没有返回则将在指定的超时时间内解锁线程继续向下执行业务。如果是异步调用则在设备响应后会执行业务,并不会锁住当前调用的线程
    • 设备主动向平台发起请求3->4:如线3设备主动发起请求给平台, 平台将先解析设备请求的报文然后调用执行业务,执行完业务之后如线4平台将生成需要响应给设备的报文并推送给设备
  2. 应用管理端和设备管理端同步和异步(1、2所示)和1相同的流程
  3. 应用管理端和设备进行同步和异步调用如:1、2、3、4
    • 如果是同步:由应用客户端发起调用如线1所示,发起调用的线程将阻塞, 然后如线4所示设备管理端向设备发起异步调用,接着等待设备响应如线3,再然后设备管理端响应应用客户端如线2,最后将释放应用客户端的线程锁继续向下执行
    • 如果是异步:和同步的区别是应用客户端不加锁
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

iot是基于netty, spring boot, redis等项目实现的物联网中间件, 已支持tcp、udp、mqtt、modbus等常用物联网协议,并且支持快速接入redis、kafka等消息队列[群:552167793] 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/lvjunjie168/iot.git
git@gitee.com:lvjunjie168/iot.git
lvjunjie168
iot
iot
main

搜索帮助