#!/bin/sh #---------------------------------------------------------------------------------------- # /var/install/bin/owswitch-status-monitor - owSwitch status monitor # # Copyright (c) 2016-2019 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2016-06-13 jed # Last Update: $Id$ # # Usage: owswitch-status-monitor [options] # # Options: --background - run script in background. # --debug - activate debug trace. # --help - show this help. # --interval - set refresh interval, default: 0.5s. # --noscreencheck - don't check the current screen size. # --quiet - suppress all screen output. # --snap - only check sensor status once. # # --sensors "sensor-id-1 sensor-id-2 ...² - list of sensor ids # (only for debug purposes) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------------------- # read eislib etc. . /var/install/include/eislib . /var/install/include/jedlib #---------------------------------------------------------------------------------------- # my echo #---------------------------------------------------------------------------------------- myecho () { case $1 in *-std ) _me_switch=$1 shift _me_outstr="`echo "$*" | sed -r 's/^(-)?-std //g'`" ;; *-info ) _me_switch=$1 shift _me_outstr="`echo "$*" | sed -r 's/^(-)?-info //g'`" ;; *-warn ) _me_switch=$1 shift _me_outstr="`echo "$*" | sed -r 's/^(-)?-warn //g'`" ;; *-error ) _me_switch=$1 shift _me_outstr="`echo "$*" | sed -r 's/^(-)?-error //g'`" ;; * ) _me_switch='' _me_outstr="$*" ;; esac if [ "${verbose}" = 'true' ] then # be verbose if [ -z "${_me_switch}" ] then _me_outstr_len=`expr length "${_me_outstr}"` if [ ${_me_outstr_len} -gt ${maxcolnum} ] then _me_outstr_len=${maxcolnum} fi if [ ${_me_outstr_len} -eq 0 ] then echo else echo "${_me_outstr}" | cut -c1-${_me_outstr_len} fi else mecho ${_me_switch} "${_me_outstr}" fi fi } #---------------------------------------------------------------------------------------- # check if owfs has been enabled # # input : $1 - 'quiet' be quiet, suppress all output # # return: 0 - owfs package enabled # 1 - owfs package disabled #---------------------------------------------------------------------------------------- check_installed_owfs () { retval=1 if [ -f ${owfsfile} ] then # owfs installed . ${owfsfile} if [ "${START_OWFS}" = 'yes' -a "${OWFS_SERVER_OWFS}" = 'yes' ] then # owfs activated myecho "OWFS server has been enabled ..." retval=0 else # owfs deactivated myecho --warn "OWFS server has been disabled ..." fi fi return ${retval} } #---------------------------------------------------------------------------------------- # check sensor availability #---------------------------------------------------------------------------------------- check_sensor_status () { myecho "checking sensor stati ..." # check if configured sensors are reachable _css_idx=1 _css_sensors=':' _css_warning=0 while [ ${_css_idx} -le ${OWSWITCH_N} ] do eval _css_sensorid='$OWSWITCH_'${_css_idx}'_ID' owread ${_css_sensorid}/type >/dev/null 2>/dev/null if [ $? -ne 0 ] then _css_warning=1 myecho --warn "- sensor '${_css_sensorid}' is currently unreachable!" else _css_sensors="${_css_sensors}${_css_sensorid}:" fi _css_idx=`expr ${_css_idx} + 1` done if [ ${_css_warning} -eq 0 ] then myecho "- all sensors reachable." fi # check if available sensors have been configured eval `grep "OWFS_SERVER_OWFS_MOUNT" ${owfsfile}` _css_warning=0 for SENSORADDR in `find ${OWFS_SERVER_OWFS_MOUNT} -maxdepth 2 -name "address" | sed "s#^${OWFS_SERVER_OWFS_MOUNT}/##"` do # '3A698402010800A0' -> '3A.698402010800' _css_sensorid=`owread ${SENSORADDR} | grep "^3A"` if [ ! -z "${_css_sensorid}" ] then _css_sensorid="`echo "${_css_sensorid}" | sed 's/^\(..\)\(.*\)..$/\1.\2/'`" echo "${_css_sensors}" | grep -q ":${_css_sensorid}:" if [ $? -ne 0 ] then myecho --warn "- sensor '${_css_sensorid}' not configured!" _css_sensors="${_css_sensors}:${_css_sensorid}" fi fi done if [ ${_css_warning} -eq 0 ] then myecho "- all available sensors configured." fi } #---------------------------------------------------------------------------------------- # read sensor stati #---------------------------------------------------------------------------------------- read_sensor_stati() { idx=1 sensor_ids='' sensor_stats='' while [ ${idx} -le ${OWSWITCH_N} ] do # check for active sensor eval ow_active='$OWSWITCH_'${idx}'_ACTIVE' if [ "${ow_active}" = 'yes' ] then # read sensor id # sensor id, e.g. 3A.3CF813000000 eval ow_id='$OWSWITCH_'${idx}'_ID' if [ ! -z "${ow_id}" ] then # check if sensor is available owread ${ow_id}/type >/dev/null 2>/dev/null if [ $? -eq 0 ] then # read default input values jdx=1 pio_def_status='' while [ ${jdx} -le ${OWSWITCH_1_PIO_N} ] do # pio function: input, output or none # pio input default eval pio_func='$OWSWITCH_'${idx}'_PIO_'${jdx}'_FUNC' eval pio_name='$OWSWITCH_'${idx}'_PIO_'${jdx}'_NAME' case ${pio_func} in input|input-uncached ) eval pio_default='$OWSWITCH_'${idx}'_PIO_'${jdx}'_INPUT_DEFAULT' ;; output|output-uncached ) pio_default='o' ;; none ) pio_default='n' ;; esac if [ -z "${pio_def_status}" ] then pio_def_status="${pio_default}" else pio_def_status="${pio_def_status} ${pio_default}" fi if [ -n "${pio_name}" ] then # '3A698402010800A0' -> 'S3A_698402010800' export S`echo "${ow_id}" | sed 's/\.//'`_${jdx}="${pio_name}" fi jdx=`expr ${jdx} + 1` done jdx=1 while [ ${jdx} -le ${OWSWITCH_1_PIO_N} ] do # pio function: input, input-uncached, output, output-uncached or none eval pio_func='$OWSWITCH_'${idx}'_PIO_'${jdx}'_FUNC' case ${pio_func} in none ) ;; input|input-uncached|output|output-uncached ) # add sensor id only once echo "${sensor_ids}" | grep -q " ${ow_id}[: ]" if [ $? -ne 0 ] then # sensor not on list case ${pio_func} in input-uncached|output-uncached ) ow_id_add="${ow_id}:u" ;; * ) ow_id_add="${ow_id}" ;; esac if [ -z "${sensor_ids}" ] then sensor_ids=" ${ow_id_add} " sensor_stats="${ow_id}[${pio_def_status}]::" else # important: keep the carriage return! sensor_ids="${sensor_ids}${ow_id_add} " sensor_stats="${sensor_stats} ${ow_id}[${pio_def_status}]::" fi fi ;; * ) # invalid choice ;; esac jdx=`expr ${jdx} + 1` done # else # echo "warning: sensor '${ow_id}' is currently unreachable!" fi else echo "error: OWSWITCH_${idx}_ID not set!" fi fi idx=`expr ${idx} + 1` done } #---------------------------------------------------------------------------------------- # monitor sensor stati # input: $1 - '--snap' - enable snap mode # $2 - whitespace separated list if sensor-ids #---------------------------------------------------------------------------------------- monitor_sensor_stati() { snap=0 if [ -n "$1" ] then if [ "$1" = '--snap' ] then snap=1 if [ -n "$2" ] then sensor_ids=" $2 " fi else sensor_ids=" $1 " fi fi # remove outdated run marker # rm -f ${cmdfile}-3A\.[0-9A-Z]*-[0-9]*-on.run # rm -f ${cmdfile}-3A\.[0-9A-Z]*-[0-9]*-off.run # check all sensors for ID in ${sensor_ids} do ow_id_flag=0 # print sensor id only once ow_id_uncached=0 # read 'cached' sensor status # read current sensor stati echo "${ID}" | grep -q ":u$" if [ $? -eq 0 ] then # read 'uncached' stati ow_id=`echo "${ID}" | cut -d: -f1` ow_id_uncached=1 pio_curr_status=`owread uncached/${ow_id}/sensed.ALL | tr ',' ' '` else # read 'cached' stati ow_id="${ID}" pio_curr_status=`owread ${ow_id}/sensed.ALL | tr ',' ' '` fi # check if last sensor stati are available pio_defaults="`echo "${sensor_stats}" | grep "^${ow_id}\[[01 ion-]*\]:" | cut -d: -f1 | sed 's/^.*\[\(.*\)\].*$/\1/'`" pio_temp=`echo "${sensor_stats}" | grep "^${ow_id}\[[01 ion-]*\]:[01]"` if [ $? -eq 0 ] then # stati available pio_prev_status="`echo "${pio_temp}" | cut -d: -f2`" # check stati idx=1 for pio_curr in ${pio_curr_status} do if [ ${ow_id_flag} -eq 0 -a ${idx} -eq 1 ] then sensor_str="ID:${ow_id}" ow_id_flag=1 else sensor_str=" " fi sensor_var_name="`echo "S${ow_id}" | sed 's/\.//'`_${idx}" eval pio_name="\$${sensor_var_name}" pio_def=`echo "${pio_defaults}" | cut -d' ' -f${idx}` case ${pio_def} in 0|1 ) function='in ' ;; n ) function='none' ;; o ) function='out ' ;; esac pio_prev=`echo "${pio_prev_status}" | cut -d' ' -f${idx}` if [ "${pio_curr}" != "${pio_prev}" ] then if [ "${pio_prev}" = '0' -a "${pio_curr}" = '1' ] then # 0->1 - enabled myecho "${sensor_str} PIO:${idx} FUNC:${function} status change 0->1 DESC:${pio_name}" /usr/bin/logger -p "local0.warning" -t "owswitch" "ID:${ow_id} PIO:${idx} FUNC:${function} DEF:${pio_def} status change 0->1 DESC:${pio_name}" rm -f ${cmdfile}-${ow_id}-${idx}-off.run if [ -f ${cmdfile}-${ow_id}-${idx}-on -a ! -f ${cmdfile}-${ow_id}-${idx}-on.run ] then # run command myecho "EXEC: ${cmdfile}-${ow_id}-${idx}-on" /bin/sh ${cmdfile}-${ow_id}-${idx}-on > ${cmdfile}-${ow_id}-${idx}-on.output if [ $? -eq 0 ] then # set run marker to prevent, repeating command execution touch ${cmdfile}-${ow_id}-${idx}-on.run fi fi elif [ "${pio_prev}" = '1' -a "${pio_curr}" = '0' ] then # 1->0 - disabled myecho "${sensor_str} PIO:${idx} FUNC:${function} DEF:${pio_def} status change 1->0 DESC:${pio_name}" /usr/bin/logger -p "local0.warning" -t "owswitch" "ID:${ow_id} PIO:${idx} FUNC:${function} DEF:${pio_def} status change 1->0 DESC:${pio_name}" rm -f ${cmdfile}-${ow_id}-${idx}-on.run if [ -f ${cmdfile}-${ow_id}-${idx}-off -a ! -f ${cmdfile}-${ow_id}-${idx}-off.run ] then # run command myecho "EXEC: ${cmdfile}-${ow_id}-${idx}-off" /bin/sh ${cmdfile}-${ow_id}-${idx}-off > ${cmdfile}-${ow_id}-${idx}-off.output if [ $? -eq 0 ] then # set run marker to prevent, repeating command execution touch ${cmdfile}-${ow_id}-${idx}-off.run fi fi fi # important: keep the carriage return! # status changed, update it sensor_stats="`echo "${sensor_stats}" | grep -v "^${ow_id}\[[01 ion-]*\]:"` ${ow_id}[${pio_defaults}]:${pio_curr_status}" else # status unchanged myecho "${sensor_str} PIO:${idx} FUNC:${function} DEF:${pio_def} status unchanged ${pio_curr}" fi idx=`expr ${idx} + 1` done else # stati unavailable idxmax=`echo "${pio_curr_status}" | awk 'BEGIN{FS=" "} {print NF}'` idx=1 while [ ${idx} -le ${idxmax} ] do pio_def=`echo "${pio_defaults}" | cut -d' ' -f ${idx}` pio_curr=`echo "${pio_curr_status}" | cut -d' ' -f${idx}` if [ ${ow_id_flag} -eq 0 -a ${idx} -eq 1 ] then sensor_str="ID:${ow_id}" ow_id_flag=1 else sensor_str=" " fi sensor_var_name="`echo "S${ow_id}" | sed 's/\.//'`_${idx}" eval pio_name="\$${sensor_var_name}" case ${pio_def} in 0|1 ) function='in ' ;; n ) function='none' ;; o ) function='out ' ;; esac if [ "${pio_curr}" = '0' ] then pio_curr_text='(on) ' else pio_curr_text='(off)' fi if [ ${snap} -eq 1 ] then # length 49 characters myecho "${sensor_str} PIO:${idx} FUNC:${function} DEF:${pio_def} STATUS:${pio_curr} ${pio_curr_text} DESC:${pio_name}" else myecho "${sensor_str} PIO:${idx} FUNC:${function} DEF:${pio_def} initialized ${pio_curr} ${pio_curr_text} DESC:${pio_name}" fi idx=`expr ${idx} + 1` done # important: keep the carriage return! # stati unavailable, set initial values sensor_stats="`echo "${sensor_stats}" | grep -v "^${ow_id}\[[01 ion-]*\]:"` ${ow_id}[${pio_defaults}]:${pio_curr_status}" fi done } #======================================================================================== # main #======================================================================================== module_name='owswitch' pgmname=`basename $0` ask_debug=false background=false debug_trace=false #debug_trace=true screencheck=true snapstatus=false verbose=true interval=0.5 while [ $# -ge 1 ] do case $1 in --background ) background=true shift ;; --debug ) debug_trace=true shift ;; --help|--?|/? ) echo echo "Usage: `basename $0` [--debug][--quiet][--snap]" echo echo ' --debug - activate debug trace' echo ' --help - show this help' echo " --quiet - don't show any screen output" echo ' --snap - only check sensor status once' echo shift ;; --interval ) interval=$2 shift; shift ;; --noscreencheck ) screencheck=false shift ;; --sensors ) sensors="$2" shift ;; --snap ) snapstatus=true shift ;; --quiet ) verbose=false shift ;; * ) shift ;; esac done # activate debug output if ${debug_trace:-false} then exec 2> /tmp/$(basename ${0})-trace-$$.log set -x ask_debug=true export ask_debug fi if [ "${screencheck}" != 'true' ] then # set value != 'tty' to suppress check of screen size _EISLIB_PRINTMODE='suppress' fi # check if screensize handling is available if [ -n "${_EISLIB_SCREENSIZE_X}" ] then if check_screensize then maxcolnum=`expr ${_EISLIB_SCREENSIZE_X} - 1` true else mecho --info "Return to calling script" exit 1 fi else # default if screensize handling is not available maxcolnum=79 fi configfile=/etc/config.d/${module_name} owfsfile=/etc/config.d/owfs cmdfile=/var/lib/owswitch/owswitch-cmd # sensor-id, pio-port, etc. will be added . ${configfile} if [ "${START_OWSWITCH}" = 'yes' ] then myecho '--- START --------------------------------------------------' OWFS_INSTALLED='no' if ${snapstatus:-false} then clrhome myecho --info 'Show sensor status/stati' myecho fi if check_installed_owfs then # installed OWFS_INSTALLED='yes' # myecho '--- CHECK SENSOR STATI -------------------------------------' check_sensor_status # myecho '--- READ PARAMS --------------------------------------------' read_sensor_stati if ${snapstatus} then # show sensor stati once myecho 'current stati ...' myecho monitor_sensor_stati --snap "${sensors}" myecho anykey elif ${background} then ( while [ 1 ] do monitor_sensor_stati "${sensors}" sleep ${interval} done ) & else # myecho "SENSOR_IDS :${sensor_ids}" # myecho "SENSOR_STATS: ${sensor_stats}" # monitor sensor stati continuesly myecho '--- MONITOR STATI ------------------------------------------' while [ 1 ] do monitor_sensor_stati "${sensors}" sleep ${interval} myecho '------------------------------------------------------------' done fi fi fi #======================================================================================== # end #======================================================================================== exit 0