New parameter to fetch and send the grid frequency
This commit is contained in:
parent
91f23b4a59
commit
bf1512cf0a
96
frequency.go
Normal file
96
frequency.go
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const SwissGridAPI = "https://www.swissgrid.ch/bin/services/apicache?path=/content/swissgrid/fr/home/operation/grid-data/current-data/jcr:content/parsys/livedatawidget_10292"
|
||||||
|
|
||||||
|
type SwissGridItem struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
MergeColumns bool `json:"mergeColumns"`
|
||||||
|
IsLabelBold bool `json:"isLabelBold"`
|
||||||
|
IsValueBold bool `json:"isValueBold"`
|
||||||
|
}
|
||||||
|
type SwissGridMaker struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
X float64 `json:"x"`
|
||||||
|
Y float64 `json:"y"`
|
||||||
|
Text1 string `json:"text1"`
|
||||||
|
Text2 string `json:"text2"`
|
||||||
|
TextColor string `json:"textColor"`
|
||||||
|
BackgroundColor string `json:"backgroundColor"`
|
||||||
|
BorderColor string `json:"borderColor"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Direction string `json:"direction"`
|
||||||
|
Rotation *string `json:"rotation"`
|
||||||
|
Opacity float64 `json:"opacity"`
|
||||||
|
FontSizeDesktop string `json:"fontSizeDesktop"`
|
||||||
|
FontSizeTablet string `json:"fontSizeTablet"`
|
||||||
|
FontSizePhone string `json:"fontSizePhone"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SwissGridData struct {
|
||||||
|
Data struct {
|
||||||
|
Table []SwissGridItem `json:"table"`
|
||||||
|
Marker []SwissGridItem `json:"marker"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func FetchFrequency() (*SwissGridData, error) {
|
||||||
|
res, err := http.Get(SwissGridAPI)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
if res.StatusCode > 299 {
|
||||||
|
return nil, fmt.Errorf("Response failed with status code: %d", res.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
dec := json.NewDecoder(res.Body)
|
||||||
|
var data SwissGridData
|
||||||
|
err = dec.Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &data, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteFrequency(writer TICWriter, freq *SwissGridData) error {
|
||||||
|
for _, i := range freq.Data.Table {
|
||||||
|
if i.Id == "FreqAct" {
|
||||||
|
f, err := strconv.ParseFloat(strings.Fields(i.Value)[0], 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
point := Point{
|
||||||
|
Data: []byte(fmt.Sprintf("%.0f", f*1000)),
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writer.AddPoints(
|
||||||
|
map[string]Point{
|
||||||
|
"FREQ": point,
|
||||||
|
},
|
||||||
|
time.Now(),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to write frequency point:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
27
main.go
27
main.go
@ -175,6 +175,7 @@ type TICWriter interface {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var legacyMode = flag.Bool("legacy-mode", false, "Assume teleinformation in legacy mode")
|
var legacyMode = flag.Bool("legacy-mode", false, "Assume teleinformation in legacy mode")
|
||||||
|
var pushFrequency = flag.Bool("push-frequency", false, "Also fetch data about the grid frequency")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if len(flag.Args()) < 1 {
|
if len(flag.Args()) < 1 {
|
||||||
@ -217,7 +218,31 @@ func main() {
|
|||||||
|
|
||||||
interrupt := make(chan os.Signal, 1)
|
interrupt := make(chan os.Signal, 1)
|
||||||
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
||||||
<-interrupt
|
if *pushFrequency {
|
||||||
|
ticker := time.NewTicker(25 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
frequencyloop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
freq, err := FetchFrequency()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("An error occurs during FetchFrequency: ", err.Error())
|
||||||
|
continue frequencyloop
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteFrequency(writer, freq)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("An error occurs during WriteFrequency: ", err.Error())
|
||||||
|
}
|
||||||
|
case <-interrupt:
|
||||||
|
break frequencyloop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
<-interrupt
|
||||||
|
}
|
||||||
|
|
||||||
writer.Close()
|
writer.Close()
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
MeasurementUnits = map[string]string{
|
MeasurementUnits = map[string]string{
|
||||||
|
"FREQ": "mHz",
|
||||||
"EAST": "Wh",
|
"EAST": "Wh",
|
||||||
"EASF01": "Wh",
|
"EASF01": "Wh",
|
||||||
"EASF02": "Wh",
|
"EASF02": "Wh",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user