dt-common-device
Version:
A secure and robust device management library for IoT applications
77 lines (76 loc) • 2.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CronicleService = void 0;
const config_1 = require("../config/config");
const axios_1 = __importDefault(require("axios"));
class CronicleService {
constructor() {
this.cronicleEndpoint = process.env.CRONICLE_ENDPOINT || "";
this.cronicleApiKey = process.env.CRONICLE_API_KEY || "";
}
async registerJob(payload) {
const { name, apiUrl, method, schedule, cronJobId, target } = payload;
try {
await axios_1.default.post(`${this.cronicleEndpoint}/create_event/v1`, {
id: cronJobId,
title: name,
category: "general",
plugin: "urlplug",
timeZone: "UTC",
enabled: 1,
target: target,
api_key: this.cronicleApiKey,
params: {
url: apiUrl,
method,
headers: {
"Content-Type": "application/json",
"x-api-key": this.cronicleApiKey,
},
},
data: payload,
timing: {
years: schedule.years,
months: schedule.months,
days: schedule.days,
weekdays: schedule.weekdays,
hours: schedule.hours,
minutes: schedule.minutes,
},
});
}
catch (error) {
(0, config_1.getConfig)().LOGGER.error(`Failed to create device: ${error.message}`);
}
}
async getJob(jobId) {
try {
const res = await axios_1.default.post(`${this.cronicleEndpoint}/get_event/v1`, {
id: jobId,
api_key: this.cronicleApiKey,
});
return res.data;
}
catch (error) {
return;
}
}
async deleteJob(jobId) {
try {
(0, config_1.getConfig)().LOGGER.info(`Deleting job: ${jobId}`);
await axios_1.default.post(`${this.cronicleEndpoint}/delete_event/v1`, {
id: jobId,
api_key: this.cronicleApiKey,
});
(0, config_1.getConfig)().LOGGER.info(`Deleted job: ${jobId}`);
}
catch (error) {
(0, config_1.getConfig)().LOGGER.error(`Failed to delete job: ${error.message}`);
throw error;
}
}
}
exports.CronicleService = CronicleService;