添加组件脚本示例

V0.5.x
xiwa 2022-06-24 11:11:54 +08:00
parent 0768ca5afa
commit 2802cf42ed
4 changed files with 318 additions and 80 deletions

View File

@ -14,7 +14,7 @@ function getPkDn(clientId){
}
function auth(head,type,payload){
if(isServerId(payload.clientid) > -1){
if(isServerId(payload.clientid)){
return {
type:"serverAuth",
data:{
@ -60,51 +60,13 @@ function acl(head,type,payload){
};
}
/**
* /sys/N523nWsCiG3CAn6X/AA:BB:CC:EE:01/c/#
* /sys/N523nWsCiG3CAn6X/AA:BB:CC:EE:01/c/register_reply
*/
if (/^\/sys\/.+\/.+\/c\/#?$/i.test(_topic)) {
if (payload.access == 1) { // 1、subscribe 2、public
return subscribe(head,type,payload);
}
if (/^\/sys\/.+\/.+\/c\/#/i.test(_topic)) {
return subscribe(head,type,payload);
}
if (/^\/sys\/.+\/.+\/s\/.*/i.test(_topic)) {
return subscribe(head,type,payload);
}
if (/^\/sys\/.+\/.+\/c\/register_reply$/i.test(_topic)) {
if (payload.access == 1) { // 1、subscribe 2、public
return subscribe(head,type,payload);
}
}
if (/^\/sys\/.+\/.+\/s\/register$/i.test(_topic)) {
if (payload.access == 2) { // 1、subscribe 2、public
return subscribe(head,type,payload);
}
}
if (/^\/sys\/.+\/.+\/s\/service\/allowJoin_reply$/i.test(_topic)) {
if (payload.access == 2) { // 1、subscribe 2、public
return subscribe(head,type,payload);
}
}
if (/^\/sys\/.+\/.+\/s\/service\/rawSend_reply$/i.test(_topic)) {
if (payload.access == 2) { // 1、subscribe 2、public
return subscribe(head,type,payload);
}
}
if (/^\/sys\/.+\/.+\/s\/event\/property\/post$/i.test(_topic)) {
if (payload.access == 2) { // 1、subscribe 2、public
return subscribe(head,type,payload);
}
}
if (/^\/sys\/.+\/.+\/s\/service\/.+_reply$/i.test(_topic)) {
if (payload.access == 2) { // 1、subscribe 2、public
return subscribe(head,type,payload);
}
}
}
function register(head,type,payload){

View File

@ -1,36 +0,0 @@
//引用api工具类
var apiTool = Java.type("cc.iotkit.comp.biz.ApiTool");
//api配置
apiTool.config("http://localhost",8086,3000);
this.onReceive=function(method,path,header,params,body){
//methodpost、get、delete...
//path请求路径
//headerhttp请求头数据,结构:{xx:xx,yy:yy}
//params请求参数结构{xx:[...],yy:[...]}
//body请求体当提交的数据为json格式时使用结构{xx:xx,yy:yy}
apiTool.log("onReceive method:"+method);
apiTool.log("onReceive path:"+path);
apiTool.log("onReceive header:"+header);
apiTool.log("onReceive params:"+params);
apiTool.log("onReceive body:"+body);
var duHeader=body.header;
var namespace=duHeader.namespace;
var requestName=duHeader.name;
var messageId=duHeader.messageId;
var duPayload=duHeader.payload;
var token=duHeader.accessToken;
//设备发现
if(namespace=="DuerOS.ConnectedHome.Discovery" && requestName=="DiscoverAppliancesRequest"){
}
return {
url:"xx",//不指定直接作为响应返回
header:{
contentType:"xx"
},
content:"xx"
}
}

View File

@ -0,0 +1,74 @@
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

@ -0,0 +1,238 @@
//api配置
apiTool.config("127.0.0.1",8085,3000);
this.onReceive=function(method,path,header,params,body){
//methodpost、get、delete...
//path请求路径
//headerhttp请求头数据,结构:{xx:xx,yy:yy}
//params请求参数结构{xx:[...],yy:[...]}
//body请求体当提交的数据为json格式时使用结构{xx:xx,yy:yy}
apiTool.log("onReceive body:"+body);
var duHeader=body.header;
var namespace=duHeader.namespace;
var requestName=duHeader.name;
var messageId=duHeader.messageId;
var duPayload=body.payload;
var token=duPayload.accessToken;
var openUid=duPayload.openUid;
//设备发现
if(namespace=="DuerOS.ConnectedHome.Discovery" && requestName=="DiscoverAppliancesRequest"){
var deviceIds=[];
var discoveredDevices=[];
var content={
header:{
namespace:"DuerOS.ConnectedHome.Discovery",
name:"DiscoverAppliancesResponse",
messageId:messageId,
payloadVersion:1
},
payload:{
discoveredAppliances:discoveredDevices,
discoveredGroups:[{
groupName:"客厅",
applianceIds:deviceIds,
groupNotes:"客厅分组控制",
additionalGroupDetails:{}
}]
}
};
var rst=apiTool.getSpaceDevices(token);
apiTool.log(JSON.stringify(rst));
if(rst && rst.status==200 && rst.data){
var devices=rst.data;
for(var i in devices){
var device=devices[i];
var did=device.deviceId;
var pk=device.productKey;
var dn=device.deviceName;
//更新设备openUid
rst=apiTool.setOpenUid(token,did,"dueros",openUid);
if(!rst || rst.status!=200){
continue;
}
//插座
if(pk=="cGCrkK7Ex4FESAwe"){
var powerstate=device.property.powerstate;
discoveredDevices.push({
actions:["turnOn","turnOff"],
applianceTypes:["SOCKET"],
additionalApplianceDetails:{},
applianceId:device.deviceId,
friendlyDescription:"智能插座",
friendlyName:device.name,
isReachable:device.online,
manufacturerName:"海曼",
modelName:"S1",
version:"v1.0",
attributes:[
{
name:"客厅的插座",
scale:"",
timestampOfSample:0,
uncertaintyInMilliseconds:10
},
{
name:"connectivity",
value:"REACHABLE",
scale:"",
timestampOfSample:0,
uncertaintyInMilliseconds:10
},
{
name:"turnOnState",
value:powerstate==1?"ON":"OFF",
scale:"",
timestampOfSample:0,
uncertaintyInMilliseconds:10,
legalValue:"(ON, OFF)"
}
]
});
}else if(pk=="Rf4QSjbm65X45753"){
//开关
var powerstate=device.property.powerstate;
discoveredDevices.push({
actions:["turnOn","turnOff"],
applianceTypes:["SWITCH"],
additionalApplianceDetails:{},
applianceId:device.deviceId,
friendlyDescription:"智能开关",
friendlyName:device.name,
isReachable:device.online,
manufacturerName:"海曼",
modelName:"S1",
version:"v1.0",
attributes:[
{
name:"客厅的开关",
scale:"",
timestampOfSample:0,
uncertaintyInMilliseconds:10
},
{
name:"connectivity",
value:"REACHABLE",
scale:"",
timestampOfSample:0,
uncertaintyInMilliseconds:10
},
{
name:"turnOnState",
value:powerstate==1?"ON":"OFF",
scale:"",
timestampOfSample:0,
uncertaintyInMilliseconds:10,
legalValue:"(ON, OFF)"
}
]
});
}
}
}
return {
url:"",//不指定直接作为响应返回
header:{
contentType:"application/json"
},
content:JSON.stringify(content)
}
}else if(namespace=="DuerOS.ConnectedHome.Control"){
//设备控制
var appliance=duPayload.appliance;
var deviceId=appliance.applianceId;
var confirmName="UnsupportedOperationError";
var rst={status:500};
//开关
if(requestName=="TurnOnRequest"){
//开
confirmName="TurnOnConfirmation";
rst=apiTool.setProperties(token,deviceId,{powerstate:1});
}else if(requestName=="TurnOffRequest"){
//关
confirmName="TurnOffConfirmation";
rst=apiTool.setProperties(token,deviceId,{powerstate:0});
}
if(rst.status!=200){
confirmName="UnsupportedOperationError";
apiTool.log("device control failed:"+JSON.stringify(rst));
}
var content={
header: {
namespace: "DuerOS.ConnectedHome.Control",
name: confirmName,
messageId: messageId,
payloadVersion: "1"
},
payload: {
"attributes": []
}
};
return {
url:"",
header:{
contentType:"application/json"
},
content:JSON.stringify(content)
}
}else if(namespace=="DuerOS.ConnectedHome.Query"){
//属性查询
if(requestName=="ReportStateRequest"){
var appliance=duPayload.appliance;
var deviceId=appliance.applianceId;
var property=appliance.attributeName;
var propertyVal="";
var success=false;
if(property=="turnOnState"){
//开关状态查询
var rst= apiTool.getSpaceDeviceDetail(token,deviceId);
if(rst && rst.status==200 && rst.data.property){
propertyVal=rst.data.property.powerstate==1?"ON":"OFF";
success=true;
}
}
var content=success?{
"header": {
"namespace": "DuerOS.ConnectedHome.Query",
"name": "ReportStateResponse",
"messageId": new Date().getTime()+"",
"payloadVersion": "1"
},
"payload": {
"attributes": [
{
"name": property,
"value": propertyVal,
"scale": "",
"timestampOfSample": new Date().getTime()/1000,
"uncertaintyInMilliseconds": 0
}
]
}
}:{};
return {
url:"",
header:{
contentType:"application/json"
},
content:JSON.stringify(content)
}
}
}
return {
url:"",//不指定直接作为响应返回
header:{
contentType:"application/json"
},
content:""
}
}