sokolov-sbs-flowserver/flow/send_to_display.js
2025-08-07 21:49:01 +02:00

84 lines
2.3 KiB
JavaScript

exports.id = 'sendtodisplay';
exports.title = 'Send to display';
exports.group = 'Worksys';
exports.color = '#5D9CEC';
exports.version = '0.0.1';
exports.output = 2;
exports.input = 2;
exports.author = 'Rastislav Kovac';
exports.icon = 'cloud-upload';
const { execSync } = require('child_process');
exports.install = function(instance) {
const getDepartures = setInterval(checkIfWeHaveDepartures, 60000);
let departures = [];
let firstDepartureTime = getCurrentTimeFormatted();
function checkIfWeHaveDepartures() {
if (departures.length === 0) instance.send(1, "reload");
}
instance.on('1', flowdata => {
departures = flowdata.data;
})
instance.on('close', function() {
clearInterval(getDepartures);
})
instance.on('0', function(_) {
// for some reason new Date() function does not set month and year in local timezone, so we use "timedatectl" command
let dateFromCommand = execSync("timedatectl", {}).toString();
let first = dateFromCommand.search("time:");
let last = dateFromCommand.search(" CE");
dateFromCommand = dateFromCommand.slice(first, last); //Thu 2022-04-07 13:38:03
const d = new Date(dateFromCommand);
let hour = d.getHours();
let minute = d.getMinutes();
console.log('******hour, minute', hour, minute)
if (minute < 10) minute = `0${minute}`;
if (hour < 10) hour = `0${hour}`;
const now = `${hour}:${minute}`;
console.log('******--------', firstDepartureTime, now);
console.log('******--------', firstDepartureTime < now);
if (firstDepartureTime < now) {
let departures_filtered = departures.filter(departure => {
const timeOfDeparture = departure[1];
if (now < timeOfDeparture) return true;
return false;
})
if (departures_filtered.length > 0) firstDepartureTime = departures_filtered[0][1];
else firstDepartureTime = "00:00";
instance.send(0, departures_filtered.slice(0, 10));
}
})
}
function getCurrentTimeFormatted() {
const now = new Date(); // Get the current date and time
const hours = now.getHours(); // Get the current hour (0-23)
const minutes = now.getMinutes(); // Get the current minute (0-59)
// Pad with leading zero if the number is less than 10
const formattedHours = hours < 10 ? '0' + hours : hours;
const formattedMinutes = minutes < 10 ? '0' + minutes : minutes;
return `${formattedHours}:${formattedMinutes}`;
}