Skip to content

Instantly share code, notes, and snippets.

@mwvent
Last active March 13, 2020 02:33
Show Gist options
  • Save mwvent/6fe8ceeb6c26ffaa62af764adf8db0ac to your computer and use it in GitHub Desktop.
Save mwvent/6fe8ceeb6c26ffaa62af764adf8db0ac to your computer and use it in GitHub Desktop.
Receive motion events from Xiongmai/Netsurv web camera and pass to Zoneminders trigger port eliminating need for image analysis on ZM Server
#!/bin/bash
# Function to translate camera serial into zoneminder monitor id
function serialToID() {
MONITORID=0
if [ "$1" == '"383c49531157b1b1"' ]; then
MONITORID=9
fi
echo $MONITORID
}
# Netcat persistent listen
ncat -lk 15002 | \
while read -r LN; do
# Lines of json often arrive with/without braces add a bunch
LN="{{{$LN}}}}}"
# Then remove all non printable chars and trim muliple braces to one
LN="$(echo $LN | tr -cd '[:print:]' | tr -s "{" | tr -s "}")"
STATUS=$(echo "$LN" | jq ".Status")
EVENT=$(echo "$LN" | jq ".Event")
SERIAL=$(echo "$LN" | jq ".SerialID")
MONITORID="$(serialToID "$SERIAL")"
if [ "$EVENT" == '"MotionDetect"' ]; then
if [ "$STATUS" == '"Start"' ]; then
SENDT="$MONITORID|on+180|$MONITORID|Motion|Motion"
else
SENDT="$MONITORID|off|$MONITORID|Motion|Motion"
fi
echo "$(date) $STATUS $EVENT - Sending $SENDT to ZM"
echo $SENDT | telnet 127.0.0.1 6802 &>/dev/null &
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment