1
0
mirror of https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks.git synced 2025-09-09 07:30:57 +02:00

Add some examples for dealing with motion events (#194)

* Add some examples for dealing with motion events

* Harmonize core motion detection & event handling to use one single motion.conf, refactor & beautify motion detection api for more consistency, host jquery locally for usecases without internet, add responsive switch to live ui

* added mqtt actions for switching motion detection on and off

* added motion detection status to status

* Update motion.conf

* Update motion.conf
This commit is contained in:
jmtatsch
2018-03-31 17:26:10 +02:00
committed by Elias Kotlyar
parent 3d8b9ce096
commit 5b9aedfbb9
13 changed files with 320 additions and 113 deletions

View File

@@ -0,0 +1,15 @@
############################################################
# edit this file and move it to /system/sdcard/config/motion.conf #
############################################################
# Configure motion core parameters
region_of_interest=0,0,1280,720
motion_sensitivity=4
motion_indicator_color=2
# Configure how to deal with motion events
motion_trigger_led=true
save_snapshot=false
publish_mqtt_message=false

View File

@@ -0,0 +1,16 @@
############################################################
# edit this file and move it to /system/sdcard/config/motion.conf #
############################################################
# Configure the motion core parameters:
region_of_interest=0,0,1280,720
motion_sensitivity=4
motion_indicator_color=2
# Configure how to deal with motion events:
motion_trigger_led=true
save_snapshot=false
publish_mqtt_message=false

View File

@@ -3,10 +3,9 @@
# Set the socket option in order to restart easily the server (socket in use)
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
if [ -f /system/sdcard/config/motion ] ; then
source /system/sdcard/config/motion 2>/dev/null
/system/sdcard/bin/setconf -k r -v ${Region} 2>/dev/null
/system/sdcard/bin/setconf -k m -v ${Sens} 2>/dev/null
/system/sdcard/bin/setconf -k z -v ${OsdColor} 2>/dev/null
if [ -f /system/sdcard/config/motion.conf ] ; then
source /system/sdcard/config/motion.conf 2>/dev/null
/system/sdcard/bin/setconf -k r -v ${region_of_interest} 2>/dev/null
/system/sdcard/bin/setconf -k m -v ${motion_sensitivity} 2>/dev/null
/system/sdcard/bin/setconf -k z -v ${motion_indicator_color} 2>/dev/null
fi;

View File

@@ -0,0 +1,22 @@
#!/bin/sh
# Source your custom motion configurations
source /system/sdcard/config/motion.conf
# Define a gpio helper
setgpio(){
GPIOPIN=$1
echo "$2" > "/sys/class/gpio/gpio$GPIOPIN/value"
}
# Turn off the amber LED
if [ "$motion_trigger_led" = true ] ; then
setgpio 38 1
fi
# Publish a mqtt message
if [ "$publish_mqtt_message" = true ] ; then
source /system/sdcard/config/mqtt
export LD_LIBRARY_PATH='/thirdlib:/system/lib:/system/sdcard/lib'
/system/sdcard/bin/mosquitto_pub.bin -h "$HOST" -u "$USER" -P "$PASS" -t "${TOPIC}"motion ${MOSQUITTOOPTS} ${MOSQUITTOPUBOPTS} -m "off"
fi

View File

@@ -0,0 +1,33 @@
#!/bin/sh
# Source your custom motion configurations
source /system/sdcard/config/motion.conf
# Define a gpio helper
setgpio(){
GPIOPIN=$1
echo "$2" > "/sys/class/gpio/gpio$GPIOPIN/value"
}
# Turn on the amber led
if [ "$motion_trigger_led" = true ] ; then
setgpio 38 0
setgpio 39 1
fi
# Save a snapshot
if [ "$save_snapshot" = true ] ; then
save_dir=/system/sdcard/motion/stills
filename=`date +%d-%m-%Y_%H.%M.%S`.jpg
if [ ! -d "$save_dir" ]; then
mkdir -p $save_dir
fi
/system/sdcard/bin/getimage > $save_dir/$filename &
fi
# Publish a mqtt message
if [ "$publish_mqtt_message" = true ] ; then
source /system/sdcard/config/mqtt
export LD_LIBRARY_PATH='/thirdlib:/system/lib:/system/sdcard/lib'
/system/sdcard/bin/mosquitto_pub.bin -h "$HOST" -u "$USER" -P "$PASS" -t "${TOPIC}"motion ${MOSQUITTOOPTS} ${MOSQUITTOPUBOPTS} -m "on"
fi

View File

@@ -41,6 +41,16 @@ while read -r line < "$FIFO"; do
/system/sdcard/bin/mosquitto_pub.bin -h "$HOST" -u "$USER" -P "$PASS" -t "${TOPIC}"Yellow_LED -m "off"
;;
"${TOPIC}motion_detection/set on")
/system/sdcard/bin/curl -m 2 "${CURLOPTS}" -s http://127.0.0.1/cgi-bin/action.cgi\?cmd=motion_detection_on -o /dev/null 2>/dev/null
/system/sdcard/bin/mosquitto_pub.bin -h "$HOST" -u "$USER" -P "$PASS" -t "${TOPIC}"motion_detection ${MOSQUITTOPUBOPTS} ${MOSQUITTOOPTS} -m "on"
;;
"${TOPIC}motion_detection/set off")
/system/sdcard/bin/curl -m 2 "${CURLOPTS}" -s http://127.0.0.1/cgi-bin/action.cgi\?cmd=motion_detection_off -o /dev/null 2>/dev/null
/system/sdcard/bin/mosquitto_pub.bin -h "$HOST" -u "$USER" -P "$PASS" -t "${TOPIC}"motion_detection ${MOSQUITTOPUBOPTS} ${MOSQUITTOOPTS} -m "off"
;;
"${TOPIC}set*")
COMMAND=$(echo "$line" | awk '{print $2}')
echo "$COMMAND"
@@ -53,4 +63,3 @@ while read -r line < "$FIFO"; do
;;
esac
done

