本文简要介绍了产品物模型通信的使用方法,并提供了示例代码
物模型通信SDK使用示例
准备工作:
- 使用SDK中的AfuConfig配置类,进行 AfuConfig afuConfig = new AfuConfig();
- 调用afuConfig.setXXX方法设置准备参数,以下是配置类详解:
_/*
请登录安服优物联网云平台—>个人中心查看
*/
_private String userAccessKey = “xxxxxx”;
private String userSecret = “xxxxxx”;
private String version = “1.0”; //默认
_/*
连接安服优物联网云平台
*/
_private String mqttUrl = “tcp://mqtt.afuiot.com:1883”; //连接安服优物联网云平台MQTT服务器
private String mqttClientId = “xxxxxx”; //用于区分MQTT客户端,每个设备请设置唯一的ClientId
private String productKey = “XXXXXX”; //创建产品后,系统生成,在产品详情页查看
private String deviceName = “XXXXXX”; //创建设备设置的名称
private String deviceSecret = “XXXXXXXXX”; //创建设备后,系统生成,在设备详情页查看
- AfuConfig配置的信息设置完成,创建 AfuDeviceLinkMqtt 对象,把AfuConfig对象传递入参,如:
AfuDeviceLinkMqtt linkMqtt = new AfuDeviceLinkMqtt(afuConfig);
- 在创建 AfuThingModelKit 对象,把AfuDeviceLinkMqtt对象传递入参,如:
AfuThingModelKit thingModelKit = new AfuThingModelKit(linkMqtt);
物模型方法使用:
//属性上报:默认模块
JSONObject params = new JSONObject();
JSONObject switch1 = new JSONObject();
switch1.put(“value”,”true”); //上报状态值
switch1.put(“time”,System.currentTimeMillis());
params.put(“switch1”,switch1); //switch1为物模型定义的功能标示
thingModelKit.handlePropertySet(params);
//属性上报:自定义模块
JSONObject params = new JSONObject();
JSONObject switch1 = new JSONObject();
switch1.put(“value”,”false”);
switch1.put(“time”,System.currentTimeMillis());
params.put(“1:switch1”,switch1); //1为自定义模块标示,switch1为物模型定义的功能标示
thingModelKit.handlePropertySet(params);
//事件上报:默认模块
JSONObject outParams = new JSONObject(); //输出参数
JSONObject value = new JSONObject();
value.put(“Power”,”off”);
value.put(“WF”,”987654321”);
outParams.put(“value”,value);
outParams.put(“time”,System.currentTimeMillis());
thingModelKit.handleEventSet(null,”defaultTestEvent”,outParams); //defaultTestEvent为事件功能标示
//事件上报:自定义模块
JSONObject outParams = new JSONObject(); //输出参数
JSONObject value = new JSONObject();
value.put(“Power”,”on”);
value.put(“WF”,”123456789”);
outParams.put(“value”,value);
outParams.put(“time”,System.currentTimeMillis());
thingModelKit.handleEventSet(“testEventModel”,”testEvent”,outParams); //testEventModel表示自定义模块标示,testEvent为事件功能标示
//服务上报:默认模块
JSONObject params = new JSONObject();
params.put(“Power”,”on”);
params.put(“WF”,”123456789”);
thingModelKit.handleServiceSet(null,”defaultTestService”,params); //defaultTestService为服务功能标示
//服务上报:自定义模块
JSONObject params = new JSONObject();
params.put(“Power”,”on”);
params.put(“WF”,”123456789”);
thingModelKit.handleServiceSet(“testServiceModel”,”defaultTestService”,params); //testServiceModel为自定义的模块标示,defaultTestService为服务功能标示
完整demo如下:
public static void main(String[] args) throws Exception {
AfuConfig afuConfig = new AfuConfig();
afuConfig.setMqttUrl("tcp://mqtt.afuiot.com:1883");
afuConfig.setMqttClientId("7sgHseeoq5K/Pfp5sUpUqm8");
afuConfig.setProductKey("Pfp5sUpUqm8");
afuConfig.setDeviceName("7sgHseeoq5K");
afuConfig.setDeviceSecret("1622519161821iyie1qRgbQhd");
afuConfig.setUserAccessKey("d83ebdc3b89941ccb5521281105e6db9nXEgEkHR");
afuConfig.setUserSecret("nsPrRO16Ti");
AfuDeviceLinkMqtt linkMqtt = new AfuDeviceLinkMqtt(afuConfig);
AfuThingModelKit thingModelKit = new AfuThingModelKit(linkMqtt);
//影子文档修改和添加
/*JSONObject params = new JSONObject();
JSONObject attribute = new JSONObject();
attribute.put("color","red");
params.put("desired",attribute);
params.put("version",1);
thingModelKit.updateShadow(params);*/
//属性上报:默认模块
JSONObject params = new JSONObject();
JSONObject switch1 = new JSONObject();
switch1.put("value","true");
switch1.put("time",System.currentTimeMillis());
params.put("switch1",switch1);
thingModelKit.handlePropertySet(params);
//属性上报:自定义模块
/*JSONObject params = new JSONObject();
JSONObject switch1 = new JSONObject();
switch1.put("value","false");
switch1.put("time",System.currentTimeMillis());
params.put("1:switch1",switch1); //1:表示自定义功能模块标示
thingModelKit.handlePropertySet(params);*/
//属性设置
//事件上报:默认模块
/*JSONObject outParams = new JSONObject(); //输出参数
JSONObject value = new JSONObject();
value.put("Power","off");
value.put("WF","987654321");
outParams.put("value",value);
outParams.put("time",System.currentTimeMillis());
thingModelKit.handleEventSet(null,"defaultTestEvent",outParams);*/
//事件上报:自定义模块
/*JSONObject outParams = new JSONObject(); //输出参数
JSONObject value = new JSONObject();
value.put("Power","on");
value.put("WF","123456789");
outParams.put("value",value);
outParams.put("time",System.currentTimeMillis());
thingModelKit.handleEventSet("testEventModel","testEvent",outParams);*/
//服务上报:默认模块
/*JSONObject params = new JSONObject();
params.put("Power","on");
params.put("WF","123456789");
thingModelKit.handleServiceSet(null,"defaultTestService",params);*/
//服务上报:自定义模块
/*JSONObject params = new JSONObject();
params.put("Power","on");
params.put("WF","123456789");
thingModelKit.handleServiceSet("testServiceModel","defaultTestService",params);*/
//根据需求,是否关闭连接
//linkMqtt.disconnect(afuConfig.getMqttClientId());
}