const dbcontroller = require('../db'); const util = require('../util'); const postRequestToDo = async function (msn, body) { try { await dbcontroller.addDevice(body, null) body.inventory.forEach(async device => { await dbcontroller.addDevice(device, body.relayMsn) }); return { success: true } } catch (e) { throw new Error(e.message) } } const getRequestToDo = async function (msn) { try { var todos = [] const children = await dbcontroller.getDeviceChildren(msn) for (const child of children) { ///// Retrieve child const device = await dbcontroller.getDevice(child.msn) var todo = { msn : child.msn } ///// Programs if (device.programs != undefined) { if (child.programmingTimestamp < device.programs.timestamp) { todo.programs = 1 } else if (child.programmingTimestamp > device.programs.timestamp) { todo.programs = 2 } } ///// Configuration if (device.configuration != undefined) { if (child.configurationTimestamp < device.configuration.timestamp) { todo.configuration = 1 } else if (child.configurationTimestamp > device.configuration.timestamp) { todo.configuration = 2 } } ///// Slots if (device.slots != undefined) { if (child.programmingTimestamp < device.slots.timestamp) { todo.slots = 1 } else if (child.programmingTimestamp > device.slots.timestamp) { todo.slots = 2 } } ///// Status if (device.statusCommand != undefined) { todo.status = 1 } ///// Manual if (device.manualCommand != undefined) { todo.manual = 1 } ///// Acknowledged if (device.acknowledgedAlerts != undefined) { todo.acknowledged = 1 } ///// Firmware if (device.firmware != undefined) { device.firmware = 1 } ///// Add objet to todos only if something to do if (Object.keys(todo).length > 1) { todos.push(todo) } } return todos } catch (e) { throw new Error(e.message) } } const getStatus = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } return device.status || {} } catch (e) { throw new Error(e.message) } } const updateStatus = async function (msn, status, timestamp) { try { ///// Retrieve device var device = await dbcontroller.getDevice(msn) if (!device) { await dbcontroller.addDevice({"msn": msn}); } device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variables if (status) { device.status = { timestamp: timestamp || util.unixTimestamp(), status: status } } else { device.status = undefined } ///// Update device await dbcontroller.updateDevice(device); return 200; } catch (e) { throw new Error(e.message) } } const getPrograms = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } return device.programs || {} } catch (e) { throw new Error(e.message) } } const postPrograms = async function (msn, programs, timestamp) { try { ///// Retrieve device var device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variables if (programs) { device.programs = { timestamp: timestamp || util.unixTimestamp(), programs: programs } } else { device.programmingTimestamp = device.programs.timestamp; // device.programs = undefined } ///// Update device await dbcontroller.updateDevice(device) return device.programs } catch (e) { throw new Error(e.message) } } const getConfiguration = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } return device.configuration || {} } catch (e) { throw new Error(e.message) } } const postConfiguration = async function (msn, configuration, timestamp) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variables if (configuration) { device.configuration = { timestamp: timestamp || util.unixTimestamp(), configuration: configuration } } else { device.configurationTimestamp = device.configuration.timestamp; // device.configuration = undefined } ///// Update device await dbcontroller.updateDevice(device) return device.configuration } catch (e) { throw new Error(e.message) } } const getSlots = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } return device.slots || {} } catch (e) { throw new Error(e.message) } } const postSlots = async function (msn, slots, timestamp) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variables if (slots) { device.slots = { timestamp: timestamp || util.unixTimestamp(), slots: slots } } else { device.programmingTimestamp = device.slots.timestamp; // device.slots = undefined } ///// Update device await dbcontroller.updateDevice(device, msn) return device.slots } catch (e) { throw new Error(e.message) } } const getManualCommand = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } return device.manualCommand || {} } catch (e) { throw new Error(e.message) } } const postManualCommand = async function (msn, manualCommand) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variable if (manualCommand) { device.manualCommand = { timestamp: util.unixTimestamp(), manualCommand: manualCommand } } else { device.manualCommand = undefined } ///// Update device await dbcontroller.updateDevice(device, msn) return device.manualCommand } catch (e) { throw new Error(e.message) } } const getStatusCommand = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } return device.statusCommand || {} } catch (e) { throw new Error(e.message) } } const postStatusCommand = async function (msn, statusCommand) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variable if (statusCommand) { device.statusCommand = { timestamp: util.unixTimestamp(), statusCommand: statusCommand } } else { device.statusCommand = undefined } ///// Update device await dbcontroller.updateDevice(device, msn) return device.statusCommand } catch (e) { throw new Error(e.message) } } const getAcknowledgedAlerts = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } return device.acknowledgedAlerts || {} } catch (e) { throw new Error(e.message) } } const postAcknowledgedAlerts = async function (msn, acknowledgedAlerts) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variable if (acknowledgedAlerts) { device.acknowledgedAlerts = { timestamp: util.unixTimestamp(), acknowledgedAlerts: acknowledgedAlerts } } else { device.acknowledgedAlerts = undefined } ///// Update device await dbcontroller.updateDevice(device, msn) return device.acknowledgedAlerts } catch (e) { throw new Error(e.message) } } module.exports = { getStatus, updateStatus, postRequestToDo, getRequestToDo, getPrograms, postPrograms, getConfiguration, postConfiguration, getSlots, postSlots, getManualCommand, postManualCommand, getStatusCommand, postStatusCommand, getAcknowledgedAlerts, postAcknowledgedAlerts, }