@isaac-platform/isaac-integration-sdk
Version:
A Typescript SDK for integrating with ISAAC
102 lines (101 loc) • 4.62 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as isaacBlocks from "./blocks/index.js";
import * as isaacPlaylists from "./playlists/index.js";
import * as isaacEvents from "./events/index.js";
import * as isaacPlayables from "./playables/index.js";
import { EventEmitter } from "events";
// import { emit } from "process";
export class scheduleController extends EventEmitter {
constructor(connection) {
super();
this.pollingInterval = 10000;
this.tickInterval = 500;
this.pollScheduleInterval = setInterval(this.updateSchedule.bind(this), this.pollingInterval);
this.tickScheduleInterval = setInterval(this.scheduleTick.bind(this), this.tickInterval);
this.running = true;
this.isaacConn = connection;
this.blocks = new isaacBlocks.blockController(this.isaacConn);
this.playlists = new isaacPlaylists.playlistController(this.isaacConn);
this.events = new isaacEvents.EventController(this.isaacConn);
this.playables = new isaacPlayables.PlayableController(this.isaacConn);
// this.scheduleBus = new EventEmitter();
}
getSchedule() {
return __awaiter(this, void 0, void 0, function* () {
this.schedule = (yield this.isaacConn.getRequest(`schedule?subsystemExternalId=${this.isaacConn.subsystemID}`))[0];
console.log("Got Schedule");
console.log(this.schedule);
return this.schedule;
});
}
updateSchedule() {
return __awaiter(this, void 0, void 0, function* () {
if (typeof this.schedule == "undefined") {
yield this.getSchedule();
return this.schedule;
}
else {
try {
const response = yield this.isaacConn.getRequest(`schedule?subsystemExternalId=${this.isaacConn.subsystemID}`, {
"headers": {
"If-Modified-Since": `${this.schedule.generatedAt}`
}
});
if (response && Array.isArray(response) && response.length > 0) {
this.schedule = response[0];
}
else {
console.warn('Invalid/Empty Schedule');
}
}
catch (error) {
console.error('Failed to update schedule:', error);
}
}
return this.schedule;
});
}
// Not proud of this implementation.
// TODO: Replace conditionals with a cue system
scheduleTick() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.running)
return;
if (typeof this.schedule == "undefined") {
console.log("Schedule not initialized");
}
else {
if (this.schedule.values.length <= 0) {
console.log("Schedule entries empty");
return;
}
this.schedule.values.filter(function (entry) {
const runnable = [
Date.parse(entry.startTime) <= Date.now(),
Date.parse(entry.startTime) >= (Date.now() - 500),
];
return runnable.indexOf(false) === -1;
}).forEach((entry) => {
this.emit('currentEntry', entry);
});
this.schedule.values.filter(function (entry) {
const runnable = [
Date.parse(entry.startTime) >= Date.now() + 1,
Date.parse(entry.startTime) <= (Date.now() + 1000)
];
return runnable.indexOf(false) === -1;
}).forEach((entry) => {
this.emit('nextEntry', entry);
});
}
});
}
}