1 // (c) Bernhard Tittelbach, 2013
7 pubsub "github.com/tuxychandru/pubsub"
12 r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/r3events"
15 type SpaceState struct {
17 buttonpress_until int64
23 xmpp_presence_events_chan_ chan interface{}
24 xmpp_login_ struct {jid string; pass string}
25 xmpp_bot_authstring_ string
26 xmpp_state_save_dir_ string
27 r3eventssub_port_ string
28 button_press_timeout_ int64 = 3600
29 brain_connect_addr_ string
35 flag.StringVar(&xmpp_login_.jid, "xjid", "realrauminfo@realraum.at/Tuer", "XMPP Bot Login JID")
36 flag.StringVar(&xmpp_login_.pass, "xpass", "", "XMPP Bot Login Password")
37 flag.StringVar(&xmpp_bot_authstring_, "xbotauth", "", "String that user use to authenticate themselves to the bot")
38 flag.StringVar(&xmpp_state_save_dir_,"xstatedir","/flash/var/lib/r3netstatus/", "Directory to save XMPP bot state in")
39 flag.StringVar(&r3eventssub_port_, "eventsubport", "tcp://wuzzler.realraum.at:4244", "zmq address to subscribe r3events")
40 flag.StringVar(&brain_connect_addr_, "brainconnect", "tcp://wuzzler.realraum.at:4245", "address to ask about most recent stored events")
46 func IfThenElseStr(c bool, strue, sfalse string) string {
47 if c {return strue} else {return sfalse}
50 func composeMessage(present, locked, shut bool, who string, ts int64) string {
51 return fmt.Sprintf("%s (Door is %s and %s and was last used%s at %s)",
52 IfThenElseStr(present, "Somebody is present!" , "Everybody left."),
53 IfThenElseStr(locked, "locked","unlocked"),
54 IfThenElseStr(shut, "shut","ajar"),
55 IfThenElseStr(len(who) == 0,"", " by " + who),
56 time.Unix(ts,0).String())
59 func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) {
60 events := ps.Sub("presence","door","buttons","updateinterval")
63 if x := recover(); x != nil {
64 fmt.Printf("handleIncomingXMPPStanzas: run time panic: %v", x)
65 ps.Unsub(events, "presence","door","buttons","updateinterval")
66 close(xmpp_presence_events_chan_)
70 var present, locked, shut bool = false, true, true
71 var last_buttonpress int64 = 0
73 button_msg := "Dooom ! The button has been pressed ! Propably someone is bored and in need of company ! ;-)"
74 present_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowOnline,"Somebody is present"}
75 notpresent_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe,"Nobody is here"}
76 button_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowFreeForChat, "The button has been pressed :-)"}
78 xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"}
80 for eventinterface := range(events) {
81 fmt.Println("event2xmpp", eventinterface)
82 switch event := eventinterface.(type) {
83 case r3events.PresenceUpdate:
84 present = event.Present
85 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true}
87 xmpp_presence_events_chan_ <- present_status
89 xmpp_presence_events_chan_ <- notpresent_status
91 case r3events.DoorCommandEvent:
92 if len(event.Who) > 0 && len(event.Using) > 0 {
93 who = fmt.Sprintf("%s (%s)",event.Who, event.Using)
97 xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0))
98 case r3events.DoorLockUpdate:
100 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
101 case r3events.DoorAjarUpdate:
103 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
104 case r3events.BoreDoomButtonPressEvent:
105 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo}
106 xmpp_presence_events_chan_ <- button_status
107 last_buttonpress = event.Ts
108 case r3events.TimeTick:
109 if present && last_buttonpress > 0 && time.Now().Unix() - last_buttonpress > button_press_timeout_ {
110 xmpp_presence_events_chan_ <- present_status
118 zmqctx, zmqsub := ZmqsInit(r3eventssub_port_)
120 if zmqsub != nil {defer zmqsub.Close()}
122 panic("zmq sockets must not be nil !!")
126 var bot *r3xmppbot.XmppBot
127 bot, xmpp_presence_events_chan_, xmpperr = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true)
131 //~ brn := brain.New()
132 //~ defer brn.Shutdown()
137 go EventToXMPP(ps, xmpp_presence_events_chan_)
140 fmt.Println("XMPP Bot disabled")
143 // --- get update on most recent events ---
144 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")}})
145 for _, a := range(answ) {
146 //~ fmt.Println("recent event:", a)
147 ParseZMQr3Event(a, ps)
150 // --- receive and distribute events ---
151 ticker := time.NewTicker(time.Duration(7) * time.Minute)
154 case e := <-zmqsub.In():
155 ParseZMQr3Event(e, ps) //, brn)
157 ps.Pub(r3events.TimeTick{time.Now().Unix()}, "updateinterval")