zm_event.sh 4.33 KB
Newer Older
nextime's avatar
nextime committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#!/bin/bash  
#
# ARGUMENTS:
#
# $1 -> start, update, stop
# $2 -> IP address of the domotikad daemon
# $3 -> type of event (motion only for the moment)
# $4 -> camera name
# $5 -> a string with camera name followed by a list of zones involved in the event, like
#       "CAM1: zone1, zone2", it take only the part after : and expect to find a list of
#       zone names separed by ", " (a comma and a space)
# $6 -> cam path (contain camera stream uri and other info like: rtsp://192.168.1.101/h264 -
# $7 -> monitor id
# $8 -> event id
#
# MYSQL CONFIGURATION:
#
# after using sudo to  permit root execution from the mysql user and add a regular shell
# to mysql user,
# 
# Add UDF sys_eval function to mysql and then:
#
# Add event to mysql zoneminder db with:
#
# delimiter EOF;;
# create trigger zmalarm_start after insert on Events
#    for each row
#       BEGIN
#          declare camname varchar(255);
#          declare campath varchar(255);
#          SELECT Name, Path INTO camname, campath FROM Monitors WHERE Id=NEW.MonitorId;
#          set @zmstart=sys_eval(
#           concat("sudo -u root /home/domotika/scripts/zm_event.sh start localhost ",
#           NEW.Cause, " '", camname, "' '", NEW.Notes, "' '", campath,
#           "' ", NEW.MonitorId, " ", NEW.Id, " ", 0, " ", 0, " ",
#           0, " ", 0, " ", 0, " ", NEW.Width,
#            " ",NEW.Height, " ", 0 ));
#       END;
# EOF;;
# create trigger zmalarm_update after update on Events
#    for each row
#       BEGIN
#         declare camname varchar(255);
#         declare campath varchar(255);
#         IF(NEW.Archived=OLD.Archived AND NEW.Videoed=OLD.Videoed AND NEW.Uploaded=OLD.Uploaded
#          AND NEW.Emailed=OLD.Emailed AND NEW.Messaged=OLD.Messaged AND NEW.Executed=OLD.Executed)
#         THEN
#             SELECT Name, Path INTO camname, campath FROM Monitors WHERE Id=NEW.MonitorId;
#             set @zmstart=sys_eval(
#              concat("sudo -u root /home/domotika/scripts/zm_event.sh update localhost ",
#              NEW.Cause, " '", camname, "' '", NEW.Notes, "' '", campath,
#              "' ", NEW.MonitorId, " ", NEW.Id, " ", NEW.AlarmFrames, " ", NEW.Frames, " ",
#              NEW.TotScore, " ", NEW.AvgScore, " ", NEW.MaxScore, " ", NEW.Width,
#               " ",NEW.Height, " ", NEW.Length ));
#          END IF;
#       END;
# EOF;;
# delimiter ;
#
#
# where localhost is the address of the domotika server that will manage the events
# and assuming that the path of this script is /home/domotika/scripts/
#
# Delete event with:
#
#    drop trigger zm.zmalarm_start;
#    drop trigger zm.zmalarm_update;
#

logger -t "[MOTION DETECTION]" "args: $@"

DBUSER="admin"
DBPASS="domotika"
WEBUSER="admin"
WEBPASS="domotika"

rawurlencode() {
  local string="${1}"
  local strlen=${#string}
  local encoded=""

  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  echo "${encoded}" 
}

STATUS="${1}"
case ${STATUS} in
   start)
      STATUS=1
      ;;
   update)
      STATUS=2
      if [ x"$(zmu -m ${7} -s -U${DBUSER} -P${DBPASS})" = x"0" ] ; then
         STATUS=4
      fi
      ;;
   stop)
      STATUS=4
      ;;
   *)
      exit 0
esac

TYPE="$(echo ${3} | tr '[:upper:]' '[:lower:]')"
case ${TYPE} in
   motion)
      TYPE=1
      ;;
   *)
      exit 0
esac

CAMNAME="$(rawurlencode ${4})"
ZONES="$(echo ${5} | awk -F ': ' '{print $2}')"

CAMPATH="$(rawurlencode ${6})"
MONITORID=${7}
EVENTID=${8}
ALARMFRAMES=${9}
FRAMES=${10}
TOTSCORE=${11}
AVGSCORE=${12}
MAXSCORE=${13}
WIDTH=${14}
HEIGHT=${15}
LENGTH=${16}


DOMOTIKAD_ADDR="${2}"
if [ -f /usr/bin/screen ] ; then
   WGET="/usr/bin/screen -d -m"
fi
WGET="$WGET $(which wget) --timeout=5 --auth-no-challenge --no-check-certificate -O /dev/null"
WGET="${WGET} https://${WEBUSER}:${WEBPASS}@${DOMOTIKAD_ADDR}/rest/v1.0/motionDetection"
WGET="${WGET}?status=${STATUS}&type=${TYPE}&camera=${CAMNAME}"
WGET="${WGET}&monitorid=${MONITORID}&eventid=${EVENTID}&campath=${CAMPATH}"
WGET="${WGET}&af=${ALARMFRAMES}&f=${FRAMES}&sc=${TOTSCORE}&avg=${AVGSCORE}"
WGET="${WGET}&max=${MAXSCORE}&w=${WIDTH}&h=${HEIGHT}&l=${LENGTH}"


IFS=", "
for z in ${ZONES}
   do
      zone=$(rawurlencode $z)
      WGET="${WGET}&zone=${zone}"
      logger -t "[MOTION EVENT]" "${WGET}"
      ${WGET} >/dev/null 2>&1
   done

exit 0