This commit is contained in:
Wolfgang Hottgenroth 2018-02-26 12:55:13 +01:00
commit 30fbbd2589
11 changed files with 1378 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
node_modules
*.map
mylogfile.log
geofences.log

15
dist/config.js vendored Normal file
View File

@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const cmdargs = require("command-line-args");
const OPTION_DEFINITIONS = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'config', alias: 'c', type: String, defaultValue: '~/geofences.conf' }
];
function readConfig() {
let options = cmdargs(OPTION_DEFINITIONS);
exports.dict = JSON.parse(fs.readFileSync(options.config, "utf8"));
}
exports.readConfig = readConfig;
readConfig();
//# sourceMappingURL=config.js.map

82
dist/log.js vendored Normal file
View File

@ -0,0 +1,82 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const moment = require("moment");
const config = require("./config");
const nodemailer = require("nodemailer");
var Level;
(function (Level) {
Level[Level["All"] = 0] = "All";
Level[Level["NoDebug"] = 1] = "NoDebug";
Level[Level["NoDebugNoInfo"] = 2] = "NoDebugNoInfo";
Level[Level["NoDebugNoInfoNoWarning"] = 3] = "NoDebugNoInfoNoWarning";
})(Level || (Level = {}));
var level = Level.NoDebug;
function timestamp() {
return moment().format('HH:mm:ss.SSS');
}
function setLevel(value) {
switch (value) {
case 'info':
level = Level.NoDebug;
break;
case 'warn':
level = Level.NoDebugNoInfo;
break;
case 'error':
level = Level.NoDebugNoInfoNoWarning;
break;
default: level = Level.All;
}
}
exports.setLevel = setLevel;
function sendAlarmMail(subject, message) {
let transport = nodemailer.createTransport({
host: config.dict.smtpHost,
port: config.dict.smtpPort,
secure: false,
tls: {
rejectUnauthorized: false
}
});
let mail = {
from: config.dict.smtpSender,
to: config.dict.smtpReceiver,
subject: subject,
text: message
};
transport.sendMail(mail)
.then((v) => {
info(`Mail sent, ${subject}, ${message}, ${v.response}`);
})
.catch((reason) => {
error(`Failure when sending alarm mail: ${message}, ${reason}`);
});
}
exports.sendAlarmMail = sendAlarmMail;
function info(message) {
if (level < Level.NoDebugNoInfo) {
console.log(`${timestamp()} [ II ] ${message}`);
}
}
exports.info = info;
function warn(message) {
if (level < Level.NoDebugNoInfoNoWarning) {
console.log(`${timestamp()} [ WW ] ${message}`);
}
}
exports.warn = warn;
function error(message) {
console.log(`${timestamp()} [ EE ] ${message}`);
}
exports.error = error;
function success(message) {
console.log(`${timestamp()} [ OK ] ${message}`);
}
exports.success = success;
function debug(message) {
if (level < Level.NoDebug) {
console.log(`${timestamp()} [ DB ] ${message}`);
}
}
exports.debug = debug;
//# sourceMappingURL=log.js.map

24
dist/main.js vendored Normal file
View File

@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express = require("express");
const logger = require("./log");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/entry', (req, res) => {
const reqData = req.body;
logger.info(`${reqData.device} arrives`);
logger.info(JSON.stringify(reqData));
res.send('OK');
});
app.post('/exit', (req, res) => {
const reqData = req.body;
logger.info(`${reqData.device} leaves`);
logger.info(JSON.stringify(reqData));
res.send('OK');
});
const server = app.listen(8000, '', () => {
logger.info('geofences server listening');
});
//# sourceMappingURL=main.js.map

10
geofences.conf Normal file
View File

@ -0,0 +1,10 @@
{
"brokerUrl": "mqtt://127.0.0.1:1883",
"brokerUser": "",
"brokerPass": "",
"brokerCa": "",
"smtpHost": "localhost",
"smtpPort": 25,
"smtpSender": "geofences@hottis.de",
"smtpReceiver": "woho@hottis.de"
}

1056
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

34
package.json Normal file
View File

