Rewrite camera uptime monitor in Golang
This commit is contained in:
1 parent
fd4c30f655
commit
90efd78669
13 files changed
+284
-1
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
type NtfyNotifier struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewNtfyNotifier() NtfyNotifier {
|
||||
return NtfyNotifier{
|
||||
client: &http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
func (n NtfyNotifier) SendNotification(camera string, online bool) {
|
||||
type ntfyJson struct {
|
||||
Topic string `json:"topic"`
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
Priority int `json:"priority"`
|
||||
Click string `json:"click"`
|
||||
Icon string `json:"icon"`
|
||||
}
|
||||
|
||||
var reqJson, jsonErr = json.Marshal(ntfyJson{
|
||||
Topic: "frigate_camera_uptime",
|
||||
Title: "Frigate",
|
||||
Message: createMessage(camera, online),
|
||||
Priority: 3,
|
||||
Click: fmt.Sprintf("https://frigate.homelab.net/cameras/%s", camera),
|
||||
Icon: "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/frigate.png",
|
||||
})
|
||||
if jsonErr != nil {
|
||||
slog.Error("Failed to construct JSON request body", jsonErr)
|
||||
return
|
||||
}
|
||||
|
||||
var req, reqErr = http.NewRequest("POST", "https://ntfy.homelab.net", bytes.NewBuffer([]byte(reqJson)))
|
||||
if reqErr != nil {
|
||||
slog.Error("Failed to create HTTP request", reqErr)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
var resp, respErr = n.client.Do(req)
|
||||
if respErr != nil {
|
||||
slog.Error("Failed to send HTTP request", respErr)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
slog.Error("Ntfy notification returned HTTP", resp.Status)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user