@ctsy/hlwl-protocol
v1.0.4
Published
![流程图](./imgs/flow.svg) # 支持 node 环境 和 浏览器 环境
Downloads
6
Readme
海纳物联 物联网产品接入协议解析库
支持 node 环境 和 浏览器 环境
以本地 HTTP 服务运行
1. 请先克隆项目或直接通过 yarn add 加载项目
2. 请启动本地服务,默认端口为 3000,可通过环境变量中的 LISTEN_PORT 来指定端口
3. 请使用以下方式调用相关服务
解码操作,当收到电信平台或其它方式推送过来的二进制数据时调用该接口获得解码结果
返回内容
{
"d": {
"Protocol": {
"V": 1,
"VID": 0,
"Area": 0,
"Type": 0,
"Control": 120,
"Status": {
"Reply": 1,
"UpDown": 1,
"Proxy": 0,
"Encrypt": 0,
"To": 0,
"From": 0,
"Split": 0,
"Next": 0
},
"Seq": 0,
"Time": 1598925677,
"Addr": 358878460005,
"Len": 105,
"Data": {
"type": "Buffer",
"data": [8, 97, 5, 0, 70, 120, 136, 53, 137, 134, 17, 24, 40, 48, 7, 115, 146, 4, 4, 96, 17, 48, 37, 54, 2, 67, 19, 0, 176, 255, 252, 255, 16, 0, 181, 255, 1, 10, 10, 86, 21, 210, 4, 0, 0, 132, 255, 255, 255, 57, 48, 0, 0, 225, 16, 109, 171, 77, 95, 254, 0, 0, 0, 3, 1, 109, 171, 77, 95, 169, 171, 77, 95, 10, 1, 0, 3, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 109, 171, 77, 95]
},
"Crc": 53,
"Start": "68"
},
"Data": {
"IMEI": "861050046788835",
"CCID": "89861118283007740000",
"IMSI": "460113025360243",
"CSQ": 19,
"ECL": 0,
"RSRP": 65456,
"RSRQ": 65532,
"SNR": 16,
"RSSI": 65461,
"V": 1,
"Vol": 25.7,
"Status": {
"Door": 2,
"Vol": 1,
"Magnetism": 0,
"Light": 1,
"Pulse": 0,
"Open": 1,
"Left": 0,
"Negative": 1,
"Max": 0,
"Min": 1,
"Manual": 0,
"Pay": 1,
"Power": 0
},
"Total": 1.234,
"Left": 4294967.172,
"DayTotal": 12.345,
"DayFlow": 4321,
"DayFlowTime": 1598925677,
"Over": 0,
"Month": 0.254,
"SUnit": 3,
"LRC": 10,
"STime": 1598925677,
"ETime": 1598925737,
"Assessor": 10,
"UpCycle": 1,
"Try": 3,
"Step1": 0.001,
"Rate1": 0.001,
"Step2": 0.002,
"Rate2": 0.002,
"Step3": 0.003,
"Rate3": 0.003,
"NextTime": 1598925677
},
"Reply": "680100000078020000c179726005004678883501000070"
},
"c": 200,
"i": "",
"e": ""
}
curl 代码
curl -X POST \
'http://localhost:3000/P/decode' \
-H 'Content-Type:application/json' \
-d '{
"Hex": "68 01 00 00 00 78 81 00 00 6D AB 4D 5F 05 00 46 78 88 35 69 00 35 88 78 46 00 05 61 08 04 92 73 07 30 28 18 11 86 89 43 02 36 25 30 11 60 04 13 00 B0 FF FC FF 10 00 B5 FF 01 0A 0A 56 15 D2 04 00 00 84 FF FF FF 39 30 00 00 E1 10 6D AB 4D 5F FE 00 00 00 03 01 6D AB 4D 5F A9 AB 4D 5F 0A 01 00 03 01 00 00 00 01 00 00 00 02 00 00 00 02 00 00 00 03 00 00 00 03 00 00 00 6D AB 4D 5F F7",
"Next": 1
}'
Java 版本代码
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body = RequestBody.create(mediaType, "{\n \"Hex\": \"68 01 00 00 00 78 81 00 00 6D AB 4D 5F 05 00 46 78 88 35 69 00 35 88 78 46 00 05 61 08 04 92 73 07 30 28 18 11 86 89 43 02 36 25 30 11 60 04 13 00 B0 FF FC FF 10 00 B5 FF 01 0A 0A 56 15 D2 04 00 00 84 FF FF FF 39 30 00 00 E1 10 6D AB 4D 5F FE 00 00 00 03 01 6D AB 4D 5F A9 AB 4D 5F 0A 01 00 03 01 00 00 00 01 00 00 00 02 00 00 00 02 00 00 00 03 00 00 00 03 00 00 00 6D AB 4D 5F F7\",\n \"Next\": 1\n}");
Request request = new Request.Builder()
.url("http://localhost:3000/P/decode")
.post(body)
.addHeader("Content-Type","application/json")
.build();
Response response = client.newCall(request).execute();
Golang 代码
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "http://localhost:3000/P/decode"
payload := strings.NewReader("{\n \"Hex\": \"68 01 00 00 00 78 81 00 00 6D AB 4D 5F 05 00 46 78 88 35 69 00 35 88 78 46 00 05 61 08 04 92 73 07 30 28 18 11 86 89 43 02 36 25 30 11 60 04 13 00 B0 FF FC FF 10 00 B5 FF 01 0A 0A 56 15 D2 04 00 00 84 FF FF FF 39 30 00 00 E1 10 6D AB 4D 5F FE 00 00 00 03 01 6D AB 4D 5F A9 AB 4D 5F 0A 01 00 03 01 00 00 00 01 00 00 00 02 00 00 00 02 00 00 00 03 00 00 00 03 00 00 00 6D AB 4D 5F F7\",\n \"Next\": 1\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type","application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}
php curl
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "3000",
CURLOPT_URL => "http://localhost:3000/P/decode",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n \"Hex\": \"68 01 00 00 00 78 81 00 00 6D AB 4D 5F 05 00 46 78 88 35 69 00 35 88 78 46 00 05 61 08 04 92 73 07 30 28 18 11 86 89 43 02 36 25 30 11 60 04 13 00 B0 FF FC FF 10 00 B5 FF 01 0A 0A 56 15 D2 04 00 00 84 FF FF FF 39 30 00 00 E1 10 6D AB 4D 5F FE 00 00 00 03 01 6D AB 4D 5F A9 AB 4D 5F 0A 01 00 03 01 00 00 00 01 00 00 00 02 00 00 00 02 00 00 00 03 00 00 00 03 00 00 00 6D AB 4D 5F F7\",\n \"Next\": 1\n}",
CURLOPT_HTTPHEADER => array(
"Content-Type:application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
nodejs
var http = require("http");
var requestInfo = {
method: "POST",
hostname: "localhost",
port: "3000",
path: "/P/decode",
headers: {
"Content-Type": "application/json",
},
};
var req = http.request(requestInfo, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(
'{\n "Hex": "68 01 00 00 00 78 81 00 00 6D AB 4D 5F 05 00 46 78 88 35 69 00 35 88 78 46 00 05 61 08 04 92 73 07 30 28 18 11 86 89 43 02 36 25 30 11 60 04 13 00 B0 FF FC FF 10 00 B5 FF 01 0A 0A 56 15 D2 04 00 00 84 FF FF FF 39 30 00 00 E1 10 6D AB 4D 5F FE 00 00 00 03 01 6D AB 4D 5F A9 AB 4D 5F 0A 01 00 03 01 00 00 00 01 00 00 00 02 00 00 00 02 00 00 00 03 00 00 00 03 00 00 00 6D AB 4D 5F F7",\n "Next": 1\n}'
);
req.end();
python
import http.client
conn = http.client.HTTPConnection("localhost:3000")
payload = "{\n \"Hex\": \"68 01 00 00 00 78 81 00 00 6D AB 4D 5F 05 00 46 78 88 35 69 00 35 88 78 46 00 05 61 08 04 92 73 07 30 28 18 11 86 89 43 02 36 25 30 11 60 04 13 00 B0 FF FC FF 10 00 B5 FF 01 0A 0A 56 15 D2 04 00 00 84 FF FF FF 39 30 00 00 E1 10 6D AB 4D 5F FE 00 00 00 03 01 6D AB 4D 5F A9 AB 4D 5F 0A 01 00 03 01 00 00 00 01 00 00 00 02 00 00 00 02 00 00 00 03 00 00 00 03 00 00 00 6D AB 4D 5F F7\",\n \"Next\": 1\n}"
headers = {
"Content-Type":"application/json"
}
conn.request("POST","/P/decode", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
编码操作,当需要下发相关数据的时候请调用该接口完成数据的二进制编码操作
返回内容
{
"d": {
"Hex": "3030300000000000000000000000000000000000000030303000000000000000000000000000000000000000",
"Base64": "MDAwAAAAAAAAAAAAAAAAAAAAAAAAADAwMAAAAAAAAAAAAAAAAAAAAAAAAAA="
},
"c": 200,
"i": "",
"e": ""
}
以168设置NB参数指令为准
curl -X POST \
'http://localhost:3000/P/encode' \
-H 'Content-Type:application/json' \
-d '{
"Protocol":{
"Control":168,
"Addr":"01020304"
},
"Data":{
}
}'