unfinished
[svn42.git] / go / r3-eventbroker_zmq / sockettoevent.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     pubsub "github.com/tuxychandru/pubsub"
7     "regexp"
8     "strconv"
9     "bufio"
10     "time"
11     //~ "./brain"
12     "net"
13     )
14
15 var (
16         re_presence_    *regexp.Regexp     = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?")
17         re_state_      *regexp.Regexp     = regexp.MustCompile("State: (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing).*")
18         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).*")
19         re_infocard_      *regexp.Regexp     = regexp.MustCompile("Info\(card\): card\(([a-fA-F0-9]+)\) (found|not found).*")
20         re_cardid_      *regexp.Regexp     = regexp.MustCompile("card\(([a-fA-F0-9]+)\)")
21         re_infoajar_      *regexp.Regexp     = regexp.MustCompile("Info\(ajar\): door is now (ajar|shut)")
22         re_command_     *regexp.Regexp     = regexp.MustCompile("(open|close|toggle|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?")
23         re_button_      *regexp.Regexp     = regexp.MustCompile("PanicButton|button\\d?")
24         re_temp_        *regexp.Regexp     = regexp.MustCompile("temp0: (\\d+\\.\\d+)")
25         re_photo_       *regexp.Regexp     = regexp.MustCompile("photo0: (\\d+)")
26 )
27
28
29 type PresenceUpdate struct {
30     Present bool
31     Ts int64
32 }
33
34 type DoorLockUpdate struct {
35     DoorID byte
36     Locked bool
37     Ts int64
38 }
39
40 type DoorAjarUpdate struct {
41     DoorID byte
42     Shut bool
43     Ts int64
44 }
45
46 type DoorCommandEvent struct {
47     Command string
48     Using string
49     Who string
50     Ts int64
51 }
52
53 type ButtonPressUpdate struct {
54     Buttonindex int
55     Ts int64
56 }
57
58 type TempSensorUpdate struct {
59     Sensorindex int
60     Value float64
61     Ts int64
62 }
63
64 type IlluminationSensorUpdate struct {
65     Sensorindex int
66     Value int64
67     Ts int64
68 }
69
70 type TimeTick struct {
71     Ts int64
72 }
73
74 type MovementSensorUpdate struct {
75     Sensorindex int
76     Ts int64
77 }
78
79 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts uint64) {
80     switch string(lines[0]) {
81         case "closed":
82             ps.Pub(DoorLockUpdate{0, true, ts}, "door")
83         case "opened":
84             ps.Pub(DoorLockUpdate{0, false, ts}, "door")
85         case "manual":   //movement
86         case "error":
87         case "reset":
88             ps.Pub(DoorLockUpdate{0, true, ts}, "door")
89         case "timeout":   //after open | after close
90         case "opening":
91         case "closing":
92         default:    
93     }
94 }
95
96
97 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub) { //, brn *brain.Brain) {
98     var tidbit interface{}
99     ts := time.Now().Unix()
100     if len(lines) < 1 { return }
101     switch string(lines[0]) {
102         case "State:":
103             if len(lines) < 2 { continue }
104             parseSocketInputLine_State(lines[1:], ps, ts)
105         case "Status:":
106             if len(lines) < 3 { continue }
107             tidbit = DoorLockUpdate{0, lines[1] == []byte("closed"), ts}
108             //~ brn.Oboite("door", tidbit)
109             ps.Pub(tidbit, "door")
110             tidbit = DoorAjarUpdate{0, lines[-1] == []byte("shut"), ts}
111             //~ brn.Oboite("door", tidbit)
112             ps.Pub(tidbit, "door")            
113         case "Info(card):":
114             if len(lines) < 3 { continue }
115             if lines[2] != []byte("found") {
116                 continue
117             }
118             match_cardid := re_cardid_.FindSubmatch(lines[1])
119             if len(match_cardid) > 1 {
120                 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
121                 nick, err := keylookup_socket.LookupCardIdNick(match_cardid[1])
122                 if err != nil {
123                     Syslog_.Print("CardID Lookup Error",err)
124                     nick := "Unresolvable KeyID"
125                 }
126                 // new event: toggle by user nick using card
127                 ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
128             }
129         case "Info(ajar):":
130             if len(lines) < 5 { continue }
131             DoorAjarUpdate{0, match_status[4] == []byte("shut"), ts}
132             //~ brn.Oboite("door", tidbit)
133             ps.Pub(tidbit, "door")                    
134         case "open", "close", "toggle", "reset":
135             ps.Pub(DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
136     }
137
138
139     //~ match_presence := re_presence_.FindStringSubmatch(line)
140     //~ match_status := re_status_.FindStringSubmatch(line)
141     //~ match_command := re_command_.FindStringSubmatch(line)
142     //~ match_button := re_button_.FindStringSubmatch(line)
143     //~ match_temp := re_temp_.FindStringSubmatch(line)
144     //~ match_photo := re_photo_.FindStringSubmatch(line)
145         //~ if match_button != nil {
146         //~ // brn.Oboite("button0", ts)
147         //~ ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
148         //~ } else if match_temp != nil {
149                 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
150                 //~ if err == nil {
151             //~ // brn.Oboite( "temp0", newtemp)
152             //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
153                 //~ }
154         //~ } else if match_photo != nil {
155                 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
156                 //~ if err == nil {
157             //~ // brn.Oboite("photo0", newphoto)
158             //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
159                 //~ }
160         //~ } else if line == "movement" {
161         //~ // brn.Oboite("movement", ts)
162         //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
163         //~ }
164 }