store events received in a session
@@ -34,6 +34,7 @@ type Session struct {
func newSessionHandler(s *Session) *handler.MessageHandler {
handler := handler.NewMessageHandler()
handler.AddHandler(types.KeepaliveType, s.handleKeepalive)
+ handler.AddHandler(types.EventType, s.handleEvent)
return handler
}
@@ -226,3 +227,22 @@ func (s *Session) handleKeepalive(payload []byte) error {
log.Println("handling keepalive: ", *keepalive)
return s.store.UpdateEntity(keepalive.Entity)
}
+
+func (s *Session) handleEvent(payload []byte) error {
+ event := &types.Event{}
+ err := json.Unmarshal(payload, event)
+ if err != nil {
+ return err
+ }
+
+ if event.Entity == nil {
+ return errors.New("event contains no entity")
+ }
+
+ if event.Check == nil {
+ return errors.New("event contains no check")
+ }
+
+ return s.store.UpdateEvent(event)
+
+}
