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|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?")
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 {
62 type MovementSensorUpdate struct {
67 func ParseSocketInputLine(line string, ps *pubsub.PubSub) { //, brn *brain.Brain) {
68 match_presence := re_presence_.FindStringSubmatch(line)
69 match_status := re_status_.FindStringSubmatch(line)
70 match_command := re_command_.FindStringSubmatch(line)
71 match_button := re_button_.FindStringSubmatch(line)
72 match_temp := re_temp_.FindStringSubmatch(line)
73 match_photo := re_photo_.FindStringSubmatch(line)
75 //~ log.Println("ParseSocketInputLine",line)
76 var tidbit interface{}
77 ts := time.Now().Unix()
78 if match_presence != nil {
79 if match_presence[2] != "" { ps.Pub(DoorStatusUpdate{match_presence[2] == "closed", true, ts}, "door"); }
80 tidbit = PresenceUpdate{match_presence[1] == "yes", ts}
81 //~ brn.Oboite("presence", tidbit)
82 ps.Pub(tidbit, "presence")
83 } else if match_status != nil {
84 tidbit = DoorStatusUpdate{match_status[1] == "closed", match_status[3] == "shut", ts}
85 //~ brn.Oboite("door", tidbit)
86 ps.Pub(tidbit, "door")
87 } else if match_command != nil {
88 tidbit = DoorCommandEvent{match_command[1], match_command[2], match_command[3], ts}
89 //~ brn.Oboite("doorcmd", tidbit)
90 ps.Pub(tidbit, "door")
91 } else if match_button != nil {
92 //~ brn.Oboite("button0", ts)
93 ps.Pub(ButtonPressUpdate{0, ts}, "buttons")
94 } else if match_temp != nil {
95 newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
97 //~ brn.Oboite( "temp0", newtemp)
98 ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
100 } else if match_photo != nil {
101 newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
103 //~ brn.Oboite("photo0", newphoto)
104 ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
106 } else if line == "movement" {
107 //~ brn.Oboite("movement", ts)
108 ps.Pub(MovementSensorUpdate{0, ts}, "movements")
112 func ReadFromUSocket(path string, c chan string) {
115 presence_socket, err := net.Dial("unix", path)
118 time.Sleep(5 * time.Second)
119 continue ReOpenSocket
121 presence_reader := bufio.NewReader(presence_socket)
123 line, err := presence_reader.ReadString('\n')
126 presence_socket.Close()
127 continue ReOpenSocket