123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import LogBook, { Log } from "./LogBook";
- import ModuleManager from "./ModuleManager";
- import { ModuleStatus } from "./types/Modules";
- // type ModuleName = keyof Modules;
- export default abstract class BaseModule {
- protected moduleManager: ModuleManager;
- protected logBook: LogBook;
- protected name: string;
- protected status: ModuleStatus;
- /**
- * Base Module
- *
- * @param name - Module name
- */
- public constructor(name: string) {
- this.moduleManager = ModuleManager.getPrimaryInstance();
- this.logBook = LogBook.getPrimaryInstance();
- this.name = name;
- this.status = "LOADED";
- this.log(`Module (${this.name}) loaded`);
- }
- /**
- * getName - Get module name
- *
- * @returns name
- */
- public getName() {
- return this.name;
- }
- /**
- * getStatus - Get module status
- *
- * @returns status
- */
- public getStatus() {
- return this.status;
- }
- /**
- * setStatus - Set module status
- *
- * @param status - Module status
- */
- public setStatus(status: ModuleStatus) {
- this.status = status;
- }
- /**
- * startup - Startup module
- */
- public async startup() {
- this.log(`Module (${this.name}) starting`);
- this.setStatus("STARTING");
- }
- /**
- * started - called with the module has started
- */
- protected async started() {
- this.log(`Module (${this.name}) started`);
- this.setStatus("STARTED");
- }
- /**
- * shutdown - Shutdown module
- */
- public async shutdown() {
- this.log(`Module (${this.name}) stopping`);
- this.setStatus("STOPPING");
- await this.stopped();
- }
- /**
- * stopped - called when the module has stopped
- */
- protected async stopped() {
- this.log(`Module (${this.name}) stopped`);
- this.setStatus("STOPPED");
- }
- /**
- * log - Add log to logbook
- *
- * @param log - Log message or object
- */
- protected log(log: string | Omit<Log, "timestamp" | "category">) {
- const {
- message,
- type = undefined,
- data = {}
- } = {
- ...(typeof log === "string" ? { message: log } : log)
- };
- this.logBook.log({
- message,
- type,
- category: `modules.${this.getName()}`,
- data: {
- moduleName: this.name,
- ...data
- }
- });
- }
- }
|