package main import ( "./r3xmppbot" pubsub "github.com/tuxychandru/pubsub" "flag" "time" "fmt" //~ "./brain" ) type SpaceState struct { present bool buttonpress_until int64 door_locked bool door_shut bool } var ( presence_socket_path_ string xmpp_presence_events_chan_ chan interface{} xmpp_login_ struct {jid string; pass string} xmpp_bot_authstring_ string xmpp_state_save_dir_ string ) //------- func init() { flag.StringVar(&xmpp_login_.jid, "xjid", "realrauminfo@realraum.at/Tuer", "XMPP Bot Login JID") flag.StringVar(&xmpp_login_.pass, "xpass", "", "XMPP Bot Login Password") flag.StringVar(&xmpp_bot_authstring_, "xbotauth", "", "String that user use to authenticate themselves to the bot") flag.StringVar(&presence_socket_path_,"presencesocket", "/var/run/tuer/presence.socket", "Path to presence socket") flag.StringVar(&xmpp_state_save_dir_,"xstatedir","/flash/var/lib/r3netstatus/", "Directory to save XMPP bot state in") flag.Parse() } //------- func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) { events := ps.Sub("presence","door","buttons") var present, locked, shut bool = false, true, true var who string = "Unknown" for eventinterface := range(events) { switch event := eventinterface.(type) { case PresenceUpdate: present = event.Present xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Ts: event.Ts} case DoorCommandEvent: who = event.Who xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0)) case DoorStatusUpdate: locked = event.Locked shut = event.Shut xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Ts: event.Ts} case ButtonPressUpdate: xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Button: true, Ts: event.Ts} } } } func main() { var err error var bot *r3xmppbot.XmppBot bot, xmpp_presence_events_chan_, err = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true) if err != nil { fmt.Println(err) return } defer bot.StopBot() newlinequeue := make(chan string, 1) ps := pubsub.New(1) //~ brn := brain.New() defer close(newlinequeue) defer ps.Shutdown() //~ defer brn.Shutdown() ticker := time.NewTicker(time.Duration(7) * time.Minute) go EventToWeb(ps) go EventToXMPP(ps, xmpp_presence_events_chan_) go ReadFromUSocket(presence_socket_path_, newlinequeue) for { select { case e := <-newlinequeue: ParseSocketInputLine(e, ps) //, brn) case <-ticker.C: ps.Pub(TimeTick{time.Now().Unix()}, "publishjson") } } }