ff94189eb58a5fa9f2f214b9dccb10d3ed64898d
[svn42.git] / go / r3-netstatus / main.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     "./r3xmppbot"
7     pubsub "github.com/tuxychandru/pubsub"
8     "flag"
9     "time"
10     "fmt"
11     //~ "./brain"
12     r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/r3events"
13 )
14
15 type SpaceState struct {
16     present           bool
17     buttonpress_until int64
18     door_locked bool
19     door_shut bool
20 }
21
22 var (
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 )
30
31 //-------
32
33 func init() {
34     flag.StringVar(&xmpp_login_.jid, "xjid", "realrauminfo@realraum.at/Tuer", "XMPP Bot Login JID")
35     flag.StringVar(&xmpp_login_.pass, "xpass", "", "XMPP Bot Login Password")
36     flag.StringVar(&xmpp_bot_authstring_, "xbotauth", "", "String that user use to authenticate themselves to the bot")
37     flag.StringVar(&xmpp_state_save_dir_,"xstatedir","/flash/var/lib/r3netstatus/",  "Directory to save XMPP bot state in")
38     flag.StringVar(&r3eventssub_port_, "eventsubport", "tcp://wuzzler.realraum.at:4244", "zmq address to subscribe r3events")
39     flag.Parse()
40 }
41
42 //-------
43
44 func IfThenElseStr(c bool, strue, sfalse string) string {
45     if c {return strue} else {return sfalse}
46 }
47
48 func composeMessage(present, locked, shut bool, who string, ts int64) string {
49     return fmt.Sprintf("%s (Door is %s and %s and was last used%s at %s)",
50         IfThenElseStr(present,  "Somebody is present!" , "Everybody left."),
51         IfThenElseStr(locked, "locked","unlocked"),
52         IfThenElseStr(shut, "shut","ajar"),
53         IfThenElseStr(len(who) == 0,"", " by " + who),
54         time.Unix(ts,0).String())
55 }
56
57 func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) {
58     events := ps.Sub("presence","door","buttons","updateinterval")
59
60     defer func() {
61         if x := recover(); x != nil {
62             fmt.Printf("handleIncomingXMPPStanzas: run time panic: %v", x)
63             ps.Unsub(events, "presence","door","buttons","updateinterval")
64             close(xmpp_presence_events_chan_)
65         }
66     }()
67
68     var present, locked, shut bool = false, true, true
69     var last_buttonpress int64 = 0
70     var who string
71     button_msg := "The button has been pressed ! Propably someone is bored and in need of company ! ;-)"
72     present_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowOnline,"Somebody is present"}
73     notpresent_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe,"Nobody is here"}
74     button_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowFreeForChat, "The button has been pressed :-)"}
75
76     xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"}
77
78     for eventinterface := range(events) {
79         fmt.Println("event2xmpp", eventinterface)
80         switch event := eventinterface.(type) {
81             case r3events.PresenceUpdate:
82                 present = event.Present
83                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true}
84                 if present {
85                     xmpp_presence_events_chan_ <- present_status
86                 } else {
87                     xmpp_presence_events_chan_ <- notpresent_status
88                 }
89             case r3events.DoorCommandEvent:
90                 if len(event.Who) > 0 && len(event.Using) > 0 {
91                     who = fmt.Sprintf("%s (%s)",event.Who, event.Using)
92                 } else {
93                     who = event.Who
94                 }
95                 xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0))
96             case r3events.DoorLockUpdate:
97                 locked = event.Locked
98                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
99            case r3events.DoorAjarUpdate:
100                 shut = event.Shut
101                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
102             case r3events.ButtonPressUpdate:
103                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo}
104                 xmpp_presence_events_chan_ <- button_status
105                 last_buttonpress = event.Ts
106             case r3events.TimeTick:
107                 if present && last_buttonpress > 0 && time.Now().Unix() - last_buttonpress > button_press_timeout_ {
108                     xmpp_presence_events_chan_ <- present_status
109                     last_buttonpress = 0
110                 }
111         }
112         }
113 }
114
115 func main() {
116     zmqctx, zmqsub := ZmqsInit(r3eventssub_port_)
117     defer zmqctx.Close()
118     if zmqsub != nil {defer zmqsub.Close()}
119     if zmqsub == nil {
120         panic("zmq sockets must not be nil !!")
121     }
122
123     var xmpperr error
124     var bot *r3xmppbot.XmppBot
125     bot, xmpp_presence_events_chan_, xmpperr = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true)
126     ps := pubsub.New(1)
127     defer ps.Shutdown()
128     //~ brn := brain.New()
129     //~ defer brn.Shutdown()
130
131     go EventToWeb(ps)
132     if xmpperr == nil {
133         defer bot.StopBot()
134         go EventToXMPP(ps, xmpp_presence_events_chan_)
135     } else {
136         fmt.Println(xmpperr)
137         fmt.Println("XMPP Bot disabled")
138     }
139
140     ticker := time.NewTicker(time.Duration(7) * time.Minute)
141
142     for {
143     select {
144         case e := <-zmqsub.In():
145             ParseZMQr3Event(e, ps) //, brn)
146         case <-ticker.C:
147             ps.Pub(r3events.TimeTick{time.Now().Unix()}, "updateinterval")
148         }
149     }
150 }