cosmetic improvements
authorBernhard Tittelbach <xro@realraum.at>
Sun, 25 Aug 2013 13:01:13 +0000 (13:01 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Sun, 25 Aug 2013 13:01:13 +0000 (13:01 +0000)
r3-netstatus/brain/brain.go
r3-netstatus/main.go
r3-netstatus/r3xmppbot/r3xmppbot.go
r3-netstatus/webstatus.go

index c7c3940..cc9b83c 100644 (file)
@@ -42,7 +42,7 @@ func (b *Brain) runBrain() {
                 } else {
                     retrievvalue.returnpath <- nil
                 }
-                
+
             case <- b.shutdown:
                 break
         }
index f7828b9..da21016 100644 (file)
@@ -46,16 +46,16 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface
         switch event := eventinterface.(type) {
             case PresenceUpdate:
                 present = event.Present
-                xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Button: false}
+                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, event.Ts)
+                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_ <- fmt.Sprintln("DoorStatus locked:",event.Locked, "shut:", event.Shut, event.Ts)
+                xmpp_presence_events_chan_ <- fmt.Sprintln("DoorStatus:",r3xmppbot.IfThenElseStr(event.Locked, "locked","unlocked"), r3xmppbot.IfThenElseStr(event.Shut, "shut","ajar"), time.Unix(event.Ts,0))
             case ButtonPressUpdate:
-                xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Button: true}
+                xmpp_presence_events_chan_ <- r3xmppbot.XMPPPresenceEvent{Present: present, Who: who, DoorLock: locked, DoorShut: shut, Button: true, Ts: event.Ts}
         }
        }
 }
@@ -69,14 +69,14 @@ func main() {
         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_)
index f94410c..1be12f8 100644 (file)
@@ -22,6 +22,10 @@ import (
         //~ log.Printf(fmt, v...)
 //~ }
 
+func IfThenElseStr(c bool, strue, sfalse string) string {
+    if c {return strue} else {return sfalse}
+}
+
 func (botdata *XmppBot) makeXMPPMessage(to string, message interface{}, subject interface{}) *xmpp.Message {
     xmppmsgheader := xmpp.Header{To: to,
                                                             From: botdata.my_jid_,
@@ -74,7 +78,7 @@ const (
     R3NoChange R3JIDDesire = -1
     R3NoInfo R3JIDDesire = iota // ignore first value by assigning to blank identifier
     R3NoOfflineInfo
-    R3AllInfo 
+    R3AllInfo
     R3DebugInfo
 )
 
@@ -96,6 +100,7 @@ type XMPPPresenceEvent struct {
     DoorLock bool
     DoorShut bool
     Button bool
+    Ts  int64
 }
 
 type RealraumXmppNotifierConfig map[string]JidData
@@ -111,7 +116,7 @@ type XmppBot struct {
     config_file_ string
     my_login_password_ string
     xmppclient_ *xmpp.Client
-    presence_events_ *chan interface{}    
+    presence_events_ *chan interface{}
 }
 
 
@@ -156,16 +161,21 @@ func init() {
 }
 
 func composeMessage(pec *XMPPPresenceEvent, both bool) *string {
-    var msg, presence_str, lock_str, ajar_str string
+    var msg string
     msg = ""
     if pec.Button {
         msg = "The button has been pressed ! Propably someone is bored and in need of company ! ;-)"
-        if both {  msg += "\n"; } else {  return &msg;  }
+        if both {  msg += "\n"; } else {
+            msg += "   --- " + time.Unix(pec.Ts,0).String()
+            return &msg;
+        }
     }
-    if pec.Present { presence_str = "Somebody is present !" } else {  presence_str = "Everybody left."  }
-    if pec.DoorLock { lock_str = "locked" } else { lock_str = "unlocked" }
-    if pec.DoorShut { ajar_str = "shut" } else { ajar_str = "ajar" }
-    msg += fmt.Sprintf("%s (Door is %s and %s and was last used by %s)", presence_str, lock_str, ajar_str, pec.Who)
+    msg += fmt.Sprintf("%s (Door is %s and %s and was last used by %s at %s)",
+        IfThenElseStr(pec.Present,  "Somebody is present!" , "Everybody left."),
+        IfThenElseStr(pec.DoorLock, "locked","unlocked"),
+        IfThenElseStr(pec.DoorShut, "shut","ajar"),
+        pec.Who,
+        time.Unix(pec.Ts,0).String())
     return &msg
 }
 
@@ -173,7 +183,7 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc
     var debug_msg bool
     var last_presence_event *XMPPPresenceEvent
     var msg *string
-    
+
        for {
         debug_msg = false
                select {
@@ -191,7 +201,7 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc
                 default:
                     break
                 }
-                
+
                 if msg == nil { continue }
 
                 for to, jiddata := range botdata.realraum_jids_  {
@@ -202,7 +212,7 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc
                         xmppout <-  botdata.makeXMPPMessage(to, msg, nil)
                     }
                 }
-                
+
                case je := <-jabber_events:
             simple_jid := removeJIDResource(je.JID)
             jid_data, jid_in_map := botdata.realraum_jids_[simple_jid]
@@ -221,7 +231,7 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc
                 botdata.realraum_jids_.saveTo(botdata.config_file_)
             }
                }
-       }    
+       }
 }
 
 func removeJIDResource(jid string) string {
@@ -237,7 +247,8 @@ func (botdata *XmppBot) isAuthenticated(jid string) bool {
     return in_map && time.Now().Unix() - authtime < botdata.auth_timeout_
 }
 
-const help_text_ string = "\n*auth*<password>* ...enables you to use the other commands\n*on* ...you will be notified of r3 status changes\n*off* ...you will no longer recieve notifications\n*on_while_offline* ...you will be notified of r3 status changes even if you are offline\n*status* ...query current status\n*bye* ...logout"
+const help_text_ string = "\n*auth*<password>* ...enables you to use more commands\n*time* ...gives bot time"
+const help_text_auth string = "You are authorized to use the following commands\n*on* ...you will be notified of r3 status changes\n*off* ...you will no longer recieve notifications\n*on_while_offline* ...you will be notified of r3 status changes even if you are offline\n*status* ...query current status\n*time* ...gives bot time\n*bye* ...logout"
 
 //~ var re_msg_auth_    *regexp.Regexp     = regexp.MustCompile("auth\s+(\S+)")
 
@@ -268,9 +279,11 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout
                 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, I can't operate the door for you." ,nil)
             case "status", "*status*":
                 jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NoChange, true}
