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