-
Notifications
You must be signed in to change notification settings - Fork 204
/
helperFunctions.sh
executable file
·115 lines (110 loc) · 3.13 KB
/
helperFunctions.sh
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
#!/bin/bash
openwbModulePublishState() {
# $1: Modultyp (EVU, LP, EVSOC, PV, BAT)
# $2: Status (0=Ok, 1=Warning, 2=Error)
# $3: Meldung (String)
# $4: Index (bei LP und PV und EVSOC)
case $1 in
"EVU")
if (( $# != 3 )); then
echo "openwbPublishStatus: Wrong number of arguments: EVU $#"
else
mosquitto_pub -t "openWB/set/evu/faultState" -r -m "$2"
mosquitto_pub -t "openWB/set/evu/faultStr" -r -m "$3"
fi
;;
"LP")
if (( $# != 4 )); then
echo "openwbPublishStatus: Wrong number of arguments: LP $#"
else
mosquitto_pub -t "openWB/set/lp/${4}/faultState" -r -m "$2"
mosquitto_pub -t "openWB/set/lp/${4}/faultStr" -r -m "$3"
fi
;;
"EVSOC")
if (( $# != 4 )); then
echo "openwbPublishStatus: Wrong number of arguments: EVSOC $#"
else
mosquitto_pub -t "openWB/set/lp/${4}/socFaultState" -r -m "$2"
mosquitto_pub -t "openWB/set/lp/${4}/socFaultStr" -r -m "$3"
fi
;;
"PV")
if (( $# != 4 )); then
echo "openwbPublishStatus: Wrong number of arguments: PV $#"
else
mosquitto_pub -t "openWB/set/pv/${4}/faultState" -r -m "$2"
mosquitto_pub -t "openWB/set/pv/${4}/faultStr" -r -m "$3"
fi
;;
"BAT")
if (( $# != 3 )); then
echo "openwbPublishStatus: Wrong number of arguments: BAT $#"
else
mosquitto_pub -t "openWB/set/houseBattery/faultState" -r -m "$2"
mosquitto_pub -t "openWB/set/houseBattery/faultStr" -r -m "$3"
fi
;;
*)
echo "openwbPublishStatus: Unknown module type: $1"
;;
esac
}
export -f openwbModulePublishState
openwbDebugLog() {
# $1: Channel (MAIN=default, EVSOC, PV, MQTT, RFID, SMARTHOME, CHARGESTAT)
# $2: Level (0=Info, 1=Regelwerte , 2=Berechnungsgrundlage)
# $3: Meldung (String)
LOGFILE="/var/log/openWB.log"
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
if [[ -z "$debug" ]]; then
# enable all levels as global $debug is not set up yet
DEBUGLEVEL=2
else
DEBUGLEVEL=$debug
fi
# echo "LVL: $2 DEBUG: $debug DEBUGLEVEL: $DEBUGLEVEL" >> $LOGFILE
if (( $2 <= DEBUGLEVEL )); then
case $1 in
"EVSOC")
LOGFILE="/var/www/html/openWB/ramdisk/soc.log"
;;
"PV")
LOGFILE="/var/www/html/openWB/ramdisk/nurpv.log"
;;
"MQTT")
LOGFILE="/var/www/html/openWB/ramdisk/mqtt.log"
;;
"RFID")
LOGFILE="/var/www/html/openWB/ramdisk/rfid.log"
;;
"SMARTHOME")
LOGFILE="/var/www/html/openWB/ramdisk/smarthome.log"
;;
"CHARGESTAT")
LOGFILE="/var/www/html/openWB/ramdisk/ladestatus.log"
;;
*)
# MAIN
LOGFILE="/var/log/openWB.log"
;;
esac
if (( DEBUGLEVEL > 0 )); then
echo "$timestamp: PID: $$: $3 (LV$2) at $(caller 0)" >> $LOGFILE
else
echo "$timestamp: PID: $$: $3 (LV$2)" >> $LOGFILE
fi
fi
}
export -f openwbDebugLog
openwbRunLoggingOutput() {
$1 2>&1 | while read -r line
do
# use path to /var/log as link in ramdisk may not be set up yet!
echo "$(date +"%Y-%m-%d %H:%M:%S"): $1: $line" >> "/var/log/openWB.log"
done
}
export -f openwbRunLoggingOutput
# Enable all python scripts to import from the "package"-directory without fiddling with sys.path individually
SCRIPT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
export PYTHONPATH="$SCRIPT_DIR/packages"