4 pubsub "github.com/tuxychandru/pubsub"
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+)")
23 type PresenceUpdate struct {
28 type DoorStatusUpdate struct {
34 type DoorCommandEvent struct {
41 type ButtonPressUpdate struct {
46 type TempSensorUpdate struct {
52 type IlluminationSensorUpdate struct {
58 type TimeTick struct {
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)
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)
91 //~ brn.Oboite( "temp0", newtemp)
92 ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
94 } else if match_photo != nil {
95 newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
97 //~ brn.Oboite("photo0", newphoto)
98 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
103 func ReadFromUSocket(path string, c chan string) {
106 presence_socket, err := net.Dial("unix", path)
109 time.Sleep(5 * time.Second)
110 continue ReOpenSocket
112 presence_reader := bufio.NewReader(presence_socket)
114 line, err := presence_reader.ReadString('\n')
117 presence_socket.Close()
118 continue ReOpenSocket