Permalink
Browse files

Merge branch 'feature-line-notification' into develop

Signed-off-by: Bofu Chen (bafu) <bofu@dt42.io>
  • Loading branch information...
Bofu Chen (bafu)
Bofu Chen (bafu) committed Oct 8, 2017
2 parents 56595ec + 2daf49d commit 454d483b99ba1b15f14d6e052921299f54079ce9
Showing with 129 additions and 13 deletions.
  1. +11 −10 berrynet-manager
  2. +6 −0 config.js
  3. +1 −1 configure
  4. +3 −1 inference/agent.js
  5. +1 −1 journal.js
  6. +84 −0 line.js
  7. +2 −0 package.json
  8. +15 −0 systemd/line.service
  9. +6 −0 utils/local-launcher.js
@@ -26,13 +26,12 @@ help() {
exit 1
}
if [ $# -lt 1 ]
then
help
if [ $# -lt 1 ]; then
help
fi
case $1 in
start | stop | status)
start | stop | status)
sudo systemctl $1 \
detection_fast_server.service \
agent.service \
@@ -41,9 +40,10 @@ case $1 in
localimg.service \
camera.service \
journal.service \
data_collector.service
;;
log)
data_collector.service \
line.service
;;
log)
sudo journalctl -x --no-pager -u detection_fast_server.service
sudo journalctl -x --no-pager -u agent.service
sudo journalctl -x --no-pager -u broker.service
@@ -52,7 +52,8 @@ case $1 in
sudo journalctl -x --no-pager -u camera.service
sudo journalctl -x --no-pager -u journal.service
sudo journalctl -x --no-pager -u data_collector.service
;;
*)
help
sudo journalctl -x --no-pager -u line.service
;;
*)
help
esac
@@ -41,6 +41,7 @@ config.topicEventCamera = padTopicBase('event/camera');
config.topicEventLocalImage = padTopicBase('event/localImage');
config.topicNotifyEmail = padTopicBase('notify/email');
config.topicNotifySMS = padTopicBase('notify/sms');
config.topicNotifyLINE = padTopicBase('notify/line');
config.topicDashboardLog = padTopicBase('dashboard/log');
config.topicDashboardSnapshot = padTopicBase('dashboard/snapshot');
config.topicDashboardInferenceResult = padTopicBase('dashboard/inferenceResult');
@@ -65,5 +66,10 @@ config.sender_email = config.senderEmail;
config.sender_password = config.senderPassword;
config.receiver_email = config.receiverEmail;
// Authentication and channel information for LINE
config.LINETargetUserID = 'LINE_TARGET_USER_ID';
config.LINEChannelSecret = 'LINE_CHANNEL_SECRET';
config.LINEChannelAccessToken = 'LINE_CHANNEL_ACCESS_TOKEN';
// make config importable
module.exports = config;
@@ -157,7 +157,7 @@ install_gateway() {
sudo mkdir -p $working_dir
sudo cp -a \
broker.js camera.js config.js dashboard data_collector.js \
inference journal.js localimg.js mail.js package.json \
inference journal.js localimg.js mail.js line.js package.json \
$working_dir
sudo cp berrynet-manager /usr/local/bin
# install npm dependencies
@@ -30,6 +30,7 @@ const topicActionLog = config.topicActionLog;
const topicActionInference = config.topicActionInference;
const topicDashboardSnapshot = config.topicDashboardSnapshot;
const topicDashboardInferenceResult = config.topicDashboardInferenceResult;
const topicNotifyLINE = config.topicNotifyLINE;
const inferenceEngine = config.inferenceEngine;
function log(m) {
@@ -102,7 +103,8 @@ client.on('message', (t, m) => {
console.log('Unknown owner ' + inferenceEngine);
}
client.publish(topicDashboardInferenceResult, result.toString().replace(/(\n)+/g, '<br />'));
client.publish(topicNotifyLINE, dashboard_image_path);
client.publish(topicDashboardInferenceResult, result.toString().replace(/(\n)+/g, '<br />'))
})
} else {
console.log('rename event for ' +
@@ -54,7 +54,7 @@ client.on('connect', () => {
client.on('message', (t, m) => {
// secretly save a copy of the image
if (t == topicNotifyEmail) {
if (t === topicNotifyEmail) {
const filename = 'snapshot.jpg';
saveBufferToImage(m, snapshot);
client.publish(topicDashboardSnapshot, filename);
84 line.js
@@ -0,0 +1,84 @@
// Copyright 2017 DT42
//
// This file is part of BerryNet.
//
// BerryNet is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// BerryNet is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with BerryNet. If not, see <http://www.gnu.org/licenses/>.
'use strict';
const mqtt = require('mqtt');
const line = require('@line/bot-sdk');
const imgur = require('imgur');
const config = require('./config');
const broker = config.brokerHost;
const client = mqtt.connect(broker);
const topicActionLog = config.topicActionLog;
const topicNotifyLINE = config.topicNotifyLINE;
const topicDashboardInferenceResult = config.topicDashboardInferenceResult;
const targetUserID = config.LINETargetUserID;
// create LINE SDK config
const LINEConfig = {
channelAccessToken: config.LINEChannelAccessToken,
channelSecret: config.LINEChannelSecret,
};
// create LINE SDK client
const LINEClient = new line.Client(LINEConfig);
function log(m) {
client.publish(topicActionLog, m);
console.log(m);
}
client.on('connect', () => {
client.subscribe(topicNotifyLINE);
client.subscribe(topicDashboardInferenceResult);
log(`client connected to ${broker} successfully.`);
});
client.on('message', (t, m) => {
const size = m.length;
log(`client on topic ${t}, received ${size} bytes.`)
if (t === topicDashboardInferenceResult) {
const result = m.toString();
LINEClient.pushMessage(targetUserID, { type: 'text', text: result });
return;
}
// save image to file and upload it to imgur for display in LINE message
const snapshot_path = m.toString();
imgur.uploadFile(snapshot_path)
.then((json) => {
var imgurLink = json.data.link;
imgurLink = imgurLink.replace('http:\/\/', 'https:\/\/');
log(`An image has been uploaded to imgur. link: ${imgurLink}`);
// Image can only be delivered via 'https://' URL, 'http://' doesn't work
LINEClient.pushMessage(targetUserID, { type: 'image',
originalContentUrl: imgurLink,
previewImageUrl: imgurLink })
.then((v) => {
log(`A message sent to ${targetUserID} successfully.`);
})
.catch((err) => {
log(`An error occurred, ${err}.`);
});
})
.catch((err) => {
log(`An error occurred. ${err}`);
});
});
@@ -8,6 +8,8 @@
"dependencies": {
"ascoltatori": "^3.1.0",
"emailjs": "^1.0.8",
"@line/bot-sdk": "^1.0.0",
"imgur": "^0.2.1",
"imagemagick": "^0.1.3",
"mocha": "^3.2.0",
"mosca": "^2.2.0",
@@ -0,0 +1,15 @@
[Unit]
Description=LINE client agent for notification
After=network.target
[Service]
Type=simple
WorkingDirectory=/usr/local/berrynet
PIDFile=/tmp/line.pid
ExecStart=/usr/bin/node line.js
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
WantedBy=graphical.target
@@ -20,6 +20,7 @@ const broker = exec('node broker.js', execCallback);
const cameraAgent = exec('node camera.js', execCallback);
//const eventNotifier = exec('node mail.js ' + config.sender_email + ' ' + config.sender_email_password + ' ' + config.receiver_email, execCallback);
const eventLogger = exec('node journal.js', execCallback);
const LINEAgent = exec('node line.js', execCallback);
const webServer = exec('cd dashboard && node server.js', execCallback);
//const dlClassifier = exec('cd inference && python classify_server.py --model_dir=model --image_dir=image', execCallback);
const dlDetector = exec('cd inference/darkflow && python detection_server.py', execCallback);
@@ -44,6 +45,10 @@ eventLogger.stdout.on('data', function(data) {
console.log('[eventLogger] ' + data);
});
LINEAgent.stdout.on('data', function(data) {
console.log('[LINEAgent] ' + data);
});
webServer.stdout.on('data', function(data) {
console.log('[webServer] ' + data);
});
@@ -65,6 +70,7 @@ process.on('SIGINT', function() {
broker.kill();
cameraAgent.kill();
eventNotifier.kill();
LINEAgent.kill();
eventLogger.kill();
webServer.kill();
//dlClassifier.kill();

0 comments on commit 454d483

Please sign in to comment.