-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcw.sh
executable file
·324 lines (272 loc) · 9.18 KB
/
cw.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
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
#!/usr/bin/env bash
# Settings
VENDOR_ID="0483"
PRODUCT_ID="5750"
CHANNELS=5
CHANNEL1="Wohnzimmer"
CHANNEL2="Keller"
CHANNEL3="Schlafzimmer"
CHANNEL4="Dachboden"
CHANNEL5="Garten"
CHANNEL6=""
CHANNEL7=""
CHANNEL8=""
DEFAULT_PORT=8010
# Init Vars
DEBUG=0
CSV_FILE=""
JSON_FILE=""
XML_FILE=""
PORT=$DEFAULT_PORT
WEB_MODE=0
# Utils
calc() { awk "BEGIN { print $*}"; }
showHelp() {
echo "ClimaViewer Version 0.1"
echo "Usage: $0 [-v] [-h] [-c <filename>] [-j <filename>] [-x <filename>] [-w [port]]"
echo ""
echo "Options:"
echo " -v Enable verbose mode (debugging)."
echo " -h Show this help message."
echo " -c Write CSV output to the specified file."
echo " -j Write JSON output to the specified file."
echo " -x Write XML output to the specified file."
echo " -w Start a webserver. Optionally specify the port (default: $PORT)."
exit 0
}
debug() {
[[ $DEBUG -eq 1 ]] && printf "%s - %s\n" "$(date +'%Y-%m-%d %H:%M:%S')" "$@"
}
# Process command line options
while getopts "vhcjxw" opt; do
case "$opt" in
v) DEBUG=1 ;;
h) showHelp ;;
c) eval nextopt=${!OPTIND}
if [[ -n $nextopt && $nextopt != -* ]] ; then
CSV_FILE="$nextopt"
else
CSV_FILE="."
fi ;;
j) eval nextopt=${!OPTIND}
if [[ -n $nextopt && $nextopt != -* ]] ; then
JSON_FILE="$nextopt"
else
JSON_FILE="."
fi ;;
x) eval nextopt=${!OPTIND}
if [[ -n $nextopt && $nextopt != -* ]] ; then
XML_FILE="$nextopt"
else
XML_FILE="."
fi ;;
w) WEB_MODE=1
# Check if the next positional parameter is a port number
eval nextopt=${!OPTIND}
if [[ -n $nextopt && $nextopt =~ ^[0-9]+$ ]] ; then
if [[ $nextopt -ge 1 && $nextopt -le 65535 ]]; then
PORT=$nextopt
OPTIND=$((OPTIND + 1))
else
echo "Invalid Port specified (not between 1 and 65535)"
exit 1
fi
else
PORT="${OPTARG:-$DEFAULT_PORT}"
fi ;;
*) showHelp ;;
esac
done
# Check for required tools
for tool in find udevadm xxd hexdump column jq awk nc; do
if ! command -v "$tool" &>/dev/null; then
echo "Error: $tool is required but not installed."
exit 1
fi
done
# Find the device
DEVICE=$(find /dev -name 'hidraw*' -exec udevadm info -q property --name={} \; | grep -A 1 "$VENDOR_ID" | grep -A 1 "$PRODUCT_ID" | grep DEVNAME | awk -F= '{print $2}')
if [ -z "$DEVICE" ]; then
echo "Error: Device not found!"
exit 1
fi
debug "Found Device: $DEVICE"
# USB-HID-Report Header
HEADER_HEX="00"
# 64-Byte-Packet as Hex-Values (Payload)
PAYLOAD_HEX="7b 03 40 7d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
# Full Report (Header + Payload)
REPORT_HEX="${HEADER_HEX} ${PAYLOAD_HEX}"
# Convert Hex-values into binary data and save it in file
echo "$REPORT_HEX" | xxd -r -p > report.bin
if [[ ! -s report.bin ]]; then
echo "Error: Failed to create report.bin"
exit 1
fi
# Ensure cleanup on exit
trap 'rm -f report.bin; exec 3>&-' EXIT
# Open device for r/w in non-blocking mode
exec 3<> "$DEVICE" || { echo "Error: Failed to open device $DEVICE"; exit 1; }
sudo cat report.bin >&3
# Read answer from device (non-blocking)
# Read max. 64 Bytes, - default size of HID Reports
RESPONSE=$(head -c 64 <&3 | hexdump -e '64/1 "%02X " "\n"')
DATA=(${RESPONSE})
exec 3>&- # Close Device
# Show response
debug "Response from device:"
debug "$RESPONSE"
CSVHELPER=""
JSONARRAY=()
XMLBODY=""
HTMLBODY=""
for (( counter=0; counter<CHANNELS; counter++ )); do
T1=${DATA[1+3*$counter]}
T2=${DATA[2+3*$counter]}
HUM=$((0x${DATA[3+3*$counter]}))
if [[ "$T1" == "FF" ]]; then
TEMP=$((0x${T1}${T2} - 0xFFFF - 0x1))
else
TEMP=$((0x${T1}${T2}))
fi
TEMP=$(calc "$TEMP/10")
CHANNEL_NAME_VAR="CHANNEL$((counter+1))"
CHANNEL_NAME=${!CHANNEL_NAME_VAR}
debug "-------------"
debug "Channel $((counter+1)) - $CHANNEL_NAME"
debug "Temperature: ${TEMP}°C"
debug "Humidity: ${HUM}%"
debug "-------------"
# Check if Temperature is out of range
if awk "BEGIN {exit !($TEMP >= 100 || $TEMP <= -100)}"; then
TEMP="n/a"
debug "Invalid Data: Temperature out of range"
fi
# Check if Humidity is out of range
if awk "BEGIN {exit !($HUM >= 100 || $HUM <= 0)}"; then
HUM="n/a"
debug "Invalid Data: Humidity out of range"
fi
CSVHELPER+="$((counter+1));$CHANNEL_NAME;$TEMP;$HUM\n"
# Prepare JSON array
JSONARRAY+=("{\"Channel\":$((counter+1)),\"Name\":\"$CHANNEL_NAME\",\"Temperature\":\"$TEMP\",\"Humidity\":\"$HUM\"}")
# Prepare XML data
XMLBODY+=$(printf '<Channel number="%d"><Name>%s</Name><Temperature>%s</Temperature><Humidity>%s</Humidity></Channel>\n' $((counter+1)) "$CHANNEL_NAME" "$TEMP" "$HUM")
# Prepare HTML data
HTMLBODY+=$(printf "<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>\n" $((counter+1)) "$CHANNEL_NAME" "$TEMP" "$HUM")
done
# Output CSV
if [[ -n "$CSV_FILE" && "$CSV_FILE" != "." ]]; then
echo -e "$CSVHELPER" > "$CSV_FILE"
echo "CSV output written to $CSV_FILE"
elif [[ "$CSV_FILE" == "." ]]; then
echo -e "$CSVHELPER"
fi
# Output JSON
if [[ -n "$JSON_FILE" && "$JSON_FILE" != "." ]]; then
# Correct JSON format
JSON_OUTPUT=$(printf "[%s]" "$(IFS=,; echo "${JSONARRAY[*]}")")
echo "$JSON_OUTPUT" | jq . > "$JSON_FILE"
echo "JSON output written to $JSON_FILE"
elif [[ "$JSON_FILE" == "." ]]; then
# Correct JSON format
JSON_OUTPUT=$(printf "[%s]" "$(IFS=,; echo "${JSONARRAY[*]}")")
echo "$JSON_OUTPUT" | jq .
fi
# Output XML
if [[ -n "$XML_FILE" && "$XML_FILE" != "." ]]; then
XML_HEADER="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
XML_ROOT="<Channels>$XMLBODY</Channels>"
printf "%s\n%s\n" "$XML_HEADER" "$XML_ROOT" > "$XML_FILE"
echo "XML output written to $XML_FILE"
elif [[ "$XML_FILE" == "." ]]; then
XML_HEADER="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
XML_ROOT="<Channels>$XMLBODY</Channels>"
printf "%s\n%s\n" "$XML_HEADER" "$XML_ROOT"
fi
# If web mode is enabled, start a simple web server
if [[ $WEB_MODE -eq 1 ]]; then
echo "Starting web server on port $PORT..."
while true; do
# Update sensor data
exec 3<> "$DEVICE" || { echo "Error: Failed to open device $DEVICE"; exit 1; }
sudo cat report.bin >&3
RESPONSE=$(head -c 64 <&3 | hexdump -e '64/1 "%02X " "\n"')
DATA=(${RESPONSE})
exec 3>&-
HTMLBODY=""
for (( counter=0; counter<CHANNELS; counter++ )); do
T1=${DATA[1+3*$counter]}
T2=${DATA[2+3*$counter]}
HUM=$((0x${DATA[3+3*$counter]}))
if [[ "$T1" == "FF" ]]; then
TEMP=$((0x${T1}${T2} - 0xFFFF - 0x1))
else
TEMP=$((0x${T1}${T2}))
fi
TEMP=$(calc "$TEMP/10")
if awk "BEGIN {exit !($TEMP >= 100 || $TEMP <= -100)}"; then
TEMP="n/a"
fi
if awk "BEGIN {exit !($HUM >= 100 || $HUM <= 0)}"; then
HUM="n/a"
fi
CHANNEL_NAME_VAR="CHANNEL$((counter+1))"
CHANNEL_NAME=${!CHANNEL_NAME_VAR}
HTMLBODY+=$(printf "<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>\n" $((counter+1)) "$CHANNEL_NAME" "$TEMP" "$HUM")
done
# Serve HTML content
{ echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta http-equiv='refresh' content='60'>
<title>Sensor Data</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 0;
color: #333;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 10px;
text-align: center;
border: 1px solid #ddd;
}
th {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<h1>Sensor Data</h1>
<table>
<thead>
<tr>
<th>Channel</th>
<th>Location</th>
<th>Temperature [°C]</th>
<th>Humidity [%]</th>
</tr>
</thead>
<tbody>
$HTMLBODY
</tbody>
</table>
</body>
</html>"; } | nc -l -p "$PORT" -q 1
sleep 60
done
fi
# If neither CSV, JSON, XML, nor web mode is specified, print the data to stdout
if [[ -z "$CSV_FILE" && -z "$JSON_FILE" && -z "$XML_FILE" && $WEB_MODE -eq 0 ]]; then
echo -e "$CSVHELPER" | column -ts\; -o$'\t' -N "Channel,Location,Temperature [°C],Humidity [%]"
fi