setPWM(red, green, blue float64)
getLocation() string
getRoutingKey() string
+ getDestination() string
getMetricValue() float64
}
-type IRGBFriendly interface {
- IRGBRelay
- getFriendlyName() string
-}
-
type ITopicRelay interface {
getTopic() 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{}
}
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)
return relay.Topic
}
+func (relay *ESPRelay) getDestination() string {
+ return relay.Topic
+}
+
func (relay *ESPRelay) getTopic() string {
return relay.Topic
}
}
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) {
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)
}
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))
}
// 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())
}
}
}
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)
}
}