-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
watching sensors and notifying through telegram
- Loading branch information
Showing
11 changed files
with
1,507 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
secrets.json | ||
node_modules | ||
*.log | ||
repo | ||
watch_bot.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
Oops, something went wrong.