X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=r3-netstatus%2Fmain.go;h=6f53ac3bd64b80a2c750cc39905473d5a5d7d49e;hb=4c36e4d9f491d1f75295e7c8a3c31e5aaa59acc8;hp=77dd32a1e6b17d3fe1c4626140cc6532f6c4913e;hpb=b78efe951c3f3bbf89f366f6e8e305d3a835f1e8;p=svn42.git diff --git a/r3-netstatus/main.go b/r3-netstatus/main.go index 77dd32a..6f53ac3 100644 --- a/r3-netstatus/main.go +++ b/r3-netstatus/main.go @@ -1,172 +1,140 @@ package main import ( - "./spaceapi" "./r3xmppbot" - "bufio" - "fmt" - "net" - "net/http" - "net/url" - "regexp" - "time" - "strconv" + pubsub "github.com/tuxychandru/pubsub" + "flag" + "time" + "fmt" + //~ "./brain" ) type SpaceState struct { - present bool - buttonpress_until int64 + present bool + buttonpress_until int64 door_locked bool door_shut bool } var ( - re_presence_ *regexp.Regexp = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?") - re_status_ *regexp.Regexp = regexp.MustCompile("Status: (closed|opened), (opening|waiting|closing|idle), (ajar|shut).*") - re_button_ *regexp.Regexp = regexp.MustCompile("PanicButton|button\\d?") - re_temp_ *regexp.Regexp = regexp.MustCompile("temp0: (\\d+\\.\\d+)") - re_photo_ *regexp.Regexp = regexp.MustCompile("photo0: (\\d+)") - re_querystresc_ *regexp.Regexp = regexp.MustCompile("[^\x30-\x39\x41-\x7E]") - spaceapidata spaceapi.SpaceInfo = spaceapi.NewSpaceInfo("realraum", "http://realraum.at", "http://realraum.at/logo-red_250x250.png", "http://realraum.at/logo-re_open_100x100.png", "http://realraum.at/logo-re_empty_100x100.png",47.065779129, 15.442322614).AddSpaceAddress("Jakoministr. 16 ground level left, 8010 Graz, Austria") - statusstate *SpaceState = new(SpaceState) - xmpp_presence_events_chan_ chan interface{} + 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 + button_press_timeout_ int64 = 3600 ) //------- -func updateStatusString() { - var spacestatus string - if statusstate.present { - if statusstate.buttonpress_until > time.Now().Unix() { - spacestatus = "Panic! Present&Bored" - } else { - spacestatus = "Leute Anwesend" - } - } else { - spacestatus = "Keiner Da" - } - spaceapidata.SetStatus(statusstate.present, spacestatus) +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 publishStateToWeb() { - updateStatusString() - jsondata_b, err := spaceapidata.MakeJSON() - if err != nil { - fmt.Println("Error:", err) - return - } - //jsondata_b := re_querystresc_.ReplaceAllFunc(jsondata_b, func(in []byte) []byte { - // out := make([]byte, 4) - // out[0] = '%' - // copy(out[1:], []byte(strconv.FormatInt(int64(in[0]), 16))) - // return out - //}) - jsondata := url.QueryEscape(string(jsondata_b)) - resp, err := http.Get("http://www.realraum.at/cgi/status.cgi?pass=jako16&set=" + jsondata) - if err != nil { - fmt.Println("Error publishing realraum info", err) - return - } - defer resp.Body.Close() -} +//------- -func parseSocketInputLine(line string) { - match_presence := re_presence_.FindStringSubmatch(line) - match_status := re_status_.FindStringSubmatch(line) - match_button := re_button_.FindStringSubmatch(line) - match_temp := re_temp_.FindStringSubmatch(line) - match_photo := re_photo_.FindStringSubmatch(line) +func IfThenElseStr(c bool, strue, sfalse string) string { + if c {return strue} else {return sfalse} +} - if match_presence != nil { - statusstate.present = (match_presence[1] == "yes") - statusstate.door_locked = (match_presence[2] == "closed") - //spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("Torwaechter", "Front Door", match_presence[2] == "closed")) - spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterLock", "Türschloß", statusstate.door_locked)) - publishStateToWeb() - xmpp_presence_events_chan_ <- r3xmppbot.PresenceEvent{statusstate.present, statusstate.door_locked, statusstate.door_shut} - } else if match_status != nil { - statusstate.door_locked = (match_status[1] == "closed") - statusstate.door_shut = (match_status[3] == "shut") - spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterLock", "Türschloß", statusstate.door_locked)) - spaceapidata.MergeInSensor(spaceapi.MakeDoorLockSensor("TorwaechterAjarSensor", "Türkontakt", statusstate.door_shut)) - //spaceapidata.MergeInSensor(spaceapi.MakeDoorAjarSensor("Torwaechter", "Front Door", match_presence[3] == "shut")) - publishStateToWeb() - xmpp_presence_events_chan_ <- r3xmppbot.PresenceEvent{statusstate.present, statusstate.door_locked, statusstate.door_shut} - } else if match_button != nil { - statusstate.buttonpress_until = time.Now().Unix() + 3600 - spaceapidata.AddSpaceEvent("PanicButton", "check-in", "The button has been pressed") - publishStateToWeb() - xmpp_presence_events_chan_ <- "The button has been pressed ! Propably someone is bored and need company ! ;-)" - } else if match_temp != nil { - newtemp, err := strconv.ParseFloat((match_temp[1]), 32) - if err == nil { - spaceapidata.MergeInSensor(spaceapi.MakeTempCSensor("Temp0","Decke",newtemp)) - } - } else if match_photo != nil { - newphoto, err := strconv.ParseInt(match_photo[1], 10, 32) - if err == nil { - spaceapidata.MergeInSensor(spaceapi.MakeIlluminationSensor("Photodiode","Decke","1024V/5V",newphoto)) - } - } +func composeMessage(present, locked, shut bool, who string, ts int64) string { + return fmt.Sprintf("%s (Door is %s and %s and was last used%s at %s)", + IfThenElseStr(present, "Somebody is present!" , "Everybody left."), + IfThenElseStr(locked, "locked","unlocked"), + IfThenElseStr(shut, "shut","ajar"), + IfThenElseStr(len(who) == 0,"", " by " + who), + time.Unix(ts,0).String()) } -func readFromUSocket(path string, c chan string) { -ReOpenSocket: - for { - presence_socket, err := net.Dial("unix", path) - if err != nil { - //Waiting on Socket - time.Sleep(5 * time.Second) - continue ReOpenSocket - } - presence_reader := bufio.NewReader(presence_socket) - for { - line, err := presence_reader.ReadString('\n') - if err != nil { - //Socket closed - presence_socket.Close() - continue ReOpenSocket - } - c <- line - } +func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) { + events := ps.Sub("presence","door","buttons","updateinterval") + + defer func() { + if x := recover(); x != nil { + fmt.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) + ps.Unsub(events, "presence","door","buttons","updateinterval") + close(xmpp_presence_events_chan_) + } + }() + + var present, locked, shut bool = false, true, true + var last_buttonpress int64 = 0 + var who string + button_msg := "The button has been pressed ! Propably someone is bored and in need of company ! ;-)" + present_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowOnline,"Somebody is present"} + notpresent_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe,"Nobody is here"} + button_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowFreeForChat, "The button has been pressed :-)"} + + xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"} + + for eventinterface := range(events) { + switch event := eventinterface.(type) { + case PresenceUpdate: + present = event.Present + xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true} + if present { + xmpp_presence_events_chan_ <- present_status + } else { + xmpp_presence_events_chan_ <- notpresent_status + } + case DoorCommandEvent: + if len(event.Who) > 0 && len(event.Using) > 0 { + who = fmt.Sprintf("%s (%s)",event.Who, event.Using) + } else { + 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.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true} + case ButtonPressUpdate: + xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo} + xmpp_presence_events_chan_ <- button_status + last_buttonpress = event.Ts + case TimeTick: + if present && last_buttonpress > 0 && time.Now().Unix() - last_buttonpress > button_press_timeout_ { + xmpp_presence_events_chan_ <- present_status + last_buttonpress = 0 + } + } } } func main() { - spaceapidata.AddSpaceFeed("calendar", "http://grical.realraum.at/s/?query=!realraum&view=rss") - spaceapidata.AddSpaceFeed("blog", "https://plus.google.com/113737596421797426873") - spaceapidata.AddSpaceFeed("wiki", "http://realraum.at/wiki") - spaceapidata.AddSpaceContactInfo("+43780700888524", "irc://irc.oftc.net/#realraum", "realraum@realraum.at", "realraum@realraum.at", "realraum@realraum.at", "vorstand@realraum.at") - - var err error + var xmpperr error var bot *r3xmppbot.XmppBot - bot, xmpp_presence_events_chan_, err = r3xmppbot.NewStartedBot("realrauminfo@realraum.at/Tuer", "d7ynC6Dg", "r3alraumOLGAXMPPInfos", true) - if err != nil { - fmt.Println(err) - return + bot, xmpp_presence_events_chan_, xmpperr = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true) + + newlinequeue := make(chan string, 1) + ps := pubsub.New(1) + //~ brn := brain.New() + defer close(newlinequeue) + defer ps.Shutdown() + //~ defer brn.Shutdown() + + go EventToWeb(ps) + if xmpperr == nil { + defer bot.StopBot() + go EventToXMPP(ps, xmpp_presence_events_chan_) + } else { + fmt.Println(xmpperr) + fmt.Println("XMPP Bot disabled") } - defer bot.StopBot() - - //~ presence_events <- PresenceEvent{true, true, true} - //~ presence_events <- PresenceEvent{true, true, false} - //~ presence_events <- PresenceEvent{true, false, false} - + go ReadFromUSocket(presence_socket_path_, newlinequeue) + ticker := time.NewTicker(time.Duration(7) * time.Minute) - eventqueue := make(chan string) - defer close(eventqueue) - - ticker := time.NewTicker(time.Duration(7) * time.Minute) - go readFromUSocket("/var/run/tuer/presence.socket", eventqueue) - for { - select { - case e := <-eventqueue: - parseSocketInputLine(e) - case <-ticker.C: - publishStateToWeb() - } - } + for { + select { + case e := <-newlinequeue: + ParseSocketInputLine(e, ps) //, brn) + case <-ticker.C: + ps.Pub(TimeTick{time.Now().Unix()}, "updateinterval") + } + } } - -/* TODO: -* Read config from an .ini file - */