🆕 #3818 【小程序】 新增设备组相关的 API 接口
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package cn.binarywang.wx.miniapp.api;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.device.*;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 小程序设备订阅消息相关 API
|
||||
* 文档:
|
||||
@@ -21,6 +22,7 @@ public interface WxMaDeviceSubscribeService {
|
||||
* 注意:
|
||||
* 设备ticket有效时间为5分钟
|
||||
* </pre>
|
||||
*
|
||||
* @param deviceTicketRequest
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
@@ -37,4 +39,53 @@ public interface WxMaDeviceSubscribeService {
|
||||
*/
|
||||
void sendDeviceSubscribeMsg(WxMaDeviceSubscribeMessageRequest deviceSubscribeMessageRequest) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 创建设备组
|
||||
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/createIotGroupId.html
|
||||
* </pre>
|
||||
*
|
||||
* @param createIotGroupIdRequest 请求参数
|
||||
* @return 设备组的唯一标识
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
String createIotGroupId(WxMaCreateIotGroupIdRequest createIotGroupIdRequest) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 查询设备组信息
|
||||
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/getIotGroupInfo.html
|
||||
* </pre>
|
||||
*
|
||||
* @param getIotGroupInfoRequest 请求参数
|
||||
* @return 设备组信息
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
WxMaIotGroupDeviceInfoResponse getIotGroupInfo(WxMaGetIotGroupInfoRequest getIotGroupInfoRequest) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 设备组添加设备
|
||||
* 一个设备组最多添加 50 个设备。 一个设备同一时间只能被添加到一个设备组中。
|
||||
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/addIotGroupDevice.html
|
||||
* </pre>
|
||||
*
|
||||
* @param request 请求参数
|
||||
* @return 成功添加的设备信息
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
List<WxMaDeviceTicketRequest> addIotGroupDevice(WxMaIotGroupDeviceRequest request) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 设备组删除设备
|
||||
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/removeIotGroupDevice.html
|
||||
* </pre>
|
||||
*
|
||||
* @param request 请求参数
|
||||
* @return 成功删除的设备信息
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
List<WxMaDeviceTicketRequest> removeIotGroupDevice(WxMaIotGroupDeviceRequest request) throws WxErrorException;
|
||||
|
||||
}
|
||||
|
||||
@@ -2,18 +2,26 @@ package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaDeviceSubscribeService;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.device.*;
|
||||
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.GET_SN_TICKET_URL;
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.SEND_DEVICE_SUBSCRIBE_MSG_URL;
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.CREATE_IOT_GROUP_ID_URL;
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.GET_IOT_GROUP_INFO_URL;
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.ADD_IOT_GROUP_DEVICE_URL;
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.REMOVE_IOT_GROUP_DEVICE_URL;
|
||||
|
||||
/**
|
||||
* 小程序设备订阅消息相关 API
|
||||
@@ -47,4 +55,46 @@ public class WxMaDeviceSubscribeServiceImpl implements WxMaDeviceSubscribeServic
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createIotGroupId(WxMaCreateIotGroupIdRequest createIotGroupIdRequest) throws WxErrorException {
|
||||
String responseContent = this.service.post(CREATE_IOT_GROUP_ID_URL, createIotGroupIdRequest.toJson());
|
||||
JsonObject jsonObject = GsonParser.parse(responseContent);
|
||||
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
return jsonObject.get("group_id").getAsString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaIotGroupDeviceInfoResponse getIotGroupInfo(WxMaGetIotGroupInfoRequest getIotGroupInfoRequest) throws WxErrorException {
|
||||
String responseContent = this.service.post(GET_IOT_GROUP_INFO_URL, getIotGroupInfoRequest.toJson());
|
||||
JsonObject jsonObject = GsonParser.parse(responseContent);
|
||||
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
return WxGsonBuilder.create().fromJson(responseContent, WxMaIotGroupDeviceInfoResponse.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<WxMaDeviceTicketRequest> addIotGroupDevice(WxMaIotGroupDeviceRequest request) throws WxErrorException {
|
||||
String responseContent = this.service.post(ADD_IOT_GROUP_DEVICE_URL, request.toJson());
|
||||
JsonObject jsonObject = GsonParser.parse(responseContent);
|
||||
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("device_list"), new TypeToken<List<WxMaDeviceTicketRequest>>() {
|
||||
}.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<WxMaDeviceTicketRequest> removeIotGroupDevice(WxMaIotGroupDeviceRequest request) throws WxErrorException {
|
||||
String responseContent = this.service.post(REMOVE_IOT_GROUP_DEVICE_URL, request.toJson());
|
||||
JsonObject jsonObject = GsonParser.parse(responseContent);
|
||||
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("device_list"), new TypeToken<List<WxMaDeviceTicketRequest>>() {
|
||||
}.getType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.binarywang.wx.miniapp.bean.device;
|
||||
|
||||
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author: yanglegetuo
|
||||
* @Date: 2025/12/22 8:51
|
||||
* @Description: 创建设备组 请求参数
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class WxMaCreateIotGroupIdRequest implements Serializable {
|
||||
private static final long serialVersionUID = 1827809470414413390L;
|
||||
/**
|
||||
* 设备型号id。通过注册设备获得(必填)
|
||||
*/
|
||||
@SerializedName("model_id")
|
||||
private String modelId;
|
||||
/**
|
||||
* 设备组的名称(创建时决定,无法修改)
|
||||
*/
|
||||
@SerializedName("group_name")
|
||||
private String groupName;
|
||||
|
||||
|
||||
public String toJson() {
|
||||
return WxMaGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.binarywang.wx.miniapp.bean.device;
|
||||
|
||||
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author: yanglegetuo
|
||||
* @Date: 2025/12/22 8:51
|
||||
* @Description: 查询设备组信息 请求参数
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class WxMaGetIotGroupInfoRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 4913375114243384968L;
|
||||
/**
|
||||
* 设备组的唯一标识(必填)
|
||||
*/
|
||||
@SerializedName("group_id")
|
||||
private String groupId;
|
||||
|
||||
public String toJson() {
|
||||
return WxMaGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package cn.binarywang.wx.miniapp.bean.device;
|
||||
|
||||
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: yanglegetuo
|
||||
* @Date: 2025/12/22 8:51
|
||||
* @Description: 设备组信息 响应参数
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class WxMaIotGroupDeviceInfoResponse implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6615660801230308048L;
|
||||
/**
|
||||
* 设备组名称
|
||||
*/
|
||||
@SerializedName("group_name")
|
||||
private String groupName;
|
||||
/**
|
||||
* 设备列表
|
||||
*/
|
||||
@SerializedName("device_list")
|
||||
private List<WxMaDeviceTicketRequest> deviceList;
|
||||
|
||||
/**
|
||||
* 设备型号id。通过注册设备获得(必填)
|
||||
*/
|
||||
@SerializedName("model_id")
|
||||
private String modelId;
|
||||
/**
|
||||
* 设备类型
|
||||
*/
|
||||
@SerializedName("model_type")
|
||||
private String modelType;
|
||||
|
||||
|
||||
public String toJson() {
|
||||
return WxMaGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package cn.binarywang.wx.miniapp.bean.device;
|
||||
|
||||
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: yanglegetuo
|
||||
* @Date: 2025/12/22 8:51
|
||||
* @Description: 设备组 添加/移除 设备 请求参数
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class WxMaIotGroupDeviceRequest implements Serializable {
|
||||
private static final long serialVersionUID = -5648997758678588138L;
|
||||
|
||||
/**
|
||||
* 设备组的唯一标识(必填)
|
||||
*/
|
||||
@SerializedName("group_id")
|
||||
private String groupId;
|
||||
/**
|
||||
* 设备列表
|
||||
*/
|
||||
@SerializedName("device_list")
|
||||
private List<WxMaDeviceTicketRequest> deviceList;
|
||||
/**
|
||||
* 是否强制更新设备列表,等于 true 时将已存在其它设备组中的设备移除并添加到当前设备组,慎用。
|
||||
*/
|
||||
@SerializedName("force_add")
|
||||
private Boolean forceAdd;
|
||||
|
||||
public String toJson() {
|
||||
return WxMaGsonBuilder.create().toJson(this);
|
||||
}
|
||||
}
|
||||
@@ -569,6 +569,14 @@ public class WxMaApiUrlConstants {
|
||||
/** 发送设备订阅消息 */
|
||||
String SEND_DEVICE_SUBSCRIBE_MSG_URL =
|
||||
"https://api.weixin.qq.com/cgi-bin/message/device/subscribe/send";
|
||||
/** 创建设备组 */
|
||||
String CREATE_IOT_GROUP_ID_URL = "https://api.weixin.qq.com/wxa/business/group/createid";
|
||||
/** 设备组添加设备 */
|
||||
String ADD_IOT_GROUP_DEVICE_URL = "https://api.weixin.qq.com/wxa/business/group/adddevice";
|
||||
/** 设备组删除设备 */
|
||||
String REMOVE_IOT_GROUP_DEVICE_URL = "https://api.weixin.qq.com/wxa/business/group/removedevice";
|
||||
/** 查询设备组信息 */
|
||||
String GET_IOT_GROUP_INFO_URL = "https://api.weixin.qq.com/wxa/business/group/getinfo";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,24 +1,27 @@
|
||||
package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.device.*;
|
||||
import cn.binarywang.wx.miniapp.test.ApiTestModule;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.inject.Inject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 小程序设备订阅消息相关 测试类
|
||||
*
|
||||
* @author <a href="https://github.com/leejuncheng">JCLee</a>
|
||||
* @since 2021-12-16 17:13:35
|
||||
*/
|
||||
@Slf4j
|
||||
@Test
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMaDeviceSubscribeServiceImplTest {
|
||||
@@ -27,7 +30,7 @@ public class WxMaDeviceSubscribeServiceImplTest {
|
||||
protected WxMaService wxService;
|
||||
|
||||
@Test
|
||||
public void testGetSnTicket() throws WxErrorException{
|
||||
public void testGetSnTicket() throws WxErrorException {
|
||||
WxMaDeviceTicketRequest wxMaDeviceTicketRequest = new WxMaDeviceTicketRequest();
|
||||
wxMaDeviceTicketRequest.setModelId("11111");
|
||||
wxMaDeviceTicketRequest.setSn("11111");
|
||||
@@ -36,7 +39,7 @@ public class WxMaDeviceSubscribeServiceImplTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendDeviceSubscribeMsg() throws WxErrorException{
|
||||
public void sendDeviceSubscribeMsg() throws WxErrorException {
|
||||
WxMaDeviceSubscribeMessageRequest wxMaDeviceSubscribeMessageRequest = new WxMaDeviceSubscribeMessageRequest();
|
||||
wxMaDeviceSubscribeMessageRequest.setToOpenidList(Lists.newArrayList("1", "2"));
|
||||
wxMaDeviceSubscribeMessageRequest.setPage("pages/index/index");
|
||||
@@ -56,4 +59,46 @@ public class WxMaDeviceSubscribeServiceImplTest {
|
||||
wxMaDeviceSubscribeMessageRequest.setData(data);
|
||||
this.wxService.getDeviceSubscribeService().sendDeviceSubscribeMsg(wxMaDeviceSubscribeMessageRequest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateIotGroupId() throws WxErrorException {
|
||||
WxMaCreateIotGroupIdRequest request = new WxMaCreateIotGroupIdRequest();
|
||||
request.setModelId("11111");
|
||||
request.setGroupName("测试设备组");
|
||||
String groupId = this.wxService.getDeviceSubscribeService().createIotGroupId(request);
|
||||
System.out.println(groupId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetIotGroupInfo() throws WxErrorException {
|
||||
WxMaGetIotGroupInfoRequest request = new WxMaGetIotGroupInfoRequest();
|
||||
request.setGroupId("12313123");
|
||||
WxMaIotGroupDeviceInfoResponse response = this.wxService.getDeviceSubscribeService().getIotGroupInfo(request);
|
||||
log.info("testGetIotGroupInfo = {}", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddIotGroupDevice() throws WxErrorException {
|
||||
WxMaDeviceTicketRequest deviceTicketRequest = new WxMaDeviceTicketRequest();
|
||||
deviceTicketRequest.setSn("2222222");
|
||||
deviceTicketRequest.setModelId("sdfeweee");
|
||||
WxMaIotGroupDeviceRequest request = new WxMaIotGroupDeviceRequest();
|
||||
request.setGroupId("12313123");
|
||||
request.setDeviceList(Collections.singletonList(deviceTicketRequest));
|
||||
request.setForceAdd(true);
|
||||
List<WxMaDeviceTicketRequest> response = this.wxService.getDeviceSubscribeService().addIotGroupDevice(request);
|
||||
log.info("testAddIotGroupDevice = {}", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveIotGroupDevice() throws WxErrorException {
|
||||
WxMaDeviceTicketRequest deviceTicketRequest = new WxMaDeviceTicketRequest();
|
||||
deviceTicketRequest.setSn("2222222");
|
||||
deviceTicketRequest.setModelId("sdfeweee");
|
||||
WxMaIotGroupDeviceRequest request = new WxMaIotGroupDeviceRequest();
|
||||
request.setGroupId("12313123");
|
||||
request.setDeviceList(Collections.singletonList(deviceTicketRequest));
|
||||
List<WxMaDeviceTicketRequest> response = this.wxService.getDeviceSubscribeService().removeIotGroupDevice(request);
|
||||
log.info("testRemoveIotGroupDevice = {}", response);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user