presence subscription both ways
[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 EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) {
41     events := ps.Sub("presence","door","buttons")
42     var present, locked, shut bool = false, true, true
43     var who string = "Unknown"
44
45     for eventinterface := range(events) {
46         switch event := eventinterface.(type) {
47             case PresenceUpdate:
48                 present = event.Present
49                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Ts: event.Ts}
50             case DoorCommandEvent:
51                 who = event.Who
52                 xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0))
53             case DoorStatusUpdate:
54                 locked = event.Locked
55                 shut = event.Shut
56                 xmpp_presence_events_chan_ <- fmt.Sprintln("DoorStatus:",r3xmppbot.IfThenElseStr(event.Locked, "locked","unlocked"), r3xmppbot.IfThenElseStr(event.Shut, "shut","ajar"), time.Unix(event.Ts,0))
57             case ButtonPressUpdate:
58                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Button: true, Ts: event.Ts}
59         }
60         }
61 }
62
63 func main() {
64     var err error
65     var bot *r3xmppbot.XmppBot
66     bot, xmpp_presence_events_chan_, err = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true)
67     if err != nil {
68         fmt.Println(err)
69         return
70     }
71     defer bot.StopBot()
72
73     newlinequeue := make(chan string, 1)
74     ps := pubsub.New(1)
75     //~ brn := brain.New()
76     defer close(newlinequeue)
77     defer ps.Shutdown()
78     //~ defer brn.Shutdown()
79
80         ticker := time.NewTicker(time.Duration(7) * time.Minute)
81     go EventToWeb(ps)
82     go EventToXMPP(ps, xmpp_presence_events_chan_)
83         go ReadFromUSocket(presence_socket_path_, newlinequeue)
84         for {
85                 select {
86                 case e := <-newlinequeue:
87                         ParseSocketInputLine(e, ps) //, brn)
88                 case <-ticker.C:
89                         ps.Pub(TimeTick{time.Now().Unix()}, "publishjson")
90                 }
91         }
92 }