show errors
authorBernhard Tittelbach <xro@realraum.at>
Thu, 7 Nov 2013 18:01:22 +0000 (18:01 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Thu, 7 Nov 2013 18:01:22 +0000 (18:01 +0000)
go/r3-netstatus/main.go
go/r3-netstatus/r3xmppbot/r3xmppbot.go

index c897e29..66763ff 100644 (file)
@@ -66,7 +66,7 @@ func composeDoorLockMessage(locked bool, frontshut r3events.DoorAjarUpdate, door
     }
 }
 
-func EventToXMPP(events <- chan interface{}, xmpp_presence_events_chan chan <- interface{}) {
+func EventToXMPP(bot *r3xmppbot.XmppBot, events <- chan interface{}, xmpp_presence_events_chan chan <- interface{}) {
 
     defer func() {
         if x := recover(); x != nil {
@@ -120,6 +120,7 @@ func EventToXMPP(events <- chan interface{}, xmpp_presence_events_chan chan <- i
                     xmpp_presence_events_chan <- present_status
                     last_buttonpress = 0
                 }
+                
             case r3events.DoorProblemEvent:
                 xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: fmt.Sprintf("Door Problem: %s. SeverityLevel: %d (%s)",event.Problem, event.Severity, time.Unix(event.Ts,0).String()), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: false}
         }
@@ -130,12 +131,16 @@ func RunXMPPBot(ps *pubsub.PubSub, zmqctx *zmq.Context) {
     var xmpperr error
     var bot *r3xmppbot.XmppBot
     var xmpp_presence_events_chan chan interface{}
-    psevents := ps.Sub("presence","door","buttons","updateinterval")
     for {
         bot, xmpp_presence_events_chan, xmpperr = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true)
         if xmpperr == nil {
+            Syslog_.Printf("Successfully (re)started XMPP Bot")
+            // subscribe before QueryLatestEventsAndInjectThem and EventToXMPP
+            psevents := ps.Sub("presence","door","buttons","updateinterval")
             QueryLatestEventsAndInjectThem(ps, zmqctx)
-            EventToXMPP(psevents, xmpp_presence_events_chan)
+            EventToXMPP(bot, psevents, xmpp_presence_events_chan)
+            // unsubscribe right away, since we don't known when reconnect will succeed and we don't want to block PubSub
+            ps.Unsub(psevents, "presence","door","buttons","updateinterval")
             bot.StopBot()
         } else {
             Syslog_.Printf("Error starting XMPP Bot: %s", xmpperr.Error())
index 293be18..34a1472 100644 (file)
@@ -136,6 +136,7 @@ type XmppBot struct {
     my_login_password_ string
     xmppclient_ *xmpp.Client
     presence_events_ *chan interface{}
+    ping_reply_ chan bool
 }
 
 
@@ -272,6 +273,9 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout
     if inmsg.Body == nil || inmsg.GetHeader() == nil {
         return
     }
+    if inmsg.GetHeader().Error != nil {
+        Syslog_.Printf("XMPP Message Error: %s", inmsg.GetHeader().Error.Error())
+    }
     bodytext :=inmsg.Body.Chardata
     if botdata.isAuthenticated(inmsg.GetHeader().From) {
         switch bodytext {
@@ -336,8 +340,9 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
             case *xmpp.Message:
                 botdata.handleIncomingMessageDialog(*stanza, xmppout, jabber_events)
             case *xmpp.Presence:
-                if stanza.GetHeader() == nil {
-                    continue
+                if stanza.GetHeader() == nil { continue }
+                if stanza.GetHeader().Error != nil {
+                    Syslog_.Printf("XMPP Presence Error: %s", stanza.GetHeader().Error.Error())
                 }
                 switch stanza.GetHeader().Type {
                     case "subscribe":
@@ -355,8 +360,9 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
                         jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false}
                 }
             case *xmpp.Iq:
-                if stanza.GetHeader() == nil {
-                    continue
+                if stanza.GetHeader() == nil { continue }
+                if stanza.GetHeader().Error != nil {
+                    Syslog_.Printf("XMPP Iq Error: %s", stanza.GetHeader().Error.Error())
                 }
         }
     }
@@ -373,6 +379,7 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure
     botdata.my_jid_ = loginjid
     botdata.my_login_password_ = loginpwd
     botdata.auth_timeout_ = 3600*2
+    botdata.ping_reply_ = make(chan bool)
 
     botdata.config_file_ = path.Join(state_save_dir, "r3xmpp."+removeJIDResource(loginjid)+".json")