package cp3000 import ( "io" "time" "src.dualinventive.com/go/lib/dilog" ) // ChildRouter is a proxied router which allows gateway-ed commands for a child connection // it relays CP3000 $GW command through a router and back type ChildRouter struct { parent Router logger dilog.Logger imei string callbacks map[Command]Callback } // NewChildRouter creates a new router which proxies commands through a parent router func NewChildRouter(logger dilog.Logger, imei string, r Router) *ChildRouter { router := ChildRouter{ parent: r, logger: logger, imei: imei, callbacks: make(map[Command]Callback), } router.SetLogger(logger) return &router } // SetLogger attaches a new logger func (c *ChildRouter) SetLogger(logger dilog.Logger) { c.logger = logger } // Register registers a callback for the child func (c *ChildRouter) Register(command Command, cb Callback) { c.callbacks[command] = cb } // Send sends a command to the device via the parent func (c *ChildRouter) Send(command Command, params ...string) error { extraparams := []string{c.imei, string(command)} return c.parent.Send(CommandGateway, append(extraparams, params...)...) } // SendReply sends a reply via parent CP3000 func (c *ChildRouter) SendReply(reply uint8) error { return c.parent.SendReply(reply) } // ChildCommand is called by the parent to dispatch a callback on the child func (c *ChildRouter) ChildCommand(command Command, params ...string) error { if cb, ok := c.callbacks[command]; ok { if err := cb(c, params); err != nil { return err } } return nil } // ReceiveReply receives a reply via CP3000 func (c *ChildRouter) ReceiveReply(timeout time.Duration) (*Msg, error) { return c.parent.ReceiveReply(timeout) } // Close has no function, for interface statisfactory func (c *ChildRouter) Close() error { return nil } // AddDevice adds a device to the parent router. This device will be closed when the parent router is closed func (c ChildRouter) AddDevice(uid string, d io.Closer) { c.parent.AddDevice(uid, d) } // RemoveDevice removes the device from the parent router func (c *ChildRouter) RemoveDevice(uid string) { c.parent.RemoveDevice(uid) } // SetReplyWriteTimeout has no function, this is set on the parent, for interface statisfactory func (c *ChildRouter) SetReplyWriteTimeout(timeout time.Duration) { } // Run is only here for interface statisfactory, it does nothing for the childrouter func (c *ChildRouter) Run() { }