Initial commit
commit
915ea9063f
@ -0,0 +1,2 @@
|
|||||||
|
osem_notify
|
||||||
|
osem_notify.exe
|
@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type NotifyConfig struct {
|
||||||
|
// Transports interface{} `json:"transports"`
|
||||||
|
Events []NotifyEvent `json:"events"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Box struct {
|
||||||
|
Id string `json:"_id"`
|
||||||
|
Sensors []struct {
|
||||||
|
Id string `json:"_id"`
|
||||||
|
LastMeasurement *struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
Date string `json:"createdAt"`
|
||||||
|
} `json:"lastMeasurement"`
|
||||||
|
} `json:"sensors"`
|
||||||
|
NotifyConf *NotifyConfig `json:"notify"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (box Box) runChecks() ([]Notification, error) {
|
||||||
|
// must return ALL events to enable Notifier to clear previous notifications
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (box Box) getNotifier() (AbstractNotifier, error) {
|
||||||
|
// validate box.NotifyConf.transport
|
||||||
|
|
||||||
|
// try to get notifier state from persistence
|
||||||
|
|
||||||
|
// return
|
||||||
|
var notifier AbstractNotifier
|
||||||
|
return notifier, nil
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"github.com/dghubble/sling"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OsemClient struct {
|
||||||
|
sling *sling.Sling
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOsemClient(client *http.Client) *OsemClient {
|
||||||
|
return &OsemClient{
|
||||||
|
sling: sling.New().Client(client).Base("https://api.opensensemap.org/"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *OsemClient) GetBox(boxId string) (Box, error) {
|
||||||
|
box := Box{}
|
||||||
|
_, err := client.sling.New().Path("boxes/").Path(boxId).ReceiveSuccess(&box)
|
||||||
|
return box, err
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/carlescere/scheduler"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkBox(boxId string, defaultConf *NotifyConfig) {
|
||||||
|
boxLogger := log.WithFields(log.Fields{"boxId": boxId})
|
||||||
|
boxLogger.Debug("checking box for due notifications")
|
||||||
|
|
||||||
|
// get box data
|
||||||
|
box, err := osem.GetBox(boxId)
|
||||||
|
if err != nil {
|
||||||
|
boxLogger.Error("could not fetch box: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// if box has no notify config, we use the defaultConf
|
||||||
|
if box.NotifyConf == nil {
|
||||||
|
box.NotifyConf = defaultConf
|
||||||
|
}
|
||||||
|
boxLogger.Debug(box.NotifyConf)
|
||||||
|
|
||||||
|
// run checks
|
||||||
|
notifications, err2 := box.runChecks()
|
||||||
|
if err2 != nil {
|
||||||
|
boxLogger.Error("could not run checks on box: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if notifications == nil {
|
||||||
|
boxLogger.Debug("all is fine")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// store notifications for later submit
|
||||||
|
// notifier, err3 := box.getNotifier()
|
||||||
|
// if err3 != nil {
|
||||||
|
// boxLogger.Error("could not get notifier for box: ", err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// notifier.AddNotifications(notifications)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkNotifications() {
|
||||||
|
log.Info("running job checkNotifications()")
|
||||||
|
checkBox("593bcd656ccf3b0011791f5a", defaultConf)
|
||||||
|
}
|
||||||
|
|
||||||
|
var osem = NewOsemClient(&http.Client{})
|
||||||
|
var defaultConf = &NotifyConfig{
|
||||||
|
// Transports: struct {
|
||||||
|
// Slack: SlackConfig{
|
||||||
|
// Channel: "asdf"
|
||||||
|
// Token: "qwer"
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
Events: []NotifyEvent{
|
||||||
|
NotifyEvent{
|
||||||
|
Type: "measurementAge",
|
||||||
|
Target: "593bcd656ccf3b0011791f5d",
|
||||||
|
Threshold: "5h",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
log.SetOutput(os.Stdout)
|
||||||
|
// log.SetFormatter(&log.JSONFormatter{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
scheduler.Every(15).Seconds().Run(checkNotifications)
|
||||||
|
// scheduler.Every(30).Seconds().Run(submitNotifications)
|
||||||
|
runtime.Goexit() // keep runtime running
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type AbstractNotifier interface {
|
||||||
|
GetName() string
|
||||||
|
SetupTransport(config interface{}) error
|
||||||
|
AddNotifications(notifications []Notification) error
|
||||||
|
SendNotifications() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Notification struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: multiple transports? one transport per event? (??)
|
||||||
|
type SlackConfig struct{}
|
||||||
|
|
||||||
|
type NotifyEvent struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Target string `json:"target"`
|
||||||
|
Threshold string `json:"threshold"`
|
||||||
|
}
|
Loading…
Reference in New Issue