-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-temperature
executable file
·97 lines (85 loc) · 2.78 KB
/
get-temperature
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
#!/bin/bash
# -*- coding: utf-8 -*-
# Wrapper for pcsensor https://github.com/peterfarsinsen/pcsensor.git
# This script extracts temperature from CSV and prints a JSON string as
# required by ScriptThermometer class of Thermod daemon.
#
# @author Simone Rossetto
# @copyright 2016 Simone Rossetto
# @license GNU General Public License v3
# @contact [email protected]
# @version 1.0
#
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# init variables
DEBUG=0
LANG=POSIX
PCSENSOR='/usr/local/bin/pcsensor -c -s'
PYMODULE='thermod.temperature'
# check required programs
REQUIRED_PROGRAMS=( logger cut printf )
for prog in ${REQUIRED_PROGRAMS[@]}; do
if ! hash "$prog" 1>/dev/null 2>/dev/null; then
echo -n "{\"error\": \"required program ${prog} not found\", \"temperature\": null}"
exit 255
fi
done
# parsing arguments for debug option
while :; do
case $1 in
-d|--debug) DEBUG=1 ;;
*) break
esac
shift
done
# logging function
log()
{
if [ "${1:-debug}" != 'debug' ]; then
logger -t "${PYMODULE}" -i -p user.$1 -- "${1^^} ${2:-no message reported}"
elif [ ${DEBUG} -eq 1 ]; then
logger -t "${PYMODULE}" -i -p user.debug -- "DEBUG ${2:-no message reported}"
fi
}
# main
log debug "querying thermometer using program '${PCSENSOR}'"
CSV=$(${PCSENSOR})
RET=$?
if [ ${RET} -ne 0 ]; then
log debug "pcsensor exited with error code ${RET} and message '${CSV}'"
printf '{"error": "%s", "temperature": null}' "${CSV}"
exit ${RET}
fi
log debug "extracting temperature from CSV output '${CSV}'"
TEMPERATURE=$(echo -n "${CSV}" | cut -d \; -f 4)
RET=$?
if [ ${RET} -ne 0 ]; then
log debug "cannot extract temperature from CSV: '${TEMPERATURE}'"
printf '{"error": "%s", "temperature": null}' "${TEMPERATURE}"
exit ${RET}
fi
log debug "converting temperature '${TEMPERATURE}' to number"
JSON=$(printf '{"temperature": %.2f, "error": null}' "${TEMPERATURE}")
RET=$?
if [ ${RET} -ne 0 ]; then
err="cannot convert temperature '${TEMPERATURE}' to number"
log debug "${err}"
printf '{"error": "%s", "temperature": null}' "${err}"
exit ${RET}
fi
log debug "$(printf 'current temperature is %.2f' "${TEMPERATURE}")"
echo -n "${JSON}"
exit 0
# vim: fileencoding=utf-8 tabstop=2 shiftwidth=2 expandtab