marshal events experiment
[svn42.git] / go / r3-eventbroker_zmq / sockettoevent.go
index f2e7a20..ea9b0e5 100644 (file)
@@ -7,9 +7,9 @@ import (
     "strconv"
     "time"
     //~ "./brain"
-    "encoding/json"
     pubsub "github.com/tuxychandru/pubsub"
-    zmq "github.com/vaughan0/go-zmq"    
+    zmq "github.com/vaughan0/go-zmq"
+    "log"
     )
 
 var (
@@ -26,21 +26,14 @@ var (
 )
 
 
-type PresenceUpdate struct {
-    Present bool
-    Ts int64
-}
-func (s PresenceUpdate) Serialize() string
-
-
 type DoorLockUpdate struct {
-    DoorID byte
+    DoorID int
     Locked bool
     Ts int64
 }
 
 type DoorAjarUpdate struct {
-    DoorID byte
+    DoorID int
     Shut bool
     Ts int64
 }
@@ -91,7 +84,7 @@ func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
         case "timeout":   //after open | after close
         case "opening":
         case "closing":
-        default:    
+        default:
     }
 }
 
@@ -100,39 +93,38 @@ func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *z
     var tidbit interface{}
     ts := time.Now().Unix()
     if len(lines) < 1 { return }
+    log.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
     switch string(lines[0]) {
         case "State:":
-            if len(lines) < 2 { continue }
+            if len(lines) < 2 { return }
             parseSocketInputLine_State(lines[1:], ps, ts)
         case "Status:":
-            if len(lines) < 3 { continue }
+            if len(lines) < 3 { return }
             tidbit = DoorLockUpdate{0, string(lines[1]) == "closed", ts}
             //~ brn.Oboite("door", tidbit)
             ps.Pub(tidbit, "door")
             tidbit = DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts}
             //~ brn.Oboite("door", tidbit)
-            ps.Pub(tidbit, "door")            
+            ps.Pub(tidbit, "door")
         case "Info(card):":
-            if len(lines) < 3 { continue }
-            if string(lines[2]) != "found" {
-                continue
-            }
+            if len(lines) < 3 { return }
+            if string(lines[2]) != "found" { return }
             match_cardid := re_cardid_.FindSubmatch(lines[1])
             if len(match_cardid) > 1 {
                 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
-                nick, err := keylookup_socket.LookupCardIdNick(match_cardid[1])
+                nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
                 if err != nil {
                     Syslog_.Print("CardID Lookup Error",err)
-                    nick := "Unresolvable KeyID"
+                    nick = "Unresolvable KeyID"
                 }
                 // new event: toggle by user nick using card
                 ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
             }
         case "Info(ajar):":
-            if len(lines) < 5 { continue }
+            if len(lines) < 5 { return }
             tidbit = DoorAjarUpdate{0, string(lines[4]) == "shut", ts}
             //~ brn.Oboite("door", tidbit)
-            ps.Pub(tidbit, "door")                    
+            ps.Pub(tidbit, "door")
         case "open", "close", "toggle", "reset":
             ps.Pub(DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
         case "photo0":
@@ -148,14 +140,6 @@ func MakeTimeTick(ps *pubsub.PubSub) {
     ps.Pub(TimeTick{time.Now().Unix()},"time")
 }
 
-func FormatEventForSocket(event_interface interface{}) (data [][]byte, err error) {
-       msg, err := json.Marshal(data)
-       if err != nil {
-               return
-       }
-    return [][]byte{msg}, nil
-}
-
     //~ match_presence := re_presence_.FindStringSubmatch(line)
     //~ match_status := re_status_.FindStringSubmatch(line)
     //~ match_command := re_command_.FindStringSubmatch(line)