does not yet work
[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     "encoding/json"
11     pubsub "github.com/tuxychandru/pubsub"
12     zmq "github.com/vaughan0/go-zmq"    
13     "log"
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 type DoorLockUpdate struct {
31     DoorID byte
32     Locked bool
33     Ts int64
34 }
35
36 type DoorAjarUpdate struct {
37     DoorID byte
38     Shut bool
39     Ts int64
40 }
41
42 type DoorCommandEvent struct {
43     Command string
44     Using string
45     Who string
46     Ts int64
47 }
48
49 type ButtonPressUpdate struct {
50     Buttonindex int
51     Ts int64
52 }
53
54 type TempSensorUpdate struct {
55     Sensorindex int
56     Value float64
57     Ts int64
58 }
59
60 type IlluminationSensorUpdate struct {
61     Sensorindex int
62     Value int64
63     Ts int64
64 }
65
66 type TimeTick struct {
67     Ts int64
68 }
69
70 type MovementSensorUpdate struct {
71     Sensorindex int
72     Ts int64
73 }
74
75 func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
76     switch string(lines[0]) {
77         case "closed":
78             ps.Pub(DoorLockUpdate{0, true, ts}, "door")
79         case "opened":
80             ps.Pub(DoorLockUpdate{0, false, ts}, "door")
81         case "manual":   //movement
82         case "error":
83         case "reset":
84             ps.Pub(DoorLockUpdate{0, true, ts}, "door")
85         case "timeout":   //after open | after close
86         case "opening":
87         case "closing":
88         default:    
89     }
90 }
91
92
93 func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
94     var tidbit interface{}
95     ts := time.Now().Unix()
96     if len(lines) < 1 { return }
97     log.Print("ParseSocketInputLine",string(lines[0]))
98     switch string(lines[0]) {
99         case "State:":
100             if len(lines) < 2 { return }
101             parseSocketInputLine_State(lines[1:], ps, ts)
102         case "Status:":
103             if len(lines) < 3 { return }
104             tidbit = DoorLockUpdate{0, string(lines[1]) == "closed", ts}
105             //~ brn.Oboite("door", tidbit)
106             ps.Pub(tidbit, "door")
107             tidbit = DoorAjarUpdate{0, string(lines[len(lines)-2]) == "shut", ts}
108             //~ brn.Oboite("door", tidbit)
109             ps.Pub(tidbit, "door")            
110         case "Info(card):":
111             if len(lines) < 3 { return }
112             if string(lines[2]) != "found" { return }
113             match_cardid := re_cardid_.FindSubmatch(lines[1])
114             if len(match_cardid) > 1 {
115                 // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
116                 nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
117                 if err != nil {
118                     Syslog_.Print("CardID Lookup Error",err)
119                     nick = "Unresolvable KeyID"
120                 }
121                 // new event: toggle by user nick using card
122                 ps.Pub(DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
123             }
124         case "Info(ajar):":
125             if len(lines) < 5 { return }
126             tidbit = DoorAjarUpdate{0, string(lines[4]) == "shut", ts}
127             //~ brn.Oboite("door", tidbit)
128             ps.Pub(tidbit, "door")                    
129         case "open", "close", "toggle", "reset":
130             ps.Pub(DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
131         case "photo0":
132             newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
133             if err == nil {
134                 // brn.Oboite("photo0", newphoto)
135                 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
136             }
137     }
138 }
139
140 func MakeTimeTick(ps *pubsub.PubSub) {
141     ps.Pub(TimeTick{time.Now().Unix()},"time")
142 }
143
144 func FormatEventForSocket(event_interface interface{}) (data [][]byte, err error) {
145         msg, err := json.Marshal(data)
146         if err != nil {
147                 return
148         }
149     return [][]byte{msg}, nil
150 }
151
152     //~ match_presence := re_presence_.FindStringSubmatch(line)
153     //~ match_status := re_status_.FindStringSubmatch(line)
154     //~ match_command := re_command_.FindStringSubmatch(line)
155     //~ match_button := re_button_.FindStringSubmatch(line)
156     //~ match_temp := re_temp_.FindStringSubmatch(line)
157     //~ match_photo := re_photo_.FindStringSubmatch(line)
158         //~ if match_button != nil {
159         //~ // brn.Oboite("button0", ts)
160         //~ ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
161         //~ } else if match_temp != nil {
162                 //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
163                 //~ if err == nil {
164             //~ // brn.Oboite( "temp0", newtemp)
165             //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
166                 //~ }
167         //~ } else if match_photo != nil {
168                 //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
169                 //~ if err == nil {
170             //~ // brn.Oboite("photo0", newphoto)
171             //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
172                 //~ }
173         //~ } else if line == "movement" {
174         //~ // brn.Oboite("movement", ts)
175         //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
176         //~ }