UNPKG

alb3rt-home-security

Version:
151 lines (123 loc) 5.12 kB
'use strict'; const FILE_ID = '[alb3rt-home-security/broadcast]', fs = require('fs-extra'), moment = require('moment'), core = require('alb3rt-core'), //googleDrive = require(corePath + 'google/drive'), email = core.email, CONFIG = core.config; module.exports = new class HomeSecurityState { constructor() { this.incidents = []; } getIncidentId() { return moment().format('YYYYMMDDHHmm'); } getIncidentTemplate(incident) { return `Incident id ${incident.id}\nSource: ${incident.source}\nType: ${incident.type}`; } sendEmail(subject, text) { console.info(FILE_ID + ' Sending email.\nEmail text:', text); CONFIG.SECURITY_EMAILS_TO.forEach(address => { if (address) { email.send({ address, from: CONFIG.SECURITY_EMAIL_FROM, subject, text }); } }); } uploadPictures(picturesPath) { const filesToUpload = []; fs.readdir(picturesPath, (err, files) => { if (!err) { if (files.length) { files.forEach(file => { filesToUpload.push(picturesPath + '/' + file); }); } if (filesToUpload.length) { // const filePath = filesToUpload[0]; // googleDrive.uploadFile(filePath, (response) => { // console.log(FILE_ID, 'File upload success, link:', 'https://drive.google.com/file/d/' + response.id + '/edit?usp=sharing'); // fs.remove(filePath, (error) => { // if (error) { // winston.logger.error(FILE_ID + ' Problem with removing file' + filePath); // } else { // this.uploadPictures(picturesPath); // } // }); // }); } else { fs.remove(picturesPath, error => { if (error) { console.error(FILE_ID + ' Problem with removing folder' + picturesPath); } }); } } else { console.error(FILE_ID + ' Problem with reading directory' + picturesPath + '. Error: ' + err); } }); } sendSummary() { if (this.incidents.length) { let text = ''; this.incidents.forEach(incident => { const picturesPath = process.env.ROOT_PATH + '/pictures/' + incident.id; this.uploadPictures(picturesPath); text += '\n' + this.getIncidentTemplate(incident); }); this.sendEmail({ subject: '[Albert Security] Incidents Summary', text }); } else { console.info(FILE_ID + ' No incidents to report.'); } } createIncidentFolders(id) { const picturesPath = process.env.ROOT_PATH + '/pictures/' + id; fs.mkdirs(picturesPath, error => { if (error) { console.error(FILE_ID + ' Problem while creating folder at' + picturesPath); } console.log(FILE_ID, ' Folder at', picturesPath, 'successfully created.'); }); } handle({name, url, port, type}) { if (CONFIG.ROLE === 'master' && CONFIG.MASTER_SERVER_ADDRESS === undefined) { const incidentId = this.getIncidentId(); let incidentHandled = false; if (this.incidents.length) { this.incidents.forEach(incident => { if (incident.id === incidentId) { incidentHandled = true; } }); } if (!incidentHandled) { console.info(FILE_ID + ' Handling security incident:', incidentId); const incident = { id: incidentId, source: 'http://' + url + ':' + port, name, type }, summary = this.getIncidentTemplate(incident); this.incidents.push(incident); this.createIncidentFolders(incidentId); this.sendEmail('[Albert Security] Alarm!', summary); //TEMPORARY // const picturesPath = process.env.ROOT_PATH + '/pictures/' + incidentId; // fs.copy(process.env.ROOT_PATH + '/test/foto.png', picturesPath + '/foto.png', error => { // if (error) { // console.error('[features/home-security/incidents] Problem while copying test file foto.png', error); // } // console.log('[features/home-security/incidents] Test file foto.png successfully copied.'); // }); } } } };