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