more proper import
[svn42.git] / go / r3-netstatus / main.go
index f387789..0af7ef8 100644 (file)
@@ -9,7 +9,7 @@ import (
     "time"
     "fmt"
     //~ "./brain"
-    r3events "svn.spreadspace.org/realraum/go.svn/r3-eventbroker_zmq/r3events"
+    r3events "svn.spreadspace.org/realraum/go.svn/r3events"
 )
 
 type SpaceState struct {
@@ -27,6 +27,7 @@ var (
     r3eventssub_port_ string
     button_press_timeout_ int64 = 3600
     brain_connect_addr_ string
+    enable_syslog_, enable_debug_ bool
 )
 
 //-------
@@ -38,6 +39,8 @@ func init() {
     flag.StringVar(&xmpp_state_save_dir_,"xstatedir","/flash/var/lib/r3netstatus/",  "Directory to save XMPP bot state in")
     flag.StringVar(&r3eventssub_port_, "eventsubport", "tcp://wuzzler.realraum.at:4244", "zmq address to subscribe r3events")
     flag.StringVar(&brain_connect_addr_, "brainconnect", "tcp://wuzzler.realraum.at:4245", "address to ask about most recent stored events")
+    flag.BoolVar(&enable_syslog_, "syslog", false, "enable logging to syslog")
+    flag.BoolVar(&enable_debug_, "debug", false, "enable debug logging")
     flag.Parse()
 }
 
@@ -61,7 +64,7 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface
 
     defer func() {
         if x := recover(); x != nil {
-            fmt.Printf("handleIncomingXMPPStanzas: run time panic: %v", x)
+            Debug_.Printf("handleIncomingXMPPStanzas: run time panic: %v", x)
             ps.Unsub(events, "presence","door","buttons","updateinterval")
             close(xmpp_presence_events_chan_)
         }
@@ -70,7 +73,7 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface
     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 ! ;-)"
+    button_msg := "Dooom ! 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 :-)"}
@@ -78,7 +81,7 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface
     xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"}
 
     for eventinterface := range(events) {
-        fmt.Println("event2xmpp", eventinterface)
+        Debug_.Println("event2xmpp", eventinterface)
         switch event := eventinterface.(type) {
             case r3events.PresenceUpdate:
                 present = event.Present
@@ -101,7 +104,7 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface
            case r3events.DoorAjarUpdate:
                 shut = event.Shut
                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
-            case r3events.ButtonPressUpdate:
+            case r3events.BoreDoomButtonPressEvent:
                 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo}
                 xmpp_presence_events_chan_ <- button_status
                 last_buttonpress = event.Ts
@@ -114,7 +117,17 @@ func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface
        }
 }
 
+func ParseZMQr3Event(lines [][]byte, ps *pubsub.PubSub) {
+    evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
+    if err != nil { return }
+    ps.Pub(evnt, pubsubcat)
+}
+
 func main() {
+    if enable_syslog_ { LogEnableSyslog(); r3xmppbot.LogEnableSyslog() }
+    if enable_debug_ { LogEnableDebuglog(); r3xmppbot.LogEnableDebuglog() }
+    Syslog_.Print("started")
+    defer Syslog_.Print("exiting")
     zmqctx, zmqsub := ZmqsInit(r3eventssub_port_)
     defer zmqctx.Close()
     if zmqsub != nil {defer zmqsub.Close()}
@@ -138,12 +151,12 @@ func main() {
     } else {
         fmt.Println(xmpperr)
         fmt.Println("XMPP Bot disabled")
+        Syslog_.Printf("XMPP Bot disabled due to error: %s", xmpperr.Error())
     }
 
     // --- get update on most recent events ---
     answ := ZmqsAskQuestionsAndClose(zmqctx, brain_connect_addr_, [][][]byte{[][]byte{[]byte("DoorLockUpdate")}, [][]byte{[]byte("DoorAjarUpdate")}, [][]byte{[]byte("DoorCommandEvent")}, [][]byte{[]byte("PresenceUpdate")}, [][]byte{[]byte("IlluminationSensorUpdate")}, [][]byte{[]byte("TempSensorUpdate")}})
     for _, a := range(answ) {
-        //~ fmt.Println("recent event:", a)
         ParseZMQr3Event(a, ps)
     }