Compare commits
3 Commits
a4b4ffe9ff
...
a1d7a7452a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1d7a7452a | ||
|
|
75ff2a6d24 | ||
|
|
d23d6c4a5e |
7
Dockerfile
Normal file
7
Dockerfile
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# LSP Dockerfile
|
||||||
|
FROM node
|
||||||
|
WORKDIR /app
|
||||||
|
COPY package.json /app
|
||||||
|
RUN npm install
|
||||||
|
COPY . /app
|
||||||
|
CMD ["node", "app.js"]
|
||||||
@@ -4,7 +4,18 @@ const models = require('../models')
|
|||||||
const mongodb_hostname = process.env.MONGODB_HOST || '0.0.0.0';
|
const mongodb_hostname = process.env.MONGODB_HOST || '0.0.0.0';
|
||||||
const mongodb_port = process.env.MONGODB_PORT || 27017;
|
const mongodb_port = process.env.MONGODB_PORT || 27017;
|
||||||
const mongodb_database = process.env.MONGODB_DATABASE || 'lsp';
|
const mongodb_database = process.env.MONGODB_DATABASE || 'lsp';
|
||||||
const mongodb_url = 'mongodb://' + mongodb_hostname + ':' + mongodb_port;
|
const mongodb_user = process.env.MONGODB_USER;
|
||||||
|
const mongodb_pass = process.env.MONGODB_PASSWORD;
|
||||||
|
|
||||||
|
// Build the MongoDB URI based on whether authentication is provided
|
||||||
|
let mongodb_url;
|
||||||
|
if (mongodb_user && mongodb_pass) {
|
||||||
|
// Authenticated connection
|
||||||
|
mongodb_url = `mongodb://${mongodb_user}:${mongodb_pass}@${mongodb_hostname}:${mongodb_port}`;
|
||||||
|
} else {
|
||||||
|
// No authentication
|
||||||
|
mongodb_url = `mongodb://${mongodb_hostname}:${mongodb_port}`;
|
||||||
|
}
|
||||||
|
|
||||||
const client = new MongoClient(mongodb_url, { useUnifiedTopology: true });
|
const client = new MongoClient(mongodb_url, { useUnifiedTopology: true });
|
||||||
|
|
||||||
@@ -21,13 +32,8 @@ var days = function(date) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var init = async function () {
|
var init = async function () {
|
||||||
await client.connect(function (err) {
|
try {
|
||||||
if (err) {
|
await client.connect();
|
||||||
console.log('MongoDB Client error');
|
|
||||||
console.log(err);
|
|
||||||
return { error: "Error connecting to DB: " + err }
|
|
||||||
|
|
||||||
} else {
|
|
||||||
_db = client.db(mongodb_database);
|
_db = client.db(mongodb_database);
|
||||||
_devices = _db.collection('devices');
|
_devices = _db.collection('devices');
|
||||||
_data = _db.collection('data');
|
_data = _db.collection('data');
|
||||||
@@ -35,19 +41,16 @@ var init = async function () {
|
|||||||
_events = _db.collection('events');
|
_events = _db.collection('events');
|
||||||
_alerts = _db.collection('alerts');
|
_alerts = _db.collection('alerts');
|
||||||
|
|
||||||
console.log('MongoDB Client connected');
|
if (mongodb_user && mongodb_pass) {
|
||||||
////////////////////////////
|
console.log('MongoDB Client connected with authentication');
|
||||||
// FLUSH DB BY UNCOMMENTING
|
} else {
|
||||||
// _devices.deleteMany(function(err, objects){});
|
console.log('MongoDB Client connected without authentication');
|
||||||
// _data.deleteMany(function(err, objects){});
|
|
||||||
// _ipx_data.deleteMany(function(err, objects){});
|
|
||||||
// _events.deleteMany(function(err, objects){});
|
|
||||||
// _alerts.deleteMany(function(err, objects){});
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
});
|
} catch (err) {
|
||||||
}
|
console.error('MongoDB Client error:', err);
|
||||||
|
return { error: "Error connecting to DB: " + err };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var addData = async function (relay, device, element) {
|
var addData = async function (relay, device, element) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
28
docker-compose.yaml
Normal file
28
docker-compose.yaml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
version: '3' # docker-compose version
|
||||||
|
services: # services which our app going to use. (list of containers we want to create)
|
||||||
|
mongodb: # container name
|
||||||
|
image: mongo # which image container will build on
|
||||||
|
ports:
|
||||||
|
- "27017:27017"
|
||||||
|
networks: # adding network
|
||||||
|
- lsp-network
|
||||||
|
volumes:
|
||||||
|
- mongo-data:/data/db
|
||||||
|
|
||||||
|
lsp:
|
||||||
|
build: . # build the Docker image for the service using the Dockerfile located in the current directory
|
||||||
|
ports:
|
||||||
|
- "4443:443"
|
||||||
|
- "8080:80"
|
||||||
|
networks: # adding network
|
||||||
|
- lsp-network
|
||||||
|
depends_on:
|
||||||
|
- mongodb
|
||||||
|
|
||||||
|
networks: # allow services to talk to each other while providing isolation from other docker container, running on the same host
|
||||||
|
lsp-network:
|
||||||
|
driver: bridge
|
||||||
|
|
||||||
|
volumes: # enable persistence of database data across container restart
|
||||||
|
mongo-data:
|
||||||
|
driver: local
|
||||||
@@ -14,6 +14,7 @@ const DeviceModel = joi.object({
|
|||||||
relayTemperature: joi.number().optional(),
|
relayTemperature: joi.number().optional(),
|
||||||
relayConfigurationTimestamp: joi.number().optional(),
|
relayConfigurationTimestamp: joi.number().optional(),
|
||||||
vsoft: joi.string().optional(),
|
vsoft: joi.string().optional(),
|
||||||
|
label: joi.string().optional(),
|
||||||
inventory: joi.array().items( joi.object({
|
inventory: joi.array().items( joi.object({
|
||||||
msn: joi.string().required(),
|
msn: joi.string().required(),
|
||||||
configurationTimestamp: joi.number().optional(),
|
configurationTimestamp: joi.number().optional(),
|
||||||
@@ -22,7 +23,7 @@ const DeviceModel = joi.object({
|
|||||||
).optional(),
|
).optional(),
|
||||||
|
|
||||||
configurationTimestamp: joi.number().optional(),
|
configurationTimestamp: joi.number().optional(),
|
||||||
programmationTimestamp: joi.number().optional(),
|
programmingTimestamp: joi.number().optional(),
|
||||||
|
|
||||||
todo: joi.array().items(joi.object({
|
todo: joi.array().items(joi.object({
|
||||||
msn: joi.string().required(),
|
msn: joi.string().required(),
|
||||||
|
|||||||
@@ -1,6 +1,44 @@
|
|||||||
const dbcontroller = require('../db');
|
const dbcontroller = require('../db');
|
||||||
const util = require('../util');
|
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) { 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) { return true; }
|
||||||
|
if (device.programs == undefined && device.slots == undefined) { return true; }
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const postRequestToDo = async function (msn, body) {
|
const postRequestToDo = async function (msn, body) {
|
||||||
try {
|
try {
|
||||||
await dbcontroller.addDevice(body, null)
|
await dbcontroller.addDevice(body, null)
|
||||||
@@ -30,23 +68,23 @@ const getRequestToDo = async function (msn) {
|
|||||||
var todo = { serialNumber : msn }
|
var todo = { serialNumber : msn }
|
||||||
|
|
||||||
///// Programs
|
///// Programs
|
||||||
if (device.programs != undefined) {
|
if (child.programmingTimestamp != undefined && deviceUsesPrograms(child)) {
|
||||||
timestamp = child.programmingTimestamp || -1
|
timestamp = child.programmingTimestamp || -1
|
||||||
if (timestamp < device.programs.timestamp) { todo.programs = 1 }
|
if ((device.programs == undefined) || (timestamp < device.programs.timestamp)) { todo.programs = 1 }
|
||||||
else if (timestamp > device.programs.timestamp) { todo.programs = 2 }
|
else if (timestamp > device.programs.timestamp) { todo.programs = 2 }
|
||||||
}
|
}
|
||||||
|
|
||||||
///// Configuration
|
///// Configuration
|
||||||
if (device.configuration != undefined) {
|
if (child.configurationTimestamp != undefined) {
|
||||||
timestamp = child.configurationTimestamp || child.relayConfigurationTimestamp || -1
|
timestamp = child.configurationTimestamp || child.relayConfigurationTimestamp || -1
|
||||||
if (timestamp < device.configuration.timestamp) { todo.configuration = 1 }
|
if ((device.configuration == undefined) || (timestamp < device.configuration.timestamp)) { todo.configuration = 1 }
|
||||||
else if (timestamp > device.configuration.timestamp) { todo.configuration = 2 }
|
else if (timestamp > device.configuration.timestamp) { todo.configuration = 2 }
|
||||||
}
|
}
|
||||||
|
|
||||||
///// Slots
|
///// Slots
|
||||||
if (device.slots != undefined) {
|
if (child.programmingTimestamp != undefined && deviceUsesSlots(child)) {
|
||||||
timestamp = child.programmingTimestamp || -1
|
timestamp = child.programmingTimestamp || -1
|
||||||
if (timestamp < device.slots.timestamp) { todo.slots = 1 }
|
if ((device.slots == undefined) || (timestamp < device.slots.timestamp)) { todo.slots = 1 }
|
||||||
else if (timestamp > device.slots.timestamp) { todo.slots = 2 }
|
else if (timestamp > device.slots.timestamp) { todo.slots = 2 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user