示例#1
0
func (g *CreeperBatteryGuage) reconnect() (ok bool) {
	var err error
	g.sig, err = device.SignalChanged()
	if err != nil {
		log.Printf("upower: %v", err)
		return false
	}
	return true
}
示例#2
0
// BatteryStateChange implements the BatteryStateNotifier interface.
func (g *CreeperBatteryGuage) BatteryStateChange(notf chan<- struct{}) (stop func()) {
	_done := make(chan struct{})

	if g.sig == nil {
		sig, err := device.SignalChanged()
		if err != nil {
			close(notf)
			return func() {}
		}
		g.sig = sig
	}

	go func() {
		if !g.reconnect() {
			return
		}
		var relay chan<- struct{}
		for {
			select {
			case s, ok := <-g.sig:
				if !ok {
					log.Printf("upower: state channel closed")

					if g.reconnect() {
						continue
					}
					return
				}
				if s.Path != g.dev {
					continue
				}
				relay = notf
			case relay <- struct{}{}:
				relay = nil
			case <-_done:
				return
			}
		}
	}()

	return func() { close(_done) }
}