iot docking platform heterogeneous document

iot docking platform heterogeneous document

Ready to work

  • XAgent development guidelines provided by the platform .pdf
  • Ordinal as demo xagent-ptp-demo
  • Add product to get on the platform and key product id
  • Foundation program requires the deployment of plug-in "xlink-xagent.zip"

The first step: add new products

Add a new product to get the Product ID and Product Key
1 to create a product

2 Create data endpoint

3 registration equipment

Step 1: Create plug-in project

Create a maven project, and the introduction of package java-xagent.jar the xlink, xagent-API-xyzjar,
Netty-Buffer-4.1.8.Final.jar, Netty-common.4.1.8.Final.jar, pf4j-2.0.0 .jar.
When ptp packing plug, jar package introduced previously mentioned do not require added.

pom.xml configuration

<?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>

Step 2: Create configuration class

PtpConfig.java can create a file using the plug-in to start class. Configuration settings required

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;

}

Step 3: Create a decoder

Create a proprietary protocol decoder for parsing vendor proprietary protocols, interfaces must implement 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)));
    }

}

Step Four: Create the encoder

Create a proprietary protocol encoder for encoding vendor proprietary protocols, interfaces must implement 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();
    }

  }

Step 5: Create processor

Create a business processor for processing vendor business, must implement 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
    }
}

Step Six: Create a plug-in starts classes

Create a plug-in startup class must inherit Plugin class; class contains two major classes PtpServer and XagentApi, which

XagentApi mainly used for uplink data operations, such as reporting data endpoint; PtpServer mainly for operating the downlink data,

The data sent to the device under

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;
  }


}

Step Seven: PTP plug-engineering package

Maven install command generates a jar package. The root jar package comprises at least three folders: placing lib java class file, placed ptp need to use third-party plug-in dependencies (if no dependencies, may be omitted) META-INF, for placing MANIFEST.MF file

Step eight: the deployment vessel operating environment

  • Plug deployed in server vendors, we need to deploy the operating environment
  • Plug deployed in the cloud, the need to deploy the operating environment, simply plug-in package to provide cloud

Step 9: deploy plugin

1 labeled jar package into the plugins directory

2 If you want to start multiple plug-ins in a single server, you need to modify 099

Start ./start.sh

PTP debug mode

Developers in the development stage, you can use debug mode, start the debug mode is different from the normal start, as follows, using the following development tools for idea

Debug configuration parameters

-Dpf4j.mode=development

-Dpf4j.pluginsDir=target

-Dnport = 3099 // custom plug-port basis

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

to sum up

This way is the use of plug-in docking proxy service mode for device and server communications MQTT, advantages: access to flexible, platform-independent, autonomous access equipment manufacturers. Disadvantages: 1 have some development effort. 2 With the addition of new product types, plug-in service will increase, and poor management.

Guess you like

Origin www.cnblogs.com/wolf12/p/11162107.html