1 out of 4 pings
[svn42.git] / go / r3-netstatus / main.go
index cc63daa..0bb8d0e 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 {
@@ -108,7 +108,7 @@ func EventToXMPP(events <- chan interface{}, xmpp_presence_events_chan chan <- i
                 if last_frontdoor_ajar.Shut != event.Shut {
                     xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: fmt.Sprintf("Frontdoor is %s  (%s)",IfThenElseStr(event.Shut,"now shut.","ajar."),time.Unix(event.Ts,0).String()), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: false}
                 }
-               last_frontdoor_ajar = event
+                last_frontdoor_ajar = event
            case r3events.BackdoorAjarUpdate:
                 xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: fmt.Sprintf("Backdoor is %s  (%s)",IfThenElseStr(event.Shut,"now shut.","ajar!"),time.Unix(event.Ts,0).String()), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: false}
             case r3events.BoreDoomButtonPressEvent:
@@ -120,6 +120,10 @@ func EventToXMPP(events <- chan interface{}, xmpp_presence_events_chan chan <- i
                     xmpp_presence_events_chan <- present_status
                     last_buttonpress = 0
                 }
+                // Try to XMPP Ping the server and if that fails, quit XMPPBot
+                if bot.PingServer(900) == false && bot.PingServer(900) == false && bot.PingServer(900) == false && bot.PingServer(900) == false{ return }
+            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}
         }
        }
 }
@@ -128,17 +132,21 @@ 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())
         }
-        time.Sleep(5 * time.Second)
+        time.Sleep(10 * time.Second)
     }
 }
 
@@ -151,12 +159,12 @@ func ParseZMQr3Event(lines [][]byte, ps *pubsub.PubSub) {
 
 func QueryLatestEventsAndInjectThem(ps *pubsub.PubSub, zmqctx *zmq.Context) {
     answ := ZmqsAskQuestionsAndClose(zmqctx, brain_connect_addr_, [][][]byte{
-        [][]byte{[]byte("DoorCommandEvent")}, 
-        [][]byte{[]byte("DoorLockUpdate")}, 
-        [][]byte{[]byte("DoorAjarUpdate")}, 
-        [][]byte{[]byte("BackdoorAjarUpdate")}, 
+        [][]byte{[]byte("BackdoorAjarUpdate")},
+        [][]byte{[]byte("DoorCommandEvent")},
+        [][]byte{[]byte("DoorLockUpdate")},
+        [][]byte{[]byte("DoorAjarUpdate")},
         [][]byte{[]byte("PresenceUpdate")},
-        [][]byte{[]byte("IlluminationSensorUpdate")}, 
+        [][]byte{[]byte("IlluminationSensorUpdate")},
         [][]byte{[]byte("TempSensorUpdate")}})
     for _, a := range(answ) {
         ParseZMQr3Event(a, ps)
@@ -183,10 +191,10 @@ func main() {
     go EventToWeb(ps)
     // --- get update on most recent events ---
     QueryLatestEventsAndInjectThem(ps, zmqctx)
-    go RunXMPPBot(ps, zmqctx) 
+    go RunXMPPBot(ps, zmqctx)
 
     // --- receive and distribute events ---
-    ticker := time.NewTicker(time.Duration(7) * time.Minute)
+    ticker := time.NewTicker(time.Duration(6) * time.Minute)
     for {
     select {
         case e := <-zmqsub.In():