github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/apis/v1/types_equal.go (about)

     1  // Copyright 2020-2022 Ewout Prangsma
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Author Ewout Prangsma
    16  //
    17  
    18  package v1
    19  
    20  // Equal returns true when both objects have the same values.
    21  func (s *PowerState) Equal(o *PowerState) bool {
    22  	return s.GetEnabled() == o.GetEnabled()
    23  }
    24  
    25  // Equal returns true when both objects have the same values.
    26  func (s *Power) Equal(o *Power) bool {
    27  	return s.GetRequest().Equal(o.GetRequest()) &&
    28  		s.GetActual().Equal(o.GetActual())
    29  }
    30  
    31  // Equal returns true when both objects have the same values.
    32  func (s *LocState) Equal(o *LocState) bool {
    33  	if s.GetDirection() != o.GetDirection() ||
    34  		s.GetSpeed() != o.GetSpeed() ||
    35  		s.GetSpeedSteps() != o.GetSpeedSteps() ||
    36  		len(s.GetFunctions()) != len(o.GetFunctions()) {
    37  		return false
    38  	}
    39  	for k, v := range s.GetFunctions() {
    40  		ov, found := o.GetFunctions()[k]
    41  		if v != ov || !found {
    42  			return false
    43  		}
    44  	}
    45  	return true
    46  }
    47  
    48  // Equal returns true when both objects have the same values.
    49  func (s *Loc) Equal(o *Loc) bool {
    50  	return s.GetAddress() == o.GetAddress() &&
    51  		s.GetRequest().Equal(o.GetRequest()) &&
    52  		s.GetActual().Equal(o.GetActual())
    53  }
    54  
    55  // Equal returns true when both objects have the same values.
    56  func (s *SensorState) Equal(o *SensorState) bool {
    57  	return s.GetValue() == o.GetValue()
    58  }
    59  
    60  // Equal returns true when both objects have the same values.
    61  func (s *Sensor) Equal(o *Sensor) bool {
    62  	return s.GetAddress() == o.GetAddress() &&
    63  		s.GetActual().Equal(o.GetActual())
    64  }
    65  
    66  // Equal returns true when both objects have the same values.
    67  func (s *OutputState) Equal(o *OutputState) bool {
    68  	return s.GetValue() == o.GetValue()
    69  }
    70  
    71  // Equal returns true when both objects have the same values.
    72  func (s *Output) Equal(o *Output) bool {
    73  	return s.GetAddress() == o.GetAddress() &&
    74  		s.GetRequest().Equal(o.GetRequest()) &&
    75  		s.GetActual().Equal(o.GetActual())
    76  }
    77  
    78  // Equal returns true when both objects have the same values.
    79  func (s *SwitchState) Equal(o *SwitchState) bool {
    80  	return s.GetDirection() == o.GetDirection()
    81  }
    82  
    83  // Equal returns true when both objects have the same values.
    84  func (s *Switch) Equal(o *Switch) bool {
    85  	return s.GetAddress() == o.GetAddress() &&
    86  		s.GetRequest().Equal(o.GetRequest()) &&
    87  		s.GetActual().Equal(o.GetActual())
    88  }
    89  
    90  // Equal returns true when both objects have the same values.
    91  func (s *Clock) Equal(o *Clock) bool {
    92  	return s.GetPeriod() == o.GetPeriod() &&
    93  		s.GetHours() == o.GetHours() &&
    94  		s.GetMinutes() == o.GetMinutes() &&
    95  		s.GetUnixtime() == o.GetUnixtime()
    96  }
    97  
    98  // Equal returns true when both objects have the same values.
    99  func (s *Device) Equal(o *Device) bool {
   100  	return s.GetId() == o.GetId() &&
   101  		s.GetType() == o.GetType() &&
   102  		s.GetAddress() == o.GetAddress()
   103  }
   104  
   105  // Equal returns true when both objects have the same values.
   106  func (s *Object) Equal(o *Object) bool {
   107  	return s.GetId() == o.GetId() &&
   108  		s.GetType() == o.GetType() &&
   109  		s.equalConnections(o) &&
   110  		s.equalConfiguration(o)
   111  }
   112  
   113  // equalConnections returns true when Connections in both objects have the same values.
   114  func (s *Object) equalConnections(o *Object) bool {
   115  	if len(s.GetConnections()) != len(o.GetConnections()) {
   116  		return false
   117  	}
   118  	ocs := o.GetConnections()
   119  	for i, sc := range s.GetConnections() {
   120  		if !sc.Equal(ocs[i]) {
   121  			return false
   122  		}
   123  	}
   124  	return true
   125  }
   126  
   127  // equalConfiguration returns true when Object in both objects have the same values.
   128  func (s *Object) equalConfiguration(o *Object) bool {
   129  	if len(s.GetConfiguration()) != len(o.GetConfiguration()) {
   130  		return false
   131  	}
   132  	ocs := o.GetConfiguration()
   133  	for k, sv := range s.GetConfiguration() {
   134  		ov := ocs[k]
   135  		if sv != ov {
   136  			return false
   137  		}
   138  	}
   139  	return true
   140  }
   141  
   142  // Equal returns true when both objects have the same values.
   143  func (s *Connection) Equal(o *Connection) bool {
   144  	return s.GetKey() == o.GetKey() &&
   145  		s.equalPins(o) &&
   146  		s.equalConfiguration(o)
   147  }
   148  
   149  // equalPins returns true when Pins in both objects have the same values.
   150  func (s *Connection) equalPins(o *Connection) bool {
   151  	if len(s.GetPins()) != len(o.GetPins()) {
   152  		return false
   153  	}
   154  	ops := o.GetPins()
   155  	for i, sc := range s.GetPins() {
   156  		if !sc.Equal(ops[i]) {
   157  			return false
   158  		}
   159  	}
   160  	return true
   161  }
   162  
   163  // equalConfiguration returns true when Configuration in both objects have the same values.
   164  func (s *Connection) equalConfiguration(o *Connection) bool {
   165  	if len(s.GetConfiguration()) != len(o.GetConfiguration()) {
   166  		return false
   167  	}
   168  	ocs := o.GetConfiguration()
   169  	for k, sv := range s.GetConfiguration() {
   170  		ov := ocs[k]
   171  		if sv != ov {
   172  			return false
   173  		}
   174  	}
   175  	return true
   176  }
   177  
   178  // Equal returns true when both objects have the same values.
   179  func (s *DevicePin) Equal(o *DevicePin) bool {
   180  	return s.GetDeviceId() == o.GetDeviceId() &&
   181  		s.GetDeviceIndex() == o.GetDeviceIndex()
   182  }
   183  
   184  // Equal returns true when both objects have the same values.
   185  func (s *LocalWorkerConfig) Equal(o *LocalWorkerConfig) bool {
   186  	return s.GetAlias() == o.GetAlias() &&
   187  		s.equalDevices(o) &&
   188  		s.equalObjects(o)
   189  }
   190  
   191  // equalDevices returns true when both objects have the same devices.
   192  func (s *LocalWorkerConfig) equalDevices(o *LocalWorkerConfig) bool {
   193  	if len(s.GetDevices()) != len(o.GetDevices()) {
   194  		return false
   195  	}
   196  	ods := o.GetDevices()
   197  	for i, sc := range s.GetDevices() {
   198  		if !sc.Equal(ods[i]) {
   199  			return false
   200  		}
   201  	}
   202  	return true
   203  }
   204  
   205  // equalObjects returns true when both objects have the same devices.
   206  func (s *LocalWorkerConfig) equalObjects(o *LocalWorkerConfig) bool {
   207  	if len(s.GetObjects()) != len(o.GetObjects()) {
   208  		return false
   209  	}
   210  	oos := o.GetObjects()
   211  	for i, sc := range s.GetObjects() {
   212  		if !sc.Equal(oos[i]) {
   213  			return false
   214  		}
   215  	}
   216  	return true
   217  }