summaryrefslogtreecommitdiff
path: root/recipes-connectivity/lora/lora-packet-forwarder/mtcap/lora-packet-forwarder.init
blob: 9ffaf6dd4f972d53ee4bdfed8c04df99cd93d6b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash

NAME="lora-packet-forwarder"
ENABLED="yes"

START_STOP_DAEMON="/usr/sbin/start-stop-daemon"

[ -f /etc/default/$NAME ] && source /etc/default/$NAME

run_dir=/var/run/lora
pkt_fwd=/opt/lora/lora_pkt_fwd
pkt_fwd_pidfile=$run_dir/lora-pkt-fwd-1.pid
log_file=/var/log/lora-pkt-fwd.log
cfg_file=/opt/lora/global_conf.json

do_start() {
    echo "Starting $NAME: "

    eui=$(mts-io-sysfs show lora/eui)
    # Remove colons
    clean_eui="${eui//:/}"
    echo "Setting gateway_id in $cfg_file to $clean_eui"
    sed -i "s/\"gateway_ID\": \".*\"/\"gateway_ID\": \"$clean_eui\"/" $cfg_file

    mkdir -p $run_dir

    # start packet forwarder
    $START_STOP_DAEMON --start --background --make-pidfile \
        --pidfile $pkt_fwd_pidfile --startas /bin/bash --chdir "/opt/lora/" \
        -- -c "exec $pkt_fwd > $log_file 2>&1"

    renice -n -20 -p $(pgrep $(basename $pkt_fwd))

    echo "OK"
}

do_stop() {
    echo -n "Stopping $NAME: "
    $START_STOP_DAEMON --stop --quiet --oknodo --pidfile $pkt_fwd_pidfile --retry 5
    rm -f $pkt_fwd_pidfile
    echo "OK"
}

if [ "$ENABLED" != "yes" ]; then
    echo "$NAME: disabled in /etc/default/$NAME"
    exit
fi

case "$1" in
    "start")
        do_start
        ;;
    "stop")
        do_stop
        ;;
    "restart")
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        do_stop
        do_start
        ;;
    *)
        ## If no parameters are given, print which are avaiable.
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac