@@ -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 { | |||||
/** | |||||
* <pre> | |||||
* 主动发送消息给设备 | |||||
* 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-3 | |||||
* </pre> | |||||
*/ | |||||
TransMsgResp transMsg(WxDeviceMsg msg) throws WxErrorException; | |||||
/** | |||||
* <pre> | |||||
* 获取一组新的deviceid和设备二维码 | |||||
* 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-6 | |||||
* </pre> | |||||
* @param productId 产品id | |||||
* @return 返回WxDeviceQrCodeResult | |||||
*/ | |||||
WxDeviceQrCodeResult getQrCode(String productId) throws WxErrorException; | |||||
/** | |||||
* <pre> | |||||
* 将device id及其属性信息提交公众平台进行授权 | |||||
* 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-6 | |||||
* </pre> | |||||
* @param wxDeviceAuthorize 授权请求对象 | |||||
* @return WxDeviceAuthorizeResult | |||||
*/ | |||||
WxDeviceAuthorizeResult authorize(WxDeviceAuthorize wxDeviceAuthorize) throws WxErrorException; | |||||
/** | |||||
* <pre> | |||||
* 第三方后台绑定成功后,通知公众平台 | |||||
* 详情请见:http://iot.weixin.qq.com/wiki/new/index.html/page=3-4-7 | |||||
* </pre> | |||||
* @param wxDeviceBind 绑定请求对象 | |||||
* @return WxDeviceBindResult | |||||
*/ | |||||
WxDeviceBindResult bind(WxDeviceBind wxDeviceBind) throws WxErrorException; | |||||
/** | |||||
* <pre> | |||||
* 强制绑定用户和设备 | |||||
* 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7 | |||||
* </pre> | |||||
* @param wxDeviceBind 强制绑定请求对象 | |||||
* @return WxDeviceBindResult | |||||
*/ | |||||
WxDeviceBindResult compelBind(WxDeviceBind wxDeviceBind) throws WxErrorException; | |||||
/** | |||||
* <pre> | |||||
* 第三方确认用户和设备的解绑操作 | |||||
* 详情请见:http://iot.weixin.qq.com/wiki/new/index.html/page=3-4-7 | |||||
* </pre> | |||||
* @param wxDeviceBind 绑定请求对象 | |||||
* @return WxDeviceBidResult | |||||
*/ | |||||
WxDeviceBindResult unbind(WxDeviceBind wxDeviceBind) throws WxErrorException; | |||||
/** | |||||
* <pre> | |||||
* 强制解绑用户和设备 | |||||
* 详情请见:http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7 | |||||
* </pre> | |||||
* @param wxDeviceBind 强制解绑请求对象 | |||||
* @return WxDeviceBindResult | |||||
*/ | |||||
WxDeviceBindResult compelUnbind(WxDeviceBind wxDeviceBind) throws WxErrorException; | |||||
} |
@@ -345,4 +345,11 @@ public interface WxMpService { | |||||
* @return WxMpTemplateMsgService | * @return WxMpTemplateMsgService | ||||
*/ | */ | ||||
WxMpTemplateMsgService getTemplateMsgService(); | WxMpTemplateMsgService getTemplateMsgService(); | ||||
/** | |||||
* 返回硬件平台相关接口方法的实现类对象,以方便调用其各个接口 | |||||
* | |||||
* @return WxMpDeviceService | |||||
*/ | |||||
WxMpDeviceService getDeviceService(); | |||||
} | } |
@@ -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); | |||||
} | |||||
} | |||||
@@ -62,6 +62,8 @@ public class WxMpServiceImpl implements WxMpService { | |||||
private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this); | private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this); | ||||
private WxMpDeviceService deviceService = new WxMpDeviceServiceImpl(this); | |||||
private CloseableHttpClient httpClient; | private CloseableHttpClient httpClient; | ||||
private HttpHost httpProxy; | private HttpHost httpProxy; | ||||
@@ -540,4 +542,8 @@ public class WxMpServiceImpl implements WxMpService { | |||||
return this.templateMsgService; | return this.templateMsgService; | ||||
} | } | ||||
@Override | |||||
public WxMpDeviceService getDeviceService() { | |||||
return this.deviceService; | |||||
} | |||||
} | } |
@@ -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); | |||||
} | |||||
} |
@@ -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; | |||||
} | |||||
} | |||||
} |
@@ -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; | |||||
} | |||||
} |
@@ -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; | |||||
} | |||||
} |
@@ -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; | |||||
} | |||||
} |
@@ -0,0 +1,36 @@ | |||||
package me.chanjar.weixin.mp.bean.device; | |||||
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<WxDevice> device_list; | |||||
public String getDeivce_num() { | |||||
return deivce_num; | |||||
} | |||||
public void setDeivce_num(String deivce_num) { | |||||
this.deivce_num = deivce_num; | |||||
} | |||||
public String getOp_type() { | |||||
return op_type; | |||||
} | |||||
public void setOp_type(String op_type) { | |||||
this.op_type = op_type; | |||||
} | |||||
public List<WxDevice> getDevice_list() { | |||||
return device_list; | |||||
} | |||||
public void setDevice_list(List<WxDevice> device_list) { | |||||
this.device_list = device_list; | |||||
} | |||||
} |
@@ -0,0 +1,24 @@ | |||||
package me.chanjar.weixin.mp.bean.device; | |||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||||
import java.util.List; | |||||
/** | |||||
* Created by keungtung on 10/12/2016. | |||||
*/ | |||||
public class WxDeviceAuthorizeResult extends AbstractDeviceBean{ | |||||
private List<BaseResp> resp; | |||||
public static WxDeviceAuthorizeResult fromJson(String response) { | |||||
return WxGsonBuilder.create().fromJson(response, WxDeviceAuthorizeResult.class); | |||||
} | |||||
public List<BaseResp> getResp() { | |||||
return resp; | |||||
} | |||||
public void setResp(List<BaseResp> resp) { | |||||
this.resp = resp; | |||||
} | |||||
} |
@@ -0,0 +1,34 @@ | |||||
package me.chanjar.weixin.mp.bean.device; | |||||
/** | |||||
* Created by keungtung on 10/12/2016. | |||||
*/ | |||||
public class WxDeviceBind extends AbstractDeviceBean{ | |||||
private String ticket; | |||||
private String device_id; | |||||
private String openid; | |||||
public String getTicket() { | |||||
return ticket; | |||||
} | |||||
public void setTicket(String ticket) { | |||||
this.ticket = ticket; | |||||
} | |||||
public String getDevice_id() { | |||||
return device_id; | |||||
} | |||||
public void setDevice_id(String device_id) { | |||||
this.device_id = device_id; | |||||
} | |||||
public String getOpenid() { | |||||
return openid; | |||||
} | |||||
public void setOpenid(String openid) { | |||||
this.openid = openid; | |||||
} | |||||
} |
@@ -0,0 +1,22 @@ | |||||
package me.chanjar.weixin.mp.bean.device; | |||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||||
/** | |||||
* Created by keungtung on 10/12/2016. | |||||
*/ | |||||
public class WxDeviceBindResult extends AbstractDeviceBean{ | |||||
private BaseResp base_resp; | |||||
public static WxDeviceBindResult fromJson(String json) { | |||||
return WxMpGsonBuilder.create().fromJson(json, WxDeviceBindResult.class); | |||||
} | |||||
public BaseResp getBase_resp() { | |||||
return base_resp; | |||||
} | |||||
public void setBase_resp(BaseResp base_resp) { | |||||
this.base_resp = base_resp; | |||||
} | |||||
} |
@@ -0,0 +1,50 @@ | |||||
package me.chanjar.weixin.mp.bean.device; | |||||
import me.chanjar.weixin.common.util.ToStringUtils; | |||||
/** | |||||
* Created by keungtung on 10/12/2016. | |||||
*/ | |||||
public class WxDeviceMsg extends AbstractDeviceBean{ | |||||
private String deviceType; | |||||
private String deviceId; | |||||
private String openId; | |||||
private String content; | |||||
@Override | |||||
public String toString() { | |||||
return ToStringUtils.toSimpleString(this); | |||||
} | |||||
public String getDeviceType() { | |||||
return deviceType; | |||||
} | |||||
public void setDeviceType(String deviceType) { | |||||
this.deviceType = deviceType; | |||||
} | |||||
public String getDeviceId() { | |||||
return deviceId; | |||||
} | |||||
public void setDeviceId(String deviceId) { | |||||
this.deviceId = deviceId; | |||||
} | |||||
public String getOpenId() { | |||||
return openId; | |||||
} | |||||
public void setOpenId(String openId) { | |||||
this.openId = openId; | |||||
} | |||||
public String getContent() { | |||||
return content; | |||||
} | |||||
public void setContent(String content) { | |||||
this.content = content; | |||||
} | |||||
} |
@@ -0,0 +1,49 @@ | |||||
package me.chanjar.weixin.mp.bean.device; | |||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||||
/** | |||||
* Created by keungtung on 10/12/2016. | |||||
*/ | |||||
public class WxDeviceQrCodeResult extends AbstractDeviceBean{ | |||||
private String deviceid; | |||||
private String qrticket; | |||||
private String devcielicence; | |||||
private RespMsg resp_msg; | |||||
public static WxDeviceQrCodeResult fromJson(String json) { | |||||
return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxDeviceQrCodeResult.class); | |||||
} | |||||
public String getDevcielicence() { | |||||
return devcielicence; | |||||
} | |||||
public void setDevcielicence(String devcielicence) { | |||||
this.devcielicence = devcielicence; | |||||
} | |||||
public RespMsg getResp_msg() { | |||||
return resp_msg; | |||||
} | |||||
public void setResp_msg(RespMsg resp_msg) { | |||||
this.resp_msg = resp_msg; | |||||
} | |||||
public String getDeviceid() { | |||||
return deviceid; | |||||
} | |||||
public void setDeviceid(String deviceid) { | |||||
this.deviceid = deviceid; | |||||
} | |||||
public String getQrticket() { | |||||
return qrticket; | |||||
} | |||||
public void setQrticket(String qrticket) { | |||||
this.qrticket = qrticket; | |||||
} | |||||
} |
@@ -0,0 +1,40 @@ | |||||
package me.chanjar.weixin.mp.api.impl; | |||||
import com.google.inject.Inject; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||||
import me.chanjar.weixin.mp.api.WxMpService; | |||||
import me.chanjar.weixin.mp.bean.device.WxDeviceQrCodeResult; | |||||
import org.testng.annotations.DataProvider; | |||||
import org.testng.annotations.Guice; | |||||
import org.testng.annotations.Test; | |||||
/** | |||||
* Created by keungtung on 14/12/2016. | |||||
*/ | |||||
@Test(groups = "deviceApi") | |||||
@Guice(modules = ApiTestModule.class) | |||||
public class WxMpDeviceServiceImplTest { | |||||
@Inject | |||||
protected WxMpService wxService; | |||||
@Test(dataProvider = "productId") | |||||
public void testGetQrcode(String productId) { | |||||
try { | |||||
WxDeviceQrCodeResult result = wxService.getDeviceService().getQrCode(productId); | |||||
println(result.toJson()); | |||||
} catch (WxErrorException e) { | |||||
println(e.getMessage()); | |||||
} | |||||
} | |||||
private void println(String content) { | |||||
System.out.println(content); | |||||
} | |||||
@DataProvider(name = "productId") | |||||
public Object[][] getProductId() { | |||||
return new Object[][]{new Object[]{"25639"}}; | |||||
} | |||||
} |