-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_json_to_server.sh
executable file
·128 lines (106 loc) · 3.5 KB
/
send_json_to_server.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
#!/usr/bin/env bash
#
# This script sendS the JSON datafiles from the
# BioReactor system to the server. It scans the data folder
# and sends each json file to the server via the httpie
# package. The return code is checked to make sure that the
# server has accepted the data. If the server responds with
# 200 then the file is moved to the sent folder.
#
# It detects whether we are on the Windows development machine
# or whether we are on the Raspberry Pi Linux machine
#
# see which platform we are on. Windows is for testing.
# Linux is raspberry pi production
OS=$OSTYPE
Linux=false
if [[ ${OS:0:5} = "Linux" || ${OS:0:5} = "linux" ]]
then
Linux=true
fi
# name of the server we are posting to.
#
# SERVER="2Connect2Biz.com"
SERVER="pbr.fixingtheatmosphere.org"
# root folder where our json files are being stored
# on Windows we are using "./" On the raspberry pi it will be "/usr/local/bin/bioreactor/"
# It needs to end in a forward slash
ROOT_FOLDER="./"
if ( $Linux )
then
ROOT_FOLDER="/usr/local/bin/BioReactor/"
fi
# the wildcard strings denoting our json files
JSON_TEMP_WILDCARD="bio_temp*.json"
JSON_GAS_WILDCARD="bio_gas*.json"
JSON_LIGHT_WILDCARD="bio_light*.json"
# JSON_ERR_WILDCARD="bio_error*.json"
# the folder where we are moving the json files that have been
# successfully posted to the server
# for Windows we are using "./sent/" On the raspberry pi we are using "/usr/local/bin/bioreactor/sent/"
# It needs to end in a forward slash
SENT_FOLDER=$ROOT_FOLDER"sent/"
#log file name
LOG_FILE="httpsend.log"
# the logfile will reside in the root folder
LOG_FILE=$ROOT_FOLDER$LOG_FILE
# Move the datafile to the sent folder
#
# @parameter string filename to move
# @parameter string logdate
#
function moveJsonToSentFolder() {
local fname=$1
local logdate=$2
local filename=$(basename "$fname")
local extension="${filename##*.}"
local fileroot="${filename%.*}"
# tack on the datetime to the sent filename
local newname="$SENT_FOLDER$fileroot"_"$logdate.$extension"
#echo $newname
#echo "filename[$filename] extension [$extension] fileroot [$fileroot]"
echo "$logdate Moving [$fname] to Sent Folder as [$newname]" >> $LOG_FILE
mv $fname $newname 2>> $LOG_FILE
}
# Send the datafile to the server
#
# @parameter string filename to send (it is full path)
#
function sendJsonToServer() {
local fname=$1
local route=$2
local logdate=$(date +"%Y_%m_%d_%H_%M_%S")
echo "$logdate Posting ["$fname"] to Server with route ["$route"]" >> $LOG_FILE
if http --check-status --timeout=2.5 POST http://$SERVER/$route < $fname &> /dev/null; then
moveJsonToSentFolder "$fname" "$logdate"
else
local rsltstat=$?
case rsltstat in
2) echo "$logdate Request timed out!" >> $LOG_FILE ;;
3) echo "$logdate Unexpected HTTP 3xx Redirection!" >> $LOG_FILE ;;
4) echo "$logdate HTTP 4xx Client Error!" >> $LOG_FILE ;;
5) echo "$logdate HTTP 5xx Server Error!" >> $LOG_FILE ;;
6) echo "$logdate Exceeded --max-redirects=<n> redirects!" >> $LOG_FILE ;;
*) echo "$logdate Other Error! $rsltstat" >> $LOG_FILE ;;
esac
fi
}
#echo "["$OSTYPE"]["$Linux"]"
#echo "["$ROOT_FOLDER$JSON_TEMP_WILDCARD"]"
#exit 0
# this line deals with the case where there are no files
shopt -s nullglob
# cycle through all the json bioreactor files in the folder
for f in $ROOT_FOLDER$JSON_TEMP_WILDCARD
do
sendJsonToServer $f pitemp
done
for f in $ROOT_FOLDER$JSON_GAS_WILDCARD
do
sendJsonToServer $f pigasflow
done
for f in $ROOT_FOLDER$JSON_LIGHT_WILDCARD
do
sendJsonToServer $f pilight
done
exit 0