commit 915ea9063f56cb994c124a765f18896093a352f2 Author: Norwin Roosen Date: Sat Jun 23 14:25:00 2018 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..262fbc8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +osem_notify +osem_notify.exe diff --git a/Box.go b/Box.go new file mode 100644 index 0000000..d75e23c --- /dev/null +++ b/Box.go @@ -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 +} diff --git a/OsemClient.go b/OsemClient.go new file mode 100644 index 0000000..c575057 --- /dev/null +++ b/OsemClient.go @@ -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 +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..8a9dd0f --- /dev/null +++ b/main.go @@ -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 +} diff --git a/notifiers.go b/notifiers.go new file mode 100644 index 0000000..c8bdead --- /dev/null +++ b/notifiers.go @@ -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"` +}