#!/bin/sh #---------------------------------------------------------------------------------------- # /var/install/bin/owswitch-set-output - set output of owswitch pio port # # Copyright (c) 2016-2019 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2016-06-13 jed # Last Update: $Id$ # # Usage: owswitch-set-output [options] - show available output sensors and the # pio-port stati # # Options: --debug - enable debug trace # # --seton "sensor-id:pio-port:... [sensor-id:pio-port:...] ..." # - set pio-port to 'on' (1) # --setoff "sensor-id:pio-port:... [sensor-id:pio-port:...] ..." # - set pio-port to 'off' (0) # --toggle sensor-id:pio-port:... [sensor-id:pio-port:...] ..." # - toggle pio-port from 'on' to 'off' (1->0) # or from 'off' to 'on' (0->1) # # Attention: 1. if multiple pio-ports of a sensor should be set at the same # time, you need to separate each pio-port number by a colon. # # 2. If multiple sensors should be set at the same time, you need # to set all into quote marks and separate each one by a whitespace. # # Sensor: DS2413 - pio status: 0 = conducting (on), 1 = non-conducting (off) # # 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 . /var/install/include/eislib #---------------------------------------------------------------------------------------- # 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 mecho ${_me_switch} "${_me_outstr}" fi } #---------------------------------------------------------------------------------------- # check if owfs has been enabled # # input : [--quiet] - be quiet, suppress all output # # return: 0 - owfs package enabled # 1 - owfs package disabled #---------------------------------------------------------------------------------------- check_installed_owfs () { _cio_retval=1 if [ -f ${owfsfile} ] then # owfs installed . ${owfsfile} if [ "${START_OWFS}" = 'yes' -a "${OWFS_SERVER_OWFS}" = 'yes' ] then # owfs activated if [ "${1}" != '--quiet' ] then myecho "OWFS server has been enabled ..." fi _cio_retval=0 else # owfs deactivated if [ "${1}" != '--quiet' ] then myecho --warn "OWFS server has been disabled ..." fi fi fi return ${_cio_retval} } #---------------------------------------------------------------------------------------- # check if sensors are reachable # # input: $1 - list of sensors #---------------------------------------------------------------------------------------- check_sensor_reachability () { myecho "checking sensor stati ..." _csr_sensors="$1" _csr_idx=1 _csr_sensors=':' _csr_warning=0 if [ -z "${_csr_sensors}" ] then # check if requested sensors are reachable for SENSOR in ${_csr_sensors} do _csr_sensorid=`echo "${SENSOR}" | cut -d: -f1` owread ${_csr_sensorid}/type >/dev/null 2>/dev/null if [ $? -ne 0 ] then _csr_warning=1 myecho --warn "- sensor '${_csr_sensorid}' is currently unreachable!" else _csr_sensors="${_csr_sensors}${_csr_sensorid}:" fi done if [ ${_csr_warning} -eq 0 ] then myecho "- all sensors reachable." fi else while [ ${_csr_idx} -le ${OWSWITCH_N} ] do eval _csr_sensorid='$OWSWITCH_'${_csr_idx}'_ID' owread ${_csr_sensorid}/type >/dev/null 2>/dev/null if [ $? -ne 0 ] then _csr_warning=1 myecho --warn "- sensor '${_csr_sensorid}' is currently unreachable!" fi _csr_sensors="${_csr_sensors}${_csr_sensorid}:" _csr_idx=`expr ${_csr_idx} + 1` done if [ ${_csr_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' _csr_sensorid=`owread ${SENSORADDR} | grep "^3A"` if [ ! -z "${_csr_sensorid}" ] then _csr_sensorid="`echo "${_csr_sensorid}" | sed 's/^\(..\)\(.*\)..$/\1.\2/'`" echo "${_csr_sensors}" | grep -q ":${_csr_sensorid}:" if [ $? -ne 0 ] then myecho --warn "- sensor '${_csr_sensorid}' not configured!" else _csr_sensors="${_csr_sensors}:${_csr_sensorid}" fi fi done if [ ${_csr_warning} -eq 0 ] then myecho "- all sensors configured." fi 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 [ -n "${ow_id}" ] 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' sensor_add_flag=0 case ${pio_func} in input ) eval pio_default='$OWSWITCH_'${idx}'_PIO_'${jdx}'_INPUT_DEFAULT' ;; output ) sensor_add_flag=1 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 jdx=`expr ${jdx} + 1` done # keep only sensors which have at least one pio-output set if [ ${sensor_add_flag} -eq 1 ] then # add sensor id only once echo "${sensor_ids}" | grep -q " ${ow_id} " if [ $? -ne 0 ] then if [ -z "${sensor_ids}" ] then sensor_ids=" ${ow_id} " sensor_stats="${ow_id}[${pio_def_status}]::" else # important: keep the carriage return! sensor_ids="${sensor_ids}${ow_id} " sensor_stats="${sensor_stats} ${ow_id}[${pio_def_status}]::" fi fi fi else echo "error: OWSWITCH_${idx}_ID not set!" fi fi idx=`expr ${idx} + 1` done } #---------------------------------------------------------------------------------------- # monitor sensor stati #---------------------------------------------------------------------------------------- show_sensor_stati() { echo "sensor status/stati:" # echo "SENSOR_IDS:${sensor_ids}" # echo "SENSOR_STATS:${sensor_stats}" # check all sensors for ID in ${sensor_ids} do id_flag=0 # read current sensor stati pio_curr_status=`owread ${ID}/sensed.ALL | tr ',' ' '` # get pio defaults pio_defaults="`echo "${sensor_stats}" | grep "^${ID}\[[01 ion-]*\]:" | cut -d: -f1 | sed 's/^.*\[\(.*\)\].*$/\1/'`" # check if last sensor stati are available pio_temp=`echo "${sensor_stats}" | grep "^${ID}\[[01 ion-]*\]:[01]"` if [ $? -eq 0 ] then # status available pio_prev_status="`echo "${pio_temp}" | cut -d: -f2`" # check stati idx=1 for pio_curr in ${pio_curr_status} do if [ ${id_flag} -eq 0 -a ${idx} -eq 1 ] then sensor_str="ID:${ID}" id_flag=1 else sensor_str=" " fi 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 # 0->1 - enabled # 1->0 - disabled if [ "${pio_prev}" = '0' -a "${pio_curr}" = '1' -o "${pio_prev}" = '1' -a "${pio_curr}" = '0' ] then myecho "${sensor_str} PIO:${idx} FUNC:${function} status change ${pio_prev}->${pio_curr}" /usr/bin/logger -p "local0.warning" -t "owswitch" "ID:${ID} PIO:${idx} FUNC:${function} DEF:${pio_def} status change ${pio_prev}->${pio_curr}" fi # important: keep the carriage return! # status changed, update it sensor_stats="`echo "${sensor_stats}" | grep -v "^${ID}:"` ${ID}:${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 # for pio_curr in ${pio_curr_status} else # stati unavailable, update it 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 [ ${id_flag} -eq 0 -a ${idx} -eq 1 ] then sensor_str="ID:${ID}" id_flag=1 else sensor_str=" " fi 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 myecho "${sensor_str} PIO:${idx} FUNC:${function} DEF:${pio_def} STATUS:${pio_curr} (${pio_curr_text})" idx=`expr ${idx} + 1` done # important: keep the carriage return! sensor_stats="`echo "${sensor_stats}" | grep -v "^${ID}\[[01 ion-]*\]:"` ${ID}[${pio_defaults}]:${pio_curr_status}" fi done # for ID in ${sensor_ids} } #---------------------------------------------------------------------------------------- # set sensor port # # input: $1 - sensor id(s) # $2 - command # on - set port to on-status # off - set port to off-status # toggle - toggle port status #---------------------------------------------------------------------------------------- set_sensor_status() { myecho "set sensor status ..." for SENSOR in $1 do echo "${SENSOR}" | grep -q ':' if [ $? -eq 0 ] then _sss_sensor=`echo "${SENSOR}" | cut -d: -f1` _sss_pios=`echo "${SENSOR}" | cut -d: -f2- | tr ':' ' '` else _sss_sensor="${SENSOR}" _sss_pios='' fi _sss_cmd=$2 echo "${sensor_stats}" | grep -q "^${_sss_sensor}\[" if [ $? -eq 0 ] then # sensor has pio-outputs set _sss_pio_defaults="`echo "${sensor_stats}" | grep "^${_sss_sensor}\[[01 ion-]*\]:" | cut -d: -f1 | sed 's/^.*\[\(.*\)\].*$/\1/'`" for PIO in ${_sss_pios} do if [ ! -z "${PIO}" ] then _sss_pio_status=`echo "${_sss_pio_defaults}" | cut -d' ' -f${PIO}` if [ "${_sss_pio_status}" = 'o' ] then # pio-port is an output, read sensor status _sss_pio_prev_status=`owread ${_sss_sensor}/sensed.ALL | tr ',' ' ' | cut -d' ' -f${PIO}` if [ "${_sss_pio_prev_text}" = '0' ] then _sss_pio_prev_text='on' else _sss_pio_prev_text='off' fi myecho "- ID:${_sss_sensor} PIO:${PIO} STATUS:${_sss_pio_prev_status} (${_sss_pio_prev_text})" # ASCII 65 -> A because pio-ports are named PIO.A, PIO.B, etc. _sss_ascii=`expr 64 + ${PIO}` _sss_char=`echo ${_sss_ascii} | awk '{printf("%c",$1)}'` # DS2413 - pio status: 0 = conducting (on) # 1 = non-conducting (off) case ${_sss_cmd} in on ) if [ "${_sss_pio_prev_status}" = '1' ] then # 1->0 owwrite ${_sss_sensor}/PIO.${_sss_char} on fi ;; off ) if [ "${_sss_pio_prev_status}" = '0' ] then # 0->1 owwrite ${_sss_sensor}/PIO.${_sss_char} off fi ;; toggle ) if [ "${_sss_pio_prev_status}" = '1' ] then # 1->0 owwrite ${_sss_sensor}/PIO.${_sss_char} on else # 0->1 owwrite ${_sss_sensor}/PIO.${_sss_char} off fi ;; esac # read current sensor status _sss_pio_curr_status=`owread ${_sss_sensor}/sensed.ALL | tr ',' ' ' | cut -d' ' -f${PIO}` if [ "${_sss_pio_curr_status}" != "${_sss_pio_prev_status}" ] then myecho " status changed ${_sss_pio_prev_status}->${_sss_pio_curr_status}" else myecho " status unchanged" fi else # pio-port is not an output case ${_sss_pio_status} in 0|1 ) _sss_function='input' ;; n|- ) _sss_function='none' ;; ' ' ) ;; esac if [ -z "${_sss_pio_status}" ] then myecho --error "ID:${_sss_sensor} PIO:${PIO} port doesn't exist" else myecho --error "ID:${_sss_sensor} PIO:${PIO} (${_sss_function}) not an output" fi fi fi done # for PIO in ${_sss_pios} else myecho --error "ID:${_sss_sensor} not found or no PIO output set!" fi done # for SENSOR in $1 } #======================================================================================== # main #======================================================================================== ask_debug=false verbose=true debug_trace=false #debug_trace=true quiet=false sensors='' while [ $# -ge 1 ] do case $1 in --debug ) debug_trace=true shift ;; --seton ) cmd='on' # '3A.698402010800:2' -> id: 3A.698402010800, pio: 2 sensors=`echo "$2" | sed -e 's/^ *//' -e 's/ *$//' | tr '[:lower:]' '[:upper:]'` shift; shift ;; --setoff ) cmd='off' # '3A.698402010800:2' -> id: 3A.698402010800, pio: 2 sensors=`echo "$2" | sed -e 's/^ *//' -e 's/ *$//' | tr '[:lower:]' '[:upper:]'` shift; shift ;; --toggle ) cmd='toggle' # '3A.698402010800:2' -> id: 3A.698402010800, pio: 2 sensors=`echo "$2" | sed -e 's/^ *//' -e 's/ *$//' | tr '[:lower:]' '[:upper:]'` shift; 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 verbose=true fi module_name='owswitch' pgmname=`basename $0` configfile=/etc/config.d/${module_name} owfsfile=/etc/config.d/owfs . ${configfile} if [ "${START_OWSWITCH}" = 'yes' ] then OWFS_INSTALLED='no' if check_installed_owfs then # installed OWFS_INSTALLED='yes' check_sensor_reachability "${sensors}" read_sensor_stati case ${cmd} in on|off|toggle ) set_sensor_status "${sensors}" ${cmd} ;; * ) # display port stati show_sensor_stati ;; esac fi fi #======================================================================================== # end #======================================================================================== exit 0