1 // (c) Bernhard Tittelbach, 2013
6 pubsub "github.com/tuxychandru/pubsub"
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+)")
27 type PresenceUpdate struct {
32 type DoorStatusUpdate struct {
38 type DoorCommandEvent struct {
45 type ButtonPressUpdate struct {
50 type TempSensorUpdate struct {
56 type IlluminationSensorUpdate struct {
62 type TimeTick struct {
66 type MovementSensorUpdate struct {
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)
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)
101 //~ brn.Oboite( "temp0", newtemp)
102 ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
104 } else if match_photo != nil {
105 newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
107 //~ brn.Oboite("photo0", newphoto)
108 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
110 } else if line == "movement" {
111 //~ brn.Oboite("movement", ts)
112 ps.Pub(MovementSensorUpdate{0, ts}, "movements")
116 func ReadFromUSocket(path string, c chan string) {
119 presence_socket, err := net.Dial("unix", path)
122 time.Sleep(5 * time.Second)
123 continue ReOpenSocket
125 presence_reader := bufio.NewReader(presence_socket)
127 line, err := presence_reader.ReadString('\n')
130 presence_socket.Close()
131 continue ReOpenSocket