dt-common-device
Version:
A secure and robust device management library for IoT applications
87 lines (86 loc) • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventHandler = void 0;
const dt_pub_sub_1 = require("dt-pub-sub");
const dt_audit_library_1 = require("dt-audit-library");
const Events_1 = require("./Events");
class EventHandler {
constructor() {
this.source = "dt-common-device";
}
async onDeviceCreate(body) {
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.CREATED, body, this.source);
const payload = {
eventType: Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.CREATED,
properties: {
...body,
},
};
await (0, dt_audit_library_1.publishAudit)(payload);
}
async onDeviceUpdate(deviceId, body) {
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.UPDATED, { deviceId, body }, this.source);
const payload = {
eventType: Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.UPDATED,
properties: {
...body,
},
};
await (0, dt_audit_library_1.publishAudit)(payload);
}
async onDeviceDelete(deviceId) {
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.DELETED, { deviceId }, this.source);
const payload = {
eventType: Events_1.DT_EVENT_TYPES.DEVICE.DEVICE.DELETED,
properties: {
deviceId,
},
};
await (0, dt_audit_library_1.publishAudit)(payload);
}
async onStateChange(deviceId, state) {
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.STATE.SET, { deviceId, state }, this.source);
const payload = {
eventType: Events_1.DT_EVENT_TYPES.DEVICE.STATE.SET,
properties: {
deviceId,
state,
},
};
await (0, dt_audit_library_1.publishAudit)(payload);
}
async onStatusChange(deviceId, status) {
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.STATUS.SET, { deviceId, status }, this.source);
const payload = {
eventType: Events_1.DT_EVENT_TYPES.DEVICE.STATUS.SET,
properties: {
deviceId,
status,
},
};
await (0, dt_audit_library_1.publishAudit)(payload);
}
async onBatteryLevelChange(deviceId, batteryLevel) {
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.BATTERY.SET, { deviceId, batteryLevel }, this.source);
const payload = {
eventType: Events_1.DT_EVENT_TYPES.DEVICE.BATTERY.SET,
properties: {
deviceId,
batteryLevel,
},
};
await (0, dt_audit_library_1.publishAudit)(payload);
}
async onDeviceMetaChange(deviceId, metaData) {
await dt_pub_sub_1.eventDispatcher.publishEvent(Events_1.DT_EVENT_TYPES.DEVICE.META_DATA.SET, { deviceId, metaData }, this.source);
const payload = {
eventType: Events_1.DT_EVENT_TYPES.DEVICE.META_DATA.SET,
properties: {
deviceId,
metaData,
},
};
await (0, dt_audit_library_1.publishAudit)(payload);
}
}
exports.EventHandler = EventHandler;