package main
import (
- "flag"
- //~ "time"
- pubsub "github.com/tuxychandru/pubsub"
- r3events "svn.spreadspace.org/realraum/go.svn/r3events"
+ "flag"
+ //~ "time"
+ pubsub "github.com/tuxychandru/pubsub"
+ r3events "svn.spreadspace.org/realraum/go.svn/r3events"
)
//~ func StringArrayToByteArray(ss []string) [][]byte {
- //~ bb := make([][]byte, len(ss))
- //~ for index, s := range(ss) {
- //~ bb[index] = []byte(s)
- //~ }
- //~ return bb
+//~ bb := make([][]byte, len(ss))
+//~ for index, s := range(ss) {
+//~ bb[index] = []byte(s)
+//~ }
+//~ return bb
//~ }
// ---------- Main Code -------------
var (
- doorsub_addr_ string
- sensorssub_port_ string
- pub_port_ string
- keylookup_addr_ string
- brain_listen_addr_ string
- door_cmd_addr_ string
- use_syslog_ bool
- enable_debuglog_ bool
+ doorsub_addr_ string
+ sensorssub_port_ string
+ pub_port_ string
+ keylookup_addr_ string
+ brain_listen_addr_ string
+ door_cmd_addr_ string
+ use_syslog_ bool
+ enable_debuglog_ bool
)
func init() {
- flag.StringVar(&door_cmd_addr_, "doorcmdaddr", "ipc:///run/tuer/door_cmd.ipc", "zmq door event publish addr")
- flag.StringVar(&doorsub_addr_, "doorsubaddr", "tcp://torwaechter.realraum.at:4242", "zmq door event publish addr")
- flag.StringVar(&sensorssub_port_, "sensorsubport", "tcp://*:4243", "zmq public/listen socket addr for incoming sensor data")
- flag.StringVar(&pub_port_, "pubport", "tcp://*:4244", "zmq port publishing consodilated events")
- flag.StringVar(&keylookup_addr_, "keylookupaddr", "ipc:///run/tuer/door_keyname.ipc", "address to use for key/name lookups")
- flag.StringVar(&brain_listen_addr_, "brainlisten", "tcp://*:4245", "address to listen for requests about latest stored event")
- flag.BoolVar(&use_syslog_, "syslog", false, "log to syslog local2 facility")
- flag.BoolVar(&enable_debuglog_, "debug", false, "enable debug logging")
- flag.Parse()
+ flag.StringVar(&door_cmd_addr_, "doorcmdaddr", "ipc:///run/tuer/door_cmd.ipc", "zmq door event publish addr")
+ flag.StringVar(&doorsub_addr_, "doorsubaddr", "tcp://torwaechter.realraum.at:4242", "zmq door event publish addr")
+ flag.StringVar(&sensorssub_port_, "sensorsubport", "tcp://*:4243", "zmq public/listen socket addr for incoming sensor data")
+ flag.StringVar(&pub_port_, "pubport", "tcp://*:4244", "zmq port publishing consodilated events")
+ flag.StringVar(&keylookup_addr_, "keylookupaddr", "ipc:///run/tuer/door_keyname.ipc", "address to use for key/name lookups")
+ flag.StringVar(&brain_listen_addr_, "brainlisten", "tcp://*:4245", "address to listen for requests about latest stored event")
+ flag.BoolVar(&use_syslog_, "syslog", false, "log to syslog local2 facility")
+ flag.BoolVar(&enable_debuglog_, "debug", false, "enable debug logging")
+ flag.Parse()
}
func main() {
- if enable_debuglog_ { LogEnableDebuglog() }
- if use_syslog_ {
- LogEnableSyslog()
- Syslog_.Print("started")
- defer Syslog_.Print("exiting")
- }
+ if enable_debuglog_ {
+ LogEnableDebuglog()
+ }
+ if use_syslog_ {
+ LogEnableSyslog()
+ Syslog_.Print("started")
+ defer Syslog_.Print("exiting")
+ }
- zmqctx, sub_in_chans, pub_out_socket, keylookup_socket := ZmqsInit(doorsub_addr_, sensorssub_port_, pub_port_, keylookup_addr_)
- if sub_in_chans != nil {defer sub_in_chans.Close()}
- defer zmqctx.Close()
- if pub_out_socket != nil {defer pub_out_socket.Close()}
- if keylookup_socket != nil {defer keylookup_socket.Close()}
- if sub_in_chans == nil || pub_out_socket == nil || keylookup_socket == nil {
- panic("zmq sockets must not be nil !!")
- }
+ zmqctx, sub_in_chans, pub_out_socket, keylookup_socket := ZmqsInit(doorsub_addr_, sensorssub_port_, pub_port_, keylookup_addr_)
+ if sub_in_chans != nil {
+ defer sub_in_chans.Close()
+ }
+ defer zmqctx.Close()
+ if pub_out_socket != nil {
+ defer pub_out_socket.Close()
+ }
+ if keylookup_socket != nil {
+ defer keylookup_socket.Close()
+ }
+ if sub_in_chans == nil || pub_out_socket == nil || keylookup_socket == nil {
+ panic("zmq sockets must not be nil !!")
+ }
- ps := pubsub.New(10)
- defer ps.Shutdown() // ps.Shutdown should be called before zmq_ctx.Close(), since it will cause goroutines to shutdown and close zqm_sockets which is needed for zmq_ctx.Close() to return
- //~ ticker := time.NewTicker(time.Duration(5) * time.Minute)
+ ps := pubsub.New(10)
+ defer ps.Shutdown() // ps.Shutdown should be called before zmq_ctx.Close(), since it will cause goroutines to shutdown and close zqm_sockets which is needed for zmq_ctx.Close() to return
+ //~ ticker := time.NewTicker(time.Duration(5) * time.Minute)
- store_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
- go BrainCenter(zmqctx, brain_listen_addr_, store_these_events_chan)
+ store_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
+ go BrainCenter(zmqctx, brain_listen_addr_, store_these_events_chan)
- go MetaEventRoutine_Movement(ps, 10, 20, 10)
- go MetaEventRoutine_Presence(ps, 21, 200)
+ go MetaEventRoutine_Movement(ps, 10, 20, 10)
+ go MetaEventRoutine_Presence(ps, 21, 200)
- // --- get update on most recent status ---
- answ := ZmqsAskQuestionsAndClose(zmqctx, door_cmd_addr_, [][][]byte{[][]byte{[]byte("status")}})
- for _, a := range(answ) {
- ParseSocketInputLine(a, ps, keylookup_socket)
- }
+ // --- get update on most recent status ---
+ answ := ZmqsAskQuestionsAndClose(zmqctx, door_cmd_addr_, [][][]byte{[][]byte{[]byte("status")}})
+ for _, a := range answ {
+ ParseSocketInputLine(a, ps, keylookup_socket)
+ }
- publish_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
- for {
- select {
- case subin := <- sub_in_chans.In():
- ParseSocketInputLine(subin, ps, keylookup_socket)
- //~ case <- ticker.C:
- //~ MakeTimeTick(ps)
- case event_interface := <- publish_these_events_chan:
- data, err := r3events.MarshalEvent2ByteByte(event_interface)
- Debug_.Printf("publishing %s",data)
- if err != nil {
- Syslog_.Print(err)
- continue
- }
- if err := pub_out_socket.Send(data); err != nil {
- panic(err)
- }
- }
- }
+ publish_these_events_chan := ps.Sub("door", "doorcmd", "presence", "sensors", "buttons", "movement")
+ for {
+ select {
+ case subin := <-sub_in_chans.In():
+ ParseSocketInputLine(subin, ps, keylookup_socket)
+ //~ case <- ticker.C:
+ //~ MakeTimeTick(ps)
+ case event_interface := <-publish_these_events_chan:
+ data, err := r3events.MarshalEvent2ByteByte(event_interface)
+ Debug_.Printf("publishing %s", data)
+ if err != nil {
+ Syslog_.Print(err)
+ continue
+ }
+ if err := pub_out_socket.Send(data); err != nil {
+ panic(err)
+ }
+ }
+ }
}
package main
import (
- "regexp"
- "strconv"
- "time"
- "bytes"
- //~ "./brain"
- pubsub "github.com/tuxychandru/pubsub"
- zmq "github.com/vaughan0/go-zmq"
- r3events "svn.spreadspace.org/realraum/go.svn/r3events"
- )
+ "bytes"
+ "regexp"
+ "strconv"
+ "time"
+ //~ "./brain"
+ pubsub "github.com/tuxychandru/pubsub"
+ zmq "github.com/vaughan0/go-zmq"
+ r3events "svn.spreadspace.org/realraum/go.svn/r3events"
+)
var (
//~ re_presence_ *regexp.Regexp = regexp.MustCompile("Presence: (yes|no)(?:, (opened|closed), (.+))?")
//~ re_state_ *regexp.Regexp = regexp.MustCompile("State: (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing).*")
//~ re_status_ *regexp.Regexp = regexp.MustCompile("Status: (closed|opened), (closed|opened|manual movement|error|reset|timeout after open|timeout after close|opening|closing), (ajar|shut).*")
//~ re_infocard_ *regexp.Regexp = regexp.MustCompile("Info\\(card\\): card\\(([a-fA-F0-9]+)\\) (found|not found).*")
- re_cardid_ *regexp.Regexp = regexp.MustCompile("card\\(([a-fA-F0-9]+)\\)")
+ re_cardid_ *regexp.Regexp = regexp.MustCompile("card\\(([a-fA-F0-9]+)\\)")
//~ re_infoajar_ *regexp.Regexp = regexp.MustCompile("Info\\(ajar\\): door is now (ajar|shut)")
//~ re_command_ *regexp.Regexp = regexp.MustCompile("(open|close|toggle|reset)(?: +(Card|Phone|SSH|ssh))?(?: +(.+))?")
//~ re_button_ *regexp.Regexp = regexp.MustCompile("PanicButton|button\\d?")
//~ re_photo_ *regexp.Regexp = regexp.MustCompile("photo0: (\\d+)")
)
-
func parseSocketInputLine_State(lines [][]byte, ps *pubsub.PubSub, ts int64) {
- switch string(lines[0]) {
- case "closed":
- ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
- case "opened":
- ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
- case "manual", "manual_movement": //movement
- ps.Pub(r3events.DoorManualMovementEvent{ts}, "door")
- case "error":
- ps.Pub(r3events.DoorProblemEvent{100, string(bytes.Join(lines,[]byte(" "))), ts}, "door")
- case "reset":
- ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
- case "timeout_after_open":
- ps.Pub(r3events.DoorProblemEvent{10, string(lines[0]), ts}, "door")
- ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
- case "timeout_after_close":
- ps.Pub(r3events.DoorProblemEvent{20, string(lines[0]), ts}, "door")
- // can't say for sure that door is locked if we ran into timeout while closing
- //~ ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
- case "opening":
- case "closing":
- default:
- Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)
- }
+ switch string(lines[0]) {
+ case "closed":
+ ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
+ case "opened":
+ ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
+ case "manual", "manual_movement": //movement
+ ps.Pub(r3events.DoorManualMovementEvent{ts}, "door")
+ case "error":
+ ps.Pub(r3events.DoorProblemEvent{100, string(bytes.Join(lines, []byte(" "))), ts}, "door")
+ case "reset":
+ ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
+ case "timeout_after_open":
+ ps.Pub(r3events.DoorProblemEvent{10, string(lines[0]), ts}, "door")
+ ps.Pub(r3events.DoorLockUpdate{false, ts}, "door")
+ case "timeout_after_close":
+ ps.Pub(r3events.DoorProblemEvent{20, string(lines[0]), ts}, "door")
+ // can't say for sure that door is locked if we ran into timeout while closing
+ //~ ps.Pub(r3events.DoorLockUpdate{true, ts}, "door")
+ case "opening":
+ case "closing":
+ default:
+ Syslog_.Print("parseSocketInputLine_State: Unexpected State:", lines)
+ }
}
-
func ParseSocketInputLine(lines [][]byte, ps *pubsub.PubSub, keylookup_socket *zmq.Socket) { //, brn *brain.Brain) {
- ts := time.Now().Unix()
- if len(lines) < 1 { return }
- Debug_.Printf("ParseSocketInputLine: %s %s",string(lines[0]), lines[1:])
- switch string(lines[0]) {
- case "State:":
- if len(lines) < 2 { return }
- parseSocketInputLine_State(lines[1:], ps, ts)
- case "Status:":
- if len(lines) < 3 { return }
- if len(lines[1]) < 4 { return }
- ps.Pub(r3events.DoorLockUpdate{string(lines[1])[0:4] != "open", ts}, "door")
- ps.Pub(r3events.DoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
- case "Info(card):":
- if len(lines) < 3 { return }
- if string(lines[2]) != "found" { return }
- match_cardid := re_cardid_.FindSubmatch(lines[1])
- if len(match_cardid) > 1 {
- // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
- nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
- if err != nil {
- Syslog_.Print("CardID Lookup Error",err)
- nick = "Unresolvable KeyID"
- }
- // new event: toggle by user nick using card
- ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts},"doorcmd")
- }
- case "Info(ajar):":
- if len(lines) < 5 { return }
- ps.Pub(r3events.DoorAjarUpdate{string(lines[4]) == "shut", ts}, "door")
- case "open", "close", "toggle", "reset":
- ps.Pub(r3events.DoorCommandEvent{string(lines[0]), string(lines[1]), string(lines[2]), ts},"doorcmd")
- case "BackdoorInfo(ajar):":
- ps.Pub(r3events.BackdoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts},"door")
- case "GasLeakAlert":
- ps.Pub(r3events.GasLeakAlert{ts},"sensors")
- case "temp0:","temp1:", "temp2:", "temp3:":
- sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
- if err != nil {return }
- newtemp, err := strconv.ParseFloat(string(lines[1]), 10)
- if err != nil {return }
- ps.Pub(r3events.TempSensorUpdate{int(sensorid), newtemp, ts}, "sensors")
- case "photo0:","photo1:", "photo2:", "photo3:":
- sensorid, err := strconv.ParseInt(string(lines[0][5]), 10, 32)
- if err != nil {return }
- newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
- if err != nil {return }
- ps.Pub(r3events.IlluminationSensorUpdate{int(sensorid), newphoto, ts}, "sensors")
- case "rh0:":
- //~ sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
- //~ if err != nil {return }
- relhumid, err := strconv.ParseInt(string(lines[1]), 10, 32)
- if err != nil {return }
- ps.Pub(r3events.RelativeHumiditySensorUpdate{0, int(relhumid), ts}, "sensors")
- case "dust0:","dust1:","dust2:":
- sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
- if err != nil {return }
- dustlvl, err := strconv.ParseInt(string(lines[1]), 10, 32)
- if err != nil {return }
- ps.Pub(r3events.DustSensorUpdate{int(sensorid), dustlvl, ts}, "sensors")
- default:
- evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
- if err == nil {
- ps.Pub(evnt, pubsubcat)
- }
- }
+ ts := time.Now().Unix()
+ if len(lines) < 1 {
+ return
+ }
+ Debug_.Printf("ParseSocketInputLine: %s %s", string(lines[0]), lines[1:])
+ switch string(lines[0]) {
+ case "State:":
+ if len(lines) < 2 {
+ return
+ }
+ parseSocketInputLine_State(lines[1:], ps, ts)
+ case "Status:":
+ if len(lines) < 3 {
+ return
+ }
+ if len(lines[1]) < 4 {
+ return
+ }
+ ps.Pub(r3events.DoorLockUpdate{string(lines[1])[0:4] != "open", ts}, "door")
+ ps.Pub(r3events.DoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
+ case "Info(card):":
+ if len(lines) < 3 {
+ return
+ }
+ if string(lines[2]) != "found" {
+ return
+ }
+ match_cardid := re_cardid_.FindSubmatch(lines[1])
+ if len(match_cardid) > 1 {
+ // PreCondition: same thread/goroutinge as created keylookup_socket !!!!
+ nick, err := LookupCardIdNick(keylookup_socket, match_cardid[1])
+ if err != nil {
+ Syslog_.Print("CardID Lookup Error", err)
+ nick = "Unresolvable KeyID"
+ }
+ // new event: toggle by user nick using card
+ ps.Pub(r3events.DoorCommandEvent{"toggle", "Card", nick, ts}, "doorcmd")
+ }
+ case "Info(ajar):":
+ if len(lines) < 5 {
+ return
+ }
+ ps.Pub(r3events.DoorAjarUpdate{string(lines[4]) == "shut", ts}, "door")
+ case "open", "close", "toggle", "reset":
+ switch len(lines) {
+ case 2:
+ ps.Pub(r3events.DoorCommandEvent{Command: string(lines[0]), Using: string(lines[1]), Ts: ts}, "doorcmd")
+ case 3:
+ ps.Pub(r3events.DoorCommandEvent{Command: string(lines[0]), Using: string(lines[1]), Who: string(lines[2]), Ts: ts}, "doorcmd")
+ default:
+ return
+ }
+ case "BackdoorInfo(ajar):":
+ ps.Pub(r3events.BackdoorAjarUpdate{string(lines[len(lines)-1]) == "shut", ts}, "door")
+ case "GasLeakAlert":
+ ps.Pub(r3events.GasLeakAlert{ts}, "sensors")
+ case "temp0:", "temp1:", "temp2:", "temp3:":
+ sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
+ if err != nil {
+ return
+ }
+ newtemp, err := strconv.ParseFloat(string(lines[1]), 10)
+ if err != nil {
+ return
+ }
+ ps.Pub(r3events.TempSensorUpdate{int(sensorid), newtemp, ts}, "sensors")
+ case "photo0:", "photo1:", "photo2:", "photo3:":
+ sensorid, err := strconv.ParseInt(string(lines[0][5]), 10, 32)
+ if err != nil {
+ return
+ }
+ newphoto, err := strconv.ParseInt(string(lines[1]), 10, 32)
+ if err != nil {
+ return
+ }
+ ps.Pub(r3events.IlluminationSensorUpdate{int(sensorid), newphoto, ts}, "sensors")
+ case "rh0:":
+ //~ sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
+ //~ if err != nil {return }
+ relhumid, err := strconv.ParseInt(string(lines[1]), 10, 32)
+ if err != nil {
+ return
+ }
+ ps.Pub(r3events.RelativeHumiditySensorUpdate{0, int(relhumid), ts}, "sensors")
+ case "dust0:", "dust1:", "dust2:":
+ sensorid, err := strconv.ParseInt(string(lines[0][4]), 10, 32)
+ if err != nil {
+ return
+ }
+ dustlvl, err := strconv.ParseInt(string(lines[1]), 10, 32)
+ if err != nil {
+ return
+ }
+ ps.Pub(r3events.DustSensorUpdate{int(sensorid), dustlvl, ts}, "sensors")
+ default:
+ evnt, pubsubcat, err := r3events.UnmarshalByteByte2Event(lines)
+ if err == nil {
+ ps.Pub(evnt, pubsubcat)
+ }
+ }
}
func MakeTimeTick(ps *pubsub.PubSub) {
- ps.Pub(r3events.TimeTick{time.Now().Unix()},"time")
+ ps.Pub(r3events.TimeTick{time.Now().Unix()}, "time")
}
- //~ match_presence := re_presence_.FindStringSubmatch(line)
- //~ match_status := re_status_.FindStringSubmatch(line)
- //~ match_command := re_command_.FindStringSubmatch(line)
- //~ match_button := re_button_.FindStringSubmatch(line)
- //~ match_temp := re_temp_.FindStringSubmatch(line)
- //~ match_photo := re_photo_.FindStringSubmatch(line)
- //~ if match_button != nil {
- //~ // brn.Oboite("button0", ts)
- //~ ps.Pub(BoreDoomButtonPressEvent{0, ts}, "buttons")
- //~ } else if match_temp != nil {
- //~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
- //~ if err == nil {
- //~ // brn.Oboite( "temp0", newtemp)
- //~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
- //~ }
- //~ } else if match_photo != nil {
- //~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
- //~ if err == nil {
- //~ // brn.Oboite("photo0", newphoto)
- //~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
- //~ }
- //~ } else if line == "movement" {
- //~ // brn.Oboite("movement", ts)
- //~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
- //~ }
+//~ match_presence := re_presence_.FindStringSubmatch(line)
+//~ match_status := re_status_.FindStringSubmatch(line)
+//~ match_command := re_command_.FindStringSubmatch(line)
+//~ match_button := re_button_.FindStringSubmatch(line)
+//~ match_temp := re_temp_.FindStringSubmatch(line)
+//~ match_photo := re_photo_.FindStringSubmatch(line)
+//~ if match_button != nil {
+//~ // brn.Oboite("button0", ts)
+//~ ps.Pub(BoreDoomButtonPressEvent{0, ts}, "buttons")
+//~ } else if match_temp != nil {
+//~ newtemp, err := strconv.ParseFloat((match_temp[1]), 32)
+//~ if err == nil {
+//~ // brn.Oboite( "temp0", newtemp)
+//~ ps.Pub(TempSensorUpdate{0, newtemp, ts}, "sensors")
+//~ }
+//~ } else if match_photo != nil {
+//~ newphoto, err := strconv.ParseInt(match_photo[1], 10, 32)
+//~ if err == nil {
+//~ // brn.Oboite("photo0", newphoto)
+//~ ps.Pub(IlluminationSensorUpdate{0, newphoto, ts}, "sensors")
+//~ }
+//~ } else if line == "movement" {
+//~ // brn.Oboite("movement", ts)
+//~ ps.Pub(MovementSensorUpdate{0, ts}, "movements")
+//~ }