ed3c9186aea97df760af8fd7005c17b30fff8ba9
[svn42.git] / go / r3-netstatus / 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_infocard_      *regexp.Regexp     = regexp.MustCompile("Info\(card\): card\(([a-fA-F0-9]+)\) (found|not found).*")
19         re_infoajar_      *regexp.Regexp     = regexp.MustCompile("Info\(ajar\): door is now (ajar|shut)")
20         re_command_     *regexp.Regexp     = regexp.MustCompile("(open|close|toggle|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?")
21         re_button_      *regexp.Regexp     = regexp.MustCompile("PanicButton|button\\d?")
22         re_temp_        *regexp.Regexp     = regexp.MustCompile("temp0: (\\d+\\.\\d+)")
23         re_photo_       *regexp.Regexp     = regexp.MustCompile("photo0: (\\d+)")
24 )
25
26
27 type PresenceUpdate struct {
28     Present bool
29     Ts int64
30 }
31
32 type DoorStatusUpdate struct {
33     Locked bool
34     Shut bool
35     Ts int64
36 }
37
38 type DoorCommandEvent struct {
39     Command string
40     Using string
41     Who string
42     Ts int64
43 }
44
45 type ButtonPressUpdate struct {
46     Buttonindex int
47     Ts int64
48 }
49
50 type TempSensorUpdate struct {
51     Sensorindex int
52     Value float64
53     Ts int64
54 }
55
56 type IlluminationSensorUpdate struct {
57     Sensorindex int
58     Value int64
59     Ts int64
60 }
61
62 type TimeTick struct {
63     Ts int64
64 }
65
66 type MovementSensorUpdate struct {
67     Sensorindex int
68     Ts int64
69 }
70
71 func ParseSocketInputLine(line string, ps *pubsub.PubSub) { //, brn *brain.Brain) {
72     match_presence := re_presence_.FindStringSubmatch(line)
73     match_status := re_status_.FindStringSubmatch(line)
74     match_command := re_command_.FindStringSubmatch(line)
75     match_button := re_button_.FindStringSubmatch(line)
76     match_temp := re_temp_.FindStringSubmatch(line)
77     match_photo := re_photo_.FindStringSubmatch(line)
78
79     //~ log.Println("ParseSocketInputLine",line)
80     var tidbit interface{}
81     ts := time.Now().Unix()
82     if match_presence != nil {
83         if match_presence[2] != "" { ps.Pub(DoorStatusUpdate{match_presence[2] == "closed", true, ts}, "door"); }
84         tidbit = PresenceUpdate{match_presence[1] == "yes", ts}
85         //~ brn.Oboite("presence", tidbit)
86         ps.Pub(tidbit, "presence")
87         } else if match_status != nil {
88         tidbit = DoorStatusUpdate{match_status[1] == "closed", match_status[3] == "shut", ts}
89         //~ brn.Oboite("door", tidbit)
90         ps.Pub(tidbit, "door")
91         } else if match_command != nil {
92         tidbit = DoorCommandEvent{match_command[1], match_command[2], match_command[3], ts}
93         //~ brn.Oboite("doorcmd", tidbit)
94         ps.Pub(tidbit, "door")
95         } else if match_button != nil {
96         //~ brn.Oboite("button0", ts)
97         ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
98         } else if match_temp != nil {
99                 newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
100                 if err == nil {
101             //~ brn.Oboite( "temp0", newtemp)
102             ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
103                 }
104         } else if match_photo != nil {
105                 newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
106                 if err == nil {
107             //~ brn.Oboite("photo0", newphoto)
108             ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
109                 }
110         } else if line == "movement" {
111         //~ brn.Oboite("movement", ts)
112         ps.Pub(MovementSensorUpdate{0, ts}, "movements")
113         }
114 }
115
116 func ReadFromUSocket(path string, c chan string) {
117 ReOpenSocket:
118         for {
119                 presence_socket, err := net.Dial("unix", path)
120                 if err != nil {
121                         //Waiting on Socket
122                         time.Sleep(5 * time.Second)
123                         continue ReOpenSocket
124                 }
125                 presence_reader := bufio.NewReader(presence_socket)
126                 for {
127                         line, err := presence_reader.ReadString('\n')
128                         if err != nil {
129                                 //Socket closed
130                                 presence_socket.Close()
131                                 continue ReOpenSocket
132                         }
133                         c <- line
134                 }
135         }
136 }