const dbcontroller = require('../db'); const util = require('../util'); const deviceUsesPrograms = function(device) { switch (device.msn.slice(0,2).toUpperCase() || 'xx') { case '1F': case '20': case '51': case '53': case '55': case '5A': case '7A': return false; default: break; } if (device.programs != undefined && device.programs.programs != undefined) { return true; } if (device.programs == undefined && device.slots == undefined) { return true; } return false; } const deviceUsesSlots = function(device) { switch (device.msn.slice(0,2).toUpperCase() || 'xx') { case '1F': case '20': case '51': case '53': case '55': case '5A': case '7A': return true; default: break; } if (device.slots != undefined && device.slots.slots != undefined) { return true; } if (device.programs == undefined && device.slots == undefined) { return true; } return false; } 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) var parent = await dbcontroller.getDevice(msn) children.push(parent) for (const child of children) { ///// Retrieve child const msn = child.msn || child.relayMsn || null const device = await dbcontroller.getDevice(msn) var todo = { serialNumber : msn } ///// Programs if (child.programmingTimestamp != undefined && deviceUsesPrograms(child)) { timestamp = child.programmingTimestamp || -1 if ((device.programs == undefined) || (timestamp > device.programs.timestamp)) { todo.programs = 2 } else if (timestamp < device.programs.timestamp) { todo.programs = 1 } } ///// Configuration if (child.configurationTimestamp != undefined) { timestamp = child.configurationTimestamp || child.relayConfigurationTimestamp || -1 if ((device.configuration == undefined) || (timestamp > device.configuration.timestamp)) { todo.configuration = 2 } else if (timestamp < device.configuration.timestamp) { todo.configuration = 1 } } ///// Slots if (child.programmingTimestamp != undefined && deviceUsesSlots(child)) { timestamp = child.programmingTimestamp || -1 if ((device.slots == undefined) || (timestamp > device.slots.timestamp)) { todo.slots = 2 } else if (timestamp < device.slots.timestamp) { todo.slots = 1 } } ///// 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 putPrograms = 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: { ...device.programs.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 deletePrograms = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variables device.programs = { timestamp: 0, programs: undefined } ///// Update device await dbcontroller.updateDevice(device, msn) 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 putConfiguration = 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: { ...device.configuration.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 deleteConfiguration = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variables device.configuration = { timestamp: 0, configuration: undefined } ///// Update device await dbcontroller.updateDevice(device, msn) 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 putSlots = 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: { ...device.slots.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 deleteSlots = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variables device.slots = { timestamp: 0, 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 putManualCommand = 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: { ...device.manualCommand.manualCommand, ...manualCommand } } } else { device.manualCommand = undefined } ///// Update device await dbcontroller.updateDevice(device, msn) return device.manualCommand } catch (e) { throw new Error(e.message) } } const deleteManualCommand = 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 device.manualCommand = { timestamp: 0, 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 putStatusCommand = 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: { ...device.statusCommand.statusCommand, ...statusCommand } } } else { device.statusCommand = undefined } ///// Update device await dbcontroller.updateDevice(device, msn) return device.statusCommand } catch (e) { throw new Error(e.message) } } const deleteStatusCommand = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variable device.statusCommand = { timestamp: 0, 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) } } const putAcknowledgedAlerts = 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: { ...device.acknowledgedAlerts.acknowledgedAlerts, ...acknowledgedAlerts } } } else { device.acknowledgedAlerts = undefined } ///// Update device await dbcontroller.updateDevice(device, msn) return device.acknowledgedAlerts } catch (e) { throw new Error(e.message) } } const deleteAcknowledgedAlerts = async function (msn) { try { ///// Retrieve device const device = await dbcontroller.getDevice(msn) if (!device) { throw({message: "module_not_found_for_this_identifier"}) } ///// Set variable device.acknowledgedAlerts = { timestamp: 0, 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, putPrograms, deletePrograms, getConfiguration, postConfiguration, putConfiguration, deleteConfiguration, getSlots, postSlots, putSlots, deleteSlots, getManualCommand, postManualCommand, putManualCommand, deleteManualCommand, getStatusCommand, postStatusCommand, putStatusCommand, deleteStatusCommand, getAcknowledgedAlerts, postAcknowledgedAlerts, putAcknowledgedAlerts, deleteAcknowledgedAlerts, }