dt-common-device
Version:
A secure and robust device management library for IoT applications
138 lines (137 loc) • 5.14 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, category, timezone, enabled = true, } = payload;
try {
await axios_1.default.post(`${this.cronicleEndpoint}/create_event/v1`, {
id: cronJobId,
title: name,
category: "general",
plugin: "urlplug",
timeZone: timezone || "UTC",
enabled: enabled ? 1 : 0,
max_children: 1,
target: target,
api_key: this.cronicleApiKey,
params: {
url: apiUrl,
method,
headers: {
"Content-Type": "application/json",
"x-api-key": this.cronicleApiKey,
"User-Agent": "Cronicle/1.0",
},
timeout: 120,
},
data: payload,
timing: {
years: schedule.years,
months: schedule.months,
days: schedule.days,
weekdays: schedule.weekdays,
hours: schedule.hours,
minutes: schedule.minutes,
},
timeout: 60,
});
}
catch (error) {
(0, config_1.getConfig)().LOGGER.error(`Failed to create Cronicle job: ${error.message}`);
}
}
async updateJob(payload) {
const { name, apiUrl, method, schedule, cronJobId, target, category, timezone, enabled, } = payload;
try {
const croniclePayload = {};
croniclePayload.params = {
timeout: 120,
headers: {
"Content-Type": "application/json",
"x-api-key": this.cronicleApiKey,
"User-Agent": "Cronicle/1.0",
},
};
if (name)
croniclePayload.title = name;
if (timezone)
croniclePayload.timeZone = timezone;
if (enabled)
croniclePayload.enabled = enabled ? 1 : 0;
if (target)
croniclePayload.target = target;
if (category)
croniclePayload.category = category;
if (apiUrl)
croniclePayload.params.url = apiUrl;
if (method)
croniclePayload.params.method = method;
if (schedule)
croniclePayload.timing = schedule;
const payload = {
id: cronJobId,
...croniclePayload,
category: "general", // Need to make this dynamic
timeout: 60,
max_children: 1,
api_key: this.cronicleApiKey,
};
await axios_1.default.post(`${this.cronicleEndpoint}/update_event/v1`, payload);
}
catch (error) {
(0, config_1.getConfig)().LOGGER.error(`Failed to update Cronicle job: ${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) {
(0, config_1.getConfig)().LOGGER.error(`Failed to get job: ${error.message}`);
return;
}
}
async getSchedules(filter) {
try {
const res = await axios_1.default.post(`${this.cronicleEndpoint}/get_schedule/v1`, {
api_key: this.cronicleApiKey,
offset: filter.offset ?? 0,
limit: filter.limit ?? 100,
});
return res.data;
}
catch (error) {
(0, config_1.getConfig)().LOGGER.error(`Failed to get schedules: ${error.message}`);
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;