-
-
Save kata0510/2eca44848d93ec1bbf6a3978a4595f88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"strconv" | |
"strings" | |
"time" | |
"github.com/prometheus/client_golang/prometheus" | |
"github.com/prometheus/client_golang/prometheus/promhttp" | |
) | |
var ( | |
smartmeter = prometheus.NewGauge(prometheus.GaugeOpts{ | |
Name: "SmartMater_Value", | |
Help: "Watt value from SmartMeter", | |
}) | |
) | |
func init() { | |
prometheus.MustRegister(smartmeter) | |
} | |
func fetchSmartMeterValue() { | |
for { | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", "http://192.168.xxx.xxx/php/get_property_m.php?ba=xx:xx:xx:xx:xx:xx:xx:xx&eoj=0x028801&epc=0xE7", nil) | |
if err != nil { | |
fmt.Println("Error:", err) | |
continue | |
} | |
req.Header.Set("Referer", "http://192.168.xxx.xxx/smartmeter.html") | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println("Error:", err) | |
continue | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println("Error:", err) | |
continue | |
} | |
resp.Body.Close() | |
hexValue := strings.TrimSpace(string(body)) | |
hexValue = strings.TrimPrefix(hexValue, "0x") | |
value, err := strconv.ParseInt(hexValue, 16, 64) | |
if err != nil { | |
fmt.Println("Error:", err) | |
continue | |
} | |
smartmeter.Set(float64(value)) | |
time.Sleep(10 * time.Second) | |
} | |
} | |
func main() { | |
go fetchSmartMeterValue() | |
http.Handle("/metrics", promhttp.Handler()) | |
http.ListenAndServe(":9091", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment