-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_ups_sched
executable file
·552 lines (490 loc) · 17.4 KB
/
mqtt_ups_sched
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/bin/sh
# mqtt_ups_sched
# Simple UPS command scheduler script (mqtt_ups_sched) for FreeBSD.
# Triggers a timed command from any specificified MQTT state event.
# Requires a running MQTT broker to query data from your inverter in real-time.
# Script can be renamed for multiple running instances and independent configuration.
# Debug script.
#set -x
# Copyright (c) 2023 - 2024 Jose Rivera (JoseMR).
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS 'AS IS' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Set environment.
PATH=${PATH}:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
SCRIPTNAME=$(basename $0)
VERSION="1.5.07"
cmd_version() {
echo "${SCRIPTNAME} ${VERSION}"
exit 0
}
cmd_usage() {
echo -e "Usage: ${SCRIPTNAME} [option]
Options:
start Start ${SCRIPTNAME} script.
stop Stop ${SCRIPTNAME} sctipt.
stats Display raw device states.
stats_json Display device states in JSON format.
version Display ${SCRIPTNAME} version.
help Display this help message."
exit 1
}
# Global variables.
CONFIG_PATH="/usr/local/etc"
CONFIG_FILE="${CONFIG_PATH}/${SCRIPTNAME}.conf"
LOG_FILE="/var/log/${SCRIPTNAME}.log"
SPEAKER_DEV="/dev/speaker"
RCCONF_FILE="/etc/rc.conf"
TIMEOUT_CMD="timeout"
MQTT_CMD="mosquitto_sub"
DEFAULT_NAME="UPS"
BROKER_MODE="MQTT"
DEFAULT_IP="localhost"
DEFAULT_PORT="1883"
DEFAULT_TOPIC_GRIDMODE="Grid"
DEFAULT_TOPIC_BATTMODE="Solar/Battery"
DEFAULT_TOPIC_LOWBATTMODE="Battery/Low"
DEFAULT_CMD="/sbin/shutdown -p now"
DEFAULT_DELAY="30"
DEFAULT_BEEP="NO"
DEFAULT_RETRY="15"
DEFAULT_LOWBATT_FASTSHUTDOWN="NO"
BEEP_TONE="G20"
PING_COUNT="1"
PING_RETRY="1"
POLL_INTERVAL="5"
BATT_STATUS=""
DEV_STATUS=""
JSON_FORMAT="0"
BATT_ALERT="0"
check_ifrunning() {
# Prevent for multiple script instances.
if pgrep -qf "${SCRIPTNAME}"; then
MSG="already running as PID: $(pgrep -f ${SCRIPTNAME})"
echo "${SCRIPTNAME} ${MSG}"
logger -t ${SCRIPTNAME} ${MSG}
exit 1
else
echo "Starting ${SCRIPTNAME}..."
fi
}
runtime_config() {
# Generate initial configuration file.
if [ ! -f "${CONFIG_FILE}" ]; then
cat << EOF > ${CONFIG_FILE}
# Run-time configuration file for ${SCRIPTNAME}.
# Auto-generated file from ${SCRIPTNAME}.
# Set the MQTT broker server local IP address.
BROKER_IP="${DEFAULT_IP}"
# Set the broker port, default is 1883 and must be allowed by firewall.
BROKER_PORT="${DEFAULT_PORT}"
# Set the broker topic of the battery voltage state, eg: "solar_assistant/inverter_1/battery_voltage/state".
BROKER_TOPIC_BATTVOLT=""
# Set the broker topic of the device mode state, eg: "solar_assistant/inverter_1/device_mode/state".
BROKER_TOPIC_DEVSTATE=""
# Set the broker state output only when operating in grid mode, eg: "Grid"
BROKER_TOPIC_GRIDMODE="${DEFAULT_TOPIC_GRIDMODE}"
# Set the broker state output only when operating in battery mode, eg: "Solar/Battery".
BROKER_TOPIC_BATTMODE="${DEFAULT_TOPIC_BATTMODE}"
# Select the desired topic/states to be displayed separated by space, eg: "sys/inv/grid/state sys/inv/batt/state sys/inv/etc/state".
# Optionally you can display all device states with the parameter: "# -R -C XX", where "XX" is the max topic/state items of the device.
BROKER_TOPIC_STATS=""
# Provide a device name here for logging and reference.
DEVICE_NAME="[${DEFAULT_NAME}]"
# Set the minimum battery voltage to be considered as "system battery low" mode, please use an integer value here.
DEVICE_BATT_LOW=""
# Set the battery last changed date in "MMDDYYYY" for logging and reference.
DEVICE_BATT_CHANGE=""
# Set the battery max age in "Y" years before replacement for logging and reference.
DEVICE_BATT_MAXAGE=""
# Set the desired command to be executed when shutdown delay time is reached, eg: "shutdown -p now".
SYS_SHUTDOWN_CMD="${DEFAULT_CMD}"
# Set the desired shutdown delay time in seconds, please use an integer value here.
# Time here may be relative due to the time it takes to query the topics and may vary per system.
SYS_SHUTDOWN_DELAY="${DEFAULT_DELAY}"
# Set "YES" here if the system must shutdown immediately if system battery low mode/threshold is reached.
SYS_LOWBATT_FASTSHUTDOWN="${DEFAULT_LOWBATT_FASTSHUTDOWN}"
# Set "YES" here to enable system beeps during some state events.
SYS_BEEP_ENABLE="${DEFAULT_BEEP}"
# Set the system max retry count in seconds during commlost/errors before the script halts, please use an integer value here.
SYS_RETRY_COUNT="${DEFAULT_RETRY}"
EOF
echo "Please check and configure [${CONFIG_FILE}] configuration file."
exit 0
fi
# Initialize run-time variables.
if [ -f "${CONFIG_FILE}" ]; then
BROKER_IP=$(sysrc -f ${CONFIG_FILE} -qn BROKER_IP)
BROKER_PORT=$(sysrc -f ${CONFIG_FILE} -qn BROKER_PORT)
BROKER_TOPIC_BATTVOLT=$(sysrc -f ${CONFIG_FILE} -qn BROKER_TOPIC_BATTVOLT)
BROKER_TOPIC_DEVSTATE=$(sysrc -f ${CONFIG_FILE} -qn BROKER_TOPIC_DEVSTATE)
BROKER_TOPIC_GRIDMODE=$(sysrc -f ${CONFIG_FILE} -qn BROKER_TOPIC_GRIDMODE)
BROKER_TOPIC_BATTMODE=$(sysrc -f ${CONFIG_FILE} -qn BROKER_TOPIC_BATTMODE)
BROKER_TOPIC_STATS=$(sysrc -f ${CONFIG_FILE} -qn BROKER_TOPIC_STATS)
DEVICE_NAME="[$(sysrc -f ${CONFIG_FILE} -qn DEVICE_NAME)]"
DEVICE_BATT_LOW=$(sysrc -f ${CONFIG_FILE} -qn DEVICE_BATT_LOW)
DEVICE_BATT_CHANGE=$(sysrc -f ${CONFIG_FILE} -qn DEVICE_BATT_CHANGE)
DEVICE_BATT_MAXAGE=$(sysrc -f ${CONFIG_FILE} -qn DEVICE_BATT_MAXAGE)
SYS_SHUTDOWN_CMD=$(sysrc -f ${CONFIG_FILE} -qn SYS_SHUTDOWN_CMD)
SYS_SHUTDOWN_DELAY=$(sysrc -f ${CONFIG_FILE} -qn SYS_SHUTDOWN_DELAY)
SYS_LOWBATT_FASTSHUTDOWN=$(sysrc -f ${CONFIG_FILE} -qn SYS_LOWBATT_FASTSHUTDOWN)
SYS_BEEP_ENABLE=$(sysrc -f ${CONFIG_FILE} -qn SYS_BEEP_ENABLE)
SYS_RETRY_COUNT=$(sysrc -f ${CONFIG_FILE} -qn SYS_RETRY_COUNT)
else
echo "Error: Config file not found."
exit 1
fi
}
check_config() {
# Ensure mosquitto_sub is present in MQTT mode.
if [ "${BROKER_MODE}" = "MQTT" -o "${BROKER_MODE}" = "mqtt" ]; then
if ! which -s "${MQTT_CMD}"; then
current_date
MSG="Required '${MQTT_CMD}' command not found, exiting!"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
exit 1
fi
fi
if [ -z "${DEVICE_NAME}" ]; then
echo "=> Setting default device name."
sysrc -f ${CONFIG_FILE} DEVICE_NAME="${DEFAULT_NAME}"
DEVICE_NAME=$(sysrc -f ${CONFIG_FILE} -qn DEVICE_NAME)
fi
if [ -z "${BROKER_IP}" ]; then
echo "=> Setting default broker ip:"
sysrc -f ${CONFIG_FILE} BROKER_IP="${DEFAULT_PORT}"
BROKER_IP=$(sysrc -f ${CONFIG_FILE} -qn BROKER_IP)
fi
if [ -z "${BROKER_PORT}" ]; then
echo "=> Setting default broker port:"
sysrc -f ${CONFIG_FILE} BROKER_PORT="${DEFAULT_PORT}"
BROKER_PORT=$(sysrc -f ${CONFIG_FILE} -qn BROKER_PORT)
fi
if [ -z "${SYS_SHUTDOWN_CMD}" ]; then
echo "=> Setting default shutdown command:"
sysrc -f ${CONFIG_FILE} SYS_SHUTDOWN_CMD="${DEFAULT_CMD}"
SYS_SHUTDOWN_CMD=$(sysrc -f ${CONFIG_FILE} -qn SYS_SHUTDOWN_CMD)
fi
if [ -z "${SYS_SHUTDOWN_DELAY}" ]; then
echo "=> Setting default shutdown delay:"
sysrc -f ${CONFIG_FILE} SYS_SHUTDOWN_DELAY="${DEFAULT_DELAY}"
SYS_SHUTDOWN_DELAY=$(sysrc -f ${CONFIG_FILE} -qn SYS_SHUTDOWN_DELAY)
fi
if [ -z "${SYS_BEEP_ENABLE}" ]; then
echo "=> Setting default system beep:"
sysrc -f ${CONFIG_FILE} SYS_BEEP_ENABLE="${DEFAULT_BEEP}"
SYS_BEEP_ENABLE=$(sysrc -f ${CONFIG_FILE} -qn SYS_BEEP_ENABLE)
fi
if [ -z "${SYS_RETRY_COUNT}" ]; then
echo "=> Setting default retry count:"
sysrc -f ${CONFIG_FILE} SYS_RETRY_COUNT="${DEFAULT_RETRY}"
SYS_RETRY_COUNT=$(sysrc -f ${CONFIG_FILE} -qn SYS_RETRY_COUNT)
fi
# If below parameters are not configured then inform and exit.
if [ -z "${BROKER_TOPIC_BATTVOLT}" ]; then
echo "=> Broker batt voltage topic parameter not configured."
exit 1
fi
if [ -z "${BROKER_TOPIC_DEVSTATE}" ]; then
echo "=> Broker device state topic parameter not configured."
exit 1
fi
if [ -z "${BROKER_TOPIC_GRIDMODE}" ]; then
echo "=> Broker grid mode topic parameter not configured."
exit 1
fi
if [ -z "${BROKER_TOPIC_BATTMODE}" ]; then
echo "=> Broker battery mode topic parameter not configured."
exit 1
fi
if [ -z "${DEVICE_BATT_LOW}" ]; then
echo "=> Device low batt parameter not configured."
exit 1
fi
if [ -z "${SYS_LOWBATT_FASTSHUTDOWN}" ]; then
echo "=> Device low batt fast shutdown parameter not configured."
exit 1
fi
}
current_date() {
# Get current date.
DATE=""
DATE=$(date +"%Y-%m-%d %T")
}
check_battage() {
# Get a quick battery age approximation just for logging and reference purposes only.
# This assumes the user properly configured the required parameter as there is no validation check.
BATTERY_YEAR=$(echo "${DEVICE_BATT_CHANGE}" | tail -c 5)
CURRENT_YEAR=$(date +%Y)
BATTERY_AGED=$(expr "${CURRENT_YEAR}" - "${BATTERY_YEAR}")
if [ "${BATTERY_AGED}" -ge "${DEVICE_BATT_MAXAGE}" ]; then
current_date
speaker_beep
MSG="Currently installed battery may reached its max age!, Battery was installed on: ${DEVICE_BATT_CHANGE}"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
BATT_ALERT="1"
fi
}
speaker_beep() {
# Play beeps on device events.
if [ "${SYS_BEEP_ENABLE}" = "YES" -o "${SYS_BEEP_ENABLE}" = "yes" ]; then
if [ -c "${SPEAKER_DEV}" ]; then
echo ${BEEP_TONE} > ${SPEAKER_DEV}
else
# Try to load the speaker kernel module if available, otherwise ignore.
if ! kldstat -qn "speaker.ko"; then
if [ -f "/boot/kernel/speaker.ko" ]; then
kldload "/boot/kernel/speaker.ko" > /dev/null 2>&1 && echo ${BEEP_TONE} > ${SPEAKER_DEV}
fi
fi
fi
fi
}
cmd_devstats() {
if pgrep -qf "${SCRIPTNAME}"; then
current_date
runtime_config
# Display device extra info/topics/stats.
if [ -n "${BROKER_TOPIC_STATS}" ]; then
echo "${DEVICE_NAME} ${DATE} DEVICE STATUS:"
if ! echo "${BROKER_TOPIC_STATS}" | grep -q "#"; then
# Display user specified topic/stats.
STAT_SELECT=""
NUM_COUNT="0"
for COUNT in ${BROKER_TOPIC_STATS}; do
NUM_COUNT=$(expr 1 + ${NUM_COUNT})
done
for TOPIC in ${BROKER_TOPIC_STATS}; do
STAT_SELECT=$(echo " -t ${TOPIC}"${STAT_SELECT})
done
if [ -n "${STAT_SELECT}" ]; then
if [ "${JSON_FORMAT}" -eq "1" ]; then
${TIMEOUT_CMD} ${SYS_RETRY_COUNT} ${MQTT_CMD} -h ${BROKER_IP} -p ${BROKER_PORT} -v ${STAT_SELECT} -C ${NUM_COUNT} -F %j
else
${TIMEOUT_CMD} ${SYS_RETRY_COUNT} ${MQTT_CMD} -h ${BROKER_IP} -p ${BROKER_PORT} -v ${STAT_SELECT} -C ${NUM_COUNT}
fi
fi
else
# Display all stats once if wildcard/args "# -R -C XX" specified.
STAT_ALL="${BROKER_TOPIC_STATS}"
if [ "${JSON_FORMAT}" -eq "1" ]; then
${TIMEOUT_CMD} ${SYS_RETRY_COUNT} ${MQTT_CMD} -h ${BROKER_IP} -p ${BROKER_PORT} -v -t ${STAT_ALL} -F %j
else
${TIMEOUT_CMD} ${SYS_RETRY_COUNT} ${MQTT_CMD} -h ${BROKER_IP} -p ${BROKER_PORT} -v -t ${STAT_ALL}
fi
fi
fi
else
echo "${SCRIPTNAME} is not running!"
exit 1
fi
}
device_status() {
# Get device status.
if [ "${BROKER_MODE}" = "MQTT" ]; then
DEV_STATUS=""
BATT_STATUS=""
# Standard UPS/Inverter status, use command timeout to avoid execution hang if poor communication.
if $(ping -c ${PING_COUNT} -t ${PING_RETRY} ${BROKER_IP} > /dev/null 2>&1); then
DEV_STATUS=$(${TIMEOUT_CMD} ${SYS_RETRY_COUNT} ${MQTT_CMD} -C 1 -h ${BROKER_IP} -p ${BROKER_PORT} -t ${BROKER_TOPIC_DEVSTATE} 2>/dev/null)
BATT_STATUS=$(${TIMEOUT_CMD} ${SYS_RETRY_COUNT} ${MQTT_CMD} -C 1 -h ${BROKER_IP} -p ${BROKER_PORT} -t ${BROKER_TOPIC_BATTVOLT} 2>/dev/null | cut -d"." -f1)
if [ -n "${DEV_STATUS}" ]; then
# Check if running on utility grid.
if [ "${DEV_STATUS}" = "${BROKER_TOPIC_GRIDMODE}" ]; then
DEV_STATUS="${BROKER_TOPIC_GRIDMODE}"
# Check if running on battery.
elif [ "${DEV_STATUS}" = "${BROKER_TOPIC_BATTMODE}" ]; then
# Check if running on low battery.
if [ -n "${BATT_STATUS}" ]; then
if [ "${BATT_STATUS}" -le "${DEVICE_BATT_LOW}" ]; then
DEV_STATUS="${DEFAULT_TOPIC_LOWBATTMODE}"
fi
fi
fi
fi
fi
else
echo "=> Unknown device or parameter."
exit 1
fi
# Get approximate battery age status and logging.
if [ -n "${DEVICE_BATT_CHANGE}" ] && [ -n "${DEVICE_BATT_MAXAGE}" ]; then
if [ "${BATT_ALERT}" -eq "0" ]; then
check_battage
fi
fi
}
device_commlost() {
current_date
speaker_beep
MSG="Device communication lost, waiting for connection!"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
RETRY="0"
while :; do
# Wait for communication before halting.
if [ "${RETRY}" -eq "${SYS_RETRY_COUNT}" ]; then
current_date
speaker_beep
MSG="Unable to read device status, retries exhausted exiting!"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
exit 1
else
# Rescan device status.
device_status
if [ -n "${DEV_STATUS}" ]; then
# Notify device communication is back.
current_date
speaker_beep
MSG="Device communication restored!"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
break
fi
fi
RETRY=$(expr ${RETRY} + 1)
sleep 1
done
}
system_shutdown_cmd() {
current_date
speaker_beep
MSG="System going to shutdown now!"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
${SYS_SHUTDOWN_CMD}
exit 0
}
system_shutdown_timer() {
speaker_beep
QUERY="0"
RETRY="0"
while :; do
if [ "${RETRY}" -eq "${SYS_SHUTDOWN_DELAY}" ]; then
# Shutdown system if still running on battery power.
system_shutdown_cmd
else
# Rescan device status before shutdown.
# Unfortunately this will add extra delay to the final shutdown time, this is due to the time it takes to ping/query the topics in regards(Device mode and battery voltage).
# Hence the time has to be relatively compensated eg. 30x3, where "30" is the wanted shutdown secs and the "3" is the secs the system need to query the device states,
# so it may yield about "90" secs or even more to the final shutdown delay if not compensated, also be aware that this may vary per system.
QUERY="1"
device_status
if [ "${DEV_STATUS}" = "${BROKER_TOPIC_GRIDMODE}" ]; then
current_date
speaker_beep
MSG="System running on utility power!"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
break
elif [ -z "${DEV_STATUS}" ]; then
# Notify communication with the device is lost.
device_commlost
fi
fi
# Workaround to append relative time compensation.
if [ "${QUERY}" = "1" ]; then
QUERY_TIME="4"
else
QUERY_TIME="1"
fi
RETRY=$(expr ${RETRY} + ${QUERY_TIME})
sleep 1
done
}
cmd_stop() {
current_date
if [ -z "${DEVICE_NAME}" ]; then
DEVICE_NAME="[$(sysrc -f ${CONFIG_FILE} -qn DEVICE_NAME)]"
fi
if pgrep -qf "${SCRIPTNAME}"; then
echo "Stopping ${SCRIPTNAME}..."
pkill -f ${SCRIPTNAME} && \
MSG="script stopped successfully!"
echo "${DEVICE_NAME} ${DATE}: ${SCRIPTNAME} ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
exit 0
else
echo "${SCRIPTNAME} is not running!"
exit 1
fi
}
cmd_main() {
current_date
check_ifrunning
runtime_config
check_config
speaker_beep
while :; do
# Scan device status loop.
device_status
if [ "${DEV_STATUS}" = "${BROKER_TOPIC_GRIDMODE}" ]; then
sleep ${POLL_INTERVAL}
elif [ "${DEV_STATUS}" = "${BROKER_TOPIC_BATTMODE}" ]; then
# Initialize the system shutdown countdown.
current_date
MSG="System running on battery power!"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
system_shutdown_timer
elif [ "${DEV_STATUS}" = "${DEFAULT_TOPIC_LOWBATTMODE}" ]; then
current_date
MSG="System running on low battery power!"
echo "${DEVICE_NAME} ${DATE}: ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
if [ "${SYS_LOWBATT_FASTSHUTDOWN}" = "YES" -o "${SYS_LOWBATT_FASTSHUTDOWN}" = "yes" ]; then
# Shutdown system immediately if running on low battery.
system_shutdown_cmd
else
# Initialize the system shutdown countdown.
system_shutdown_timer
fi
elif [ -z "${DEV_STATUS}" ]; then
# Notify we lost communication with the device.
device_commlost
fi
done &
if [ "$?" -eq "0" ]; then
MSG="script started successfully!"
echo "${DEVICE_NAME} ${DATE}: ${SCRIPTNAME} ${MSG}" >> ${LOG_FILE}
logger -t ${SCRIPTNAME} ${MSG}
fi
}
# Handle script options.
case "${1}" in
start)
cmd_main;;
stop)
cmd_stop;;
stats)
cmd_devstats;;
stats_json)
JSON_FORMAT="1"
cmd_devstats;;
version|-v)
cmd_version;;
help|-h)
cmd_usage;;
*)
cmd_usage;;
esac