Skip to content

Commit 04cdd4b

Browse files
committed
Merge pull request arduino#26 from ToSa27/development
Installation Instructions and RPi init script and fixes for use of serial gateway
2 parents f34e592 + 961828a commit 04cdd4b

File tree

5 files changed

+505
-9
lines changed

5 files changed

+505
-9
lines changed

NodeJsController/MySensors.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
# pidfile must be first argument
3+
PIDFILE=$1
4+
mkdir -p /home/pi/logs
5+
cd /home/pi/Arduino/NodeJsController
6+
/opt/node/bin/node NodeJsController.js >>/home/pi/logs/NodeJsController.log 2>&1 </dev/null &
7+
CHILD="$!"
8+
echo $CHILD > $PIDFILE
+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#! /bin/sh
2+
### BEGIN INIT INFO
3+
# Provides: mysensors
4+
# Required-Start: $remote_fs $syslog $mongodb
5+
# Required-Stop: $remote_fs $syslog
6+
# Default-Start: 2 3 4 5
7+
# Default-Stop: 0 1 6
8+
# Short-Description: MySensors NodeJs Controller
9+
# Description: MySensors NodeJs Controller
10+
### END INIT INFO
11+
12+
# Author: Foo Bar <[email protected]>
13+
#
14+
# Please remove the "Author" lines above and replace them
15+
# with your own name if you copy and modify this script.
16+
17+
# Do NOT "set -e"
18+
19+
# PATH should only include /usr/* if it runs after the mountnfs.sh script
20+
PATH=/sbin:/usr/sbin:/bin:/usr/bin
21+
DESC="MySensors NodeJs Controller"
22+
NAME="MySensors.sh"
23+
PIDFILE="/usr/local/var/run/$NAME.pid"
24+
DAEMON="/home/pi/Arduino/NodeJsController/$NAME"
25+
DAEMON_ARGS="$PIDFILE"
26+
DAEMON_USER="pi"
27+
SCRIPTNAME=/etc/init.d/$NAME
28+
29+
# Exit if the package is not installed
30+
[ -x "$DAEMON" ] || exit 0
31+
32+
# Read configuration variable file if it is present
33+
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
34+
35+
# Load the VERBOSE setting and other rcS variables
36+
. /lib/init/vars.sh
37+
38+
# Define LSB log_* functions.
39+
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
40+
# and status_of_proc is working.
41+
. /lib/lsb/init-functions
42+
43+
#
44+
# Function that starts the daemon/service
45+
#
46+
do_start()
47+
{
48+
# Return
49+
# 0 if daemon has been started
50+
# 1 if daemon was already running
51+
# 2 if daemon could not be started
52+
start-stop-daemon --start --quiet --chuid $DAEMON_USER --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
53+
|| return 1
54+
start-stop-daemon --start --quiet --chuid $DAEMON_USER --pidfile $PIDFILE --exec $DAEMON -- \
55+
$DAEMON_ARGS \
56+
|| return 2
57+
# Add code here, if necessary, that waits for the process to be ready
58+
# to handle requests from services started subsequently which depend
59+
# on this one. As a last resort, sleep for some time.
60+
}
61+
62+
#
63+
# Function that stops the daemon/service
64+
#
65+
do_stop()
66+
{
67+
# Return
68+
# 0 if daemon has been stopped
69+
# 1 if daemon was already stopped
70+
# 2 if daemon could not be stopped
71+
# other if a failure occurred
72+
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
73+
RETVAL="$?"
74+
[ "$RETVAL" = 2 ] && return 2
75+
# Wait for children to finish too if this is a daemon that forks
76+
# and if the daemon is only ever run from this initscript.
77+
# If the above conditions are not satisfied then add some other code
78+
# that waits for the process to drop all resources that could be
79+
# needed by services started subsequently. A last resort is to
80+
# sleep for some time.
81+
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
82+
[ "$?" = 2 ] && return 2
83+
# Many daemons don't delete their pidfiles when they exit.
84+
rm -f $PIDFILE
85+
return "$RETVAL"
86+
}
87+
88+
#
89+
# Function that sends a SIGHUP to the daemon/service
90+
#
91+
do_reload() {
92+
#
93+
# If the daemon can reload its configuration without
94+
# restarting (for example, when it is sent a SIGHUP),
95+
# then implement that here.
96+
#
97+
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
98+
return 0
99+
}
100+
101+
case "$1" in
102+
start)
103+
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
104+
do_start
105+
case "$?" in
106+
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
107+
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
108+
esac
109+
;;
110+
stop)
111+
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
112+
do_stop
113+
case "$?" in
114+
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
115+
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
116+
esac
117+
;;
118+
status)
119+
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
120+
;;
121+
#reload|force-reload)
122+
#
123+
# If do_reload() is not implemented then leave this commented out
124+
# and leave 'force-reload' as an alias for 'restart'.
125+
#
126+
#log_daemon_msg "Reloading $DESC" "$NAME"
127+
#do_reload
128+
#log_end_msg $?
129+
#;;
130+
restart|force-reload)
131+
#
132+
# If the "reload" option is implemented then remove the
133+
# 'force-reload' alias
134+
#
135+
log_daemon_msg "Restarting $DESC" "$NAME"
136+
do_stop
137+
case "$?" in
138+
0|1)
139+
do_start
140+
case "$?" in
141+
0) log_end_msg 0 ;;
142+
1) log_end_msg 1 ;; # Old process is still running
143+
*) log_end_msg 1 ;; # Failed to start
144+
esac
145+
;;
146+
*)
147+
# Failed to stop
148+
log_end_msg 1
149+
;;
150+
esac
151+
;;
152+
*)
153+
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
154+
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
155+
exit 3
156+
;;
157+
esac
158+
159+
:
160+

