4 pubsub "github.com/tuxychandru/pubsub"
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+)")
25 type PresenceUpdate struct {
30 type DoorStatusUpdate struct {
36 type DoorCommandEvent struct {
43 type ButtonPressUpdate struct {
48 type TempSensorUpdate struct {
54 type IlluminationSensorUpdate struct {
60 type TimeTick struct {
64 type MovementSensorUpdate struct {
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)
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)
99 //~ brn.Oboite( "temp0", newtemp)
100 ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
102 } else if match_photo != nil {
103 newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
105 //~ brn.Oboite("photo0", newphoto)
106 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
108 } else if line == "movement" {
109 //~ brn.Oboite("movement", ts)
110 ps.Pub(MovementSensorUpdate{0, ts}, "movements")
114 func ReadFromUSocket(path string, c chan string) {
117 presence_socket, err := net.Dial("unix", path)
120 time.Sleep(5 * time.Second)
121 continue ReOpenSocket
123 presence_reader := bufio.NewReader(presence_socket)
125 line, err := presence_reader.ReadString('\n')
128 presence_socket.Close()
129 continue ReOpenSocket