X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=go%2Fr3-netstatus%2Fmain.go;h=cd880ed6db80880653bbb791f6418c755985683c;hb=3ccab0493b7a182ef2efce4cfffb1e5107eab14b;hp=cc63daa1b4f66f9933b7ce8e9983b853b87673f7;hpb=cb2342c75d929f11f901e6f8e3d44f6bdc0261b4;p=svn42.git diff --git a/go/r3-netstatus/main.go b/go/r3-netstatus/main.go index cc63daa..cd880ed 100644 --- a/go/r3-netstatus/main.go +++ b/go/r3-netstatus/main.go @@ -30,6 +30,8 @@ var ( enable_syslog_, enable_debug_ bool ) +type EventToXMPPStartupFinished struct{} + //------- func init() { @@ -66,8 +68,25 @@ func composeDoorLockMessage(locked bool, frontshut r3events.DoorAjarUpdate, door } } -func EventToXMPP(events <- chan interface{}, xmpp_presence_events_chan chan <- interface{}) { +func composePresence(present bool, temp_cx float64, light_lothr, last_buttonpress int64) r3xmppbot.XMPPStatusEvent { + present_msg := "Somebody is present" + notpresent_msg := "Nobody is here" + button_msg := "The button has been pressed :-)" + msg := "%s (CX: %.2f°C, LoTHR light: %d)" + + if present { + if last_buttonpress > 0 && time.Now().Unix() - last_buttonpress < button_press_timeout_ { + return r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowFreeForChat, fmt.Sprintf(msg, button_msg, temp_cx, light_lothr)} + } else { + return r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowOnline, fmt.Sprintf(msg, present_msg, temp_cx, light_lothr)} + } + } else { + return r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, fmt.Sprintf(msg, notpresent_msg, temp_cx, light_lothr)} + } +} +func EventToXMPP(bot *r3xmppbot.XmppBot, events <- chan interface{}, xmpp_presence_events_chan chan <- interface{}) { + button_msg := "Dooom ! The button has been pressed ! Propably someone is bored and in need of company ! ;-)" defer func() { if x := recover(); x != nil { //defer ist called _after_ EventToXMPP function has returned. Thus we recover after returning from this function. @@ -76,50 +95,58 @@ func EventToXMPP(events <- chan interface{}, xmpp_presence_events_chan chan <- i }() var present, frontlock bool = false, true - var last_buttonpress int64 = 0 - var last_door_cmd r3events.DoorCommandEvent; - var last_frontdoor_ajar r3events.DoorAjarUpdate = r3events.DoorAjarUpdate{true, 0}; - button_msg := "Dooom ! The button has been pressed ! Propably someone is bored and in need of company ! ;-)" - present_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowOnline,"Somebody is present"} - notpresent_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe,"Nobody is here"} - button_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowFreeForChat, "The button has been pressed :-)"} + var last_buttonpress, light_lothr int64 = 0, 0 + var temp_cx float64 = 0.0 + var last_door_cmd r3events.DoorCommandEvent + var last_frontdoor_ajar r3events.DoorAjarUpdate = r3events.DoorAjarUpdate{true, 0} + var standard_distribute_level r3xmppbot.R3JIDDesire = r3xmppbot.R3DebugInfo // initial state, changed after startup finished event recieved xmpp_presence_events_chan <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"} for eventinterface := range(events) { Debug_.Printf("event2xmpp: %T %+v", eventinterface, eventinterface) switch event := eventinterface.(type) { + case EventToXMPPStartupFinished: + //after we received all events from QueryLatestEventsAndInjectThem, we get this event and start sending new events normally + standard_distribute_level = r3xmppbot.R3OnlineOnlyInfo case r3events.PresenceUpdate: present = event.Present - if present { - xmpp_presence_events_chan <- present_status - } else { - xmpp_presence_events_chan <- notpresent_status - } + if ! present { last_buttonpress = 0 } + xmpp_presence_events_chan <- composePresence(present, temp_cx, light_lothr, last_buttonpress) case r3events.DoorCommandEvent: last_door_cmd = event xmpp_presence_events_chan <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0)) case r3events.DoorLockUpdate: if frontlock != event.Locked { - xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: composeDoorLockMessage(event.Locked, last_frontdoor_ajar, last_door_cmd, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true} + xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: composeDoorLockMessage(event.Locked, last_frontdoor_ajar, last_door_cmd, event.Ts), DistributeLevel:standard_distribute_level, RememberAsStatus: true} } frontlock = event.Locked case r3events.DoorAjarUpdate: 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} + 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: standard_distribute_level, RememberAsStatus: false} + case r3events.GasLeakAlert: + xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: fmt.Sprintf("ALERT !! GasLeak Detected !!! (%s)", time.Unix(event.Ts,0).String()), DistributeLevel: r3xmppbot.R3NeverInfo, RememberAsStatus: false} + case r3events.IlluminationSensorUpdate: + light_lothr = event.Value + case r3events.TempSensorUpdate: + if event.Sensorindex == 1 { + temp_cx = event.Value + } case r3events.BoreDoomButtonPressEvent: - xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo} - xmpp_presence_events_chan <- button_status last_buttonpress = event.Ts + xmpp_presence_events_chan <- composePresence(present, temp_cx, light_lothr, last_buttonpress) + xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: standard_distribute_level} case r3events.TimeTick: - if present && last_buttonpress > 0 && time.Now().Unix() - last_buttonpress > button_press_timeout_ { - xmpp_presence_events_chan <- present_status - last_buttonpress = 0 - } + // update presence text with sensor and button info + xmpp_presence_events_chan <- composePresence(present, temp_cx, light_lothr, last_buttonpress) + // 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 && 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 +155,22 @@ 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","sensors","xmppmeta") QueryLatestEventsAndInjectThem(ps, zmqctx) - EventToXMPP(psevents, xmpp_presence_events_chan) + ps.Pub(EventToXMPPStartupFinished{}, "xmppmeta") + 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","sensors","xmppmeta") bot.StopBot() } else { Syslog_.Printf("Error starting XMPP Bot: %s", xmpperr.Error()) } - time.Sleep(5 * time.Second) + time.Sleep(10 * time.Second) } } @@ -151,12 +183,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 +215,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():