View File

@@ -36,7 +36,7 @@ ir_led=$(getgpio 49)
if [ "$ir_led" == "0" ]; then ir_led="on"; fi
if [ "$ir_led" == "1" ]; then ir_led="off"; fi
## RTSP status
# RTSP status
string=$(pidof v4l2rtspserver-master)
if [[ ${#string} == "0" ]]; then
RTSPRUNNING="not running"
@@ -44,17 +44,26 @@ else
RTSPRUNNING="running"
fi
# Motion detection status
motion_sensitivity=`/system/sdcard/bin/setconf -g m 2>/dev/null`
if test $motion_sensitivity -lt 0;
then motion_detection="off";
else
motion_detection="on";
fi
if [ "$STATUSFORMAT" == "json" ]; then
NOISELEVEL=$(echo "$QUALITY" | awk '{ print $6}' | sed -e 's/.*=//' | sed -e 's/\/100/\%/')
LINKQUALITY=$(echo "$QUALITY" | awk '{ print $2}' | sed -e 's/.*=//' | sed -e 's/\/100/\%/')
SIGNALLEVEL=$(echo "$QUALITY" | awk '{ print $4}' | sed -e 's/.*=//' | sed -e 's/\/100/\%/')
echo "{\"Uptime\":\"$UPTIME\", \"RTSP-Server\":\"$RTSPRUNNING\", \"IR-Cut\":\"$ir_cut\", \"Wifi\":{\"SSID\":\"$SSID\", \"Bitrate\":\"$BITRATE\", \"SignalLevel\":\"$SIGNALLEVEL\", \"Linkquality\":\"$LINKQUALITY\", \"NoiseLevel\":\"$NOISELEVEL\"}, \"LEDs\":{\"Blue\":\"$blue\", \"Yellow\":\"$yellow\", \"Infrared\":\"$ir_led\"}}"
echo "{\"Uptime\":\"$UPTIME\", \"RTSP-Server\":\"$RTSPRUNNING\", \"motion_detection\":\"$motion_detection\", \"IR-Cut\":\"$ir_cut\", \"Wifi\":{\"SSID\":\"$SSID\", \"Bitrate\":\"$BITRATE\", \"SignalLevel\":\"$SIGNALLEVEL\", \"Linkquality\":\"$LINKQUALITY\", \"NoiseLevel\":\"$NOISELEVEL\"}, \"LEDs\":{\"Blue\":\"$blue\", \"Yellow\":\"$yellow\", \"Infrared\":\"$ir_led\"}}"
else
echo "$UPTIME - "
echo "SSID: $SSID, Bitrate: $BITRATE, $QUALITY - "
echo "LEDs: blue=$blue, yellow=$yellow, IR=$ir_led - "
echo "IR-Cut=$ir_cut - "
echo "RTSP-Server: $RTSPRUNNING"
echo "RTSP-Server: $RTSPRUNNING - "
echo "motion_detection: $motion_detection"
fi
## enable this for the format used before

View File

@@ -6,8 +6,15 @@ echo ""
source func.cgi
setgpio(){
GPIOPIN=$1
echo "$2" > "/sys/class/gpio/gpio$GPIOPIN/value"
GPIOPIN=$1
echo "$2" > "/sys/class/gpio/gpio$GPIOPIN/value"
}
rewrite_config(){
cfg_path=$1
cfg_key=$2
new_value=$3
sed -i -e "/$cfg_key=/ s/=.*/=$new_value/" $cfg_path
}
echo "<br/>"
@@ -209,17 +216,30 @@ if [ -n "$F_cmd" ]; then
flip-off)
/system/sdcard/bin/setconf -k f -v 0
;;
setRegion)
echo "Region=${F_X0},${F_Y0},${F_X1},${F_Y1}" > /system/sdcard/config/motion
echo "Sens=${F_Sensitivity}" >> /system/sdcard/config/motion
echo "OsdColor=${F_osdColor}" >> /system/sdcard/config/motion
/system/sdcard/bin/setconf -k r -v ${F_X0},${F_Y0},${F_X1},${F_Y1}
/system/sdcard/bin/setconf -k m -v ${F_Sensitivity}
/system/sdcard/bin/setconf -k z -v ${F_osdColor}
motion_detection_on)
/system/sdcard/bin/setconf -k m -v 4
;;
motion_detection_off)
/system/sdcard/bin/setconf -k m -v -1
;;
set_region_of_interest)
rewrite_config /system/sdcard/config/motion.conf region_of_interest "${F_x0},${F_y0},${F_x1},${F_y1}"
rewrite_config /system/sdcard/config/motion.conf motion_sensitivity "${F_motion_sensitivity}"
rewrite_config /system/sdcard/config/motion.conf motion_indicator_color "${F_motion_indicator_color}"
# echo "region_of_interest=${F_x0},${F_y0},${F_x1},${F_y1}" > /system/sdcard/config/motion.conf
# echo "motion_sensitivity=${F_motion_sensitivity}" >> /system/sdcard/config/motion.conf
# echo "motion_indicator_color=${F_motion_indicator_color}" >> /system/sdcard/config/motion.conf
/system/sdcard/bin/setconf -k r -v ${F_x0},${F_y0},${F_x1},${F_y1}
/system/sdcard/bin/setconf -k m -v ${F_motion_sensitivity}
/system/sdcard/bin/setconf -k z -v ${F_motion_indicator_color}
# Changed the detection region, need to restart the server
if [ ${F_restartServer} == "1" ]
if [ ${F_restart_server} == "1" ]
then
processName="v4l2rtspserver-master"
#get the process pid
@@ -236,7 +256,7 @@ if [ -n "$F_cmd" ]; then
${cmdLine} &>/dev/null
else
echo "Not found"
echo "process v4l2rtspserver-master was not found"
fi
fi

