删除多余文件,修复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 mid=1;
var access_token="";
function getMid(){ function getMid(){
mid++; mid++;
if(mid>10000){ if(mid>10000){
@ -9,58 +7,94 @@ function getMid(){
} }
return mid; return mid;
}; };
function getPingData(data){ function getPkDn(deviceKey){
var ping={ var arr=deviceKey.split("_");
productKey:"", return {
deviceName:"", 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:{ content:{
id:getMid(), id:data.id,
type:data 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 { return {
type:"action", type:"register",
data:{ data:data,
productKey:"",
deviceName:"",
state:""
},
action:{ action:{
type:"ack", type:"ack",
content:JSON.stringify(ping) 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方法 //必须提供onReceive方法
this.onReceive=function(head,type,payload){ this.onReceive=function(head,type,payload){
var data=JSON.parse(payload) var data=JSON.parse(payload)
if(data.type=="auth_required"){ if(data.type=="register"){
var auth={ apiTool.log("data:"+payload);
productKey:"", return register(data)
deviceName:"", }else if(data.type=="online"){
content:{ return online(data);
type:"auth", }else if(data.type=="offline"){
access_token:access_token return offline(data);
}
};
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);
} }
return { return {
productKey:"", productKey:"",
@ -70,3 +104,7 @@ this.onReceive=function(head,type,payload){
} }
} }
}; };
this.onRegistered=function (data,status) {
apiTool.log("onRegistered调用");
}

View File

@ -1,7 +1,7 @@
var mid=1; var mid=1;
var gatewayPk="BRD3x4fkKxkaxXFt" var gatewayPk="BRD3x4fkKxkaxXFt"
var smartMeterPk="PjmkANSTDt85bZPj" var smartMeterPk="PwMfpXmp4ZWkGahn"
function getMid(){ function getMid(){
mid++; 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", "id": "FreshAir",
"name": "新风", "name": "新风",
"createAt": 1681444312184 "createAt": 1681444312184
},
{
"id": "SmartMeter",
"name": "智能电表",
"createAt": 1681444312184
} }
] ]

View File

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