X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=go%2Fr3-netstatus%2Fmain.go;h=7841399a6120f51b5955ba075571dffa1f9fbe95;hb=0eff4b5b15229a5efe0088de99b1b1ea59f8ad7a;hp=d5b3e2fcfc8b00b382aafc7edc62a12b52ebf5dd;hpb=fa43684a042825be410260f547e092be3b432317;p=svn42.git diff --git a/go/r3-netstatus/main.go b/go/r3-netstatus/main.go index d5b3e2f..7841399 100644 --- a/go/r3-netstatus/main.go +++ b/go/r3-netstatus/main.go @@ -40,7 +40,7 @@ func init() { flag.StringVar(&r3eventssub_port_, "eventsubport", "tcp://wuzzler.realraum.at:4244", "zmq address to subscribe r3events") flag.StringVar(&brain_connect_addr_, "brainconnect", "tcp://wuzzler.realraum.at:4245", "address to ask about most recent stored events") flag.BoolVar(&enable_syslog_, "syslog", false, "enable logging to syslog") - flag.BoolVar(&enable_debug_, "debug", false, "enable debug logging") + flag.BoolVar(&enable_debug_, "debug", false, "enable debug output") flag.Parse() } @@ -50,13 +50,20 @@ func IfThenElseStr(c bool, strue, sfalse string) string { if c {return strue} else {return sfalse} } -func composeMessage(present, locked, shut bool, who string, ts int64) string { - return fmt.Sprintf("%s (Door is %s and %s and was last used%s at %s)", - IfThenElseStr(present, "Somebody is present!" , "Everybody left."), - IfThenElseStr(locked, "locked","unlocked"), - IfThenElseStr(shut, "shut","ajar"), - IfThenElseStr(len(who) == 0,"", " by " + who), - time.Unix(ts,0).String()) +func composeDoorLockMessage(locked, frontshut bool, doorcmd r3events.DoorCommandEvent, ts int64) string { + var ajarstring string = ""; + if ! frontshut { + ajarstring = " (still ajar)" + } + if ts - doorcmd.Ts < 30 { + if len(doorcmd.Who) == 0 || doorcmd.Who == "-" { + return fmt.Sprintf("The%s frontdoor was %s by %s at %s.",ajarstring, IfThenElseStr(locked, "locked","unlocked"), doorcmd.Using, time.Unix(ts,0).String()) + } else { + return fmt.Sprintf("%s %s the%s frontdoor by %s at %s.",doorcmd.Who, IfThenElseStr(locked, "locked","unlocked"), ajarstring, doorcmd.Using, time.Unix(ts,0).String()) + } + } else { + return fmt.Sprintf("The%s frontdoor was %s manually at %s.", ajarstring, IfThenElseStr(locked, "locked","unlocked"), time.Unix(ts,0).String()) + } } func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) { @@ -64,15 +71,15 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface defer func() { if x := recover(); x != nil { - Debug_.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) + Syslog_.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) ps.Unsub(events, "presence","door","buttons","updateinterval") close(xmpp_presence_events_chan_) } }() - var present, locked, shut bool = false, true, true + var present, frontlock, frontshut bool = false, true, true var last_buttonpress int64 = 0 - var who string + var last_door_cmd r3events.DoorCommandEvent; 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"} @@ -85,25 +92,26 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface switch event := eventinterface.(type) { case r3events.PresenceUpdate: present = event.Present - xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true} if present { xmpp_presence_events_chan_ <- present_status } else { xmpp_presence_events_chan_ <- notpresent_status } case r3events.DoorCommandEvent: - if len(event.Who) > 0 && len(event.Using) > 0 { - who = fmt.Sprintf("%s (%s)",event.Who, event.Using) - } else { - who = event.Who - } + 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: - locked = event.Locked - xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true} + if frontlock != event.Locked { + xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeDoorLockMessage(event.Locked, frontshut, last_door_cmd, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true} + } + frontlock = event.Locked case r3events.DoorAjarUpdate: - shut = event.Shut - xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true} + if frontshut != 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} + } + frontshut = event.Shut + 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: xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo} xmpp_presence_events_chan_ <- button_status @@ -156,7 +164,7 @@ func main() { } // --- get update on most recent events --- - answ := ZmqsAskQuestionsAndClose(zmqctx, brain_connect_addr_, [][][]byte{[][]byte{[]byte("DoorLockUpdate")}, [][]byte{[]byte("DoorAjarUpdate")}, [][]byte{[]byte("DoorCommandEvent")}, [][]byte{[]byte("PresenceUpdate")}, [][]byte{[]byte("IlluminationSensorUpdate")}, [][]byte{[]byte("TempSensorUpdate")}}) + answ := ZmqsAskQuestionsAndClose(zmqctx, brain_connect_addr_, [][][]byte{[][]byte{[]byte("DoorCommandEvent")}, [][]byte{[]byte("DoorLockUpdate")}, [][]byte{[]byte("DoorAjarUpdate")}, [][]byte{[]byte("PresenceUpdate")}, [][]byte{[]byte("IlluminationSensorUpdate")}, [][]byte{[]byte("TempSensorUpdate")}}) for _, a := range(answ) { ParseZMQr3Event(a, ps) }