UNPKG

alb3rt-home-security

Version:
64 lines (55 loc) 1.83 kB
'use strict'; const FILE_ID = '[alb3rt-home-security/motion]', state = require('../state'), core = require('alb3rt-core'), sms = require('alb3rt-sms'), email = core.email, moment = require('moment'), CONFIG = core.config; module.exports = new class HomeSecurityMotion { constructor() { sms.init(); } sendSms(data) { CONFIG.SECURITY_MOBILE_NUMBERS.forEach(number => { if (number) { console.log(FILE_ID, 'Sending security SMS alert to', number); sms.send({ to: number, body: `Security alert at ${data.name} ${moment().format()}!` }); } }); } sendEmail(data) { CONFIG.SECURITY_EMAILS_TO.forEach(address => { if (address) { console.log(FILE_ID, 'Sending security email alert to', address); email.send({ address, subject: '[Alb3rt Alert]', text: `Security alert at ${data.name} ${moment().format()}!` }); } }); } handle(data, response) { if (state.enabled) { console.log(FILE_ID, 'Security enabled. Handling motion at ' + data.name); if (state.armed) { console.log(FILE_ID, 'Alarm armed, triggering...'); core.broadcast.trigger('security', { type: 'alert', action: 'play' }); this.sendSms(data); this.sendEmail(data); } } else { console.log(FILE_ID, 'Security disabled. Ignoring motion at ' + data.name); } core.api.responder.send(response, { status: 200 }); } };