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