initial
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
*.map
|
6
config.json
Normal file
6
config.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"brokerUrl": "mqtts://broker.hottis.de:8883",
|
||||
"brokerUser": "orange",
|
||||
"brokerPass": "Rethinking123",
|
||||
"brokerCa": "/home/wn/server-ca.crt"
|
||||
}
|
14
dist/config.js
vendored
Normal file
14
dist/config.js
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
"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: '~/smallSender.conf' }
|
||||
];
|
||||
function readConfig() {
|
||||
let options = cmdargs(OPTION_DEFINITIONS);
|
||||
exports.dict = JSON.parse(fs.readFileSync(options.config, "utf8"));
|
||||
}
|
||||
exports.readConfig = readConfig;
|
||||
//# sourceMappingURL=config.js.map
|
86
dist/log.js
vendored
Normal file
86
dist/log.js
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const chalk = require("chalk");
|
||||
const moment = require("moment");
|
||||
// import * as nodemailer from '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;
|
||||
/*
|
||||
export function sendAlarmMail(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: "Alarm from Dispatcher",
|
||||
text: message
|
||||
};
|
||||
|
||||
transport.sendMail(mail)
|
||||
.then((v : nodemailer.SentMessageInfo) => {
|
||||
info(`Alarm mail sent, ${message}, ${v.response}`)
|
||||
})
|
||||
.catch((reason : any) => {
|
||||
error(`Failure when sending alarm mail: ${message}, ${reason}`)
|
||||
})
|
||||
|
||||
}
|
||||
*/
|
||||
function info(message) {
|
||||
if (level < Level.NoDebugNoInfo) {
|
||||
console.log(`${timestamp()} ${chalk.bold.cyan('[ II ]')} ${message}`);
|
||||
}
|
||||
}
|
||||
exports.info = info;
|
||||
function warn(message) {
|
||||
if (level < Level.NoDebugNoInfoNoWarning) {
|
||||
console.log(`${timestamp()} ${chalk.bold.yellow('[ WW ]')} ${message}`);
|
||||
}
|
||||
}
|
||||
exports.warn = warn;
|
||||
function error(message) {
|
||||
console.log(`${timestamp()} ${chalk.bold.red('[ EE ]')} ${message}`);
|
||||
}
|
||||
exports.error = error;
|
||||
function success(message) {
|
||||
console.log(`${timestamp()} ${chalk.bold.green('[ OK ]')} ${message}`);
|
||||
}
|
||||
exports.success = success;
|
||||
function debug(message) {
|
||||
if (level < Level.NoDebug) {
|
||||
console.log(`${timestamp()} ${chalk.bold.magenta('[ DB ]')} ${message}`);
|
||||
}
|
||||
}
|
||||
exports.debug = debug;
|
||||
//# sourceMappingURL=log.js.map
|
27
dist/main.js
vendored
Normal file
27
dist/main.js
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const log = require("./log");
|
||||
const config = require("./config");
|
||||
const Mqtt = require("mqtt");
|
||||
const fs = require("fs");
|
||||
log.info("SmallSender starting");
|
||||
config.readConfig();
|
||||
let mqttOptions = {
|
||||
username: config.dict.brokerUser,
|
||||
password: config.dict.brokerPass,
|
||||
ca: fs.readFileSync(config.dict.brokerCa, 'ascii'),
|
||||
rejectUnauthorized: true
|
||||
};
|
||||
let mqttClient = Mqtt.connect(config.dict.brokerUrl, mqttOptions);
|
||||
mqttClient.on('error', log.error);
|
||||
mqttClient.on('connect', () => {
|
||||
log.info("connected to mqtt broker");
|
||||
});
|
||||
let cnt = 0;
|
||||
let timer = setInterval(() => {
|
||||
log.info(`Publishing a message ${cnt}`);
|
||||
mqttClient.publish("IoT/DeviceInfo", `OrangePi ${cnt}`);
|
||||
cnt += 1;
|
||||
}, 10 * 1000);
|
||||
log.info("SmallSender running");
|
||||
//# sourceMappingURL=main.js.map
|
27
package.json
Normal file
27
package.json
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "smallsender",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "dist/main.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"build": "./node_modules/typescript/bin/tsc",
|
||||
"debug": "npm run build && node dist/main.js --loglevel debug",
|
||||
"start": "node dist/main.js"
|
||||
},
|
||||
"author": "Wolfgang Hottgenroth <woho@hottis.de>",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/chalk": "^0.4.31",
|
||||
"@types/command-line-args": "^4.0.1",
|
||||
"@types/mqtt": "0.0.34",
|
||||
"@types/node": "^8.0.14",
|
||||
"typescript": "^2.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"chalk": "^2.0.1",
|
||||
"command-line-args": "^4.0.7",
|
||||
"moment": "^2.18.1",
|
||||
"mqtt": "^2.9.2"
|
||||
}
|
||||
}
|
18
src/config.ts
Normal file
18
src/config.ts
Normal file
@ -0,0 +1,18 @@
|
||||
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: '~/smallSender.conf' }
|
||||
];
|
||||
|
||||
|
||||
export let dict
|
||||
|
||||
export function readConfig() {
|
||||
let options = cmdargs(OPTION_DEFINITIONS)
|
||||
dict = JSON.parse(fs.readFileSync(options.config, "utf8"))
|
||||
}
|
||||
|
82
src/log.ts
Normal file
82
src/log.ts
Normal file
@ -0,0 +1,82 @@
|
||||
import * as chalk from 'chalk'
|
||||
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(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: "Alarm from Dispatcher",
|
||||
text: message
|
||||
};
|
||||
|
||||
transport.sendMail(mail)
|
||||
.then((v : nodemailer.SentMessageInfo) => {
|
||||
info(`Alarm mail sent, ${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()} ${chalk.bold.cyan('[ II ]')} ${message}`)
|
||||
}
|
||||
}
|
||||
|
||||
export function warn(message: string): void {
|
||||
if (level < Level.NoDebugNoInfoNoWarning) {
|
||||
console.log(`${timestamp()} ${chalk.bold.yellow('[ WW ]')} ${message}`)
|
||||
}
|
||||
}
|
||||
|
||||
export function error(message: string): void {
|
||||
console.log(`${timestamp()} ${chalk.bold.red('[ EE ]')} ${message}`)
|
||||
}
|
||||
|
||||
export function success(message: string): void {
|
||||
console.log(`${timestamp()} ${chalk.bold.green('[ OK ]')} ${message}`)
|
||||
}
|
||||
|
||||
export function debug(message: string): void {
|
||||
if (level < Level.NoDebug) {
|
||||
console.log(`${timestamp()} ${chalk.bold.magenta('[ DB ]')} ${message}`)
|
||||
}
|
||||
}
|
43
src/main.ts
Normal file
43
src/main.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import * as log from './log'
|
||||
import * as config from './config'
|
||||
import * as Mqtt from 'mqtt'
|
||||
import * as fs from 'fs'
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
log.info("SmallSender starting")
|
||||
|
||||
config.readConfig()
|
||||
|
||||
|
||||
|
||||
let mqttOptions : Mqtt.IClientOptions = {
|
||||
username : config.dict.brokerUser,
|
||||
password : config.dict.brokerPass,
|
||||
ca : fs.readFileSync(config.dict.brokerCa, 'ascii'),
|
||||
rejectUnauthorized : true
|
||||
}
|
||||
|
||||
let mqttClient : Mqtt.Client = Mqtt.connect(config.dict.brokerUrl, mqttOptions)
|
||||
mqttClient.on('error', log.error)
|
||||
mqttClient.on('connect', (): void => {
|
||||
log.info("connected to mqtt broker")
|
||||
})
|
||||
|
||||
let cnt : number = 0
|
||||
let timer : NodeJS.Timer = setInterval(() => {
|
||||
log.info(`Publishing a message ${cnt}`)
|
||||
mqttClient.publish("IoT/DeviceInfo", `OrangePi ${cnt}`)
|
||||
cnt += 1
|
||||
}, 10 * 1000)
|
||||
|
||||
|
||||
log.info("SmallSender running")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
25
tsconfig.json
Normal file
25
tsconfig.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"lib": ["es6"],
|
||||
"strictNullChecks": true,
|
||||
//"noImplicitAny": true,
|
||||
"noEmitOnError": true,
|
||||
"outDir": "dist",
|
||||
"typeRoots": [
|
||||
"node_modules/@types"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.*"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist",
|
||||
"proto",
|
||||
"kernel"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user