· 6 years ago · May 04, 2020, 07:44 AM
1#!/bin/bash
2
3api_key="<your API Key>";
4site_id="<your site id>";
5api_host="https://monitoringapi.solaredge.com";
6
7#how often to call this url
8check_interval=300
9
10#retry how many times
11retry_count=5
12
13#check for a keyword to accept as valid
14valid_keyword="\"currentPower\""
15
16#store result from file
17store_file="/tmp/solaredge.dat"
18
19######################################################################
20
21data=""
22
23if [ -f "$store_file" ]; then
24 #check if its older than a set time
25 sec_now=`date +%s`
26 sec_file=`stat -c %Z "$store_file"`
27 sec_passed=$(($sec_now - $sec_file));
28 if [ $sec_passed -lt $check_interval ]; then
29 data=`cat "$store_file"`
30 fi
31fi
32
33if [ "$data" = "" ]; then
34 request="$api_host/site/$site_id/overview?api_key=$api_key";
35 for (( i=1; i<=$retry_count; i++ )); do
36 data=`curl -s -m 10 "$request"`
37 echo "$data" > "$store_file"
38 #check if content is valid
39 chk=`grep "$valid_keyword" "$store_file"`
40 if [ "$chk" != "" ]; then
41 break;
42 fi
43 done
44fi
45
46data=`echo "$data" | awk -F "\"currentPower\":{\"power\":" '{print $2}' | awk -F "}" '{print $1}' | tr -c -d '0-9.'`
47if [ "$data" = "" ]; then
48 data="0";
49 rm "$store_file"
50fi
51
52echo $data;