Motion Event HTTP API SDK

This Forum its to discuss the new Smart Home Devices.
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Motion Event HTTP API SDK

Post by pcabral »

I wanted to start a new thread to discuss the possibility of using the "Subscribe to event message" as a means of replacing the builtin lack luster algorithms that NVRs provide as a replacement for false motion detections, since doorbell utilizes the PIR sensor to detect motion and generate events for each motion detected.

This is the API in question and has anyone looked into it by any chance?

Code: Select all


Table 4-124
Syntax
http://<server>/cgi-bin/eventManager.cgi?action=attach&codes=[<eventCode>,<eventCode>,...][&keep alive = 20]
Method
GET
Description
Subscribe to messages that event of code eventCode happens.
Example
http://192.168.1.108/cgi-bin/eventManager.cgi?action=attach&codes=[AlarmLocal%2CV ideoMotion%2 CV ideoLoss%2CV ideoBlind]
http://192.168.1.108/cgi-bin/eventManager.cgi?action=attach&codes=[All]
Success Return
HTTP Code: 200 OK\r\n
Cache-Control: no-cache\r\n
Pragma: no-cache\r\n
Expires: Thu, 01 Dec 2099 16:00:00 GMT\r\n Connection: close\r\n
Content-Type: multipart/x-mixed-replace; boundary=<boundary>\r\n Body:
--<boundary>\r\n
Content-Type: text/plain\r\n
131

Content-Length: <data length>\r\n <eventInfo>\r\n\r\n --<boundary>\r\n
Content-Type: text/plain\r\n Content-Length: <data length>\r\n <eventInfo>\r\n\r\n
For example:
HTTP Code: 200 OK\r\n
Cache-Control: no-cache\r\n
Pragma: no-cache\r\n
Expires: Thu, 01 Dec 2099 16:00:00 GMT\r\n
Connection: close\r\n
Content-Type: multipart/x-mixed-replace; boundary=myboundary\r\n\r\n
Body:
-- myboundary \r\n
Content-Type: text/plain\r\n
Content-Length: 39\r\n
Code=VideoMotion; action=Start; index=0\r\n\r\n
-- myboundary \r\n
Content-Type: text/plain\r\n
Content-Length: 38\r\n
Code=VideoBlind; action=Start; index=0\r\n\r\n
-- myboundary \r\n
Content-Type: text/plain\r\n
Content-Length: 38\r\n
Code= MDResult; action=Pulse; index=0; data=61708863,61708863...\r\n\r\n -- myboundary \r\n

Code: Select all


Comment
eventCode can be any one of the standard codes defined in DHIIF, or “All”. All means all kinds of the eventcode.
eventcode include:
VideoMotion:
VideoLoss:
VideoBlind:
AlarmLocal: CrossLineDetection: CrossRegionDetection: LeftDetection: TakenAwayDetection: VideoAbnormalDetection: FaceDetection: AudioMutation: AudioAnomaly: VideoUnFocus: WanderDetection: RioterDetection: ParkingDetection: MoveDetection: StorageNotExist: StorageFailure: StorageLowSpace: AlarmOutput:
tripwire event
intrusion event
abandoned object detection missing object detection scene change event
face detect event
intensity change
input abnormal
defocus detect event loitering detection event People Gathering event parking detection event
fast moving event
storage not exist event. storage failure event. storage low space event. alarm output event.
motion detection event video loss detection event video blind detection event. alarm detection event.
MDResult: motion detection data reporting event. The motion detect window contains 18 rows and 22 columns. The event info contains motion detect data with mask of every row.
HeatImagingTemper: temperature alarm event
133

keepalive: if this param exist, the client must send any data to device by this connection in cycle. The keepalive is in range
of [1,60] second. For example:
The keeplive data can be the string “keep alive”.
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Re: Motion Event HTTP API SDK

Post by pcabral »

My assumption is that the responses are streamed for as long as the connection remains active. For example Synology's Surveillance Station action rules could establish the connection and generate it's on events to its client apps based on the reception of these HTTP API data streams (I think).
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Re: Motion Event HTTP API SDK

Post by pcabral »

My assumptions were correct the subscription to events is indeed a persistent http connection which is really good news. Perhaps a simple script to parse the responses then feed the events to a Synology webhook would work. The following is the curl command for those who want to play with it:

Note: %2c is a comma, but needs to be escaped for http url parsing.

Code: Select all

curl -s --digest -u admin:password "http://ip_address/cgi-bin/eventManager.cgi?action=attach&codes=[AlarmLocal%2CVideoMotion%2CVideoLoss%2CVideoBlind]"
The http stream responses look like the following:

Code: Select all

--myboundary
Content-Type: text/plain
Content-Length:37

Code=VideoMotion;action=Start;index=0

--myboundary
Content-Type: text/plain
Content-Length:36

Code=VideoMotion;action=Stop;index=0
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Re: Motion Event HTTP API SDK

Post by pcabral »

Further testing has revealed that in order to accurately identify an object being detected (person) that the following responses will be generated:

Code: Select all


--myboundary
Content-Type: text/plain
Content-Length:37

Code=VideoMotion;action=Start;index=0

--myboundary
Content-Type: text/plain
Content-Length:36

Code=AlarmLocal;action=Start;index=6

--myboundary
Content-Type: text/plain
Content-Length:36

Code=VideoMotion;action=Stop;index=0

--myboundary
Content-Type: text/plain
Content-Length:35

Code=AlarmLocal;action=Stop;index=6
I now have enough information to create a script where we can accurately relay these events to NVR as motion triggers by utilizing both VideoMotion and AlarmLocal event "Codes" where the action value is of type "Start". I should be able write a generic shell script using curl, json parser and tcp/telnet socket to forward the custom notification to my Synology.

