ec65f06cdaeaf2f5dd895196995cf8ec08bfa325
[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 )
26
27 //-------
28
29 func init() {
30     flag.StringVar(&xmpp_login_.jid, "xjid", "realrauminfo@realraum.at/Tuer", "XMPP Bot Login JID")
31     flag.StringVar(&xmpp_login_.pass, "xpass", "", "XMPP Bot Login Password")
32     flag.StringVar(&xmpp_bot_authstring_, "xbotauth", "", "String that user use to authenticate themselves to the bot")
33     flag.StringVar(&presence_socket_path_,"presencesocket", "/var/run/tuer/presence.socket",  "Path to presence socket")
34     flag.StringVar(&xmpp_state_save_dir_,"xstatedir","/flash/var/lib/r3netstatus/",  "Directory to save XMPP bot state in")
35     flag.Parse()
36 }
37
38 //-------
39
40 func IfThenElseStr(c bool, strue, sfalse string) string {
41     if c {return strue} else {return sfalse}
42 }
43
44 func composeMessage(present, locked, shut bool, who string, ts int64) string {
45     return fmt.Sprintf("%s (Door is %s and %s and was last used%s at %s)",
46         IfThenElseStr(present,  "Somebody is present!" , "Everybody left."),
47         IfThenElseStr(locked, "locked","unlocked"),
48         IfThenElseStr(shut, "shut","ajar"),
49         IfThenElseStr(len(who) == 0,"", " by " + who),
50         time.Unix(ts,0).String())
51 }
52
53 func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) {
54     events := ps.Sub("presence","door","buttons")
55
56     defer func() {
57         if x := recover(); x != nil {
58             fmt.Printf("handleIncomingXMPPStanzas: run time panic: %v", x)
59             ps.Unsub(events, "presence","door","buttons")
60             close(xmpp_presence_events_chan_)
61         }
62     }()
63
64     var present, locked, shut bool = false, true, true
65     var who string
66     button_msg := "The button has been pressed ! Propably someone is bored and in need of company ! ;-)"
67
68     for eventinterface := range(events) {
69         switch event := eventinterface.(type) {
70             case PresenceUpdate:
71                 present = event.Present
72                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true}
73             case DoorCommandEvent:
74                 if len(event.Who) > 0 && len(event.Using) > 0 {
75                     who = fmt.Sprintf("%s (%s)",event.Who, event.Using)
76                 } else {
77                     who = event.Who
78                 }
79                 xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0))
80             case DoorStatusUpdate:
81                 locked = event.Locked
82                 shut = event.Shut
83                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
84             case ButtonPressUpdate:
85                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo}
86         }
87         }
88 }
89
90 func main() {
91     var xmpperr error
92     var bot *r3xmppbot.XmppBot
93     bot, xmpp_presence_events_chan_, xmpperr = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true)
94
95     newlinequeue := make(chan string, 1)
96     ps := pubsub.New(1)
97     //~ brn := brain.New()
98     defer close(newlinequeue)
99     defer ps.Shutdown()
100     //~ defer brn.Shutdown()
101
102     go EventToWeb(ps)
103     if xmpperr == nil {
104         defer bot.StopBot()
105         go EventToXMPP(ps, xmpp_presence_events_chan_)
106     } else {
107         fmt.Println(xmpperr)
108         fmt.Println("XMPP Bot disabled")
109     }
110     go ReadFromUSocket(presence_socket_path_, newlinequeue)
111     ticker := time.NewTicker(time.Duration(7) * time.Minute)
112
113     for {
114     select {
115         case e := <-newlinequeue:
116             ParseSocketInputLine(e, ps) //, brn)
117         case <-ticker.C:
118             ps.Pub(TimeTick{time.Now().Unix()}, "publishjson")
119         }
120     }
121 }