1、设备绑定

1.1 功能概述

  设备绑定:设备在进行其他操作前需要先和特定的商户绑定起来。

1.2 请求地址:

    http://iot.znskiot.com:6208/tags

1.3 信息依赖

  同步返回信息,不依赖平台推送

1.4 HTTP 请求方式

  POST

1.5 返回格式

  JSON

1.6 请求参数

参数名称 参数类型 是否必须 描述
action String 参数取值为:"bind"
payload String 携带参数:masterid|activekey|deviceid|type

payload参数说明

masterid:

  masterid 为平台向企业分配的商户号

activekey:

  使用HMAC-SHA256加密算法,结合base64加密的动态消息密钥。具体生成过程为:

  1、取当前的Unix时间戳(Unix timestamp)为消息明文 timestamp,取给定的企业私钥为密钥 secret (注1:Unix时间戳转换工具和定义)

  2、使用 HMAC-SHA256 算法得到加密串 hstr

  3、对 hstr 进行base64加密,得到bstr

  4、取bstr的前面 10 位 + 时间戳 timestamp 作为最终的 activekey

  我们使用标准Unix时间戳,即时间戳只取得秒级。不同编程语言中获取现在的Unix时间戳可参考前面的标注

  本章末有 Golang 、PHP、Python、JAVA 生成 activekey 的实现,更多语言HMAC-SHA256的base64的加密请参考 此CSDN文章

  特别提醒:因base64编码含有URI的保留字符序列(加号 "+" 和斜杠 "/" ),需要对 activekey 字段数据做URL编码处理

deviceid:

  设备(节点)编号

type:

  设备类型,具体为:

type取值 含义 / 功能描述 备注
0 RF 耳标 不支持数据下行
1 Lora 耳标 -

1.7 请求示例

RF 耳标绑定到商户 1001 :

    curl -d "action=bind&payload=1001|MTVhMDhjNz1586826744|1000062|0" http://iot.znskiot.com:6208/tags

1.8 服务响应

请求成功时服务器同步返回下面的JSON格式结果。

{
    "action":"bind",
    "payload":"msgseq|deviceid|type",
    "error":"error_code"
}

action 参数说明

 action与请求时的topic参数一致,为 "bind"

payload 参数说明

msgseq 为消息序号。

deviceid、type 与请求中payload参数相关字段一致

error 参数说明

error 参数的 error_code 为错误的响应码,参照【附录一:错误的响应码】相关说明

1.9 生成 activekey 的实现

Go语言

func CreatPw(timestamp string, secret string) string {
    key := []byte(secret)
    hstr := hmac.New(sha256.New, key)
    hstr.Write([]byte(timestamp))
    bstr := base64.StdEncoding.EncodeToString(hstr.Sum(nil))
    return bstr[:10] + timestamp
}

PHP语言

function CreatPw($timestamp, $secret){
    $hstr = hash_hmac('sha256', $timestamp, $secret, true);
    return substr(base64_encode($hstr), 0, 10) . $timestamp;
}

Python语言

def CreatPw(timestamp, secret):
    hstr = hmac.new(secret.encode('utf-8'), timestamp.encode('utf-8'), digestmod = hashlib.sha256).digest()
    bstr = base64.b64encode(hstr)
    return bstr[:10].decode() + timestamp

JAVA语言

static String creatPwd(Long timestamp, String secret) {
    String hString = "";
    try {
        Mac sha256_hmac = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
        sha256_hmac.init(secret_key);
        String hash = Base64.getEncoder().encodeToString(sha256_hmac.doFinal(String.valueOf(timestamp).getBytes()));
        hString = URLEncoder.encode(hash.substring(0, 10) + String.valueOf(timestamp), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hString;
}

results matching ""

    No results matching ""