View File

@@ -1,26 +1,27 @@
#!/bin/sh
echo "Content-type: text/plain"
echo
color=`/system/sdcard/bin/setconf -g z 2>/dev/null`
if [ "${color}X" == "X" ]
then
color="0"
fi
echo "osdColor=${color};"
sens=`/system/sdcard/bin/setconf -g m 2>/dev/null`
if [ "${sens}X" == "X" ]
motion_indicator_color=`/system/sdcard/bin/setconf -g z 2>/dev/null`
if [ "${motion_indicator_color}X" == "X" ]
then
sens="0"
motion_indicator_color="0"
fi
echo "sens=${sens};"
echo "motion_indicator_color=${motion_indicator_color};"
region=`/system/sdcard/bin/setconf -g r 2>/dev/null`
if [ "${region}X" == "X" ]
motion_sensitivity=`/system/sdcard/bin/setconf -g m 2>/dev/null`
if [ "${motion_sensitivity}X" == "X" ]
then
region="0,0,0,0"
motion_sensitivity="0"
fi
echo "RegionSize=[${region}];"
echo "motion_sensitivity=${motion_sensitivity};"
region_of_interest=`/system/sdcard/bin/setconf -g r 2>/dev/null`
if [ "${region_of_interest}X" == "X" ]
then
region_of_interest="0,0,0,0"
fi
echo "region_of_interest=[${region_of_interest}];"

