Skip to content

Instantly share code, notes, and snippets.

@kata0510

kata0510/main.go Secret

Created April 14, 2024 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kata0510/2eca44848d93ec1bbf6a3978a4595f88 to your computer and use it in GitHub Desktop.
Save kata0510/2eca44848d93ec1bbf6a3978a4595f88 to your computer and use it in GitHub Desktop.
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