Motion Event HTTP API SDK

This Forum its to discuss the new Smart Home Devices.
GaryOkie
Posts: 418
Joined: Mon Apr 27, 2020 7:23 pm

Re: Motion Event HTTP API SDK

Post by GaryOkie »

Yeah, I think you're right that the videomotion event is redundant along with alarmLocal. Weird that there are 2 AlarmLocal start events.

Here's a sequence of events I captured of someone walking up to the door (triggering a motion/alarm event (both 6&7 index) and then pushing the doorbell button.


Code=VideoMotion;action=Start;index=0
Code=AlarmLocal;action=Start;index=6
Code=AlarmLocal;action=Start;index=7
Code=ProfileAlarmTransmit;action=Start;index=0;data={\n "SenseMethod" : "PassiveInfrared",\n "UTC" : 1597748954\n'}

Code=_DoTalkAction_;action=Pulse;index=0;data={\n "Action" : "Invite",\n "CallID" : "xxxx@[email protected]",\n "CallSrcMask" : 4
Code=CallNoAnswered;action=Start;index=0;data={\n "CallID" : "7"\n'

Code=VideoMotion;action=Stop;
Code=UpdateFile;action=Pulse;
Code=NewFile;action=Pulse;index=1701079381;data={\n "File" : "\\/mnt\\/sd\\/SnapShot\\/2020-08-18\\/11\\/11-23-18[0].jpg",\n "Size" : 309467\n'

Code=AlarmLocal;action=Stop;
Code=AlarmLocal;action=Stop;
Code=_DoTalkAction_;action=Pulse;index=0;data={\n "Action" : "Hangup",\n "CallID" : "xxxx",\n "CallSrcMask" : 4,\n "HangupReason" : "HangupByPhone"\n'
Code=CallNoAnswered;action=Stop;
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Re: Motion Event HTTP API SDK

Post by pcabral »

This is probably the final version of this script. It utilizes the standard shell provided by all linux distributions and now has watchdog capabilities.... meaning if the curl sub-process is terminated, it will be automatically respawned. Very handy if there are power outages or reboots. Lastly, I'm only monitoring AlarmLocal moving forward as it is enough to trigger motion events.

Code: Select all


#!/bin/sh

# Set trap on EXIT for cleanup
trap cleanExit INT

# Configuration
NAME="amcrest-ad110-events"
DEVICE="doorbell"
EVENTS="AlarmLocal"
ADDRESS="$DEVICE"
TRIGGER="http://synology_ip_address:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.Webhook&method=\"Incoming\"&version=1&token=provided_by_synology"
TMP_DIR="."

# Authentication
USER="admin"
PASS="secret_password"

# Auto-populated
PID="$TMP_DIR/$NAME.$DEVICE.pid"
FIFO="$TMP_DIR/$NAME.$DEVICE.fifo"
TOPIC="http://$ADDRESS/cgi-bin/eventManager.cgi?action=attach&codes=[$EVENTS]"
WATCHDOG=""

# Remove files and exit
cleanExit()
{
kill -9 $WATCHDOG
purgeFiles
exit 0
}

# Monitor and restart on error
watchDog()
{
while true;
do
if ! kill -0 `cat "$PID"` > /dev/null 2>&1; then
purgeFiles
attachDevice
return
fi

sleep 1
done
}

# Removes stale files
purgeFiles()
{
if [ -f "$PID" ]; then
kill -9 `cat "$PID"` > /dev/null 2>&1
rm -rf "$PID" "$FIFO"
fi
}

# Establishes the connection
attachDevice()
{
mkfifo "$FIFO"
curl -s --digest -u $USER:$PASS $TOPIC --trace-ascii "$FIFO" &
echo "$!" > "$PID"
watchDog &
WATCHDOG=$!
}

# Send messages to listeners
publishEvents()
{
while true;
do
(read LINE < "$FIFO") > /dev/null 2>&1

if [ -z "$LINE" ]; then
continue
fi

if [ ! -z $(echo "$LINE" | grep "Code=AlarmLocal;action=Start;index=6") ]; then
curl -s "$TRIGGER"
fi
done
}

# Main Entry
purgeFiles
attachDevice
publishEvents
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Re: Motion Event HTTP API SDK

Post by pcabral »

Actually there are two bugs with previous version of the script.

1. Watchdog was inconsistent and is now fixed.
2. reading of curl data was broken in last update and is now fixed.

Code: Select all


#!/bin/sh

# Set trap on EXIT for cleanup
trap cleanExit INT

# Configuration
NAME="amcrest-ad110-events"
DEVICE="doorbell"
EVENTS="AlarmLocal"
ADDRESS="$DEVICE"
TRIGGER="http://synology_ip_address:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.Webhook&method=\"Incoming\"&version=1&token=provided_by_synology"
TMP_DIR="."

# Authentication
USER="admin"
PASS="secret_password"

# Auto-populated
DOG="$TMP_DIR/$NAME.$DEVICE.dog"
PID="$TMP_DIR/$NAME.$DEVICE.pid"
FIFO="$TMP_DIR/$NAME.$DEVICE.fifo"
TOPIC="http://$ADDRESS/cgi-bin/eventManager.cgi?action=attach&codes=[$EVENTS]"

# Remove files and exit
cleanExit()
{
kill -9 `cat "$DOG"`
purgeFiles
exit 0
}

# Monitor and restart on error
watchDog()
{
while true;
do
if ! kill -0 `cat "$PID"` > /dev/null 2>&1; then
purgeFiles
attachDevice
return
fi

sleep 1
done
}

# Removes stale files
purgeFiles()
{
if [ -f "$PID" ]; then
kill -9 `cat "$PID"` > /dev/null 2>&1
rm -rf "$DOG" "$PID" "$FIFO"
fi
}

# Establishes the connection
attachDevice()
{
mkfifo "$FIFO"
curl -s --digest -u $USER:$PASS $TOPIC --trace-ascii "$FIFO" &
echo "$!" > "$PID"
watchDog &
echo "$!" > "$DOG"
}

# Send messages to listeners
publishEvents()
{
while true;
do
read LINE < "$FIFO"

if [ -z "$LINE" ]; then
continue
fi

if [ ! -z "$(echo "$LINE" | grep "Code=AlarmLocal;action=Start;index=6")" ]; then
curl -s "$TRIGGER"
fi
done
}

# Main Entry
purgeFiles
attachDevice
publishEvents
wallace4793
Posts: 24
Joined: Thu Mar 11, 2021 4:21 pm

Re: Motion Event HTTP API SDK

Post by wallace4793 »

Hi,

Is there a basic HTTP GET call that can be made to see if motion is detected?

http://192.168.1.108/cgi-bin/alarm.cgi? ... etOutState

The above is posted in the api for a result of 1 should motion be detected but does not work on my AD110.

Thanks

John
User avatar
Revo2Maxx
Site Admin
Posts: 5883
Joined: Sat Jun 15, 2019 3:05 pm

Re: Motion Event HTTP API SDK

Post by Revo2Maxx »

Well while not all API will work with Smarthome cameras and might be an issue of it works for one and not another.. Also FW can have an impact on API as well. Looking back at some of the older Release camera FW updates there was some cameras that mention about updating the camera api for current at that time and so on relating to that Update along with other things.. So people that say had 1.7api cameras trying to run 1.4 codes while some would work not all of them would.. Gary has a good understanding of the Ad110 and loads of testing on the API over time if there is something that he has worked with found working I am sure he will give something to help.. Other then that I was told there was no API for the Smarthome and things that work can work but can be risk running commands that can change things within the camera.. Some make changes and doing so at their own risk..
Be Safe.
wallace4793
Posts: 24
Joined: Thu Mar 11, 2021 4:21 pm

Re: Motion Event HTTP API SDK

Post by wallace4793 »

Hi,

Thanks I will tread carefully. I tried a few from the api but the camera started misbehaving- the bitrate went really low and colours dropped to 4, not sure if it was related but hard reset corrected it.

John
User avatar
Revo2Maxx
Site Admin
Posts: 5883
Joined: Sat Jun 15, 2019 3:05 pm

Re: Motion Event HTTP API SDK

Post by Revo2Maxx »

Yes sadly that is how it works if something it don't like is input it can kill the camera and sometimes it isn't until after night fall that it happens either caused by needing more volts or current or what the issue but one camera I now own my friend tried some things not listening to my warning and it bricked his camera.. I wasn't able to figure out what it was so it must be outside hardware and software related within the cameras cpu only paid 30 for it to help him buy another one he say he will not do any extra playing around and leave it as it works. Plus its new the old one was out of warranty so its good he learned lol
Be Safe.
GaryOkie
Posts: 418
Joined: Mon Apr 27, 2020 7:23 pm

Re: Motion Event HTTP API SDK

Post by GaryOkie »

@wallace4793 It's not as simple as a single API call to detect motion or any other event. It will require a small app to subscribe to the desired event, listen/wait for the event to occur, then trigger whatever action you need.

In addition to the script pcabral wrote above, there is a python library specifically for amcrest/dahua cameras which also works for the AD110. This python-amcrest library is documented here:
https://github.com/tchellomello/python-amcrest

You can also find sample code that was tested on the AD110 for not only monitoring motion events, but also doorbell press events. See: this post of mine which provides a small app for displaying EVERY event the camera/doorbell will issue.
https://github.com/tchellomello/python- ... -702815674
wallace4793
Posts: 24
Joined: Thu Mar 11, 2021 4:21 pm

Re: Motion Event HTTP API SDK

Post by wallace4793 »

Hi Gary,

Thanks greatly for your reponse - I see you have done lots of work on this. I also see lots around the HA community talking about Home Assistant, although Ive not used it - Im using The Home Remote (thehomeremote.com) which is similar. I learned a little JS for this system but i know nothing about python so I may need to learn some to integrate your system.

The Home Remote uses plugins in JS that can utilise HTTP servers to listen on ports for responses as you suggest so it may be possible to do this - although without knowing how your python is working its probably out of scope for me at the moment.

John
Post Reply