iobroker.fully-tablet-control
Version:
957 lines (853 loc) • 253 kB
JavaScript
'use strict';
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const { default: axios } = require('axios');
const schedule = require('cron').CronJob; // Cron Scheduler
const SunCalc = require('suncalc2');
const object = require('./lib/object_definition');
const device_Folder_Object = object['object_device_Folder_definitions'];
const main_Object = object['object_main_definitions'];
const command_Object = object['object_commands_definitions'];
const device_info_Object = object['object_device_info_definitions'];
const memory_Object = object['object_memory_definitions'];
const kiosk_Object = object['object_kiosk_definitions'];
const vis_View_object = object['object_vis_View_definitions'];
//Timeout init
let requestTimeout = null;
let automatic_briTimeout = null;
let viewTimer = null;
let ScreensaverReturn = null;
const foregroundAppTimer = [];
const logMessageTimer = [];
const kioskPinTimeout = [];
let BriRequestTimeout = null;
const captureTimeout = [];
//global variables
const ip = [];
const User = [];
const port = [];
const password = [];
const deviceInfo = [];
const tabletName = [];
let day_Time = null;
const deviceEnabled = [];
let fireTabletInterval = null;
let interval = null;
let imageTimeout = null;
let checkInterval = null;
let brightnessControlEnabled = null;
const telegramStatus = [];
const foregroundStart = [];
const messageSend = [];
const AlertMessageSend = [];
const logMessage = [];
const messageCharging = [];
const enabledBrightness = [];
const screensaverTimer = [];
const screenSaverTime = [];
const enableScreenSaverBrightness = [];
const viewNumber = [];
let timeMode = null;
const chargeDeviceValue = [];
let homeView = null;
const imageNr = [];
const loop = [];
const wishView = [];
const time = [];
const channelFolder = ['device_info', 'commands'];
const manualBrightnessMode = [];
const motionID = [];
const motionVal = [];
const commandsID = [];
const brightness = [];
const isInScreensaver = [];
const foreground = [];
const versionCheck = [];
const screensaverOnURL = [];
const screensaverOffURL = [];
const motionDetectorStatus = [];
const imageObj = {};
let shareObj = {};
const manuel_screenSaver = [false];
class FullyTabletControl extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'fully-tablet-control',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
await this.initialization();
await this.stateRequest();
await this.astroTime();
await this.automatic_bri();
// @ts-ignore
// await this.checkView();
}
async initialization() {
try {
// let hostname = this.host
// console.log(hostname)
this.log.debug(`prepare adapter for initialization`);
this.log.debug(`Adapter config is read out`);
// Interval init
try {
// polling min 5 sec.
interval = this.config.interval * 1000;
if (interval < 10000) {
this.log.warn(
`the request interval time falls below the permitted limit of 10 sec ==> ${interval} ms => ${
interval / 1000
} sec`,
);
interval = 10000;
}
this.log.debug(`Adapter config for request interval readout --> ${interval} ms`);
// polling min 1 min.
checkInterval = this.config['checkInterval'] * 60000;
if (checkInterval < 60000) {
this.log.warn(
`the brightness interval time falls below the permitted limit of 1 min ==> ${checkInterval} ms => ${
checkInterval / 60000
} min`,
);
checkInterval = 60000;
}
this.log.debug(`Adapter config for brightness interval readout --> ${checkInterval} ms`);
// polling min 1 min.
fireTabletInterval = this.config['fireTablet'] * 60000;
if (fireTabletInterval < 60000) {
this.log.warn(
`the fire Tablet Interval time falls below the permitted limit of 1 min ==> ${fireTabletInterval} ms => ${
fireTabletInterval / 60000
} min`,
);
fireTabletInterval = 60000;
}
this.log.debug(`Adapter config for request interval readout --> ${interval} ms`);
// polling min 1 sec / max 5 sec.
imageTimeout = this.config['imageTimeout'] * 1000;
if (imageTimeout < 1000) {
this.log.warn(
`the image Timeout time falls below the permitted limit of 1 sec ==> ${imageTimeout} ms => ${
imageTimeout / 1000
} sec`,
);
imageTimeout = 1000;
}
if (imageTimeout > 5000) {
this.log.warn(
`the image timeout time is greater than 5 sec ==> ${imageTimeout} ms => ${
imageTimeout / 1000
} sec. is reset to 5 sec.`,
);
imageTimeout = 5000;
}
this.log.debug(`Adapter config for image timeout readout --> ${imageTimeout} ms`);
this.log.debug(`Interval initialization has been fully initialized`);
} catch (error) {
this.log.error(
`Interval initialization funktion has a problem: ${error.message}, stack: ${error.stack}`,
);
}
// Login init
try {
//read devices and created httpLink
const login = this.config.devices;
if ((!login && login['length'] !== 0) || (login !== [] && login['length'] !== 0)) {
for (const i in login) {
ip[i] = login[i].ip;
port[i] = login[i].port;
password[i] = encodeURIComponent(login[i].password);
deviceEnabled[i] = login[i].enabled;
tabletName[i] = login[i].name;
deviceInfo[i] = `http://${ip[i]}:${port[i]}/?cmd=deviceInfo&type=json&password=${password[i]}`;
this.log.debug(`the initialization Ip for ${tabletName[i]} was Successfully: ${ip[i]}`);
this.log.debug(`the initialization port for ${tabletName[i]} was Successfully: ${port[i]}`);
this.log.debug(
`the initialization password for ${tabletName[i]} was Successfully: ${password[i]}`,
);
this.log.debug(
`the initialization deviceInfoUrl for ${tabletName[i]} was Successfully: ${deviceInfo[i]}`,
);
this.log.debug(`Check whether the IP address is available for the ${tabletName[i]}`);
deviceEnabled[i] = ip[i] !== '' && deviceEnabled[i];
if (ip[i] === '') this.log.warn(`${tabletName[i]} has no ip address device is not queried`);
if (deviceEnabled[i])
this.log.debug(`the initialization for ${tabletName[i]} was Successfully`);
this.log.debug(`it is checked whether the name of the device is entered`);
// Prepare tablet name
if (tabletName[i] !== '') {
this.log.debug(`the name of the device is entered and is used --> ${tabletName[i]}`);
tabletName[i] = await this.replaceFunction(tabletName[i]);
} else if (deviceEnabled[i]) {
this.log.debug(
`The name of the device is not entered; the IP address is used for the name --> ${ip[i]}`,
);
tabletName[i] = await this.replaceFunction(ip[i]);
}
this.log.debug(`Tablet name is being prepared: ${tabletName[i]}`);
this.log.debug(`${tabletName[i]} login has been fully initialized`);
}
} else {
deviceEnabled[1] = false;
}
} catch (error) {
this.log.error(`login initialization funktion has a problem: ${error.message}, stack: ${error.stack}`);
}
// Telegram User init
try {
//read telegram user
this.log.debug(`read out the adapter config for Telegram users`);
const telegramOn = this.config['telegram_enabled'];
const telegramUser = this.config.telegram;
if (telegramOn) {
// @ts-ignore
if (
(!telegramUser && telegramUser.length !== 0) ||
(telegramUser !== [] && telegramUser.length !== 0)
) {
for (const u in telegramUser) {
User[u] = telegramUser[u].telegramUser;
}
this.log.debug(`Telegram users were read out ==> ${JSON.stringify(User)}`);
}
if (telegramOn) this.log.debug(`Telegram messages are switched on`);
if (!telegramOn) this.log.debug(`Telegram messages are switched off`);
}
this.log.debug(`TelegramUser initialization has been fully initialized`);
} catch (error) {
this.log.error(
`Telegram initialization funktion has a problem: ${error.message}, stack: ${error.stack}`,
);
}
//image_funktion init
try {
this.log.debug(`image_funktion is now set to default value 1 for all devices`);
for (const s in deviceEnabled) {
imageNr[s] = 1;
loop[s] = 1;
}
// set a CronJob that resets the variable with the number of stored images to the start value at midnight
const imageSafeCron = new schedule(`1 0 * * * `, async () => {
for (const s in deviceEnabled) {
imageNr[s] = 1;
}
this.log.debug(`imageSafeCron resets the number of stored images. ${new Date()}`);
});
imageSafeCron.start();
this.log.debug(`image_funktion initialization has been fully initialized`);
} catch (error) {
this.log.error(`image_funktion has a problem: ${error.message}, stack: ${error.stack}`);
}
//SendStatus init
try {
this.log.debug(`send status is now set to false for all devices`);
for (const s in deviceEnabled) {
telegramStatus[s] = false;
this.log.debug(`telegramSendStatus set for ${tabletName[s]} to: ${telegramStatus[s]}`);
messageSend[s] = true;
this.log.debug(`message Send status set for ${tabletName[s]} to: ${messageSend[s]}`);
AlertMessageSend[s] = false;
this.log.debug(`AlertMessage Send status set for ${tabletName[s]} to: ${AlertMessageSend[s]}`);
logMessage[s] = false;
this.log.debug(`logMessage status set for ${tabletName[s]} to: ${logMessage[s]}`);
messageCharging[s] = false;
this.log.debug(`messageCharging status set for ${tabletName[s]} to: ${messageCharging[s]}`);
versionCheck[s] = false;
this.log.debug(`versionCheck status set for ${tabletName[s]} to: ${versionCheck[s]}`);
}
this.log.debug(`SendStatus initialization has been fully initialized`);
} catch (error) {
this.log.error(`SendStatus funktion has a problem: ${error.message}, stack: ${error.stack}`);
}
// foregroundStatus init
try {
this.log.debug(`foregroundStatus is now set to false for all devices`);
for (const f in deviceEnabled) {
if (deviceEnabled[f]) {
foregroundStart[f] = false;
this.log.debug(
`foregroundStart status set for ${tabletName[f]} to: ${JSON.stringify(telegramStatus)}`,
);
}
}
this.log.debug(`foregroundStatus initialization has been fully initialized`);
} catch (error) {
this.log.error(`foregroundStatus funktion has a problem: ${error.message}, stack: ${error.stack}`);
}
// BrightnessControl init
try {
this.log.debug(`Check whether the brightness control is activated`);
brightnessControlEnabled = JSON.parse(this.config['brightness_on']);
const brightnessObject = this.config.brightness;
if (brightnessControlEnabled) {
// @ts-ignore
if (
(!brightnessObject && brightnessObject.length !== 0) ||
(brightnessObject !== [] && brightnessObject.length !== 0)
) {
for (const b in deviceEnabled) {
this.log.debug(`Check whether the brightness control is activated for ${tabletName[b]}`);
if (brightnessObject[b] !== undefined) {
enabledBrightness[b] = brightnessObject[b]['enabledBrightness'];
} else {
this.log.warn(
`Attention the brightness control of ${tabletName[b]} is not activated correctly ==> undefined`,
);
enabledBrightness[b] = false;
this.log.warn(
`it is switched off temporarily, please deactivate it or activate it correctly`,
);
}
manualBrightnessMode[b] = await this.getStateAsync(
`device.${tabletName[b]}.brightness_control_mode`,
);
if (manualBrightnessMode[b] === null) {
manualBrightnessMode[b] = false;
} else {
manualBrightnessMode[b] = manualBrightnessMode[b].val;
}
}
} else {
for (const b in deviceEnabled) {
this.log.debug(
`The brightness settings for the devices are not defined. The manual mode is now switched on`,
);
enabledBrightness[b] = false;
// await this.setStateAsync(`device.${await this.replaceFunction(tabletName[b])}.brightness_control_mode`, true, true);
}
}
} else {
for (const b in deviceEnabled) {
this.log.debug(
`automatic brightness control for ${tabletName[b]} is switched off the manual control is now switched on`,
);
enabledBrightness[b] = false;
}
}
this.log.debug(`BrightnessControl initialization has been fully initialized`);
} catch (error) {
this.log.error(
`BrightnessControl init funktion has a problem: ${error.message}, stack: ${error.stack}`,
);
}
// motion Detector ID
try {
this.log.debug(`motion detector ID is read from the config and subscribed to`);
//read motion ID from Admin and subscribe
const motion = this.config.motion;
const motionSensor_enabled = JSON.parse(this.config.motionSensor_enabled);
if (motionSensor_enabled) {
// @ts-ignore
if ((!motion && motion.length !== 0) || (motion !== [] && motion.length !== 0)) {
for (const sensor in motion) {
this.log.debug(`Check if the sensor is activated`);
if (motion[sensor]['enabled']) {
this.log.debug(`Check whether the sensor has an ID entry`);
if (motion[sensor]['motionid'] !== '') {
this.log.debug(`read out all Motion ID's on the config`);
motionID[sensor] = motion[sensor]['motionid'];
motionVal[sensor] = false;
this.log.debug(`subscribe all Motion ID's`);
this.subscribeForeignStates(motion[sensor]['motionid']);
} else {
this.log.warn(`Attention there is no motion detector ID entered`);
}
} else {
this.log.debug(
`the motion detector with the id: ${motion[sensor]['motionid']} is deactivated !!`,
);
}
}
this.log.debug(`motion Detector ID initialization has been fully initialized`);
} else {
this.log.warn(
`Motion detector is not defined, please define at least one or switch off the motion detector control!`,
);
}
}
} catch (error) {
this.log.error(`motionDetectorID funktion has a problem: ${error.message}, stack: ${error.stack}`);
}
// Screensaver init
try {
// read screensaverTimer and screenSaverSelect
this.log.debug(`The screensaver config is now read out`);
const screenSaverON = JSON.parse(this.config['screenSaverON']);
const screenSaverObj = this.config['screenSaver'];
const screenSaverDeletion = JSON.parse(this.config['screenSaverDeletion']);
this.log.debug(`It is checked whether the screensaver control is switched on`);
if (screenSaverON) {
this.log.debug(`Screensaver control is switched on`);
// @ts-ignore
if (
(!screenSaverObj && screenSaverObj.length !== 0) ||
(screenSaverObj !== [] && screenSaverObj.length !== 0)
) {
for (const s in deviceEnabled) {
if (deviceEnabled[s]) {
if (screenSaverObj[s] !== undefined) {
manuel_screenSaver[s] = false;
this.log.debug(`Screensaver time is now read out`);
screenSaverTime[s] = JSON.parse(screenSaverObj[s]['minute']) * 60000;
this.log.debug(
`Screensaver time was successfully read out for ${tabletName[s]} ==> ${screenSaverTime[s]}`,
);
screensaverOnURL[
s
] = `http://${ip[s]}:${port[s]}/?cmd=startScreensaver&password=${password[s]}`;
screensaverOffURL[
s
] = `http://${ip[s]}:${port[s]}/?cmd=stopScreensaver&password=${password[s]}`;
// manuel_screenSaver[s] = false;
this.log.debug(`the screensaver mode is read from the config`);
const screensaverMode = JSON.parse(screenSaverObj[s]['screensaverMode']);
this.log.debug(`the screensaver mode was successfully read from the config`);
this.log.debug(
`Url for the screensaver is now requested for ${tabletName[s]} from the config`,
);
const screenSaverUrl = screenSaverObj[s].url;
this.log.debug(
`Url for ${tabletName[s]} was successfully obtained from the config`,
);
this.log.debug(
`It is now checked whether the screensaver brightness synchronization is switched on for the ${tabletName[s]}`,
);
enableScreenSaverBrightness[s] = screenSaverObj[s]['enabled'];
this.log.debug(
`For the ${tabletName[s]} the screensaver brightness synchronization switched on`,
);
this.log.debug(
`Check for ${tabletName[s]} whether the screensaver brightness synchronization has been correctly initialized`,
);
if (enableScreenSaverBrightness[s] === undefined) {
enableScreenSaverBrightness[s] = false;
this.log.error(
`[ATTENTION] the brightness synchronization for ${tabletName[s]} was not initialized, please check the box and save it.`,
);
} else {
enableScreenSaverBrightness[s] = screenSaverObj[s]['enabled'];
this.log.debug(
`the screensaver brightness synchronization for ${tabletName[s]} was correctly initialized`,
);
}
if (screensaverMode) {
if (screenSaverUrl === '') {
const playlistUrl = [];
playlistUrl.push(
`http://${ip[s]}:${port[s]}/?cmd=setStringSetting&key=screensaverPlaylist&value=&password=${password[s]}`,
);
playlistUrl.push(
`http://${ip[s]}:${port[s]}/?cmd=setStringSetting&key=screensaverWallpaperURL&value=fully://color black&password=${password[s]}`,
);
this.log.debug(
`The screensaver url for ${tabletName[s]} is not set the screensaver is set to a black image`,
);
for (const playlistUrlKey in playlistUrl) {
await axios
.get(playlistUrl[playlistUrlKey])
.then(async (result) => {
this.log.debug(
`${tabletName[s]} send status for playlistUrl = status Code: ${result.status} => status Message: ${result.statusText}`,
);
})
.catch(async (error) => {
this.log.error(
`${tabletName[s]} send status for playlistUrl has a problem => ${error.message}, stack: ${error.stack}`,
);
});
}
} else {
const screenUrl = [
{
type: 4,
url: screenSaverUrl,
loopItem: true,
loopFile: false,
fileOrder: 0,
nextItemOnTouch: false,
nextFileOnTouch: false,
nextItemTimer: 0,
nextFileTimer: 0,
},
];
const playlistUrl = `http://${ip[s]}:${
port[s]
}/?cmd=setStringSetting&key=screensaverPlaylist&value=${JSON.stringify(
screenUrl,
)}&password=${password[s]}`;
this.log.debug(
`YouTube Screensaver Url => ${screenSaverUrl} is set for ${tabletName[s]}`,
);
await axios
.get(playlistUrl)
.then(async (result) => {
this.log.debug(
`${tabletName[s]} send status for playlistUrl = status Code: ${result.status} => status Message: ${result.statusText}`,
);
})
.catch(async (error) => {
this.log.error(
`${tabletName[s]} send status for playlistUrl could not be sent => ${error.message}, stack: ${error.stack}`,
);
});
}
} else if (!screensaverMode) {
if (screenSaverUrl === '') {
const playlistUrl = [];
playlistUrl.push(
`http://${ip[s]}:${port[s]}/?cmd=setStringSetting&key=screensaverPlaylist&value=&password=${password[s]}`,
);
playlistUrl.push(
`http://${ip[s]}:${port[s]}/?cmd=setStringSetting&key=screensaverWallpaperURL&value=fully://color black&password=${password[s]}`,
);
this.log.debug(
`The screensaver url for ${tabletName[s]} is not set the screensaver is set to a black image`,
);
for (const playlistUrlKey in playlistUrl) {
await axios
.get(playlistUrl[playlistUrlKey])
.then(async (result) => {
this.log.debug(
`${tabletName[s]} send status for playlistUrl = status Code: ${result.status} => status Message: ${result.statusText}`,
);
})
.catch(async (error) => {
this.log.error(
`${tabletName[s]} send status for playlistUrl has a problem => ${error.message}, stack: ${error.stack}`,
);
});
}
} else {
const playlistUrl = [];
playlistUrl.push(
`http://${ip[s]}:${port[s]}/?cmd=setStringSetting&key=screensaverPlaylist&value=&password=${password[s]}`,
);
playlistUrl.push(
`http://${ip[s]}:${port[s]}/?cmd=setStringSetting&key=screensaverWallpaperURL&value=${screenSaverUrl}&password=${password[s]}`,
);
this.log.debug(
`${tabletName[s]} screensaver is switched on for wallpaper the playlist is now deleted and WallpaperUrl is set`,
);
this.log.debug(
`Wallpaper Screensaver Url => ${screenSaverUrl} is set for ${tabletName[s]}`,
);
for (const playlistUrlKey in playlistUrl) {
await axios
.get(playlistUrl[playlistUrlKey])
.then(async (result) => {
this.log.debug(
`${tabletName[s]} send status for playlistUrl = status Code: ${result.status} => status Message: ${result.statusText}`,
);
})
.catch(async (error) => {
this.log.error(
`${tabletName[s]} send status for playlistUrl has a problem => ${error.message}, stack: ${error.stack}`,
);
});
}
}
}
} else {
manuel_screenSaver[s] = true;
this.log.warn(`There is no screensaver config set for ${tabletName[s]}`);
this.log.warn(
`Please add a screensaver configuration or switch off the screensaver control.`,
);
}
} else {
manuel_screenSaver[s] = true;
}
}
}
} else {
if (screenSaverDeletion) {
for (const deviceEnabledkey in deviceEnabled) {
const playlistUrl = [];
if (deviceEnabled[deviceEnabledkey]) {
playlistUrl.push(
`http://${ip[deviceEnabledkey]}:${port[deviceEnabledkey]}/?cmd=setStringSetting&key=timeToScreensaverV2&value=0&password=${password[deviceEnabledkey]}`,
);
playlistUrl.push(
`http://${ip[deviceEnabledkey]}:${port[deviceEnabledkey]}/?cmd=setStringSetting&key=screensaverPlaylist&value=&password=${password[deviceEnabledkey]}`,
);
playlistUrl.push(
`http://${ip[deviceEnabledkey]}:${port[deviceEnabledkey]}/?cmd=setStringSetting&key=screensaverWallpaperURL&value=fully://color black&password=${password[deviceEnabledkey]}`,
);
}
for (const playlistUrlKey in playlistUrl) {
await axios
.get(playlistUrl[playlistUrlKey])
.then(async (result) => {
this.log.debug(
`${tabletName[deviceEnabledkey]} send status for playlistUrl = status Code: ${result.status} => status Message: ${result.statusText}`,
);
})
.catch(async (error) => {
this.log.error(
`${tabletName[deviceEnabledkey]} send status for playlistUrl has a problem => ${error.message}, stack: ${error.stack}`,
);
});
}
}
}
}
this.log.debug(`Screensaver initialization has been fully initialized`);
} catch (error) {
this.log.error(`Screensaver init funktion has a problem: ${error.message}, stack: ${error.stack}`);
}
// visView init
try {
this.log.debug(`Vis View control is now initialized`);
const viewName = [];
this.log.debug(`Check whether the visView control is switched on`);
const view_enabled = JSON.parse(this.config['viewChange_enabled']);
if (view_enabled) {
this.log.debug(`the visView control is switched on `);
this.log.debug(`visView config is now read out`);
const visView = this.config.visView;
this.log.debug(`the visView config was read out successfully`);
this.log.debug(
`now visProjekt / viewName / view Number the homeView / wishView and the time are read out from the config and chamfered together`,
);
for (const view in visView) {
const visProjekt = visView[view]['visProjekt'];
viewName[view] = visView[view]['viewName'];
if (visView[view]['viewNumber'] !== '') {
viewNumber[view] = JSON.parse(visView[view]['viewNumber']);
}
const tempTime = visView[view].time;
homeView = `${visProjekt}/${viewName[0]}`;
wishView[view] = `${visProjekt}/${viewName[view]}`;
if (tempTime === '0' || tempTime === '00') {
time[view] = 0;
} else {
time[view] = JSON.parse(visView[view].time);
}
}
this.log.debug(`the homeView was created successfully => ${homeView}`);
this.log.debug(`the wishView was created successfully => ${wishView}`);
this.log.debug(`the viewNumber was created successfully => ${viewNumber}`);
this.log.debug(`the viewTime was created successfully => ${time}`);
}
this.log.debug(`visView initialization has been fully initialized`);
} catch (error) {
this.log.error(`Screensaver init funktion has a problem: ${error.message}, stack: ${error.stack}`);
}
// AstroTime restart
try {
const astroTimeCron = new schedule(`0 2 * * * `, async () => {
await this.astroTime();
});
astroTimeCron.start();
this.log.debug(`AstroTime restart is set to 2 o'clock in the morning`);
} catch (error) {
this.log.error(
`AstroTime restart init funktion has a problem: ${error.message}, stack: ${error.stack}`,
);
}
} catch (error) {
this.log.error(`[initialization] funktion : ${error.message}, stack: ${error.stack}`);
}
}
async stateRequest() {
try {
if (requestTimeout) clearTimeout(requestTimeout);
if (deviceInfo.length !== 0) {
for (const i in deviceInfo) {
if (deviceEnabled[i]) {
this.log.debug(`device: ${tabletName[i]} enabled`);
const deviceID = await this.replaceFunction(tabletName[i]);
this.log.debug(`API request started ...`);
await axios
.get(deviceInfo[i])
.then(async (apiResult) => {
if (apiResult['status'] === 200) {
if (apiResult['data']['status'] !== 'Error') {
this.log.debug(
`API request ended successfully --> result from api Request: ${JSON.stringify(
apiResult['data'],
)}`,
);
this.log.debug(`State Create is now running ...`);
await this.create_state(i);
this.log.debug(`State Create was carried out`);
this.log.debug(`check if battery level is> = 0 if yes then restart app`);
this.log.debug(`States are now written`);
await this.state_write(apiResult, i, deviceID);
//set is Wallpanel Alive to true if the request was successful
this.setState(`device.${deviceID}.isFullyAlive`, { val: true, ack: true });
this.log.debug(`states were written`);
// clear log message timer
if (logMessageTimer[i]) clearTimeout(logMessageTimer[i]);
this.log.debug(`logMessageTimer for ${tabletName[i]} will be deleted`);
logMessage[i] = false;
this.log.debug(`logMessage set to ${logMessage[i]} for ${tabletName[i]}`);
// await this.charger();
} else {
this.log.error(`${apiResult['data']['statustext']}`);
}
}
})
.catch(async (error) => {
if (!logMessage[i]) {
logMessage[i] = true;
this.log.debug(`logMessage set to ${logMessage[i]} for ${tabletName[i]}`);
this.log.error(`[Request] ${tabletName[i]} Unable to contact: ${error} | ${error}`);
} else if (!logMessageTimer[i]) {
if (logMessageTimer[i]) clearTimeout(logMessageTimer[i]);
this.log.debug(`logMessageTimer for ${tabletName[i]} will be deleted`);
this.log.debug(
`set logMessageTimer for ${tabletName[i]} to ${3600000 / 60000} min`,
);
logMessageTimer[i] = setTimeout(async () => {
logMessage[i] = false;
this.log.debug(`logMessage set to ${logMessage[i]} for ${tabletName[i]}`);
}, 3600000);
}
this.setState(`device.${deviceID}.isFullyAlive`, { val: false, ack: true });
this.log.debug(`set isFullyAlive to false for ${tabletName[i]}`);
});
}
}
}
requestTimeout = setTimeout(async () => {
this.log.debug(`start devices to query for new values`);
await this.stateRequest();
}, interval);
} catch (error) {
this.log.error(`Request function has a problem : ${error.message}, stack: ${error.stack}`);
}
}
/**
* Check on number.
* @param {Object} apiResult
* @param {string|number} index
* @param {string} deviceID
*/
async state_write(apiResult, index, deviceID) {
try {
this.log.debug(`prepare state write`);
const objects = apiResult['data'];
this.log.debug(`[result]: ${JSON.stringify(objects)}`);
for (const obj in objects) {
switch (obj) {
//TODO: alle cases in {} setzen
case 'isInScreensaver':
isInScreensaver[index] = objects['isInScreensaver'];
this.setState(`device.${deviceID}.device_info.isInScreensaver`, {
val: isInScreensaver[index],
ack: true,
});
this.log.debug(`IP state for ${deviceID} : ${isInScreensaver[index]}`);
if (
!JSON.parse(this.config['motionSensor_enabled']) &&
!isInScreensaver[index] &&
!manuel_screenSaver[index]
) {
const screenSaverObj = this.config.screenSaver;
// @ts-ignore
if (
(!screenSaverObj && screenSaverObj.length !== 0) ||
(screenSaverObj !== [] && screenSaverObj.length !== 0)
) {
manuel_screenSaver[index] = true;
await this.screensaverManuel(index);
}
}
break;
case 'currentFragment': {
const currentFragment = objects['currentFragment'];
this.setState(`device.${deviceID}.device_info.currentFragment`, {
val: currentFragment,
ack: true,
});
this.log.debug(`currentFragment state for ${deviceID} : ${currentFragment}`);
break;
}
case 'topFragmentTag': {
// new form App Version 1.40.3
const topFragmentTag = objects['topFragmentTag'];
this.setState(`device.${deviceID}.device_info.currentFragment`, {
val: topFragmentTag,
ack: true,
});
this.log.debug(`currentFragment state for ${deviceID} : ${topFragmentTag}`);
break;
}
case 'deviceModel': {
const deviceModel = objects['deviceModel'];
this.setState(`device.${deviceID}.device_info.deviceModel`, { val: deviceModel, ack: true });
this.log.debug(`deviceModel state for ${deviceID} : ${deviceModel}`);
break;
}
case 'deviceName': {
const deviceName = objects['deviceName'];
this.setState(`device.${deviceID}.device_info.deviceName`, { val: deviceName, ack: true });
this.log.debug(`deviceName state for ${deviceID} : ${deviceName}`);
break;
}
case 'wifiSignalLevel': {
const wifiSignalLevel = objects['wifiSignalLevel'];
this.setState(`device.${deviceID}.device_info.wifiSignalLevel`, {
val: wifiSignalLevel,
ack: true,
});
this.log.debug(`wifiSignalLevel state for ${deviceID} : ${wifiSignalLevel}`);
break;
}
case 'kioskMode': {
const kioskMode = objects['kioskMode'];
this.setState(`device.${deviceID}.device_info.kioskMode`, { val: kioskMode, ack: true });
this.log.debug(`kioskMode state for ${deviceID} : ${kioskMode}`);
break;
}
case 'displayHeightPixels': {
const displayHeightPixels = objects['displayHeightPixels'];
this.setState(`device.${deviceID}.device_info.displayHeightPixels`, {
val: displayHeightPixels,
ack: true,
});
this.log.debug(`displayHeightPixels state for ${deviceID} : ${displayHeightPixels}`);
break;
}
case 'appVersionName': {
const appVersionName = objects['appVersionName'];
this.setState(`device.${deviceID}.device_info.appVersionName`, {
val: appVersionName,
ack: true,
});
this.log.debug(`appVersionName state for ${deviceID} : ${appVersionName}`);
break;
}
case 'maintenanceMode': {
const maintenanceMode = objects['maintenanceMode'];
this.setState(`device.${deviceID}.device_info.maintenanceMode`, {
val: maintenanceMode,
ack: true,
});
this.log.debug(`maintenanceMode state for ${deviceID} : ${maintenanceMode}`);
break;
}
case 'mac': {
const mac = objects.mac;
this.setState(`device.${deviceID}.device_info.mac`, { val: mac, ack: true });
this.log.debug(`mac state for ${deviceID} : ${mac}`);
break;
}
case 'Mac': {