minor lights changes
authorjweigele <jweigele@local>
Fri, 19 Jan 2024 18:40:29 +0000 (10:40 -0800)
committerjweigele <jweigele@local>
Fri, 19 Jan 2024 18:40:29 +0000 (10:40 -0800)
 * remove the sleep between light controls, it doesn't really improve
   timeouts and looks better all at the same time
 * clean up the gauge map, _all_ relays now included in it in order to
   allow for many->one location->dest tracking

lights/main.go

index a895e50a095239c58774bedc6f6abb1df5ab513d..06ef1254631fe9ec3da6c5f62286c7a8fd59df5d 100644 (file)
@@ -183,14 +183,10 @@ type IRGBRelay interface {
        setPWM(red, green, blue float64)
        getLocation() string
        getRoutingKey() string
+       getDestination() string
        getMetricValue() float64
 }
 
-type IRGBFriendly interface {
-       IRGBRelay
-       getFriendlyName() string
-}
-
 type ITopicRelay interface {
        getTopic() string
 }
@@ -284,6 +280,11 @@ func (relay *RGBRelay) getLocation() string {
        return relay.Location
 }
 
+// since we'll just use rgb_relay as the routing key, this differentiates dests
+func (relay *RGBRelay) getDestination() string {
+       return relay.Location
+}
+
 func (relay *RGBRelay) getMotionLocs() []string {
        return []string{}
 }
@@ -572,6 +573,11 @@ func (zig *RGBZig) getFriendlyName() string {
        return zig.FriendlyName
 }
 