@ -0,0 +1,34 @@
{
"name": "geofences",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc -p ./",
"start": "node dist/main.js"
},
"author": "Wolfgang Hottgenroth <woho@hottis.de>",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"chalk": "^2.3.0",
"command-line-args": "^4.0.7",
"express": "^4.16.2",
"moment": "^2.20.1",
"mqtt": "^2.15.0",
"nodemailer": "^4.4.1",
"simple-node-logger": "^0.93.33"
},
"devDependencies": {
"@types/body-parser": "^1.16.8",
"@types/chalk": "^2.2.0",
"@types/command-line-args": "^4.0.2",
"@types/express": "^4.11.1",
"@types/moment": "^2.13.0",
"@types/mqtt": "^2.5.0",
"@types/node": "^8.5.8",
"@types/nodemailer": "^4.3.1",
"typescript": "^2.6.2"
}
}

19
src/config.ts Normal file
View File

@ -0,0 +1,19 @@
import * as fs from 'fs'
import * as cmdargs from 'command-line-args'
const OPTION_DEFINITIONS = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'config', alias: 'c', type: String, defaultValue: '~/geofences.conf' }
];
export let dict : any
export function readConfig() {
let options = cmdargs(OPTION_DEFINITIONS)
dict = JSON.parse(fs.readFileSync(options.config, "utf8"))
}
readConfig()

79
src/log.ts Normal file
View File

@ -0,0 +1,79 @@
import * as moment from 'moment'
import * as config from './config'
import * as nodemailer from 'nodemailer'
enum Level {
All,
NoDebug,
NoDebugNoInfo,
NoDebugNoInfoNoWarning
}
var level = Level.NoDebug
function timestamp(): string {
return moment().format('HH:mm:ss.SSS')
}
export function setLevel(value: string): void {
switch (value) {
case 'info': level = Level.NoDebug; break
case 'warn': level = Level.NoDebugNoInfo; break
case 'error': level = Level.NoDebugNoInfoNoWarning; break
default: level = Level.All
}
}
export function sendAlarmMail(subject : string, message : string): void {
let transport = nodemailer.createTransport({
host: config.dict.smtpHost,
port: config.dict.smtpPort,
secure: false,
tls: {
rejectUnauthorized: false
}
});
let mail : nodemailer.SendMailOptions = {
from: config.dict.smtpSender,
to: config.dict.smtpReceiver,
subject: subject,
text: message
};
transport.sendMail(mail)
.then((v : nodemailer.SentMessageInfo) => {
info(`Mail sent, ${subject}, ${message}, ${v.response}`)
})
.catch((reason : any) => {
error(`Failure when sending alarm mail: ${message}, ${reason}`)
})
}
export function info(message: string): void {
if (level < Level.NoDebugNoInfo) {
console.log(`${timestamp()} [ II ] ${message}`)
}
}
export function warn(message: string): void {
if (level < Level.NoDebugNoInfoNoWarning) {
console.log(`${timestamp()} [ WW ] ${message}`)
}
}
export function error(message: string): void {
console.log(`${timestamp()} [ EE ] ${message}`)
}
export function success(message: string): void {
console.log(`${timestamp()} [ OK ] ${message}`)
}
export function debug(message: string): void {
if (level < Level.NoDebug) {
console.log(`${timestamp()} [ DB ] ${message}`)
}
}

30
src/main.ts Normal file
View File

@ -0,0 +1,30 @@
import * as http from 'http'
import * as express from 'express'
import * as logger from './log'
import * as bodyParser from 'body-parser'
const app = express()
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/entry', (req, res) => {
const reqData = req.body
logger.info(`${reqData.device} arrives`)
logger.info(JSON.stringify(reqData))
res.send('OK')
})
app.post('/exit', (req, res) => {
const reqData = req.body
logger.info(`${reqData.device} leaves`)
logger.info(JSON.stringify(reqData))
res.send('OK')
})
const server = app.listen(8000, '', () => {
logger.info('geofences server listening')
})

25
tsconfig.json Normal file
View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"lib": ["es2017"],
"strictNullChecks": true,
"noImplicitAny": true,
"noEmitOnError": true,
"outDir": "dist",
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"src/**/*.*"
],
"exclude": [
"node_modules",
"dist",
"proto",
"kernel"
]
}