iot平台异构对接文档

准备工作

  • 平台提供的XAgent开发指南.pdf
  • demo程序xagent-ptp-demo
  • 平台上添加产品得到产品id和key
  • 部署时需要插件的基础程序《xlink-xagent.zip》

第一步:添加新产品

添加新产品得到产品ID和产品key

1创建产品

2创建数据端点

3注册设备

第一步:创建插件工程

创建一个maven工程,并引入java 包xlink-xagent.jar,xagent-api-x.y.z.jar,

netty-buffer-4.1.8.Final.jar, netty-common.4.1.8.Final.jar, pf4j-2.0.0.jar。

在打包ptp插件时,前面提到的引入的jar包都不需要添加进去。

pom.xml的配置

  1. <?xml version="1.0"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  3. xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>xlink.xagent</groupId>
  6. <artifactId>xagent-ptp-demo</artifactId>
  7. <version>0.0.1</version>
  8. <name>xagent-ptp-demo</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <plugin.id>ptp_demo</plugin.id>
  13. <plugin.class>xlink.xagent.ptp.demo.main.ILockPlugin</plugin.class>
  14. <plugin.version>0.0.1</plugin.version>
  15. <plugin.provider>xlink</plugin.provider>
  16. <plugin.dependencies/>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.pf4j</groupId>
  21. <artifactId>pf4j</artifactId>
  22. <version>2.0.0</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>xlink.xagent</groupId>
  26. <artifactId>xagent-api</artifactId>
  27. <version>0.0.3</version>
  28. <scope>system</scope>
  29. <systemPath>${project.basedir}/lib/xagent-api-0.0.3.jar</systemPath>
  30. </dependency>
  31. <dependency>
  32. <groupId>io.netty</groupId>
  33. <artifactId>netty-all</artifactId>
  34. <version>4.1.8.Final</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>redis.clients</groupId>
  38. <artifactId>jedis</artifactId>
  39. <version>2.9.0</version>
  40. </dependency>
  41. <!--xagent appliaction dependency -->
  42. <dependency>
  43. <groupId>org.apache.commons</groupId>
  44. <artifactId>commons-dbcp2</artifactId>
  45. <version>2.1</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.apache.derby</groupId>
  49. <artifactId>derby</artifactId>
  50. <version>10.12.1.1</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>com.google.code.gson</groupId>
  54. <artifactId>gson</artifactId>
  55. <version>2.8.1</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>com.squareup.okhttp3</groupId>
  59. <artifactId>okhttp</artifactId>
  60. <version>3.9.0</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>log4j</groupId>
  64. <artifactId>log4j</artifactId>
  65. <version>1.2.16</version>
  66. </dependency>
  67. <!--<dependency>-->
  68. <!--<groupId>org.slf4j</groupId>-->
  69. <!--<artifactId>slf4j-log4j12</artifactId>-->
  70. <!--<version>1.7.5</version>-->
  71. <!--</dependency>-->
  72. <dependency>
  73. <groupId>commons-lang</groupId>
  74. <artifactId>commons-lang</artifactId>
  75. <version>2.6</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>com.alibaba</groupId>
  79. <artifactId>fastjson</artifactId>
  80. <version>1.2.28</version>
  81. </dependency>
  82. <!-- <dependency> -->
  83. <!-- <groupId>org.apache.tomcat</groupId> -->
  84. <!-- <artifactId>tomcat-util</artifactId> -->
  85. <!-- <version>9.0.1</version> -->
  86. <!-- </dependency> -->
  87. <!--xagent appliaction dependency end-->
  88. </dependencies>
  89. <build>
  90. <resources>
  91. <resource>
  92. <filtering>false</filtering>
  93. <directory>src/main/java</directory>
  94. <excludes>
  95. <exclude>**/*.java</exclude>
  96. </excludes>
  97. </resource>
  98. <resource>
  99. <directory>src/main/resources</directory>
  100. </resource>
  101. </resources>
  102. <plugins>
  103. <plugin>
  104. <groupId>org.apache.maven.plugins</groupId>
  105. <artifactId>maven-compiler-plugin</artifactId>
  106. <configuration>
  107. <source>1.8</source>
  108. <target>1.8</target>
  109. </configuration>
  110. </plugin>
  111. <plugin>
  112. <groupId>org.apache.maven.plugins</groupId>
  113. <artifactId>maven-antrun-plugin</artifactId>
  114. <version>1.6</version>
  115. <executions>
  116. <execution>
  117. <id>unzip jar file</id>
  118. <phase>package</phase>
  119. <configuration>
  120. <target>
  121. <unzip src="target/${artifactId}-${version}.${packaging}"
  122. dest="target/plugin-classes"/>
  123. </target>
  124. </configuration>
  125. <goals>
  126. <goal>run</goal>
  127. </goals>
  128. </execution>
  129. </executions>
  130. </plugin>
  131. <plugin>
  132. <artifactId>maven-assembly-plugin</artifactId>
  133. <version>2.3</version>
  134. <configuration>
  135. <descriptors>
  136. <descriptor>
  137. src/main/assembly/assembly.xml
  138. </descriptor>
  139. </descriptors>
  140. <appendAssemblyId>false</appendAssemblyId>
  141. <archive>
  142. <manifestFile>target/plugin-classes/META-INF/MANIFEST.MF</manifestFile>
  143. </archive>
  144. </configuration>
  145. <executions>
  146. <execution>
  147. <id>make-assembly</id>
  148. <phase>package</phase>
  149. <goals>
  150. <goal>attached</goal>
  151. </goals>
  152. </execution>
  153. </executions>
  154. </plugin>
  155. <plugin>
  156. <groupId>org.apache.maven.plugins</groupId>
  157. <artifactId>maven-jar-plugin</artifactId>
  158. <version>2.4</version>
  159. <configuration>
  160. <archive>
  161. <manifestEntries>
  162. <Plugin-Id>${plugin.id}</Plugin-Id>
  163. <Plugin-Class>${plugin.class}</Plugin-Class>
  164. <Plugin-Version>${plugin.version}</Plugin-Version>
  165. <Plugin-Provider>${plugin.provider}</Plugin-Provider>
  166. <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
  167. </manifestEntries>
  168. </archive>
  169. </configuration>
  170. </plugin>
  171. <plugin>
  172. <artifactId>maven-deploy-plugin</artifactId>
  173. <configuration>
  174. <skip>true</skip>
  175. </configuration>
  176. </plugin>
  177. </plugins>
  178. </build>
  179. </project>

第二步:创建配置类

可以创建一个PtpConfig.java文件,在插件启动类中使用。设置必填的配置

  1. public class ProductConfig {
  2. //产品id
  3. public static final String PRODUCT_ID = "1607d4ba3a9a00011607d4ba3a9a2201";
  4. //产吕key
  5. public static final String PRODUCT_KEY = "6a67e0a37ab925d5953544d5dcd893ca";
  6. //PTP插件服务的端口
  7. public static final int SERVER_PORT = 33799;
  8. //PTP与设备的心跳时间,如果设备端在指定时间60秒内没有与PTP通讯,PTP会自动断掉与此设备的连接。
  9. public static final int SERVER_HEART_SECONDS = 60;
  10. //TCP的通讯模式。
  11. public static final PtpServerStrategy SERVER_STRATEGY = PtpServerStrategy.TCP;
  12. }

第三步:创建解码器

创建一个私有协议的解码器,用于解析厂商私有协议,必须实现IPtpDecoder接口

  1. public class Decoder implements IPtpDecoder {
  2. private static final Logger logger = LoggerFactory.getLogger(Decoder.class);
  3. @Override
  4. public void doDecode(ByteBuf in, List<Object>
  5. out) throws PtpException {
  6. int size = in.readableBytes();
  7. byte[] data = new byte[size];
  8. in.readBytes(data);
  9. // TODO
  10. out.add(new
  11. DemoMessage(Unpooled.wrappedBuffer(data)));
  12. }
  13. }

第四步:创建编码器

创建一个私有协议的编码器,用于编码厂商私有协议,必须实现IPtpEncoder接口

  1. public class Encoder implements IPtpEncoder {
  2. private static final Logger logger = LoggerFactory.getLogger(Encoder.class);
  3. @Override
  4. public ByteBuf doEncode(ByteBufAllocator
  5. byteBufAllocator, IPtpMessage message,
  6. List<Object> out) {
  7. DemoMessage demoMsg = (DemoMessage)
  8. message;
  9. ByteBuf buf = demoMsg.toValue();
  10. out.add(buf);
  11. return demoMsg.toValue();
  12. }
  13. }

第五步:创建处理器

创建业务处理器,用于处理厂商业务,必须实现IPtpProtocolProcessor

  1. public class DemoProcessor implements
  2. IPtpProtocolProcessor {
  3. @Override
  4. public void process(PtpServer server, int
  5. channelId, Object msg) {
  6. IPtpMessage iPtpMsg = (IPtpMessage)msg;
  7. // TODO
  8. }
  9. @Override
  10. public void channelBuild(int channelId) {
  11. // TODO channel
  12. }
  13. @Override
  14. public void channelClose(int channelId) {
  15. // TODO channel
  16. }
  17. }

第六步:创建插件启动类

创建插件启动类,必须继承Plugin类;该类包含两个主要类PtpServer和XagentApi,其中

XagentApi主要用于操作上行数据,如上报数据端点;PtpServer主要用于操作下行数据,

如给设备下发数据

  1. public class ILockPlugin extends Plugin {
  2. private static final Logger logger = LoggerFactory.getLogger(ILockPlugin.class);
  3. // ptp server 对象
  4. public static PtpServer ptpServer;
  5. // xagent操作对象
  6. private static XagentApi xagent;
  7. private PluginWrapper wrapper;
  8. public ILockPlugin(PluginWrapper wrapper) {
  9. super(wrapper);
  10. this.wrapper = wrapper;
  11. }
  12. @Override
  13. public void start() {
  14. try {
  15. xagent = this.wrapper.getPluginManager().getExtensions(XagentApi.class).get(0);
  16. logger.info("start demo plugin,trtrt");
  17. ptpServer =
  18. xagent.createServer(
  19. this.wrapper.getPluginId(),
  20. ProductConfig.PRODUCT_ID,//产品ID
  21. ProductConfig.PRODUCT_KEY,//产品key
  22. ProductConfig.SERVER_PORT,//PTP端口
  23. new Decoder(), //解码器
  24. new Encoder(),//编码器
  25. new BusinessLogicProcessor(), //处理器
  26. ProductConfig.SERVER_HEART_SECONDS,
  27. ProductConfig.SERVER_STRATEGY);//PTP与设备的通讯协议
  28. xagent.setDatapointSetListener(new DatapointDataHandler());//数据上报与下报处理器
  29. ptpServer.startServer();
  30. } catch (Exception e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. public static PtpServer getPtpServer() {
  36. return ptpServer;
  37. }
  38. public static XagentApi getXagentApi() {
  39. return xagent;
  40. }
  41. }

第七步: PTP插件工程打包

使用maven的install命令,生成一个jar包。jar包根目录下至少包含三个文件夹: 放置java的class文件 lib,放置ptp插件需要使用到的第三方依赖包(如果没有使用依赖包,可以省略) META-INF,用于放置MANIFEST.MF文件

第八步:部署容器运行环境

  • 插件部署在厂商服务器下,需要部署该运行环境
  • 插件部署在云端,不需要部署该运行环境,只需提供插件包到云端

第九步:部署插件

1打成的jar包放到的plugins目录

2如果要在一台服务器启动多个插件,需要修改099

启动./start.sh

PTP调试模式

开发者在开发阶段,可使用调试模式,调试模式的启动有别于正常启动,具体如下,以下 使用开发工具为idea

配置Debug参数

-Dpf4j.mode=development

-Dpf4j.pluginsDir=target

-Dnport=3099 //自定义基础插件的端口

-config_file=lib/xagent/config/config.properties

总结

本对接方式是采用插件代理服务方式实现设备与MQTT服务器的通讯,优点:接入灵活,可以不依赖平台,设备厂家自主接入。缺点:1有一定的开发工作量。2随着新产品类型的增加,插件服务也会随之增多,不好管理。

iot平台异构对接文档的更多相关文章

  1. 001-Java®语言规范、Java平台标准版文档、JVM概述

    一.概述 相关api地址:JDK10   JDK 9   JDK 8   JDK 7   JDK 6 Java语言和虚拟机规范: https://docs.oracle.com/javase/spec ...

  2. ECO开放平台对接文档说明

    应用集成: http://open.teewon.net:1000/static/index.html#/docs/flow/integrate统一认证集成文档: http://open.teewon ...

  3. 【IoT平台技术对接分享】如何上传正确的消息推送证书

    消息推送应用实现消息推送的接口,部署证书,同时上传根证书到平台. 目前消息推送失败,很大一部分原因是证书上传不对.推荐小伙伴们使用下面的方法导出证书. 推送:平台调用应用服务器的restful接口将数 ...

  4. EasyNVR无插件直播流媒体服务器云端集中管控的EasyNVS云管理平台安装使用文档

    EasyNVS - EasyNVR云端集中管理服务 EasyNVS云管理平台是一套专门用于集中化管理EasyNVR 的解决方案. EasyNVR 采用主动注册的方式接入到 EasyNVS, 再由 Ea ...

  5. linux平台的office文档转pdf(程序员的菜)

    需要材料: 1.  Openoffice3.4(我是32位的centos,可以根据自己的系统下载指定的openoffice软件包) 下载地址:http://sourceforge.net/projec ...

  6. OpenPAI大规模人工智能平台安装部署文档

    环境要求: 如果需要图形界面,需要在Ubuntu系统安装,否则centos系统安装时是没有问题的(web端和命令行进行任务提交) 安装过程需要有另外一台控制端机器(注意:区别于集群所在的任何一台服务器 ...

  7. 这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)

    在前2篇文章这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧 和这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,大伙热情高涨.再次拿出自己的私货,在.NET平台 ...

  8. .NET平台开源文档与报表处理组件包括Execel PDF Word等

    在前2篇文章这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧 和这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,大伙热情高涨.再次拿出自己的私货,在.NET平台 ...

  9. 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览

    在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...

随机推荐

  1. Flask-SQLAlchemy操作

    Flask-SQLAlchemy   SQLAlchemy 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言 ...

  2. MySQL ALTER命令-修改数据表名或者修改数据表字段

    需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 删除,添加或修改表字段 如下命令使用了 ALTER 命令及 DROP 子句来删除表的 i 字段: ALTER TABLE ...

  3. seaweedfs文件存储服务器搭建

    官方网站: https://github.com/chrislusf/seaweedfs/wiki/Getting-Started 概述 seaweedfs是一个非常优秀的由 golang 开发的分布 ...

  4. [Python]编译错误:编码错误问题(SyntaxError: (unicode error) )

    1 错误信息 python文件 1 #coding:utf-8 2 3 class Clz: 4 def func(filePath): 5 """ 6 func 7 - ...

  5. 关于CAShapeLayer的一些基本操作

    设置圆形进度条: 实现效果如下: 实现代码如下:(注释很详细啦!!!) UIView *circleView = [[UIView alloc]initWithFrame:CGRectMake(, , ...

  6. WordPress自定义菜单和修改去除多余的css

    这里主要是用于模板制作的,一般前端已经写好了,我们只要将前端的内容套用WordPress后台就可以了. 所以我们在模板制作过程中,需要自定义WordPress菜单. 在functions.php文件中 ...

  7. 解决Vue跨域问题 : 正向代理与反向代理

    你需要做一个反向代理的东西   ===> 打开你的vue项目的config文件夹下的index.js 找到以下代码 dev: { proxyTable: { '/api': { target: ...

  8. jqGrid只向服务器请求一次的设置

    也就是说,在表格初始化时请求一次服务器,以后翻页就不再请求服务器,翻页的也只是初始化数据. 一次复制别人的代码时,一直不知道为什么翻页不请求服务器. 搞到人都爆炸,原来只是一个设置的地方. loado ...

  9. 一个自己稍作修改了的美赛论文 LaTeX 模板

    警告:这是旧版模板的发布页面.本站已经发布了最新版的美赛模板 easymcm(2020 年美赛可用),请到该页面查看: https://www.cnblogs.com/xjtu-blacksmith/ ...

  10. C++零散知识笔记本

    目录 1.符号 1.1符号输出 1.2运算符 2.基本内置类型 wchar_t 3.内置类型所占字节数 内置类型的简写 4.变量的本质 变量与指针的故事 (1)malloc函数 (2)new关键字 5 ...