iot平台异构对接文档

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的配置

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xlink.xagent</groupId>
    <artifactId>xagent-ptp-demo</artifactId>
    <version>0.0.1</version>
    <name>xagent-ptp-demo</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <plugin.id>ptp_demo</plugin.id>
        <plugin.class>xlink.xagent.ptp.demo.main.ILockPlugin</plugin.class>
        <plugin.version>0.0.1</plugin.version>
        <plugin.provider>xlink</plugin.provider>
        <plugin.dependencies/>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.pf4j</groupId>
            <artifactId>pf4j</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>xlink.xagent</groupId>
            <artifactId>xagent-api</artifactId>
            <version>0.0.3</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/xagent-api-0.0.3.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.8.Final</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <!--xagent appliaction dependency -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.12.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>

        <!--<dependency>-->
            <!--<groupId>org.slf4j</groupId>-->
            <!--<artifactId>slf4j-log4j12</artifactId>-->
            <!--<version>1.7.5</version>-->
        <!--</dependency>-->

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
<!-- 		<dependency> -->
<!-- 		    <groupId>org.apache.tomcat</groupId> -->
<!-- 		    <artifactId>tomcat-util</artifactId> -->
<!-- 		    <version>9.0.1</version> -->
<!-- 		</dependency> -->
		        
        <!--xagent appliaction dependency end-->
    </dependencies>

    <build>
        <resources>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>unzip jar file</id>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <unzip src="target/${artifactId}-${version}.${packaging}"
                                       dest="target/plugin-classes"/>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptors>
                        <descriptor>
                            src/main/assembly/assembly.xml
                        </descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                    <archive>
                        <manifestFile>target/plugin-classes/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Plugin-Id>${plugin.id}</Plugin-Id>
                            <Plugin-Class>${plugin.class}</Plugin-Class>
                            <Plugin-Version>${plugin.version}</Plugin-Version>
                            <Plugin-Provider>${plugin.provider}</Plugin-Provider>
                            <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

第二步:创建配置类

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

public class ProductConfig {

	//产品id
	public static final String PRODUCT_ID = "1607d4ba3a9a00011607d4ba3a9a2201";

	//产吕key
	public static final String PRODUCT_KEY = "6a67e0a37ab925d5953544d5dcd893ca";

	//PTP插件服务的端口
	public static final int SERVER_PORT = 33799;

	//PTP与设备的心跳时间,如果设备端在指定时间60秒内没有与PTP通讯,PTP会自动断掉与此设备的连接。
	public static final int SERVER_HEART_SECONDS = 60;

	//TCP的通讯模式。
	public static final PtpServerStrategy SERVER_STRATEGY = PtpServerStrategy.TCP;

}

第三步:创建解码器

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

public class Decoder implements IPtpDecoder {

	private static final Logger logger = LoggerFactory.getLogger(Decoder.class);

	@Override
	public void doDecode(ByteBuf in, List<Object>
			out) throws PtpException {
		int size = in.readableBytes();
		byte[] data = new byte[size];
		in.readBytes(data);
		// TODO 
		out.add(new
				DemoMessage(Unpooled.wrappedBuffer(data)));
	}

}

第四步:创建编码器

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

public class Encoder implements IPtpEncoder {
  private static final Logger logger = LoggerFactory.getLogger(Encoder.class);

	@Override
	public ByteBuf doEncode(ByteBufAllocator
									byteBufAllocator, IPtpMessage message,
							List<Object> out) {
		DemoMessage demoMsg = (DemoMessage)
				message;
		ByteBuf buf = demoMsg.toValue();
		out.add(buf);
		return demoMsg.toValue();
	}

  }

第五步:创建处理器

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

public class DemoProcessor implements
		IPtpProtocolProcessor {
	@Override
	public void process(PtpServer server, int
			channelId, Object msg) {
		IPtpMessage iPtpMsg = (IPtpMessage)msg;
		// TODO 
	}
	@Override
	public void channelBuild(int channelId) {
		// TODO channel
	}
	@Override
	public void channelClose(int channelId) {
		// TODO channel
	}
}

第六步:创建插件启动类

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

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

如给设备下发数据

public class ILockPlugin extends Plugin {

  private static final Logger logger = LoggerFactory.getLogger(ILockPlugin.class);

  // ptp server 对象
  public static PtpServer ptpServer;
  // xagent操作对象
  private static XagentApi xagent;
  private PluginWrapper wrapper;


  public ILockPlugin(PluginWrapper wrapper) {
    super(wrapper);
    this.wrapper = wrapper;
  }


  @Override
  public void start() {
    try {
    xagent = this.wrapper.getPluginManager().getExtensions(XagentApi.class).get(0);
      logger.info("start demo plugin,trtrt");
      ptpServer =
          xagent.createServer(
        		  this.wrapper.getPluginId(), 
        		  ProductConfig.PRODUCT_ID,//产品ID
        		  ProductConfig.PRODUCT_KEY,//产品key
        		  ProductConfig.SERVER_PORT,//PTP端口
        		  new Decoder(), //解码器
        		  new Encoder(),//编码器
        		  new BusinessLogicProcessor(), //处理器
        		  ProductConfig.SERVER_HEART_SECONDS, 
        		  ProductConfig.SERVER_STRATEGY);//PTP与设备的通讯协议
      xagent.setDatapointSetListener(new DatapointDataHandler());//数据上报与下报处理器
      ptpServer.startServer();
      
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public static PtpServer getPtpServer() {
    return ptpServer;
  }

  public static XagentApi getXagentApi() {
    return xagent;
  }


}

第七步: 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随着新产品类型的增加,插件服务也会随之增多,不好管理。

posted on 2019-07-10 10:10  wolf12  阅读(516)  评论(0编辑  收藏  举报