@corvina/device-example
Version:
Corvina Device Example based on @corvina/device-client
135 lines • 6.34 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var Json_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Json = void 0;
const common_1 = require("@nestjs/common");
const device_client_1 = require("@corvina/device-client");
const swagger_1 = require("@nestjs/swagger");
const axios_1 = __importDefault(require("axios"));
let Json = Json_1 = class Json {
l = new common_1.Logger(Json_1.name);
deviceService;
async post(tagName = undefined, timestamp = undefined, qos = undefined, postCallback = undefined, v) {
const t = timestamp || Date.now();
const dataPoints = new Array();
const dp = {
tagName,
value: v,
timestamp: t,
};
dataPoints.push(dp);
await this.deviceService.post(dataPoints, {
qos: qos || 0,
cb: postCallback
? (error, tagName, modelPath) => {
axios_1.default.post(postCallback, { error: { message: error?.message }, tagName, modelPath });
}
: undefined,
});
return dataPoints;
}
};
exports.Json = Json;
__decorate([
(0, common_1.Inject)(),
__metadata("design:type", device_client_1.DeviceService)
], Json.prototype, "deviceService", void 0);
__decorate([
(0, swagger_1.ApiOperation)({
summary: "Post a new JSON value",
callbacks: {
result: {
"{$request.query#/callback}": {
post: {
requestBody: {
required: true,
content: {
"application/json": {
schema: {
type: "object",
properties: {
error: {
type: "object",
properties: {
message: {
type: "string",
},
},
},
tagName: {
type: "string",
},
modelPath: {
type: "string",
},
},
},
},
},
},
responses: {
"200": { description: "The server acknowledged the request" },
},
},
},
},
},
}),
(0, swagger_1.ApiQuery)({
name: "tagName",
description: "Prefix of device identifier (name) of data source. The actual tag names advertised to the cloud are automatically generated as this prefix plus each property JSON path. The prefix can be undefined, so that only JSON paths are used",
schema: { default: undefined, example: "temperature" },
required: false,
}),
(0, swagger_1.ApiQuery)({
name: "timestamp",
description: "Specify a timestamp (if omitted the request timestamp is used) ",
required: false,
}),
(0, swagger_1.ApiQuery)({
name: "callback",
description: "Specify an optional callback to be invoked when the message has been delivered",
schema: { type: "string", format: "uri", example: "http://localhost:30001" },
required: false,
}),
(0, swagger_1.ApiQuery)({
name: "qos",
description: "Specify an optional qos (by default is zeo)",
schema: { type: "number", minimum: 0, maximum: 2, default: 0, example: 1 },
required: false,
}),
(0, swagger_1.ApiBody)({
description: "The json value to set. Each property in the json is assigned as full tag name advertised to the cloud as ```availableTags``` the corresponding json path prefixed by ```tagName```. For instance, if ```tagName=\"tag\"``` and value is ```{ a: {b : 1 } }``` the advertised list of available tags is ```['tag.a.b']```. If ```tagName``` is undefined the advertised list of available tags is simply ```['a.b']```",
schema: { default: "{}", example: { Tag1: 1 } },
}),
(0, common_1.Post)(),
__param(0, (0, common_1.Query)("tagName")),
__param(1, (0, common_1.Query)("timestamp")),
__param(2, (0, common_1.Query)("qos")),
__param(3, (0, common_1.Query)("callback")),
__param(4, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object, Object, Object, Object]),
__metadata("design:returntype", Promise)
], Json.prototype, "post", null);
exports.Json = Json = Json_1 = __decorate([
(0, swagger_1.ApiTags)("device"),
(0, common_1.Controller)("/device/json/"),
(0, common_1.Injectable)()
], Json);
//# sourceMappingURL=json.controller.js.map