+func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) {
+ events := ps.Sub("presence","door","buttons","updateinterval")
+
+ defer func() {
+ if x := recover(); x != nil {
+ fmt.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 last_buttonpress int64 = 0
+ var who string
+ button_msg := "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 :-)"}
+
+ xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"}
+
+ for eventinterface := range(events) {
+ switch event := eventinterface.(type) {
+ case 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 DoorCommandEvent:
+ if len(event.Who) > 0 && len(event.Using) > 0 {
+ who = fmt.Sprintf("%s (%s)",event.Who, event.Using)
+ } else {
+ who = event.Who
+ }
+ xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0))
+ case DoorStatusUpdate:
+ locked = event.Locked
+ shut = event.Shut
+ xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
+ case ButtonPressUpdate:
+ xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo}
+ xmpp_presence_events_chan_ <- button_status
+ last_buttonpress = event.Ts
+ case TimeTick:
+ if present && last_buttonpress > 0 && time.Now().Unix() - last_buttonpress > button_press_timeout_ {
+ xmpp_presence_events_chan_ <- present_status
+ last_buttonpress = 0
+ }
+ }