r3events package
[svn42.git] / go / r3-eventbroker_zmq / sockettoevent.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     "regexp"
7     "strconv"
8     "time"
9     //~ "./brain"
10     pubsub "github.com/tuxychandru/pubsub"
11     zmq "github.com/vaughan0/go-zmq"
12     "log"
13     "./r3events"
14     )
15
16 var (
17         re_presence_    *regexp.Regexp     = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?")
18         re_state_      *regexp.Regexp     = regexp.MustCompile("State: (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing).*")
19         re_status_      *regexp.Regexp     = regexp.MustCompile("Status: (closed|opened) (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing) (ajar|shut).*")
20         re_infocard_      *regexp.Regexp     = regexp.MustCompile("Info\\(card\\): card\\(([a-fA-F0-9]+)\\) (found|not found).*")
21         re_cardid_      *regexp.Regexp     = regexp.MustCompile("card\\(([a-fA-F0-9]+)\\)")
22         re_infoajar_      *regexp.Regexp     = regexp.MustCompile("Info\\(ajar\\): door is now (ajar|shut)")
23         re_command_     *regexp.Regexp     = regexp.MustCompile("(open|close|toggle|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?")
24         re_button_      *regexp.Regexp     = regexp.MustCompile("PanicButton|button\\d?")
25         re_temp_        *regexp.Regexp     = regexp.MustCompile("temp0: (\\d+\\.\\d+)")
26         re_photo_       *regexp.Regexp     = regexp.MustCompile("photo0: (\\d+)")
27 )
28
29
30 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
31     switch string(lines[0]) {
32         case "closed":
33             ps.Pub(r3events.DoorLockUpdate{0, true, ts}, "door")
34         case "opened":
35             ps.Pub(r3events.DoorLockUpdate{0, false, ts}, "door")
36         case "manual":   //movement
37         case "error":
38         case "reset":
39             ps.Pub(r3events.DoorLockUpdate{0, true, ts}, "door")
40         case "timeout":   //after open | after close
41         case "opening":
42         case "closing":
43         default:
44     }
45 }
46
47
48 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
49     var tidbit interface{}
50     ts := time.Now().Unix()
51     if len(lines) < 1 { return }
52     log.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
53     switch string(lines[0]) {
54         case "State:":
55             if len(lines) < 2 { return }
56             parseSocketInputLine_State(lines[1:], ps, ts)
57         case "Status:":
58             if len(lines) < 3 { return }
59             tidbit = r3events.DoorLockUpdate{0, string(lines[1]) == "closed", ts}
60             //~ brn.Oboite("door", tidbit)
61             ps.Pub(tidbit, "door")
62             tidbit = r3events.DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts}
63             //~ brn.Oboite("door", tidbit)
64             ps.Pub(tidbit, "door")
65         case "Info(card):":
66             if len(lines) < 3 { return }
67             if string(lines[2]) != "found" { return }
68             match_cardid := re_cardid_.FindSubmatch(lines[1])
69             if len(match_cardid) > 1 {
70                 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
71                 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
72                 if err != nil {
73                     Syslog_.Print("CardID Lookup Error",err)
74                     nick = "Unresolvable KeyID"
75                 }
76                 // new event: toggle by user nick using card
77                 ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
78             }
79         case "Info(ajar):":
80             if len(lines) < 5 { return }
81             tidbit = r3events.DoorAjarUpdate{0, string(lines[4]) == "shut", ts}
82             //~ brn.Oboite("door", tidbit)
83             ps.Pub(tidbit, "door")
84         case "open", "close", "toggle", "reset":
85             ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
86         case "photo0":
87             newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
88             if err == nil {
89                 // brn.Oboite("photo0", newphoto)
90                 ps.Pub(r3events.IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
91             }
92     }
93 }
94
95 func MakeTimeTick(ps *pubsub.PubSub) {
96     ps.Pub(r3events.TimeTick{time.Now().Unix()},"time")
97 }
98
99     //~ match_presence := re_presence_.FindStringSubmatch(line)
100     //~ match_status := re_status_.FindStringSubmatch(line)
101     //~ match_command := re_command_.FindStringSubmatch(line)
102     //~ match_button := re_button_.FindStringSubmatch(line)
103     //~ match_temp := re_temp_.FindStringSubmatch(line)
104     //~ match_photo := re_photo_.FindStringSubmatch(line)
105         //~ if match_button != nil {
106         //~ // brn.Oboite("button0", ts)
107         //~ ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
108         //~ } else if match_temp != nil {
109                 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
110                 //~ if err == nil {
111             //~ // brn.Oboite( "temp0", newtemp)
112             //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
113                 //~ }
114         //~ } else if match_photo != nil {
115                 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
116                 //~ if err == nil {
117             //~ // brn.Oboite("photo0", newphoto)
118             //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
119                 //~ }
120         //~ } else if line == "movement" {
121         //~ // brn.Oboite("movement", ts)
122         //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
123         //~ }