Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
* 文档:
Expand All @@ -21,6 +22,7 @@ public interface WxMaDeviceSubscribeService {
* 注意:
* 设备ticket有效时间为5分钟
* </pre>
*
* @param deviceTicketRequest
* @return
* @throws WxErrorException
Expand All @@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

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
Expand Down Expand Up @@ -47,4 +55,46 @@ public void sendDeviceSubscribeMsg(WxMaDeviceSubscribeMessageRequest deviceSubsc
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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,14 @@ public interface DeviceSubscribe {
/** 发送设备订阅消息 */
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";
}

/**
Expand Down
Loading