+            case "time", "*time*":
+                xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String() , nil)
             default:
                 //~ auth_match = re_msg_auth_.FindStringSubmatch(inmsg.Body.Chardata)
-                xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_, "Available Commands")
+                xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil)
         }
     } else {
         switch bodytext {
@@ -278,9 +291,11 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout
                 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_, "Available Commands")
             case botdata.auth_cmd_, botdata.auth_cmd2_:
                 botdata.jid_lastauthtime_[inmsg.GetHeader().From] = time.Now().Unix()
-                xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "You are now authorized to use commands" , "Authorized")
+                xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil)
             case "status", "*status*":
                 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, you need to be authorized to do that." , nil)
+            case "time", "*time*":
+                xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String() , nil)
             default:
                 //~ auth_match = re_msg_auth_.FindStringSubmatch(inmsg.Body.Chardata)
                 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "A nice day to you too !\nDo you need \"help\" ?", nil)
@@ -322,16 +337,16 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure
     botdata.my_jid_ = loginjid
     botdata.my_login_password_ = loginpwd
     botdata.auth_timeout_ = 3600*2
-    
+
     botdata.config_file_ = path.Join(state_save_dir, "r3xmpp."+removeJIDResource(loginjid)+".json")
-    
+
     //~ log.Println(botdata.config_file_)
-    
+
     //~ logger := &StdLogger{}
     //~ xmpp.Debug = logger
     //~ xmpp.Info = logger
     //~ xmpp.Warn = logger
-    
+
     xmpp.TlsConfig = tls.Config{InsecureSkipVerify: insecuretls}
     botdata.realraum_jids_.loadFrom(botdata.config_file_)
 
index 40b9960..7605175 100644 (file)
@@ -5,7 +5,7 @@ import (
     "./spaceapi"
     "regexp"
        "net/http"
-       "net/url"   
+       "net/url"
     "log"
     "time"
     )
@@ -19,7 +19,7 @@ type spaceState struct {
 var (
        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)
-       re_querystresc_ *regexp.Regexp     = regexp.MustCompile("[^\x30-\x39\x41-\x7E]")    
+       re_querystresc_ *regexp.Regexp     = regexp.MustCompile("[^\x30-\x39\x41-\x7E]")
 )