th7/api/service.go

46 lines
799 B
Go
Raw Normal View History

2023-12-12 21:07:04 +00:00
package api
import (
"log"
"th7/data/core"
"th7/ports"
"time"
"encoding/json"
)
const (
2023-12-13 08:15:23 +00:00
CheckChannelDur = time.Millisecond * 1200
2023-12-12 21:07:04 +00:00
StartupSleepDur = time.Second * 5
)
2023-12-13 08:15:23 +00:00
type Wrapper struct {
Channels []core.Channel `json:"channels"`
Ratio core.Ratio `json:"ratio"`
Timestamp string `json:"time"`
2023-12-12 21:07:04 +00:00
}
2023-12-13 08:15:23 +00:00
// Regularly get the newest data and bung it out on ws
2023-12-12 21:07:04 +00:00
func startService(man *Manager, core ports.CorePort) {
2023-12-13 08:15:23 +00:00
var wrap Wrapper
2023-12-12 21:07:04 +00:00
time.Sleep(StartupSleepDur)
for {
2023-12-13 08:15:23 +00:00
wrap.Channels = core.GetChannels()
wrap.Ratio = core.GetRatio()
wrap.Timestamp = time.Now().Format(time.RFC1123)
marshaled, err := json.Marshal(wrap)
2023-12-12 21:07:04 +00:00
if err != nil {
2023-12-13 08:15:23 +00:00
log.Println("Error marshaling struct:", wrap)
2023-12-12 21:07:04 +00:00
}
2023-12-13 08:15:23 +00:00
man.broadcast <- []byte(marshaled)
2023-12-12 21:07:04 +00:00
time.Sleep(CheckChannelDur)
}
}