设备功能
在“设备功能”处添加产品,接入方案选择“平台基础接入方案”
添加成功后,就有100个配额
服务器配置
URL是后台回调接口地址,Token是为了验证回调信息的合法性的,EncodingAESKey随机生成即可
服务器配置回调校验开发
配置校验回调是以GET方式回调的,同消息事件回调是一个地址,只是请求方式不同。
如果验证成功,则响应回调请求的echostr值,否则啥也不返回。
代码示例:
/**
* 微信硬件平台配置校验
*
* @param appId
* @param request
* @param response
*/
@RequestMapping(value = "/hardware/{appId}", method = RequestMethod.GET)
@ApiOperation(value="微信硬件平台配置校验回调", notes="微信硬件平台配置校验回调")
public void receiveIotMsg(@PathVariable("appId") String appId, HttpServletRequest request, HttpServletResponse response) {
logger.info("收到微信硬件消息回调GET:{}", appId);
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
Map<String, String[]> map = request.getParameterMap();
for (Map.Entry<String, String[]> entry : map.entrySet()) {
logger.info("{}={}", entry.getKey(), entry.getValue() != null ? entry.getValue()[0] : "");
}
// 获取公众号信息
SingleParamRequest<String> singleParamRequest = new SingleParamRequest<>();
singleParamRequest.setData(appId);
ServiceJsonBean<OfficialAccount> officialAccountJsonBean = weiXinOfficialAccountService.getOfficialAccountByAppId(singleParamRequest);
String iotToken = "";
if (officialAccountJsonBean.getCode() == ErrorCode.SUCCESS.getCode() && officialAccountJsonBean.getData() != null) {
iotToken = officialAccountJsonBean.getData().getIotToken();
}
String curSign = SecurityUtils.encryptSHA1(iotToken, timestamp, nonce);
if (StringUtils.isEquals(signature, curSign) && StringUtils.isNotEmpty(echostr)) {
logger.info("验证成功!");
try {
response.getWriter().write(echostr);
} catch (IOException e) {
logger.error("response输出异常", e);
}
}
}
设备消息、事件回调开发
消息、事件回调跟校验回调是同一个接口地址,但是请求方式为POST,Content-Type: application/json
消息、事件会以json格式的传输到后台
/**
* 微信硬件平台消息回调接口
*
* @param appId
* @return
*/
@RequestMapping(value = "/hardware/{appId}", method = {RequestMethod.POST}, headers = "Accept=application/json")
@ResponseBody
@ApiOperation(value="微信硬件平台消息回调", notes="微信硬件平台消息回调")
public WebJsonBean receiveIotMsg(@PathVariable("appId") String appId, @RequestBody DeviceBindMsg deviceBindMsg) {
logger.info("收到微信硬件消息回调POST:{}", JSONObject.toJSONString(deviceBindMsg));
HardwareMsgEvent hardwareMsgEvent = new HardwareMsgEvent(applicationContext, deviceBindMsg, appId);
applicationContext.publishEvent(hardwareMsgEvent);
return new WebJsonBean(ErrorCode.SUCCESS);
}