作者: 陈辉平

前言

鸿蒙设备接入华为云后的长稳定并发压测总结,编写应用模拟器,根据收的传感器上报的数据。判断下发命令到其他设备执行,(比如压力传感器上报压力过大,然后下发命令到阀门设备,进行关闭阀门操作)

1、模拟器下发消息

public class CreateMessageSolution {

   // REGION_ID:如果是上海一,请填写"cn-east-3";如果是北京四,请填写"cn-north-4";如果是华南广州,请填写"cn-south-4"

private static final String REGION_ID = "cn-south-4";
// ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
private static final String ENDPOINT = "4ef04886da.st1.iotda-app.cn-south-1.myhuaweicloud.com";

public static void main(String[] args) {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    String ak = "ZO8QQXJKPRU8KQQRYXXX";        
    String sk = "WOov5WfFpVSS6n6ozbIepbcRb4XkDY1S95SdrkzT";

    ICredential auth = new BasicCredentials()
            .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
            .withAk(ak)
            .withSk(sk);

    // 创建IoTDAClient实例并初始化
    IoTDAClient client = IoTDAClient.newBuilder()
            .withCredential(auth)
            // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
            .withRegion(new Region(REGION_ID, ENDPOINT))
           // .withRegion(IoTDARegion.CN_NORTH_4)
            .build();

    
    
    CreateMessageRequest request = new CreateMessageRequest();
    DeviceMessageRequest body = new DeviceMessageRequest();
    body.withMessage("HelloWorld");
    body.withName("messageName");
    body.withMessageId("99b32da9-cd17-4cdf-a286-f6e849cbc368");
    request.withBody(body);
    
   request.setDeviceId("65717fcb727edb69dadb7a01_chp101367999");
    
    request.setInstanceId("ea13791f-2659-4472-b8d8-d9b8637169c5");
    
    
    try {
        CreateMessageResponse response = client.createMessage(request);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getRequestId());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
}

}

2、模拟器下发命令

public class CreateCommandSolution {

   // REGION_ID:如果是上海一,请填写"cn-east-3";如果是北京四,请填写"cn-north-4";如果是华南广州,请填写"cn-south-4"
private static final String REGION_ID = "cn-south-4";
// ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
private static final String ENDPOINT = "4ef04886da.st1.iotda-app.cn-south-1.myhuaweicloud.com";

public static void main(String[] args) {
  
    
    while(true)
    {
        try
        {
            Thread.sleep(5000);
            sendCommand();
            
        }
        catch(Exception ex)
        {
            
        }
    

    }
}


public static void sendCommand()
{
    
      // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
      String ak = "ZO8QQXJKPRU8KQQRYXXX";        
      String sk = "WOov5WfFpVSS6n6ozbIepbcRb4XkDY1S95SdrkzT";

      
    ICredential auth = new BasicCredentials()
            .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
            .withAk(ak)
            .withSk(sk);

    IoTDAClient client1 = IoTDAClient.newBuilder()
            .withCredential(auth)
            .withRegion(IoTDARegion.valueOf("cn-south-1"))
            .build();
    
    // 创建IoTDAClient实例并初始化
    IoTDAClient client = IoTDAClient.newBuilder()
            .withCredential(auth)
            // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
            .withRegion(new Region(REGION_ID, ENDPOINT))
           // .withRegion(IoTDARegion.CN_NORTH_4)
            .build();

    
    
    CreateCommandRequest request = new CreateCommandRequest();
    DeviceCommandRequest body = new DeviceCommandRequest();
    body.withParas("{\"value\":\"ONAAA\"}");
    body.withCommandName("setReportingFrequency");
    body.withServiceId("smokeDetector");
    request.setDeviceId("65717fcb727edb69dadb7a01_chp101367999");
    request.withBody(body);
    try {
        CreateCommandResponse response = client.createCommand(request);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getRequestId());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
    
}

}

3、获取设备消息

public class ListDeviceMessagesSolution {