+// since we'll just use rgb_relay as the routing key, this differentiates dests
+func (relay *RGBZig) getDestination() string {
+       return relay.FriendlyName
+}
+
 // RGBZig overrides
 func (zig *RGBZig) getRoutingKey() string {
        return fmt.Sprintf("zigbee2mqtt.%s.set", zig.FriendlyName)
@@ -593,6 +599,10 @@ func (relay *ESPRelay) getRoutingKey() string {
        return relay.Topic
 }
 
+func (relay *ESPRelay) getDestination() string {
+       return relay.Topic
+}
+
 func (relay *ESPRelay) getTopic() string {
        return relay.Topic
 }
@@ -647,11 +657,11 @@ func (pico *RGBPico) Init(sendChannel chan helper.RabbitSend) {
 }
 
 type RGBManager struct {
-       relays             []IRGBRelay
-       switches           []*Switch
-       friendlyToLocation map[string][]string
-       friendlyMutex      sync.Mutex
-       rabbitHelper       *helper.RabbitConfig
+       relays         []IRGBRelay
+       switches       []*Switch
+       destToLocation map[string][]string
+       friendlyMutex  sync.Mutex
+       rabbitHelper   *helper.RabbitConfig
 }
 
 func (manager *RGBManager) addRelay(relay IRGBRelay) {
@@ -661,47 +671,40 @@ func (manager *RGBManager) addRelay(relay IRGBRelay) {
        defer manager.friendlyMutex.Unlock()
        logger.V(4).Info("(re)building friendly to location map now")
 
-       manager.friendlyToLocation = make(map[string][]string, 0)
-       // build friendlyToLocation map, in order to update linked locations later
+       manager.destToLocation = make(map[string][]string, 0)
+       // build destToLocation map, in order to update linked locations later
        for _, relay := range manager.relays {
                // check if we implement the interface giving friendlynames (which means we're an RGBZig or subclass)
-               curZig, ok := relay.(IRGBFriendly)
-               if ok {
-                       logger.V(4).Info("Found an RGBZig relay", "relay", curZig)
-                       _, isPresent := manager.friendlyToLocation[curZig.getFriendlyName()]
-                       if !isPresent {
-                               manager.friendlyToLocation[curZig.getFriendlyName()] = make([]string, 0)
-                       }
-                       if !slices.Contains(manager.friendlyToLocation[curZig.getFriendlyName()], curZig.getLocation()) {
-                               manager.friendlyToLocation[curZig.getFriendlyName()] = append(manager.friendlyToLocation[curZig.getFriendlyName()], curZig.getLocation())
-                       }
+               _, isPresent := manager.destToLocation[relay.getDestination()]
+               if !isPresent {
+                       manager.destToLocation[relay.getDestination()] = make([]string, 0)
+               }
+               if !slices.Contains(manager.destToLocation[relay.getDestination()], relay.getLocation()) {
+                       manager.destToLocation[relay.getDestination()] = append(manager.destToLocation[relay.getDestination()], relay.getLocation())
                }
        }
 
-       logger.V(4).Info("Final friendlyToLocation map", "map", manager.friendlyToLocation)
+       logger.V(4).Info("Final destToLocation map", "map", manager.destToLocation)
 
 }
 
 func (manager *RGBManager) getLinkedLocations(sourceLocation string) []string {
        // using a map as a set just to build all the friendly names we care about
-       uniqueFriendlyNames := make(map[string]int, 0)
+       uniqueDestinations := make(map[string]int, 0)
 
        for _, relay := range manager.relays {
                // did we get a friendly name capable relay?
-               curZig, ok := relay.(IRGBFriendly)
-               if ok {
-                       if curZig.getLocation() == sourceLocation {
-                               uniqueFriendlyNames[curZig.getFriendlyName()] = 1
-                       }
+               if relay.getLocation() == sourceLocation {
+                       uniqueDestinations[relay.getDestination()] = 1
                }
        }
 
        // same thing here, for the retval
        var retval []string
        // all the keys stored above, aka all the unique friendly names
-       for friendlyName, _ := range uniqueFriendlyNames {
+       for friendlyName, _ := range uniqueDestinations {
                // all the values in the map, aka all locations for every unique friendly name (may overlap!)
-               for _, linkedLocation := range manager.friendlyToLocation[friendlyName] {
+               for _, linkedLocation := range manager.destToLocation[friendlyName] {
                        if !slices.Contains(retval, linkedLocation) {
                                retval = append(retval, linkedLocation)
                        }
@@ -768,22 +771,15 @@ func (manager *RGBManager) handleMotion(data map[string]interface{}) {
 
 func (manager *RGBManager) handlePWM(data map[string]interface{}) {
        logger.V(2).Info("Got PWM data", "data", data)
-       var uniqueFriendlyNames = make(map[string]IRGBFriendly, 0)
+       var uniqueDestinations = make(map[string]IRGBRelay, 0)
        for _, relay := range manager.relays {
-               // did we get a friendly name capable relay?
-               curZig, ok := relay.(IRGBFriendly)
-               if ok {
-                       // set (or overwrite) the friendly name reference to this relay
-                       // essentially, get _only_ one relay per friendly name that we'll action
-                       uniqueFriendlyNames[curZig.getFriendlyName()] = curZig
-               } else {
-                       // immediately set the PWM for non-friendly name enabled relays (since we know there's no dupes)
-                       relay.setPWM(data["red"].(float64), data["green"].(float64), data["blue"].(float64))
-               }
+               // set (or overwrite) the friendly name reference to this relay
+               // essentially, get _only_ one relay per friendly name that we'll action
+               uniqueDestinations[relay.getDestination()] = relay
 
        }
        // here, we don't care about the key, just the fact that we only get one relay out per
-       for _, relay := range uniqueFriendlyNames {
+       for _, relay := range uniqueDestinations {
                relay.setPWM(data["red"].(float64), data["green"].(float64), data["blue"].(float64))
        }
 
@@ -794,20 +790,7 @@ func (manager *RGBManager) updateMetrics() {
                // sleep for 1 second then update
                time.Sleep(time.Duration(1) * time.Second)
                for _, relay := range manager.relays {
-                       // did we get a friendly name capable relay?
-                       curZig, zigOk := relay.(IRGBFriendly)
-                       // this should give us a fairly unique value, location is friendlyname where enabled, otherwise location is location
-                       if zigOk {
-                               lightGauge.With(prometheus.Labels{"location": curZig.getFriendlyName()}).Set(curZig.getMetricValue())
-                       } else {
-                               curTopic, topicOk := relay.(ITopicRelay)
-                               if topicOk {
-                                       lightGauge.With(prometheus.Labels{"location": curTopic.getTopic()}).Set(relay.getMetricValue())
-                               } else {
-
-                                       lightGauge.With(prometheus.Labels{"location": relay.getLocation()}).Set(relay.getMetricValue())
-                               }
-                       }
+                       lightGauge.With(prometheus.Labels{"location": relay.getDestination()}).Set(relay.getMetricValue())
                }
        }
 }
@@ -869,7 +852,7 @@ func sendLoop(channel chan helper.RabbitSend, rabbit helper.RabbitConfig) {
                        os.Exit(1)
                }
                // give zigbee a bit of a delay to catch up?
-               time.Sleep(time.Duration(500) * time.Millisecond)
+               //time.Sleep(time.Duration(500) * time.Millisecond)
        }
 }