NodeJsController/NodeJsController.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const gwPort = 9999;
44

55
//const gwType = 'Serial';
66
//const gwPort = 'COM4';
7+
//const gwPort = '/dev/ttyUSB0';
78
//const gwBaud = 115200;
89

910
const dbAddress = '127.0.0.1';
@@ -116,6 +117,7 @@ const P_ULONG32 = 5;
116117
const P_CUSTOM = 6;
117118

118119
var fs = require('fs');
120+
var appendedString="";
119121

120122
function crcUpdate(old, value) {
121123
var c = old ^ value;
@@ -516,6 +518,22 @@ function sendRebootMessage(destination, gw) {
516518
gw.write(td);
517519
}
518520

521+
522+
function appendData(str, db, gw) {
523+
pos=0;
524+
while (str.charAt(pos) != '\n' && pos < str.length) {
525+
appendedString=appendedString+str.charAt(pos);
526+
pos++;
527+
}
528+
if (str.charAt(pos) == '\n') {
529+
rfReceived(appendedString.trim(), db, gw);
530+
appendedString="";
531+
}
532+
if (pos < str.length) {
533+
appendData(str.substr(pos+1,str.length-pos-1), db, gw);
534+
}
535+
}
536+
519537
function rfReceived(data, db, gw) {
520538
if ((data != null) && (data != "")) {
521539
console.log('<- ' + data);
@@ -526,7 +544,10 @@ function rfReceived(data, db, gw) {
526544
var command = +datas[2];
527545
var ack = +datas[3];
528546
var type = +datas[4];
529-
var rawpayload = datas[5].trim();
547+
var rawpayload="";
548+
if (datas[5]) {
549+
rawpayload = datas[5].trim();
550+
}
530551
var payload;
531552
if (command == C_STREAM) {
532553
payload = [];
@@ -635,9 +656,7 @@ dbc.connect('mongodb://' + dbAddress + ':' + dbPort + '/' + dbName, function(err
635656
gw.on('connect', function() {
636657
console.log('connected to ethernet gateway at ' + gwAddress + ":" + gwPort);
637658
}).on('data', function(rd) {
638-
var rds = rd.toString().split('\n');
639-
for (var i = 0; i < rds.length; i++)
640-
rfReceived(rds[i].trim(), db, gw);
659+
appendData(rd.toString(), db, gw);
641660
}).on('end', function() {
642661
console.log('disconnected from gateway');
643662
}).on('error', function() {
@@ -646,14 +665,13 @@ dbc.connect('mongodb://' + dbAddress + ':' + dbPort + '/' + dbName, function(err
646665
gw.setEncoding('ascii');
647666
});
648667
} else if (gwType == 'Serial') {
649-
gw = require('serialport').SerialPort(gwPort, { baudrate: gwBaud }, false);
668+
var SerialPort = require('serialport').SerialPort;
669+
gw = new SerialPort(gwPort, { baudrate: gwBaud });
650670
gw.open();
651671
gw.on('open', function() {
652672
console.log('connected to serial gateway at ' + gwPort);
653673
}).on('data', function(rd) {
654-
var rds = rd.toString().split('\n');
655-
for (var i = 0; i < rds.length; i++)
656-
rfReceived(rds[i].trim(), db, gw);
674+
appendData(rd.toString(), db, gw);
657675
}).on('end', function() {
658676
console.log('disconnected from gateway');
659677
}).on('error', function() {

0 commit comments

Comments
 (0)