View File

@@ -95,7 +95,19 @@ if [ -n "$F_cmd" ]; then
fi
echo $sound_on_startup
;;
motion_detection)
motion_sensitivity=`/system/sdcard/bin/setconf -g m 2>/dev/null`
if [ "${motion_sensitivity}X" == "X" ];
then motion_sensitivity="0"
fi
if test $motion_sensitivity -lt 0;
then motion_detection="off";
else
motion_detection="on";
fi
echo $motion_detection
;;
*)
echo "Unsupported command '$F_cmd'"
;;

View File

@@ -2,87 +2,128 @@
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Use http://odyniec.net/projects/imgareaselect/ -->
<head>
<link rel="stylesheet" type="text/css" href="css/imgareaselect-animated.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js"></script>
<title>Motion Configuration</title>
<style type="text/css">
body { background-color: #B0E0E6; font-family: verdana, sans-serif; }
.err { color: red; }
hr { height: 1px; border: 0; border-top: 1px solid #aaa; }
button, input[type=submit] { background-color: #ddeaff; }
.tbl { border-collapse: collapse; border-spacing: 0;}
.tbl th { text-align: left; vertical-align: top; font-weight: bold; padding: 10px 5px; border-style: solid; border-width: 1px; overflow: hidden; word-break: normal; }
.tbl td { padding: 10px 5px; border-style: solid; border-width: 1px; overflow: hidden; word-break: normal; }
</style>
<script type="text/javascript">
<head>
<link rel="stylesheet" type="text/css" href="css/imgareaselect-animated.css" />
<script type="text/javascript" src="scripts/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js"></script>
<title>Motion Configuration</title>
<style type="text/css">
body {
background-color: #B0E0E6;
font-family: verdana, sans-serif;
}
function preview(img, selection) {
if (!selection.width || !selection.height)
.err {
color: red;
}
hr {
height: 1px;
border: 0;
border-top: 1px solid #aaa;
}
button,
input[type=submit] {
background-color: #ddeaff;
}
.tbl {
border-collapse: collapse;
border-spacing: 0;
}
.tbl th {
text-align: left;
vertical-align: top;
font-weight: bold;
padding: 10px 5px;
border-style: solid;
border-width: 1px;
overflow: hidden;
word-break: normal;
}
.tbl td {
padding: 10px 5px;
border-style: solid;
border-width: 1px;
overflow: hidden;
word-break: normal;
}
</style>
<script type="text/javascript">
function preview(img, selection) {
if (!selection.width || !selection.height)
return;
var scaleX = 100 / selection.width;
var scaleY = 100 / selection.height;
var scaleX = 100 / selection.width;
var scaleY = 100 / selection.height;
$('#X0').val(selection.x1);
$('#Y0').val(selection.y1);
$('#X1').val(selection.x2);
$('#Y1').val(selection.y2);
$('#restartServer').val(1);
}
$('#x0').val(selection.x1);
$('#y0').val(selection.y1);
$('#x1').val(selection.x2);
$('#y1').val(selection.y2);
$('#restart_server').val(1);
}
$(function () {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'cgi-bin/getMotionInfo.cgi', false);
httpRequest.send(null);
eval(httpRequest.responseText)
$('#picture').imgAreaSelect({ handles: true, fadeSpeed: 200, onSelectChange: preview,movable:true,persistent:true });
var ias = $('#picture').imgAreaSelect({ instance: true });
ias.setOptions({ show: true });
ias.update();
$(function() {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'cgi-bin/getMotionInfo.cgi', false);
httpRequest.send(null);
eval(httpRequest.responseText)
$('#picture').imgAreaSelect({
handles: true,
fadeSpeed: 200,
onSelectChange: preview,
movable: true,
persistent: true
});
var ias = $('#picture').imgAreaSelect({
instance: true
});
ias.setOptions({
show: true
});
ias.update();
if (RegionSize[0] == 0 && RegionSize[1] == 0 && RegionSize[2]==0 && RegionSize[3] == 0)
ias.setSelection(0,0,width, height, true);
else
ias.setSelection(RegionSize[0],RegionSize[1],RegionSize[2],RegionSize[3], false);
if (region_of_interest[0] == 0 && region_of_interest[1] == 0 && region_of_interest[2] == 0 && region_of_interest[3] == 0)
ias.setSelection(0, 0, width, height, true);
else
ias.setSelection(region_of_interest[0], region_of_interest[1], region_of_interest[2], region_of_interest[3], false);
$('#X0').val(RegionSize[0]);
$('#Y0').val(RegionSize[1]);
$('#X1').val(RegionSize[2]);
$('#Y1').val(RegionSize[3]);
$('#restartServer').val(0);
$('#osdColor').val(osdColor).change();
$('#Sensitivity').val(sens).change();
});
</script>
$('#x0').val(region_of_interest[0]);
$('#y0').val(region_of_interest[1]);
$('#x1').val(region_of_interest[2]);
$('#y1').val(region_of_interest[3]);
$('#restart_server').val(0);
$('#motion_indicator_color').val(motion_indicator_color).change();
$('#motion_sensitivity').val(motion_sensitivity).change();
});
</script>
</head>
<body onload="">
<div class="container">
<div style="float: left; width: 50%;">
<p class="instructions">
Select motion detection region (changing region will restart the rtsp server)
</p>
<div class="container">
<div style="float: left; width: 50%;">
<p class="instructions">
Select motion detection region (changing region will restart the rtsp server)
</p>
<div class="frame" style="margin: 0 0.3em;">
<img id="picture" src="/cgi-bin/currentpic.cgi" />
</div>
<form style="margin: 0px" action="/cgi-bin/action.cgi?cmd=setRegion" method="post">
<input id="X0" name="X0" type="hidden" />
<input id="Y0" name="Y0" type="hidden" />
<input id="X1" name="X1" type="hidden" />
<input id="Y1" name="Y1" type="hidden" />
<input id="restartServer" name="restartServer" type="hidden" />
Set Sensitivity or deactivate
<select name="Sensitivity" id="Sensitivity">
<div class="frame" style="margin: 0 0.3em;">
<img id="picture" src="/cgi-bin/currentpic.cgi" />
</div>
<form style="margin: 0px" action="/cgi-bin/action.cgi?cmd=set_region_of_interest" method="post">
<input id="x0" name="x0" type="hidden" />
<input id="y0" name="y0" type="hidden" />
<input id="x1" name="x1" type="hidden" />
<input id="y1" name="y1" type="hidden" />
<input id="restart_server" name="restart_server" type="hidden" /> Set sensitivity or deactivate
<select name="motion_sensitivity" id="motion_sensitivity">
<option value="-1">Motion deactivated</option>
<option value="0">0</option>
<option value="1">1</option>
@@ -91,8 +132,8 @@ $(function () {
<option value="4">4</option>
</select>
<br>Display circle when motion detected
<select name="osdColor" id="osdColor">
<br>Display motion indicator if motion is detected
<select name="motion_indicator_color" id="motion_indicator_color">
<option value="-1">Deactivated</option>
<option value="0">White</option>
<option value="1">Black</option>
@@ -104,10 +145,10 @@ $(function () {
<option value="7">Purple</option>
</select>
<br>
<input type="submit" value="Configure motion"/>
<br>
<input type="submit" value="Configure motion" />
</form>
</form>
</div>
</div>
</div>
</body>
</body>

View File

@@ -162,7 +162,7 @@
}
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="scripts/jquery-3.3.1.min.js"></script>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "300px";
@@ -192,7 +192,7 @@
});
}
var entities = ["yellow_led", "blue_led", "ir_led", "ir_cut", "rtsp_h264", "rtsp_mjpeg", "auto_night_detection", "mqtt_status", "mqtt_control", "sound_on_startup"];
var entities = ["yellow_led", "blue_led", "ir_led", "ir_cut", "rtsp_h264", "rtsp_mjpeg", "auto_night_detection", "mqtt_status", "mqtt_control", "sound_on_startup", "motion_detection"];
for (var i in entities) {
sync_toggle(entities[i]);
}
@@ -246,6 +246,12 @@
<span class="slider round"></span>
</label>
</a>
<a id="motion_detection" href="javascript:void(0)">Motion Detection
<label class="switch">
<input id="motion_detection_toggle" type="checkbox" checked>
<span class="slider round"></span>
</label>
</a>
<a id="rtsp_h264" href="javascript:void(0)">RTSP H264 Server
<label class="switch">
<input id="rtsp_h264_toggle" type="checkbox" checked>
@@ -281,6 +287,7 @@
<div class="dropdown-container">
<a id="conf" href="javascript:void(0)">Configuration</a>
<a id="scripts" href="javascript:void(0)">Scripts</a>
<a id="motion" href="javascript:void(0)">Motion detection</a>
<a id="network" href="javascript:void(0)">Network</a>
<a id="logs" href="javascript:void(0)">Logs</a>
<a id="reboot" href="javascript:void(0)">Reboot</a>
@@ -420,6 +427,22 @@
});
})
// MOTION DETECTION TOGGLE
$("#motion_detection").click(function() {
$.get("/cgi-bin/state.cgi", {
cmd: "motion_detection"
})
.done(function(status) {
if (status.trim() == "on") {
$.get("/cgi-bin/action.cgi?cmd=motion_detection_off").done(function(data) {});
$('#motion_detection_toggle').prop('checked', false);
} else {
$.get("/cgi-bin/action.cgi?cmd=motion_detection_on").done(function(data) {});
$('#motion_detection_toggle').prop('checked', true);
}
});
})
// RTSP H264 TOGGLE
$("#rtsp_h264").click(function() {
$.get("/cgi-bin/state.cgi", {
@@ -510,6 +533,11 @@
$("#content").load("/cgi-bin/scripts.cgi");
})
//CONFIGURE MOTION
$("#motion").click(function() {
$("#content").load("/configmotion.html");
})
//NETWORK
$("#network").click(function() {
$("#content").load("/cgi-bin/network.cgi");

File diff suppressed because one or more lines are too long