   // REGION_ID:如果是上海一,请填写"cn-east-3";如果是北京四,请填写"cn-north-4";如果是华南广州,请填写"cn-south-4"

private static final String REGION_ID = "cn-south-4";
// ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
private static final String ENDPOINT = "4ef04886da.st1.iotda-app.cn-south-1.myhuaweicloud.com";

public static void main(String[] args) {
  
    String DeviceId="65717fcb727edb69dadb7a01_chp101367999";
    getmsg(DeviceId);
    
}

public static void getmsg(String DeviceId)
{
    
      // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
     String ak = "ZO8QQXJKPRU8KQQRYXXX";        
     String sk = "WOov5WfFpVSS6n6ozbIepbcRb4XkDY1S95SdrkzT";
     String projectId = "4cbf723a0b124c49ad16679a819ec808";
     // 创建认证
     ICredential auth = new BasicCredentials()
            .withAk(ak)
            .withSk(sk)
            // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate"
            .withDerivedPredicate(BasicCredentials.DEFAULT_DERIVED_PREDICATE)
            .withProjectId(projectId);

    // 创建IoTDAClient实例并初始化
    IoTDAClient client = IoTDAClient.newBuilder()
            .withCredential(auth)
            // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
            .withRegion(new Region(REGION_ID, ENDPOINT))
           // .withRegion(IoTDARegion.CN_NORTH_4)
            .build();

    
    ListDeviceMessagesRequest request = new ListDeviceMessagesRequest();
    DeviceCommandRequest body = new DeviceCommandRequest();
    request.setDeviceId(DeviceId);
    
    request.setInstanceId("ea13791f-2659-4472-b8d8-d9b8637169c5");
    
    try {
        ListDeviceMessagesResponse response = client.listDeviceMessages(request);
        
        List<DeviceMessage> msglist=    response.getMessages();
        
        for(int i=0 ;i<msglist.size();i++)
        
        {
            DeviceMessage  msg=msglist.get(i);
            
               System.out.println("msg:"+msg.getMessage());
            
        }
        
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getRequestId());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
}

}

4、获取设备信息

public class ListDevicesSolution {

// REGION_ID:如果是上海一,请填写"cn-east-3";如果是北京四,请填写"cn-north-4";如果是华南广州,请填写"cn-south-4"
private static final String REGION_ID = "cn-south-4";
// ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
private static final String ENDPOINT = "4ef04886da.st1.iotda-app.cn-south-1.myhuaweicloud.com";

public static void main(String[] args) {

    sendCommand();
    
    
}


public static void sendCommand()
{
    // 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
    // 本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
    String ak = "ZO8QQXJKPRU8KQQRYXXX";        
    String sk = "WOov5WfFpVSS6n6ozbIepbcRb4XkDY1S95SdrkzT";
    String projectId = "4cbf723a0b124c49ad16679a819ec808";

    // 创建认证
    ICredential auth = new BasicCredentials()
           .withAk(ak)
           .withSk(sk)
           // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate"
           .withDerivedPredicate(BasicCredentials.DEFAULT_DERIVED_PREDICATE)
           .withProjectId(projectId);

    // 创建IoTDAClient实例并初始化
    IoTDAClient client = IoTDAClient.newBuilder()
            .withCredential(auth)
            // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
            .withRegion(new Region(REGION_ID, ENDPOINT))
           // .withRegion(IoTDARegion.CN_NORTH_4)
            .build();

    // 实例化请求对象
    ListDevicesRequest request = new ListDevicesRequest();
    try {
        // 调用查询设备列表接口
        ListDevicesResponse response = client.listDevices(request);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
    
}

}

总结

鸿蒙应用模拟器可以控制消息的下发命令,可以覆盖测试很多生产场景和稳定性测试的能力。

更多原创内容请关注:中软国际 OpenHarmony 技术团队

入门到精通、技巧到案例,系统化分享OpenHarmony开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