diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpDeviceService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpDeviceService.java new file mode 100644 index 00000000..a565011c --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpDeviceService.java @@ -0,0 +1,80 @@ +package me.chanjar.weixin.mp.api; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.mp.bean.device.*; + +/** + * Created by keungtung on 10/12/2016. + */ +public interface WxMpDeviceService { + /** + *
+ * 主动发送消息给设备 + * 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-3 + *+ */ + TransMsgResp transMsg(WxDeviceMsg msg) throws WxErrorException; + + /** + *
+ * 获取一组新的deviceid和设备二维码 + * 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-6 + *+ * @param productId 产品id + * @return 返回WxDeviceQrCodeResult + */ + WxDeviceQrCodeResult getQrCode(String productId) throws WxErrorException; + + /** + *
+ * 将device id及其属性信息提交公众平台进行授权 + * 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-6 + *+ * @param wxDeviceAuthorize 授权请求对象 + * @return WxDeviceAuthorizeResult + */ + WxDeviceAuthorizeResult authorize(WxDeviceAuthorize wxDeviceAuthorize) throws WxErrorException; + + + /** + *
+ * 第三方后台绑定成功后,通知公众平台 + * 详情请见:http://iot.weixin.qq.com/wiki/new/index.html/page=3-4-7 + *+ * @param wxDeviceBind 绑定请求对象 + * @return WxDeviceBindResult + */ + WxDeviceBindResult bind(WxDeviceBind wxDeviceBind) throws WxErrorException; + + /** + *
+ * 强制绑定用户和设备 + * 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7 + *+ * @param wxDeviceBind 强制绑定请求对象 + * @return WxDeviceBindResult + */ + WxDeviceBindResult compelBind(WxDeviceBind wxDeviceBind) throws WxErrorException; + + /** + *
+ * 第三方确认用户和设备的解绑操作 + * 详情请见:http://iot.weixin.qq.com/wiki/new/index.html/page=3-4-7 + *+ * @param wxDeviceBind 绑定请求对象 + * @return WxDeviceBidResult + */ + WxDeviceBindResult unbind(WxDeviceBind wxDeviceBind) throws WxErrorException; + + /** + *
+ * 强制解绑用户和设备 + * 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7 + *+ * @param wxDeviceBind 强制解绑请求对象 + * @return WxDeviceBindResult + */ + WxDeviceBindResult compelUnbind(WxDeviceBind wxDeviceBind) throws WxErrorException; + + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java index 4fa91bad..dc05a2ee 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java @@ -345,4 +345,11 @@ public interface WxMpService { * @return WxMpTemplateMsgService */ WxMpTemplateMsgService getTemplateMsgService(); + + /** + * 返回硬件平台相关接口方法的实现类对象,以方便调用其各个接口 + * + * @return WxMpDeviceService + */ + WxMpDeviceService getDeviceService(); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDeviceServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDeviceServiceImpl.java new file mode 100644 index 00000000..e7dbea1b --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDeviceServiceImpl.java @@ -0,0 +1,72 @@ +package me.chanjar.weixin.mp.api.impl; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.mp.api.WxMpDeviceService; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.bean.device.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by keungtung on 10/12/2016. + */ +public class WxMpDeviceServiceImpl implements WxMpDeviceService { + private static final String API_URL_PREFIX = "https://api.weixin.qq.com/device"; + private static Logger log = LoggerFactory.getLogger(WxMpMenuServiceImpl.class); + + private WxMpService wxMpService; + + WxMpDeviceServiceImpl(WxMpService wxMpService) { + this.wxMpService = wxMpService; + } + + @Override + public TransMsgResp transMsg(WxDeviceMsg msg) throws WxErrorException { + String url = API_URL_PREFIX + "/transmsg"; + String response = this.wxMpService.post(url,msg.toJson()); + return TransMsgResp.fromJson(response); + } + + @Override + public WxDeviceQrCodeResult getQrCode(String productId) throws WxErrorException { + String url = API_URL_PREFIX + "/getqrcode"; + String response = this.wxMpService.get(url, "product_id=" + productId); + return WxDeviceQrCodeResult.fromJson(response); + } + + @Override + public WxDeviceAuthorizeResult authorize(WxDeviceAuthorize wxDeviceAuthorize) throws WxErrorException { + String url = API_URL_PREFIX + "/authorize_device"; + String response = this.wxMpService.post(url,wxDeviceAuthorize.toJson()); + return WxDeviceAuthorizeResult.fromJson(response); + } + + @Override + public WxDeviceBindResult bind(WxDeviceBind wxDeviceBind) throws WxErrorException { + String url = API_URL_PREFIX + "/bind"; + String response = this.wxMpService.post(url,wxDeviceBind.toJson()); + return WxDeviceBindResult.fromJson(response); + } + + @Override + public WxDeviceBindResult compelBind(WxDeviceBind wxDeviceBind) throws WxErrorException { + String url = API_URL_PREFIX + "/compel_bind"; + String response = this.wxMpService.post(url,wxDeviceBind.toJson()); + return WxDeviceBindResult.fromJson(response); + } + + @Override + public WxDeviceBindResult unbind(WxDeviceBind wxDeviceBind) throws WxErrorException { + String url = API_URL_PREFIX + "/unbind?"; + String response = this.wxMpService.post(url, wxDeviceBind.toJson()); + return WxDeviceBindResult.fromJson(response); + } + + @Override + public WxDeviceBindResult compelUnbind(WxDeviceBind wxDeviceBind) throws WxErrorException { + String url = API_URL_PREFIX + "/compel_unbind?"; + String response = this.wxMpService.post(url, wxDeviceBind.toJson()); + return WxDeviceBindResult.fromJson(response); + } +} + diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java index 46fa3157..3c602325 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java @@ -62,6 +62,8 @@ public class WxMpServiceImpl implements WxMpService { private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this); + private WxMpDeviceService deviceService = new WxMpDeviceServiceImpl(this); + private CloseableHttpClient httpClient; private HttpHost httpProxy; @@ -540,4 +542,8 @@ public class WxMpServiceImpl implements WxMpService { return this.templateMsgService; } + @Override + public WxMpDeviceService getDeviceService() { + return this.deviceService; + } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/AbstractDeviceBean.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/AbstractDeviceBean.java new file mode 100644 index 00000000..3a327069 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/AbstractDeviceBean.java @@ -0,0 +1,12 @@ +package me.chanjar.weixin.mp.bean.device; + +import me.chanjar.weixin.common.util.json.WxGsonBuilder; + +/** + * Created by keungtung on 14/12/2016. + */ +public abstract class AbstractDeviceBean { + public String toJson() { + return WxGsonBuilder.create().toJson(this); + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/BaseResp.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/BaseResp.java new file mode 100644 index 00000000..5a3082f6 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/BaseResp.java @@ -0,0 +1,55 @@ +package me.chanjar.weixin.mp.bean.device; + +/** + * Created by keungtung on 10/12/2016. + */ +public class BaseResp extends AbstractDeviceBean{ + private BaseInfo base_info; + private Integer errcode; + private String errmsg; + + public Integer getErrcode() { + return errcode; + } + + public void setErrcode(Integer errcode) { + this.errcode = errcode; + } + + public BaseInfo getBase_info() { + return base_info; + } + + public void setBase_info(BaseInfo base_info) { + this.base_info = base_info; + } + + public String getErrmsg() { + return errmsg; + } + + public void setErrmsg(String errmsg) { + this.errmsg = errmsg; + } + + private class BaseInfo { + private String device_type; + private String device_id; + + public String getDevice_type() { + return device_type; + } + + public void setDevice_type(String device_type) { + this.device_type = device_type; + } + + public String getDevice_id() { + return device_id; + } + + public void setDevice_id(String device_id) { + this.device_id = device_id; + } + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/RespMsg.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/RespMsg.java new file mode 100644 index 00000000..e0291fbb --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/RespMsg.java @@ -0,0 +1,26 @@ +package me.chanjar.weixin.mp.bean.device; + +/** + * Created by keungtung on 10/12/2016. + */ + +public class RespMsg extends AbstractDeviceBean{ + private Integer retCode; + private String errorInfo; + + public Integer getRetCode() { + return retCode; + } + + public void setRetCode(Integer retCode) { + this.retCode = retCode; + } + + public String getErrorInfo() { + return errorInfo; + } + + public void setErrorInfo(String errorInfo) { + this.errorInfo = errorInfo; + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/TransMsgResp.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/TransMsgResp.java new file mode 100644 index 00000000..6d5fbe06 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/TransMsgResp.java @@ -0,0 +1,49 @@ +package me.chanjar.weixin.mp.bean.device; + +import me.chanjar.weixin.common.util.json.WxGsonBuilder; + +/** + * Created by keungtung on 14/12/2016. + */ +public class TransMsgResp extends AbstractDeviceBean{ + private Integer ret; + private String ret_info; + private Integer errcode; + private String errmsg; + + public static TransMsgResp fromJson(String json) { + return WxGsonBuilder.create().fromJson(json, TransMsgResp.class); + } + + public Integer getRet() { + return ret; + } + + public void setRet(Integer ret) { + this.ret = ret; + } + + public String getRet_info() { + return ret_info; + } + + public void setRet_info(String ret_info) { + this.ret_info = ret_info; + } + + public Integer getErrcode() { + return errcode; + } + + public void setErrcode(Integer errcode) { + this.errcode = errcode; + } + + public String getErrmsg() { + return errmsg; + } + + public void setErrmsg(String errmsg) { + this.errmsg = errmsg; + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/WxDevice.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/WxDevice.java new file mode 100644 index 00000000..f36c4b10 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/WxDevice.java @@ -0,0 +1,106 @@ +package me.chanjar.weixin.mp.bean.device; + +/** + * Created by keungtung on 10/12/2016. + */ +public class WxDevice { + private String id; + private String mac; + private String connect_protocol; + private String auth_key; + private String close_strategy; + private String conn_strategy; + private String crypt_method; + private String auth_ver; + private String manu_mac_pos; + private String ser_mac_pos; + private String ble_simple_protocol; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getMac() { + return mac; + } + + public void setMac(String mac) { + this.mac = mac; + } + + public String getConnect_protocol() { + return connect_protocol; + } + + public void setConnect_protocol(String connect_protocol) { + this.connect_protocol = connect_protocol; + } + + public String getAuth_key() { + return auth_key; + } + + public void setAuth_key(String auth_key) { + this.auth_key = auth_key; + } + + public String getClose_strategy() { + return close_strategy; + } + + public void setClose_strategy(String close_strategy) { + this.close_strategy = close_strategy; + } + + public String getConn_strategy() { + return conn_strategy; + } + + public void setConn_strategy(String conn_strategy) { + this.conn_strategy = conn_strategy; + } + + public String getCrypt_method() { + return crypt_method; + } + + public void setCrypt_method(String crypt_method) { + this.crypt_method = crypt_method; + } + + public String getAuth_ver() { + return auth_ver; + } + + public void setAuth_ver(String auth_ver) { + this.auth_ver = auth_ver; + } + + public String getManu_mac_pos() { + return manu_mac_pos; + } + + public void setManu_mac_pos(String manu_mac_pos) { + this.manu_mac_pos = manu_mac_pos; + } + + public String getSer_mac_pos() { + return ser_mac_pos; + } + + public void setSer_mac_pos(String ser_mac_pos) { + this.ser_mac_pos = ser_mac_pos; + } + + public String getBle_simple_protocol() { + return ble_simple_protocol; + } + + public void setBle_simple_protocol(String ble_simple_protocol) { + this.ble_simple_protocol = ble_simple_protocol; + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/WxDeviceAuthorize.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/WxDeviceAuthorize.java new file mode 100644 index 00000000..e1dcd9be --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/device/WxDeviceAuthorize.java @@ -0,0 +1,42 @@ +package me.chanjar.weixin.mp.bean.device; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +/** + * Created by keungtung on 10/12/2016. + */ +public class WxDeviceAuthorize extends AbstractDeviceBean { + private String deivce_num; + private String op_type; + private List