get_id_gatewaysChange.sh 2.8 KB
Newer Older
1
#!/bin/bash
2

3
4
#TODAY="2021-03-02"
TODAY="$(date +"%Y-%m-%d")"
5

6
#On stock les ids des gateways d'aujourd'hui
7
ids=$(jq --raw-output ".result[] | .id" .gateways.json)
8
9
10
11
12
13
14
id=()
i=0
for ID in $ids
do
    id[$i]=$ID
    i=$((i+1))
done
15
16

#On stock les dates de dernière activité des gateways d'aujourd'hui
17
dates=$(jq --raw-output ".result[] | .lastSeenAt" .gateways.json)
18
date=()
19
date=($dates)
20
state=()
21
full_date=()
22
d=()
23
for (( i=0; i<${#date[@]}; i++ ))
24
do
25
26
27
28
29
30
31
32
33
34
35
36
    if [[ ${date[$i]} == "null" ]]
    then
        full_date[$i]=${date[$i]}
        d[$i]=${full_date[$i]}
    else
        full_date[$i]=$(echo $date[$i] | tr "T" "\n")
        for arg in $full_date[$i]
        do
            d[$i]=$arg
            break
        done
    fi
37
done
38
39


40
41
#On stock les states des gateways d'aujourd'hui dans un tableau en regardant la date
#de dernière activité des gateways
42
43
44
45
46
47
48
49
50
51
for (( i=0; i<${#d[@]}; i++ ))
do
    if [[ "${d[$i]}" == "$TODAY" ]]
    then
        state[$i]="active"
    else
        state[$i]="passive"
    fi
done

52
#On stock les ids des gateways d'hier
53
ids_2=$(jq --raw-output ".result[] | .id" lastGatewaysStates.json)
54
55
56
57
58
59
60
61
id_2=()
i=0
for ID in $ids_2
do
    id_2[$i]=$ID
    i=$((i+1))
done

62
#On stock les states des gateways d'hier
63
full_state=$(jq --raw-output ".result[] | .lastState" lastGatewaysStates.json)
64
65
66
67
68
69
70
71
states_2=()
j=0
for s in $full_state
do
    state_2[$j]=$s
    j=$((j+1))
done

72
73

#On compare les states des gateways d'aujourd'hui et hier
74
75
76
77
78
79
for (( i=0; i<${#id[@]}; i++ ))
do
    for (( j=0; j<${#id_2[@]}; j++ ))
    do
        if [[ "$id[$i]" == "$id_2[$j]" ]]
        then
80
            if [[ "${state[$i]}" != "${state_2[$j]}" ]]
81
            then
82
83
                if [[ "${state[$i]}" == "active" ]]
                then
84
85
                    tmp=$(grep ${id[$i]} < .gateways.html)
                    replacement=${tmp/"<li>"/"<li style="color:green">"}
86
                else
87
88
                    tmp=$(grep ${id[$i]} < .gateways.html)
                    replacement=${tmp/"<li>"/"<li style="color:red">"}
89
90
                fi
                sed -i "/${id[$i]}/c $replacement" .gateways.html
91
92
93
            fi
        fi
    done
94
95
96
97
98
99
100
    if [[ ${date[$i]} != "null" ]]
    then
        tmp=$(grep ${id[$i]} < .gateways.html)
        dateFromNow=$(node changeDateMomentjs.js ${date[$i]})
        replacement=${tmp/${date[$i]}/"$dateFromNow"}
        sed -i "/${id[$i]}/c $replacement" .gateways.html
    fi
101
102
done

103
104
105
#On met à jour les states des gateways pour le prochain jour
echo "{
  \"totalCount\": \"${#id[@]}\",
106
  \"result\": [" > lastGatewaysStates.json
107
108
109
110
111
112

for (( i=0; i<$((${#id[@]}-1)); i++ ))
do
    echo "    {
      \"id\": \"${id[$i]}\",
      \"lastState\": \"${state[$i]}\"
113
    }," >> lastGatewaysStates.json
114
115
116
117
118
119
120
done

echo "    {
      \"id\": \"${id[$((${#id[@]}-1))]}\",
      \"lastState\": \"${state[$((${#id[@]}-1))]}\"
    }
  ]
121
}" >> lastGatewaysStates.json