--bug, movement, identation
[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) (?:(Card|Phone) )?(.+)")
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         tidbit = PresenceUpdate{match_presence[1] == "yes", ts}
80         //~ brn.Oboite("presence", tidbit)
81         ps.Pub(tidbit, "presence")
82         } else if match_status != nil {
83         tidbit = DoorStatusUpdate{match_status[1] == "closed", match_status[3] == "shut", ts}
84         //~ brn.Oboite("door", tidbit)
85         ps.Pub(tidbit, "door")
86         } else if match_command != nil {
87         tidbit = DoorCommandEvent{match_command[1], match_command[2], match_command[3], ts}
88         //~ brn.Oboite("doorcmd", tidbit)
89         ps.Pub(tidbit, "door")
90         } else if match_button != nil {
91         //~ brn.Oboite("button0", ts)
92         ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
93         } else if match_temp != nil {
94                 newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
95                 if err == nil {
96             //~ brn.Oboite( "temp0", newtemp)
97             ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
98                 }
99         } else if match_photo != nil {
100                 newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
101                 if err == nil {
102             //~ brn.Oboite("photo0", newphoto)
103             ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
104                 }
105         } else if line == "movement" {
106         //~ brn.Oboite("movement", ts)
107         ps.Pub(MovementSensorUpdate{0, ts}, "movements")
108         }
109 }
110
111 func ReadFromUSocket(path string, c chan string) {
112 ReOpenSocket:
113         for {
114                 presence_socket, err := net.Dial("unix", path)
115                 if err != nil {
116                         //Waiting on Socket
117                         time.Sleep(5 * time.Second)
118                         continue ReOpenSocket
119                 }
120                 presence_reader := bufio.NewReader(presence_socket)
121                 for {
122                         line, err := presence_reader.ReadString('\n')
123                         if err != nil {
124                                 //Socket closed
125                                 presence_socket.Close()
126                                 continue ReOpenSocket
127                         }
128                         c <- line
129                 }
130         }
131 }