More to come....
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Re: Motion Event HTTP API SDK

Post by pcabral »

Here is the proof-of-concept. It's a bash script which detects a motion + alarm event and generates a http post to the Synology Surveillance Station. The contents are pretty straight forward for manipulating it for you own use. At the moment I am creating snapshots since there is no option to update the camera's timeline with a notification marker.

script name:
amcrest-ad110-events

Mandatory fields to update:
PASS == Device password.
TRIGGER == Synology webhook url or something similiar if not synology.

Optional fields to update:
ADDRESS == Specify one if you don't use hostnames.
TMP_DIR == Where to write the PID and Named Pipe files.

Code: Select all


#!/bin/bash

# Set trap on EXIT for cleanup
trap purgeAll EXIT

# Configuration
NAME="amcrest-ad110-events"
DEVICE="doorbell"
EVENTS="AlarmLocal%2CVideoMotion"
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="my_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}]"

# Performs cleanup
function purgeAll
{
if [[ -f "${PID}" ]]; then
kill -9 `cat "${PID}"`
rm -rf "${PID}" "${FIFO}"
fi
}

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

# Send messages to listeners
function publishEvents
{
MOTION=0
ALARMS=0

while true
do
read LINE < "${FIFO}"

if [[ -z ${LINE} ]]; then
continue
fi

if [[ ! -z $(echo "${LINE}" | grep "Code=VideoMotion;action=Start") ]]; then
MOTION=1
fi

if [[ ! -z $(echo "${LINE}" | grep "Code=AlarmLocal;action=Start") ]]; then
ALARMS=1
fi

if [[ $MOTION -eq 1 && $ALARMS -eq 1 ]]; then
echo "Object Detected"

curl -s "${TRIGGER}"

MOTION=0
ALARMS=0
fi
done
}

# Main Entry
purgeAll
attachDevice
publishEvents
[code]
GaryOkie
Posts: 418
Joined: Mon Apr 27, 2020 7:23 pm

Re: Motion Event HTTP API SDK

Post by GaryOkie »

@pcabral - It's great to see the advances you are making in improving this doorbells motion handling.

In my testing, the undocumented Amcrest-specific "_DoTalkAction_" event with the Action "Invite" signals the doorbell push. I am working with the python-amcrest devs to get this added so that Home Assistant's Amcrest integration will have a new doorbell button sensor.

I'm not understanding how you think this doorbell is capable of PEOPLE detection. That is an advanced IVS feature of Dahua cameras and I sure wasn't aware that any Amcrest devices were capable of it (unless flashed with Dahua firmware). So you are saying that AlarmLocal;action=Start;index=6 signifies a person detected?
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Re: Motion Event HTTP API SDK

Post by pcabral »

Bad wording on my end.... not people but an object was detected which is a combination of video motion and the alarm local event.... which I'm assuming it is utilizing both video and PIR?!?

I did notice that there are constant events with video motion events being generated but they do not translate into an amcrest app notification.

It's only when an AlarmLocal and VideoMotion are both generated that the amcrest app is then notified it would seem.
jack7
Posts: 904
Joined: Tue May 29, 2018 7:46 pm

Re: Motion Event HTTP API SDK

Post by jack7 »

@GaryOkie
GaryOkie wrote: Sat Aug 22, 2020 12:17 pm "I'm not understanding how you think this doorbell is capable of PEOPLE detection. That is an advanced IVS feature of Dahua cameras and I sure wasn't aware that any Amcrest devices were capable of it (unless flashed with Dahua firmware). "
Human detection for Tripwire or Intrusion should be possibe for the AD110 when using an Amcrest AI NVR.
https://support.amcrest.com/hc/en-us/ar ... -an-AI-NVR
https://support.amcrest.com/hc/en-us/ar ... es-AI-NVR-

Also, the Amcrest Smart Home ASH21 has a Human Detection feature.
GaryOkie
Posts: 418
Joined: Mon Apr 27, 2020 7:23 pm

Re: Motion Event HTTP API SDK

Post by GaryOkie »

Thanks @jack7 for pointing out some Amcrest devices do have AI person detection. As to the doorbell, that's a feature it really ought to have without an NVR required.

@pcabral - So that's interesting that a MotionDetection event by itself isn't sufficient to trigger an alert and recording in the SH app. This actually meshes with what I noticed when you toggle Motion Detection in the app. The Motiondetection config setting internally in the doorbell never changes - it's always enabled. But when you turn off MD in the app, two things get set to false in the doorbell config: Alarm[0].Enable & Alarm[0].EventHandler.SnapshotEnable (I have no idea why snapshot was necessary to be disabled).

This motion detection event logic apparently is unique to the AD110 doorbell.

EDIT: This also probably means that the motion detection logic in Home Assistant/python-amcrest and others will respond to MD events even when MD has been turned off by the SH app, since they aren't AND'ing alarm events too to my knowledge. I wonder how the NVR's are handling this if this is a SmartHome-specific contrivance? Something else for me to test when I get back home next week...
pcabral
Posts: 61
Joined: Tue Jun 16, 2020 7:13 pm

Re: Motion Event HTTP API SDK

Post by pcabral »

@GaryOkie honestly I'm thinking the video motion event can be ignored all together and only handle the AlarmLocal with index 6 or 7 should be good enough.

I will also update my script to reflect that change and also use basic shell support and not bash to allow this script to be run from busy box and other devices where bash is not available.

I'm also betting that index 6 and 7 are used in some sort of lookup table to identify the detection type.
Post Reply