Skip to content

终端接入易信平台应用开发实例

此次终端接入易信平台应用实例所采用的终端设备是大疆无人机

1. 申请终端对应的易信账号

易信账号和密码,用于登录易信平台

2. 申请开发者信息

client-id = ""
client-key = ""
client-key2048 = ""
client-secret = ""
# api endpoint
endpoint = "http://testapi.teamshub.com"

3. 工程依赖

新建Maven工程,pom.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<project 
  xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.thingshub.iot.dji</groupId>
  <artifactId>dji-backend</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.12.RELEASE</version>
  </parent>

  <properties>
    <java.version>1.8</java.version>
    <skipTests>true</skipTests>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
      <groupId>com.sitech.yixin</groupId>
      <artifactId>oncon-apisdk</artifactId>
      <version>1.0.0</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/lib/oncon-apisdk.jar</systemPath>
    </dependency>
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.14.0</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
    </dependency>
    <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
    </dependency>
    <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
    </dependency>
    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcprov-jdk18on</artifactId>
      <version>1.78.1</version>
    </dependency>
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcpkix-jdk18on</artifactId>
      <version>1.78.1</version>
    </dependency>
    <dependency>
      <groupId>org.igniterealtime.smack</groupId>
      <artifactId>smack</artifactId>
      <version>3.2.1.1</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/lib/smack-3.2.1.1.jar</systemPath>
    </dependency>
    <dependency>
      <groupId>com.oncon</groupId>
      <artifactId>oncon-sdkim-java</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.73</version>
    </dependency>    
    <dependency>
      <groupId>org.igniterealtime.smack</groupId>
      <artifactId>smackx</artifactId>
      <version>3.2.1</version>
    </dependency>    
    <dependency>
      <groupId>org.igniterealtime.smack</groupId>
      <artifactId>smackx-jingle</artifactId>
      <version>3.2.1</version>
    </dependency>    
    <dependency>
      <groupId>org.igniterealtime.smack</groupId>
      <artifactId>smackx-debug</artifactId>
      <version>3.2.1</version>
    </dependency>
    <dependency>
      <groupId>commons-digester</groupId>
      <artifactId>commons-digester</artifactId>
      <version>1.8</version>
    </dependency>
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>commons-configuration</groupId>
      <artifactId>commons-configuration</artifactId>
      <version>1.8</version>
    </dependency>
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.8.3</version>
    </dependency>
    <dependency>
      <groupId>commons-net</groupId>
      <artifactId>commons-net</artifactId>
      <version>3.3</version>
    </dependency>
    <dependency>
      <groupId>commons-id</groupId>
      <artifactId>commons-id</artifactId>
      <version>1.0</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/lib/commons-id-1.0.jar</systemPath>
    </dependency>
    <dependency>
      <groupId>commons-discovery</groupId>
      <artifactId>commons-discovery</artifactId>
      <version>20040218.194635</version>
    </dependency>
    <dependency>
      <groupId>org.xerial.snappy</groupId>
      <artifactId>snappy-java</artifactId>
      <version>1.1.7.3</version>
    </dependency>
    <dependency>
      <groupId>com.thingshub</groupId>
      <artifactId>support</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-integration</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.integration</groupId>
      <artifactId>spring-integration-mqtt</artifactId>
    </dependency>
  </dependencies>
  <build>
    <finalName>dji-backend.${version}</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <target>1.8</target>
          <source>1.8</source>
          <encoding>utf-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

4. 获取API Token

api = "/oncon-service/token3"
client-secret = ""
client-key = ""
client-key2048 = ""
client-id = ""
ts = ""
flowId = ""
signatureCode = ""
encryptionType = ""
url = "http://testapi.teamshub.com/oncon-service/token3?client_id=${client-id}&flow_id=${flowId}&ts=${ts}&grant_type=client_credential&sign=${sign}"
sign = {client_id = "${client-id}", flow_id = "${flowId}", ts = "${ts}", grant_type = "client_credential"}
// request = {client_secret = ${client_secret}, signature_code = ${signature_code}, is_customized = false}
HttpClientUtils.encryptContentByPublicKey(EncryptionType.PREFIX_R2048, request, null, clientKey, clientKey2048);
// params = {client_id = ${client_id}, flow_id = ${flow_id}, ts = ${ts}, grant_type = client_credential}
SignUtils.sign(params, clientSecret, request);

val responseText = HttpClientUtils.sendPost(url, request);

responseText = HttpClientUtils.decryptContentByPubicKey(response, clientSecret, null, null);

Map<String, Object> result = objectMapper.readValue(responseText, Map.class);
return getString(result, "access_token");

5. 配置XMPP

endpoint = "http://testapi.teamshub.com/"
# 从易信申请的client-id
client-id = "yx_fi2dmn376azkmnf9"
# 从易信申请的client-secret
client-secret = "qolaap3wkweulg8zxbvkvr5oxcfsocue"
# 从易信申请的client-key2048
client-key2048 = ""
# 易信侧提供
host = "testim.teamshub.com"
# 获取accessToken 
accessToken = ""

# 订阅大疆无人机响应主题
topic = "thing/product/${gatewaySn}/services_reply"

6. 终端登录易信

# imusername, impwd
api = "/oncon-service/gateway/m1_contact_login/v4.0"

7. IM登录

// username 调用易信登录接口返回的imusername
// password 调用易信登录接口返回的impwd
// packetListener 接收到178的会议邀请消息并处理
// packetFilter 消息过滤
// connectionListener 
PacketListener packetListener = new AppPacketListener() {
    @Override
    protected void processMessage(Message message) {
        try {
            String body = message.getBody();
            if (MessageHelper.isNormalMessage(body)) {
                String bodyType = MessageHelper.parseBodyType(body);
                if ("178".equals(bodyType)) {
                    Map<String, Object> map178 = MessageHelper.deserialize(body, Map.class);
                }
            }
        } catch(Exception ex) {
            log.error(ex.getMessage());
        }
    }
};
XMPPManager.login(username, password, packetListener, packetFilter, connectionListener);

8. 会议邀请处理

这里使用了Spring框架的消息处理机制,当会议邀请消息到来时,发送 InviteEvent 事件,由事件监听器 InviteEventListener 处理

# 从邀请消息中解析以下业务字段

# 会议ID
confId = ""
# 邀请人
caller = ""
# 会议室ID
meetingId = ""

# 判断是否在通话
is_talking_api = "/oncon-service/gateway/media_call_doing_query/v1.0"

# 在通话,关闭通话
close_talk_api = "/oncon-service/gateway/media_call_doing_close/v1.0"

# 没在通话,加入会议
join_conference_api = "/oncon-service/gateway/video_conf_user_live_mode_query/v1.0"

# 加入会议后获取到推流地址,给大疆无人机下发推流指令
topic = "thing/product/${gatewaySn}/services"