diff --git a/sd/test/crontabs_root b/sd/test/crontabs_root new file mode 100755 index 0000000..35f595e --- /dev/null +++ b/sd/test/crontabs_root @@ -0,0 +1,23 @@ +# cat /var/spool/cron/crontabs/root +# Edit this file to introduce tasks to be run by cron. +# +# Each task to run has to be defined through a single line +# indicating with different fields when the task will be run +# and what command to run for the task +# +# To define the time you can provide concrete values for +# minute (m), hour (h), day of month (dom), month (mon), +# and day of week (dow) or use '*' in these fields (for 'any').# +# Notice that tasks will be started based on the cron's system +# daemon's notion of time and timezones. +# +# Output of the crontab jobs (including errors) is sent through +# email to the user the crontab file belongs to (unless redirected). +# +# For example, you can run a backup of all your user accounts +# at 5 a.m every week with: +# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ +# +# For more information see the manual pages of crontab(5) and cron(8) +# +# m h dom mon dow command diff --git a/sd/test/equip_test.sh b/sd/test/equip_test.sh index 32cc818..bceeff2 100644 --- a/sd/test/equip_test.sh +++ b/sd/test/equip_test.sh @@ -49,7 +49,7 @@ LOG_FILE=${LOG_DIR}/log.txt log_init() { # clean the previous log file and add a starting line - echo "Starting to log..." > /home/hd1/test/log.txt + echo "[$(date +'%Y%m%d-%H%M%S')] Starting to log..." > /home/hd1/test/log.txt } log() { @@ -114,8 +114,8 @@ cd /home/3518 himm 0x20050074 0x06802424 ### Let ppl hear that we start -/home/rmm "/home/hd1/voice/welcome.g726" 1 -/home/rmm "/home/hd1/voice/wait.g726" 1 +/home/rmm "/home/hd1/test/voice/welcome.g726" 1 +/home/rmm "/home/hd1/test/voice/wait.g726" 1 ### start blinking blue led for configuration in progress #/home/led_ctl -boff -yon & @@ -261,7 +261,7 @@ log "Debug mode = $(get_config DEBUG)" # first, configure wifi ### Let ppl hear that we start connect wifi -/home/rmm "/home/hd1/voice/connectting.g726" 1 +/home/rmm "/home/hd1/test/voice/connectting.g726" 1 log "Check for wifi configuration file...*" log $(find /home -name "wpa_supplicant.conf") @@ -300,7 +300,7 @@ log "New datetime is $(date)" ### Check if reach gateway and notify ping -c1 -W2 $(get_config GATEWAY) > /dev/null if [ 0 -eq $? ]; then - /home/rmm "/home/hd1/voice/wifi_connected.g726" 1 + /home/rmm "/home/hd1/test/voice/wifi_connected.g726" 1 fi ### set the root password @@ -391,6 +391,9 @@ crontab_folder="/var/spool/cron/crontabs" if [ ! -r "$crontab_folder" ]; then mkdir -p "$crontab_folder" fi +if [ ! -r "/var/spool/cron/crontabs/root" ]; then + cp /home/hd1/test/crontabs_root /var/spool/cron/crontabs/root +fi # Start crond daemon /usr/sbin/crond -b @@ -400,7 +403,7 @@ fi ping -c1 -W2 $(get_config GATEWAY) > /dev/null if [ 0 -eq $? ]; then led $(get_config LED_WHEN_READY) - /home/rmm "/home/hd1/voice/success.g726" 1 + /home/rmm "/home/hd1/test/voice/success.g726" 1 else led -boff -yfast fi diff --git a/sd/test/scripts/ftp_upload/common_lib.sh b/sd/test/scripts/ftp_upload/common_lib.sh new file mode 100755 index 0000000..8ae5b58 --- /dev/null +++ b/sd/test/scripts/ftp_upload/common_lib.sh @@ -0,0 +1,167 @@ +#!/bin/sh + +DEFAULT_SCRIPT_DIR="/tmp/hd1/test/scripts" +DEFAULT_RECORD_DIR="/tmp/hd1/record/" +DEFAULT_CONFIG_FILE="/home/hd1/test/yi-hack.cfg" + +max_d01=31 +max_d02=28 +max_d03=31 +max_d04=30 +max_d05=31 +max_d06=30 +max_d07=31 +max_d08=31 +max_d09=30 +max_d10=31 +max_d11=30 +max_d12=31 + +led() { + # example usage : + # led -boff -yon + # options : + # -bfast + # -bon + # -boff + # -yfast + # -yon + # -yoff + + # first, kill current led_ctl process + kill $(ps | grep led_ctl | grep -v grep | awk '{print $1}') + # then process + /home/led_ctl $@ & + +} + +get_config() +{ + param=$1 + conf_file=${2-"$DEFAULT_CONFIG_FILE"} + count=$(grep -e ^\s*${param}\s* ${conf_file} | wc -l) + if [ $count -gt 1 ]; then + log "ERROR: Found $count line for ${param}" + fi + + grep -e ^\s*${param}\s* $conf_file | cut -d"=" -f2 +} + +is_server_live() +{ + ping -c1 -W2 $1 > /dev/null + return $? +} + +is_pid_exist() +{ + count=$(ps | grep $1 | wc -l) + if [ $count -gt 1 ]; then + return 0 + fi + return 1 +} + +pid_store() +{ + if [ ! -r "$2" ]; then + log "[$(basename "$0")] PID file $2 not existed" + fi + echo $1 > $2 +} + +pid_get() +{ + if [ ! -r "$1" ]; then + log "[$(basename "$0")] PID file $1 not existed" + fi + cat $1 +} + +pid_clear() +{ + if [ ! -r "$1" ]; then + log "[$(basename "$0")] PID file $1 not existed" + fi + cat /dev/null > $1 +} + +log() +{ + echo $3 "$(date +'%Y-%m-%dT%H:%M:%S%z') $1" >> $2 +} + +unreach_get() +{ + if [ ! -r "$1" ]; then + log "[$(basename "$0")] Unreach file $1 not existed" + fi + urcount=$(cat $1) + if [ -z "$urcount" ]; then + urcount=0 + fi + echo $urcount +} + +unreach_increase() +{ + urfile=$1 + if [ ! -r "$urfile" ]; then + log "[$(basename "$0")] Unreach file $urfile not existed" + fi + urcount=$(unreach_get $urfile) + urcount=$((urcount + 1)) + echo $urcount > $urfile +} + +unreach_reset() +{ + if [ ! -r "$1" ]; then + log "[$(basename "$0")] Unreach file $1 not existed" + fi + echo 0 > $1 +} + + +is_leap_year() +{ + year=$1 + if [ $((year % 400)) -eq 0 ]; then + return 0 + elif [ $((year % 4)) -eq 0 ] && [ $((year % 100)) -ne 0 ]; then + return 0 + else + return 1 + fi +} + + +check_offline_duration() +{ + # Check if execution time within Gateway Off duration + gw_off_start=$(get_config GW_OFF_START) + gw_off_end=$(get_config GW_OFF_END) + if [ ! -z "$gw_off_start" ] && [ ! -z "$gw_off_end" ]; then + log "[$NAME] Check gateway offline duration" ${FTP_LOG} + gw_off_start=$(echo $gw_off_start | sed 's/://g') + gw_off_end=$(echo $gw_off_end | sed 's/://g') + current_time=$(date +%H%M%S) + if [ $gw_off_start -gt $gw_off_end ]; then + if [ $gw_off_start -lt 1200 ]; then + gw_off_start=$((gw_off_start + 2400)) + fi + if [ $gw_off_end -lt 1200 ]; then + gw_off_end=$((gw_off_end + 2400)) + fi + if [ $current_time -lt 1200 ]; then + current_time=$((current_time + 2400)) + fi + fi + if [ $current_time -gt $gw_off_start ] && [ $current_time -lt $gw_off_end ]; then + log "[$NAME] Excution at $gw_off_start < $current_time < $gw_off_end. Do nothing" ${FTP_LOG} + return 1 + fi + fi + return 0 +} + diff --git a/sd/test/scripts/ftp_upload/ftp.pid b/sd/test/scripts/ftp_upload/ftp.pid new file mode 100755 index 0000000..8b13789 --- /dev/null +++ b/sd/test/scripts/ftp_upload/ftp.pid @@ -0,0 +1 @@ + diff --git a/sd/test/scripts/ftp_upload/ftp_upload.mem b/sd/test/scripts/ftp_upload/ftp_upload.mem new file mode 100755 index 0000000..512be7b --- /dev/null +++ b/sd/test/scripts/ftp_upload/ftp_upload.mem @@ -0,0 +1 @@ +2016Y12M15D13H/53M00S.mp4 diff --git a/sd/test/scripts/ftp_upload/ftp_upload.sh b/sd/test/scripts/ftp_upload/ftp_upload.sh new file mode 100755 index 0000000..907e73e --- /dev/null +++ b/sd/test/scripts/ftp_upload/ftp_upload.sh @@ -0,0 +1,202 @@ +#!/bin/sh + +source "/tmp/hd1/test/scripts/ftp_upload/common_lib.sh" + +NAME=`basename "$0"` +FTP_MEM_FILE="/tmp/hd1/test/scripts/ftp_upload/ftp_upload.mem" +FTP_LOG="/tmp/hd1/test/scripts/ftp_upload/log.txt" +PID_FILE="/tmp/hd1/test/scripts/ftp_upload/ftp.pid" + + +mem_store() +{ + if [ ! -r "$3" ]; then + log "[$(basename "$0")] Mem file $3 not existed" ${FTP_LOG} + fi + last_folder=$1 + last_file=$2 + echo "${last_folder}/${last_file}" > $3 +} + +mem_get() +{ + if [ ! -r "$1" ]; then + log "[$(basename "$0")] Mem file $1 not existed" ${FTP_LOG} + fi + mfile=$1 + last_folder=$(cat ${mfile} | cut -d'/' -f1) + last_file=$(cat ${mfile} | cut -d'/' -f2) + if [ -z "${last_folder}" ] || [ -z "${last_file}" ]; then + log "[$(basename "$0")] Cannot find last folder and file in $mfile" ${FTP_LOG} + log "[$(basename "$0")] The file should content as: 2016Y08M01D13H/23M00S.mp4" ${FTP_LOG} + exit 1 + fi +} + +ftp_mkd() +{ + (sleep 1; + echo "USER $(get_config FTP_USER)"; + sleep 1; + echo "PASS $(get_config FTP_PASS)"; + sleep 1; + echo "MKD $(get_config FTP_DIR)/$1"; + sleep 1; + echo "QUIT"; + sleep 1 ) | telnet $(get_config FTP_HOST) $(get_config FTP_PORT) >> $FTP_LOG 2>&1 +} + +ftp_upload() +{ + from_f=$1 + to_f=$2 + ftpput -u $(get_config FTP_USER) -p $(get_config FTP_PASS) -P $(get_config FTP_PORT) \ + $(get_config FTP_HOST) $(get_config FTP_DIR)/${to_f} ${from_f} >> $FTP_LOG 2>&1 + return $? +} + + +main() +{ + last_folder="" + last_file="" + + # Here we goooooo! + is_server_live $(get_config FTP_HOST) + if [ $? -ne 0 ]; then + log "[$NAME] $(get_config FTP_HOST) is unreachable!!!" ${FTP_LOG} + pid_clear $PID_FILE + exit 1 + fi + log "[$NAME] $(get_config FTP_HOST) is reachable" ${FTP_LOG} + + mem_get $FTP_MEM_FILE + log "[$NAME] last folder: $last_folder last file: $last_file" ${FTP_LOG} + + last_y=$(echo $last_folder | cut -d'Y' -f1) + last_m=$(echo $last_folder | cut -d'M' -f1 | cut -d'Y' -f2) + last_d=$(echo $last_folder | cut -d'D' -f1 | cut -d'M' -f2) + last_h=$(echo $last_folder | cut -d'H' -f1 | cut -d'D' -f2) + last_i=$(echo $last_file | cut -d'M' -f1) + last_s=$(echo $last_file | cut -d'S' -f1 | cut -d'M' -f2) + + now_h=$(date +"%H") + now_m=$(date +"%m") + now_d=$(date +"%d") + now_y=$(date +"%Y") + + cont_last=1 + is_leap_year last_y + if [ $? -eq 0 ]; then + max_d02=29 + fi + + while [ 1 -eq 1 ]; do + if [ -d "${DEFAULT_RECORD_DIR}${last_folder}" ]; then + cd "${DEFAULT_RECORD_DIR}${last_folder}" + list_file=$(ls) + if [ -n "$list_file" ]; then + log "[$NAME] Create ${last_folder}" ${FTP_LOG} + # Make dir of FTP again to ensure it exists + ftp_mkd ${last_folder} + if [ $cont_last -eq 1 ]; then + # Use current last_i and last_s + cont_last=0 + else + last_i="00" + last_s="00" + fi + + fi + for file in $list_file; do + #log $file + check_offline_duration + if [ $(echo $file | grep tmp | wc -l) -gt 0 ]; then + log "[$NAME] Skip tmp file" ${FTP_LOG} + continue + fi + this_i=$(echo $file | cut -d'M' -f1) + this_s=$(echo $file | cut -d'S' -f1 | cut -d'M' -f2) + if [ "${this_i}${this_s}" -gt "${last_i}${last_s}" ]; then + log "[$NAME] Uploading ${last_folder}/${file}" ${FTP_LOG} + ftp_upload ${DEFAULT_RECORD_DIR}/${last_folder}/${file} ${last_folder}/${file} + upload_res=$? + mem_store ${last_folder} ${file} ${FTP_MEM_FILE} + if [ $upload_res -ne 0 ]; then + log "[$NAME] FAILED" ${FTP_LOG} + exit 1 + fi + last_file=$file + fi + done + fi + # If last_h between 01 to 09 then remove leading 0 for calculation + if [ $(expr match "$last_h" '0*') -gt 0 ]; then + last_h=${last_h:1} + fi + last_h=$(printf %02d $((last_h + 1))) + if [ $last_h -gt 23 ]; then + last_h=00 + if [ $(expr match "$last_d" '0*') -gt 0 ]; then + last_d=${last_d:1} + fi + last_d=$(printf %02d $((last_d + 1))) + fi + eval max_d='$max_d'$last_m + if [ $last_d -gt $max_d ]; then + last_d=01 + if [ $(expr match "$last_m" '0*') -gt 0 ]; then + last_m=${last_m:1} + fi + last_m=$(printf %02d $((last_m + 1))) + fi + if [ $last_m -gt 12 ]; then + last_m=01 + last_y=$((last_y + 1)) + is_leap_year $last_y + if [ $? -eq 0 ]; then + max_d02=29 + else + max_d02=28 + fi + fi + if [ "${last_y}${last_m}${last_d}${last_h}" -gt "${now_y}${now_m}${now_d}${now_h}" ]; then + # Nothing more to do, break the loop + break + fi + last_folder="${last_y}Y${last_m}M${last_d}D${last_h}H" + log "[$NAME] Next folder: $last_folder" ${FTP_LOG} + done + pid_clear $PID_FILE +} + +# +# Start the main script +# + +# Check offline duration at beginning +check_offline_duration + +is_server_live $(get_config FTP_HOST) +if [ $? -ne 0 ]; then + log "[$NAME] Unreach FTP server $(get_config FTP_HOST)" ${FTP_LOG} + exit 0 +fi + +# If pass all above check, start the FTP upload +last_pid=$(pid_get $PID_FILE) + +if [ -n "$last_pid" ]; then + is_pid_exist "$last_pid.*ftp_upload" + if [ $? -eq 0 ]; then + exit 0 + else + log "[$NAME] $last_pid is not existed. Start new" ${FTP_LOG} + pid_clear $PID_FILE + fi +fi + +main & + +pid_store $! $PID_FILE + diff --git a/sd/test/scripts/ftp_upload/log.txt b/sd/test/scripts/ftp_upload/log.txt new file mode 100755 index 0000000..e69de29 diff --git a/sd/test/scripts/housekeeper/housekeeper.pid b/sd/test/scripts/housekeeper/housekeeper.pid new file mode 100755 index 0000000..e69de29 diff --git a/sd/test/scripts/housekeeper/housekeeper.sh b/sd/test/scripts/housekeeper/housekeeper.sh new file mode 100755 index 0000000..ef17c57 --- /dev/null +++ b/sd/test/scripts/housekeeper/housekeeper.sh @@ -0,0 +1,100 @@ +#!/bin/sh + +source "/tmp/hd1/test/scripts/ftp_upload/common_lib.sh" + +NAME=`basename "$0"` +HK_LOG="/tmp/hd1/test/scripts/housekeeper/log.txt" +PID_FILE="/tmp/hd1/test/scripts/housekeeper/housekeeper.pid" +UNREACH_FILE="/tmp/hd1/test/scripts/housekeeper/unreach_count.mem" +LAST_CLEANDAY_FILE="/tmp/hd1/test/scripts/housekeeper/last_record_cleanup.mem" + +check_gw_to_alert() +{ + retry_time=$(get_config RETRY_TO_ALERT) + ur_count=$(unreach_get $UNREACH_FILE) + if [ $ur_count -eq $retry_time ]; then + log "[$NAME] Reach retry $retry_time time reboot $retry_time. Reset and reboot." $HK_LOG + unreach_reset $UNREACH_FILE + led -boff -yfast + return 1 + fi + + is_server_live $(get_config GATEWAY) + if [ $? -ne 0 ]; then + log "[$NAME] Unreach gateway, increase unreach count" $HK_LOG + unreach_increase $UNREACH_FILE + return 1 + fi + + # Pass unreach gateway check, so reset it + log "[$NAME] Check unreach pass. Reset unreach count" $HK_LOG + unreach_reset $UNREACH_FILE + led $(get_config LED_WHEN_READY) + return 0 +} + +cleanup_record() +{ + last_day=$(cat $LAST_CLEANDAY_FILE) + if [ ! -z "$last_day" ]; then + today=$(date +'%Y%m%d') + if [ $today -eq $last_day ]; then + log "[$NAME] Today cleanup check done" $HK_LOG + return 0 + fi + fi + echo $today > $LAST_CLEANDAY_FILE + + number_keep_day=$(get_config RECORD_KEEP_DAYS) + keep_date=$(date -D %s -d $(( $(date +%s) - ((86400 * $number_keep_day)) )) \ + +'%Y%m%d') + log "[$NAME] Keep record until $keep_date" $HK_LOG + for item in $(ls -l /tmp/hd1/record | awk '{print $9}'); do + log "[$NAME] Work on $item" $HK_LOG + if [ $(echo $item | egrep '.*Y.*M.*D.*H.*' | wc -l) -gt 0 ]; then + y=$(echo $item | cut -d'Y' -f1) + m=$(echo $item | cut -d'M' -f1 | cut -d'Y' -f2) + d=$(echo $item | cut -d'D' -f1 | cut -d'M' -f2) + if [ "${y}${m}${d}" -lt "${keep_date}" ]; then + log "[$NAME] Delete folder $item" $HK_LOG + rm -Rf $item + else + log "[$NAME] Stop at $item" $HK_LOG + break + fi + fi + done +} + +main() +{ + check_offline_duration + if [ $? -eq 0 ]; then + log "[$NAME] Check gw and alert." $HK_LOG + check_gw_to_alert + fi + log "[$NAME] Cleanup records" $HK_LOG + cleanup_record + pid_clear $PID_FILE +} + +# +# Start the main script +# + +last_pid=$(pid_get $PID_FILE) + +if [ -n "$last_pid" ]; then + is_pid_exist "$last_pid.*ftp_upload" + if [ $? -eq 0 ]; then + exit 0 + else + log "[$NAME] $last_pid is not existed. Start new" ${FTP_LOG} + pid_clear $PID_FILE + fi +fi + +main & + +pid_store $! $PID_FILE + diff --git a/sd/test/scripts/housekeeper/last_record_cleanup.mem b/sd/test/scripts/housekeeper/last_record_cleanup.mem new file mode 100755 index 0000000..162675c --- /dev/null +++ b/sd/test/scripts/housekeeper/last_record_cleanup.mem @@ -0,0 +1 @@ +20161220 diff --git a/sd/test/scripts/housekeeper/log.txt b/sd/test/scripts/housekeeper/log.txt new file mode 100755 index 0000000..e69de29 diff --git a/sd/test/scripts/housekeeper/unreach_count.mem b/sd/test/scripts/housekeeper/unreach_count.mem new file mode 100755 index 0000000..573541a --- /dev/null +++ b/sd/test/scripts/housekeeper/unreach_count.mem @@ -0,0 +1 @@ +0 diff --git a/sd/test/yi-hack.cfg b/sd/test/yi-hack.cfg index d4bb3b1..8a6f851 100644 --- a/sd/test/yi-hack.cfg +++ b/sd/test/yi-hack.cfg @@ -40,3 +40,30 @@ NTP_SERVER=0.uk.pool.ntp.org # Values : yes|no DEBUG=no +### FTP server for uploading recorded files +# - IP address of FTP server +# - Port, keep it default if you didn't have special FTP server port +# - FTP username +# - FTP password +# - Folder in home folder of user on FTP server. +FTP_HOST=192.168.1.1 +FTP_PORT=21 +FTP_USER=ftp_username +FTP_PASS=ftp_password +FTP_DIR=camera1 + +### Gateway offline duaration +# If you schedule your router to offline (during midnight for example). +# - GW_OFF_START: Start time of offline duration. Ex: 23:00 +# - GW_OFF_END: End time of offline duration. Ex: 02:00 +# +# Please use 24h format. In your timezone. +# Let them blank if not use. +GW_OFF_START= +GW_OFF_END= + +### Housekeeper option +# - RETRY_TO_ALERT: Number of unreachable ping to Gateway before alert by yellow flash +# - RECORD_KEEP_DAYS: Number days that recorded videos are kept. Older ones are deleted. +RETRY_TO_ALERT=10 +RECORD_KEEP_DAYS=7