#!/bin/bash

# WildlifeSystems - Heartbeat

# This script is used to send a heartbeat to the WildlifeSystems server with
# the current status of the device. This is used to determine if the device is
# online or offline and its operational condition.
#
# This script is part of the WildlifeSystems project. For further information
# please refer to https://docs.wildlife.systems, or for more information on
# the project itself, please refer to https://wildlife.systems.

# If status is not set, set it to empty.
STATUS="${1:-}"

# If the token file exists, send the heartbeat to the server.
WSTOKEN_PATH=/etc/ws/ws-token
if [[ -f "$WSTOKEN_PATH" ]]; then
  TOKEN=$(<"$WSTOKEN_PATH")
  SERIAL=$(pi-data serial)
  HNAME=$(pi-data hostname)
  HB=$(sr onboard)
  MACADDR=$(<"/sys/class/net/eth0/address")
  ACTION=$(curl -s --data \
    "token=$TOKEN&node_id=$SERIAL&status=$STATUS&hostname=$HNAME&heartbeat=$HB&mac=$MACADDR" \
    https://devices.wildlife.systems/heartbeat/index.php)
else
  echo "No token."
  exit 2
fi
