删除多余文件,修复init数据

V0.5.x
tangfudong 2023-05-04 18:43:14 +08:00
parent 37fda2af75
commit 17d3bf586e
7 changed files with 97 additions and 128 deletions

View File

@ -1,7 +1,5 @@
var mid=1;
var access_token="";
function getMid(){
mid++;
if(mid>10000){
@ -9,58 +7,94 @@ function getMid(){
}
return mid;
};
function getPingData(data){
var ping={
productKey:"",
deviceName:"",
content:{
id:getMid(),
type:data
}
};
function getPkDn(deviceKey){
var arr=deviceKey.split("_");
return {
type:"action",
data:{
productKey:"",
deviceName:"",
state:""
},
action:{
type:"ack",
content:JSON.stringify(ping)
pk:arr[1],
dn:deviceKey
};
}
function register(data){
var device=getPkDn(data.data.deviceName)
var subDevicesList=data.data.subDevices
var subDevices=[]
if(subDevicesList!=undefined&&subDevicesList.length>0){
apiTool.log("device:"+subDevicesList);
for (var i = 0; i < subDevicesList.length; i++) {
var deviceKey=subDevicesList[i]
var subDevice=getPkDn(deviceKey)
subDevices.push({
productKey:subDevice.pk,
deviceName:subDevice.dn,
model:''
})
}
}
};
var reply=
{
productKey:device.pk,
deviceName:device.dn,
mid:"0",
content:{
id:data.id,
type:data.type,
result:'success'
}
};
var data={
productKey:device.pk,
deviceName:device.dn
}
if(subDevices.length>0){
data['subDevices']=subDevices
}
apiTool.log("subDevices:"+JSON.stringify(data));
return {
type:"register",
data:data,
action:{
type:"ack",
content:JSON.stringify(reply)
}
};
}
function online(data){
apiTool.log("data:"+JSON.stringify(data));
var device=getPkDn(data.data.deviceName)
return {
type:"state",
data:{
productKey:device.pk,
deviceName:device.dn,
state:data.type
}
};
}
function offline(data){
var device=getPkDn(data.deviceKey)
return {
type:"state",
data:{
productKey:device.pk,
deviceName:device.dn,
state:data.type
}
};
}
//必须提供onReceive方法
this.onReceive=function(head,type,payload){
var data=JSON.parse(payload)
if(data.type=="auth_required"){
var auth={
productKey:"",
deviceName:"",
content:{
type:"auth",
access_token:access_token
}
};
return {
type:"action",
data:{
productKey:"",
deviceName:"",
state:""
},
action:{
type:"ack",
content:JSON.stringify(auth)
}
}
}else if(data.type=="auth_ok"){
return getPingData(data.heartBeatData);
}else if(data.type=="pong"){
apiTool.log("receive pong!");
}else if("ping"==type){
return getPingData(data.heartBeatData);
if(data.type=="register"){
apiTool.log("data:"+payload);
return register(data)
}else if(data.type=="online"){
return online(data);
}else if(data.type=="offline"){
return offline(data);
}
return {
productKey:"",
@ -69,4 +103,8 @@ this.onReceive=function(head,type,payload){
content:{
}
}
};
};
this.onRegistered=function (data,status) {
apiTool.log("onRegistered调用");
}

View File

@ -1,7 +1,7 @@
var mid=1;
var gatewayPk="BRD3x4fkKxkaxXFt"
var smartMeterPk="PjmkANSTDt85bZPj"
var smartMeterPk="PwMfpXmp4ZWkGahn"
function getMid(){
mid++;

View File

@ -1,74 +0,0 @@
var pidPkMap={
"H5Z31yKBmy":"3ptfx2dRescPAwTn",
"xOCy76jn6k":"jzC6eQGRse6hDZPB"
}
this.onReceive=function(method,path,header,params,body){
var type=header["Content-Type"];
if(type=="application/json"){
var msg=JSON.parse(body.msg);
var productId=msg.productId;
var deviceName=msg.deviceName;
var messageType=msg.messageType;
var data=msg.data;
var pk=pidPkMap[productId];
if(!pk){
return {
url:"",
header:{
contentType:"application/json"
},
content:"error"
}
}
if(messageType=="lifeCycle"){
//登录、登出
var online=data.status=="online";
deviceBehaviour.deviceStateChange(pk,deviceName,online);
}else if(messageType=="notify"){
//设备消息
//消息类型
var notifyType=msg.notifyType;
if(notifyType=="property"){
//属性上报
var propertyData={};
for(var p in data.params){
propertyData[p]=data.params[p].value;
}
deviceBehaviour.reportMessage(JSON.stringify({
mid:data.id,
productKey:pk,
deviceName:deviceName,
type:"property",
identifier:"report",
data:propertyData
}));
}else if(notifyType=="event"){
//事件上报
var identifier="";
var paramData={};
for(var p in data.params){
identifier=p;
paramData=data.params[p];
}
deviceBehaviour.reportMessage(JSON.stringify({
mid:data.id,
productKey:pk,
deviceName:deviceName,
type:"event",
identifier:identifier,
data:paramData.value
}));
}
}
}
return {
url:"",
header:{
contentType:"application/json"
},
content:JSON.stringify(params.msg)
}
};

View File

@ -43,5 +43,10 @@
"id": "FreshAir",
"name": "新风",
"createAt": 1681444312184
},
{
"id": "SmartMeter",
"name": "智能电表",
"createAt": 1681444312184
}
]

View File

@ -984,8 +984,8 @@
}
},
{
"id": "a5fnnx3ksu7n2n0f",
"productKey": "a5fnnx3ksu7n2n0f",
"id": "bGdZt8ffBETtsirm",
"productKey": "bGdZt8ffBETtsirm",
"model": {
"properties": [
{
@ -1115,8 +1115,8 @@
}
},
{
"id": "PjmkANSTDt85bZPj",
"productKey": "PjmkANSTDt85bZPj",
"id": "PwMfpXmp4ZWkGahn",
"productKey": "PwMfpXmp4ZWkGahn",
"model": {
"properties": [
{