214 lines
4.0 KiB
JavaScript
214 lines
4.0 KiB
JavaScript
DNE_PARAM = 1;
|
|
DNE_OPNOTSUPP = 6;
|
|
|
|
gDeviceError = false;
|
|
gDeviceToken = 1337;
|
|
gDeviceActive = false;
|
|
gDeviceService = false;
|
|
|
|
function isInteger(x) { return typeof x === "number" && isFinite(x) && Math.floor(x) === x; }
|
|
function isFloat(x) { return !!(x % 1); }
|
|
function isBool(x) { return typeof x === "boolean" }
|
|
|
|
////
|
|
// Device class
|
|
////
|
|
|
|
function getDeviceState() {
|
|
if (gDeviceService)
|
|
return "service";
|
|
if (gDeviceActive)
|
|
return "active";
|
|
if (gDeviceToken)
|
|
return "armed";
|
|
return "idle";
|
|
}
|
|
|
|
function getDeviceData() {
|
|
return {
|
|
"state" : getDeviceState(),
|
|
"error" : gDeviceError
|
|
};
|
|
}
|
|
|
|
function getDeviceInfo() {
|
|
return {
|
|
"type" : "zkl-3000-rc",
|
|
"version" : {
|
|
"fw-main": "1.0.0",
|
|
"hw-main": "1.2",
|
|
"hw-switch": "todo",
|
|
"fw-switch-control": "todo",
|
|
"fw-switch-drive": "todo",
|
|
"fw-switch-measure": "todo",
|
|
},
|
|
};
|
|
}
|
|
|
|
// device:data periodic publish
|
|
devsim.setInterval(function() {
|
|
devsim.publish("device:data", [getDeviceData()]);
|
|
}, 1000);
|
|
|
|
devsim.request("device:info", function() {
|
|
return {"result":[getDeviceInfo()]};
|
|
})
|
|
|
|
////
|
|
// Sensor class
|
|
////
|
|
|
|
gBat1Voltage = 12.00;
|
|
gBat1State = 5;
|
|
|
|
devsim.request("sensor:info", function() {
|
|
return {"result" : [
|
|
{"uid" : 1, "label" : "bat1-voltage", "type" : "number"},
|
|
{"uid" : 2, "label" : "bat1-state", "type" : "enum" },
|
|
]};
|
|
})
|
|
|
|
// bat1 voltage and state publish
|
|
devsim.setInterval(function() {
|
|
time = new Date().getTime();
|
|
devsim.publish("sensor:data", [
|
|
{"uid" : 1, "value" : gBat1Voltage, "time" : time},
|
|
{"uid" : 2, "value" : gBat1State, "time" : time},
|
|
]);
|
|
}, 1000);
|
|
|
|
|
|
////
|
|
// Config class
|
|
////
|
|
|
|
devsim.request("config:info", function() {
|
|
return {"result" : [
|
|
{"uid" : 1, "label" : "token" },
|
|
{"uid" : 2, "label" : "activate"},
|
|
]};
|
|
})
|
|
|
|
// Device token (config uid 1)
|
|
|
|
// Token config:set
|
|
devsim.request("config:set", 1, function() {
|
|
if (getDeviceState() != "idle") {
|
|
return {"error" : DNE_OPNOTSUPP};
|
|
}
|
|
|
|
token = this.Params.value;
|
|
if (!isInteger(token) || token == 0) {
|
|
return {"error" : DNE_PARAM};
|
|
}
|
|
|
|
gDeviceToken = token;
|
|
gDeviceState = "armed";
|
|
return this;
|
|
});
|
|
|
|
// Token config:get
|
|
devsim.request("config:get", 1, function() {
|
|
this.Result.uid = this.Params.uid;
|
|
this.Result.value = gDeviceToken;
|
|
return this;
|
|
});
|
|
|
|
// Token config:reset
|
|
devsim.request("config:reset", 1, function() {
|
|
if (getDeviceState() != "armed") {
|
|
return {"error" : DNE_OPNOTSUPP};
|
|
}
|
|
gDeviceToken = 0;
|
|
return this;
|
|
});
|
|
|
|
// Activation state (config uid 2)
|
|
|
|
// Activation config:set
|
|
devsim.request("config:set", 2, function() {
|
|
if (getDeviceState() != "armed") {
|
|
return {"error" : DNE_OPNOTSUPP};
|
|
}
|
|
|
|
active = this.Params.value;
|
|
|
|
if (!isBool(active)) {
|
|
return {"error" : DNE_PARAM};
|
|
}
|
|
|
|
gDeviceActive = active;
|
|
return this;
|
|
});
|
|
|
|
// Activation config:get
|
|
devsim.request("config:get", 2, function() {
|
|
this.Result.uid = this.Params.uid;
|
|
this.Result.value = gDeviceActive;
|
|
return this;
|
|
});
|
|
|
|
// Activation config:reset
|
|
devsim.request("config:reset", 2, function() {
|
|
if (getDeviceState() != "active") {
|
|
return {"error" : DNE_OPNOTSUPP};
|
|
}
|
|
gDeviceActive = false;
|
|
return this;
|
|
});
|
|
|
|
////
|
|
// Action class
|
|
////
|
|
|
|
devsim.request("action:info", function() {
|
|
return {"result" : [
|
|
]};
|
|
})
|
|
|
|
////
|
|
// Notify class
|
|
////
|
|
|
|
devsim.request("notify:info", function() {
|
|
return {"result" : [
|
|
]};
|
|
})
|
|
|
|
////
|
|
// Simulator class
|
|
////
|
|
|
|
devsim.request("simulator:info", function() {
|
|
return {"result" : [{
|
|
"uri" : "src.dualinventive.com/simulator/zkl-3000-rc",
|
|
"author" : "jerry.jacobs@dualinventive.com",
|
|
"version" : "1.0.0"
|
|
}]};
|
|
});
|
|
|
|
devsim.request("simulator:set", function() {
|
|
console.log(JSON.stringify(this.Params))
|
|
|
|
if (this.Params.property == "sensor") {
|
|
if (this.Params.uid == 1) {
|
|
gBat1Voltage = this.Params.value;
|
|
}
|
|
if (this.Params.uid == 2) {
|
|
gBat1State = this.Params.value;
|
|
}
|
|
}
|
|
|
|
return this;
|
|
});
|
|
|
|
devsim.request("simulator:get", function() {
|
|
console.log(JSON.stringify(this.Params))
|
|
return this;
|
|
});
|
|
|
|
devsim.request("simulator:reset", function() {
|
|
console.log(JSON.stringify(this.Params))
|
|
return this;
|
|
});
|