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隨着新產品類型的增加,插件服務也會隨之增多,不好管理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章