Skip to content

Commit

Permalink
watching sensors and notifying through telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
wassfila committed Aug 9, 2020
1 parent 745c95e commit 14fbd55
Show file tree
Hide file tree
Showing 11 changed files with 1,507 additions and 12 deletions.
16 changes: 4 additions & 12 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},

{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${file}"
}
"program": "${workspaceRoot}/js/watch_bots/run.js"
}

]
}
5 changes: 5 additions & 0 deletions js/watch_bots/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
secrets.json
node_modules
*.log
repo
watch_bot.log
69 changes: 69 additions & 0 deletions js/watch_bots/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"mqtt":{
"host":"10.0.0.42",
"port":1883,
"keepalive":60,
"client_id":"watch_bots",
"lists":{
"eurotronics":[
"lzig/living heat",
"lzig/kitchen heat",
"lzig/bedroom heat",
"lzig/office heat",
"lzig/bathroom heat"
],
"nrf":[
"nrf/livingroom window tag",
"nrf/hallway tag",
"nrf/balcony tag",
"nrf/bathroom tag",
"nrf/livingroom tag",
"nrf/office tag",
"nrf/bedroom tag",
"nrf/kitchen tag"
],
"aqara":[
"mzig/bed weather",
"lzig/bedroom window",
"lzig/kitchen window",
"+/office weather",
"lzig/office switch",
"lzig/fridge weather",
"mzig/balcony window left",
"mzig/balcony window right",
"mzig/balcony door",
"lzig/office window left",
"lzig/office window right"
]
},
"publish" :true,
"subscribe" :true
},
"watch":{
"nrf":{
"voltage":{
"minimum":2.1
}
},
"aqara":{
"voltage":{
"minimum":1800
}
},
"eurotronics":{
"battery":{
"minimum":15
}
}
},
"alive_minutes":{
"nrf":15,
"aqara":30,
"eurotronics":60
},
"log":{
"logfile_test":"/home/pi/share/watch_bots(date).log",
"logfile":"watch_bots.log",
"level":"info"
}
}
36 changes: 36 additions & 0 deletions js/watch_bots/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs');
const {transports,createLogger, format} = require('winston');

const config = JSON.parse(fs.readFileSync(__dirname+'\\config.json'))

let date = new Date().toISOString()
let date_day = date.split('T')[0]
let filename = config.log.logfile.replace("(date)",date_day)

var logger = createLogger({
format: format.combine(
format.timestamp(),
format.printf(log => {
return `${log.timestamp} |${log.level}\t| ${log.message}`;
})
),
transports: [
new transports.Console({
level:config.log.level,
json:false
}),
new transports.File({
filename: filename ,
json:false
})
],
exceptionHandlers: [
new transports.File({
filename: 'log_exceptions.log' ,
level:config.log.level,
json:false
})
]
});

module.exports = {logger:logger};
41 changes: 41 additions & 0 deletions js/watch_bots/mqtt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const fs = require('fs')
const mqtt = require('mqtt')
const {logger} = require('./logger.js')
const events = require('events')
const Emitter = new events.EventEmitter()

const config = JSON.parse(fs.readFileSync(__dirname+'\\config.json'))
var client;

function onConnect() {
for(let [key,list] of Object.entries(config.mqtt.lists)){
list.forEach((topic)=>{
client.subscribe(topic)
})
}

logger.info("onConnect");
}

function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
logger.warn("onConnectionLost:"+responseObject.errorMessage);
}
}

function onMessageArrived(topic,message) {
logger.debug(`mqtt> ${topic} : ${message}`);
Emitter.emit('mqtt',{topic:topic,msg:JSON.parse(message)});
}

function start(){
logger.info(`creating client connection to ${config.mqtt.host}:${config.mqtt.port}`);
client = mqtt.connect(`mqtt://${config.mqtt.host}:${config.mqtt.port}`);//unused config.mqtt.clien_id
client.on('connect',onConnect);
//client.onConnectionLost = onConnectionLost;
client.on('message',onMessageArrived);

}

//----------------------------------------------------------------------------------
module.exports = {start,Emitter}
Loading

0 comments on commit 14fbd55

Please sign in to comment.