7f0d20c56ec423cc236bed930206d012edccd4f8
[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
63 func ParseSocketInputLine(line string, ps *pubsub.PubSub) { //, brn *brain.Brain) {
64         match_presence := re_presence_.FindStringSubmatch(line)
65         match_status := re_status_.FindStringSubmatch(line)
66         match_command := re_command_.FindStringSubmatch(line)
67         match_button := re_button_.FindStringSubmatch(line)
68         match_temp := re_temp_.FindStringSubmatch(line)
69         match_photo := re_photo_.FindStringSubmatch(line)
70
71     var tidbit interface{}
72     ts := time.Now().Unix()
73         if match_presence != nil {
74         tidbit = PresenceUpdate{match_presence[1] == "yes", ts}
75         //~ brn.Oboite("presence", tidbit)
76         ps.Pub(tidbit, "presence")
77         } else if match_status != nil {
78         tidbit = DoorStatusUpdate{match_status[1] == "closed", match_status[3] == "shut", ts}
79         //~ brn.Oboite("door", tidbit)
80         ps.Pub(tidbit, "door")
81         } else if match_command != nil {
82         tidbit = DoorCommandEvent{match_status[1], match_status[2], match_status[3], ts}
83         //~ brn.Oboite("doorcmd", tidbit)
84         ps.Pub(tidbit, "door")
85         } else if match_button != nil {
86         //~ brn.Oboite("button0", ts)
87         ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
88         } else if match_temp != nil {
89                 newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
90                 if err == nil {
91             //~ brn.Oboite( "temp0", newtemp)
92             ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
93                 }
94         } else if match_photo != nil {
95                 newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
96                 if err == nil {
97             //~ brn.Oboite("photo0", newphoto)
98             ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
99                 }
100         }
101 }
102
103 func ReadFromUSocket(path string, c chan string) {
104 ReOpenSocket:
105         for {
106                 presence_socket, err := net.Dial("unix", path)
107                 if err != nil {
108                         //Waiting on Socket
109                         time.Sleep(5 * time.Second)
110                         continue ReOpenSocket
111                 }
112                 presence_reader := bufio.NewReader(presence_socket)
113                 for {
114                         line, err := presence_reader.ReadString('\n')
115                         if err != nil {
116                                 //Socket closed
117                                 presence_socket.Close()
118                                 continue ReOpenSocket
119                         }
120                         c <- line
121                 }
122         }
123 }