gobot.io/x/gobot/v2@v2.1.0/drivers/aio/temperature_sensor_driver_test.go (about)

     1  package aio
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"gobot.io/x/gobot/v2/gobottest"
    11  )
    12  
    13  func TestTemperatureSensorDriver(t *testing.T) {
    14  	testAdaptor := newAioTestAdaptor()
    15  	d := NewTemperatureSensorDriver(testAdaptor, "123")
    16  	gobottest.Assert(t, d.Connection(), testAdaptor)
    17  	gobottest.Assert(t, d.Pin(), "123")
    18  	gobottest.Assert(t, d.interval, 10*time.Millisecond)
    19  }
    20  
    21  func TestTemperatureSensorDriverNtcScaling(t *testing.T) {
    22  	var tests = map[string]struct {
    23  		input int
    24  		want  float64
    25  	}{
    26  		"smaller_than_min": {input: -1, want: 457.720219684306},
    27  		"min":              {input: 0, want: 457.720219684306},
    28  		"near_min":         {input: 1, want: 457.18923673420545},
    29  		"mid_range":        {input: 127, want: 87.9784401845593},
    30  		"T25C":             {input: 232, want: 24.805280460718336},
    31  		"T0C":              {input: 248, want: -0.9858175109026774},
    32  		"T-25C":            {input: 253, want: -22.92863536929451},
    33  		"near_max":         {input: 254, want: -33.51081663999781},
    34  		"max":              {input: 255, want: -273.15},
    35  		"bigger_than_max":  {input: 256, want: -273.15},
    36  	}
    37  	a := newAioTestAdaptor()
    38  	d := NewTemperatureSensorDriver(a, "4")
    39  	ntc1 := TemperatureSensorNtcConf{TC0: 25, R0: 10000.0, B: 3950} //Ohm, R25=10k, B=3950
    40  	d.SetNtcScaler(255, 1000, true, ntc1)                           //Ohm, reference value: 3300, series R: 1k
    41  	for name, tt := range tests {
    42  		t.Run(name, func(t *testing.T) {
    43  			// arrange
    44  			a.TestAdaptorAnalogRead(func() (val int, err error) {
    45  				val = tt.input
    46  				return
    47  			})
    48  			// act
    49  			got, err := d.Read()
    50  			// assert
    51  			gobottest.Assert(t, err, nil)
    52  			gobottest.Assert(t, got, tt.want)
    53  		})
    54  	}
    55  }
    56  
    57  func TestTemperatureSensorDriverLinearScaling(t *testing.T) {
    58  	var tests = map[string]struct {
    59  		input int
    60  		want  float64
    61  	}{
    62  		"smaller_than_min": {input: -129, want: -40},
    63  		"min":              {input: -128, want: -40},
    64  		"near_min":         {input: -127, want: -39.450980392156865},
    65  		"T-25C":            {input: -101, want: -25.17647058823529},
    66  		"T0C":              {input: -55, want: 0.07843137254902288},
    67  		"T25C":             {input: -10, want: 24.7843137254902},
    68  		"mid_range":        {input: 0, want: 30.274509803921575},
    69  		"near_max":         {input: 126, want: 99.45098039215688},
    70  		"max":              {input: 127, want: 100},
    71  		"bigger_than_max":  {input: 128, want: 100},
    72  	}
    73  	a := newAioTestAdaptor()
    74  	d := NewTemperatureSensorDriver(a, "4")
    75  	d.SetLinearScaler(-128, 127, -40, 100)
    76  	for name, tt := range tests {
    77  		t.Run(name, func(t *testing.T) {
    78  			// arrange
    79  			a.TestAdaptorAnalogRead(func() (val int, err error) {
    80  				val = tt.input
    81  				return
    82  			})
    83  			// act
    84  			got, err := d.Read()
    85  			// assert
    86  			gobottest.Assert(t, err, nil)
    87  			gobottest.Assert(t, got, tt.want)
    88  		})
    89  	}
    90  }
    91  
    92  func TestTempSensorPublishesTemperatureInCelsius(t *testing.T) {
    93  	sem := make(chan bool, 1)
    94  	a := newAioTestAdaptor()
    95  	d := NewTemperatureSensorDriver(a, "1")
    96  	ntc := TemperatureSensorNtcConf{TC0: 25, R0: 10000.0, B: 3975} //Ohm, R25=10k
    97  	d.SetNtcScaler(1023, 10000, false, ntc)                        //Ohm, reference value: 1023, series R: 10k
    98  
    99  	a.TestAdaptorAnalogRead(func() (val int, err error) {
   100  		val = 585
   101  		return
   102  	})
   103  	d.Once(d.Event(Value), func(data interface{}) {
   104  		gobottest.Assert(t, fmt.Sprintf("%.2f", data.(float64)), "31.62")
   105  		sem <- true
   106  	})
   107  	gobottest.Assert(t, d.Start(), nil)
   108  
   109  	select {
   110  	case <-sem:
   111  	case <-time.After(1 * time.Second):
   112  		t.Errorf(" Temperature Sensor Event \"Data\" was not published")
   113  	}
   114  
   115  	gobottest.Assert(t, d.Value(), 31.61532462352477)
   116  }
   117  
   118  func TestTempSensorPublishesError(t *testing.T) {
   119  	sem := make(chan bool, 1)
   120  	a := newAioTestAdaptor()
   121  	d := NewTemperatureSensorDriver(a, "1")
   122  
   123  	// send error
   124  	a.TestAdaptorAnalogRead(func() (val int, err error) {
   125  		err = errors.New("read error")
   126  		return
   127  	})
   128  
   129  	gobottest.Assert(t, d.Start(), nil)
   130  
   131  	// expect error
   132  	d.Once(d.Event(Error), func(data interface{}) {
   133  		gobottest.Assert(t, data.(error).Error(), "read error")
   134  		sem <- true
   135  	})
   136  
   137  	select {
   138  	case <-sem:
   139  	case <-time.After(1 * time.Second):
   140  		t.Errorf(" Temperature Sensor Event \"Error\" was not published")
   141  	}
   142  }
   143  
   144  func TestTempSensorHalt(t *testing.T) {
   145  	d := NewTemperatureSensorDriver(newAioTestAdaptor(), "1")
   146  	done := make(chan struct{})
   147  	go func() {
   148  		<-d.halt
   149  		close(done)
   150  	}()
   151  	gobottest.Assert(t, d.Halt(), nil)
   152  	select {
   153  	case <-done:
   154  	case <-time.After(100 * time.Millisecond):
   155  		t.Errorf(" Temperature Sensor was not halted")
   156  	}
   157  }
   158  
   159  func TestTempDriverDefaultName(t *testing.T) {
   160  	d := NewTemperatureSensorDriver(newAioTestAdaptor(), "1")
   161  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "TemperatureSensor"), true)
   162  }
   163  
   164  func TestTempDriverSetName(t *testing.T) {
   165  	d := NewTemperatureSensorDriver(newAioTestAdaptor(), "1")
   166  	d.SetName("mybot")
   167  	gobottest.Assert(t, d.Name(), "mybot")
   168  }
   169  
   170  func TestTempDriver_initialize(t *testing.T) {
   171  	var tests = map[string]struct {
   172  		input TemperatureSensorNtcConf
   173  		want  TemperatureSensorNtcConf
   174  	}{
   175  		"B_low_tc0": {
   176  			input: TemperatureSensorNtcConf{TC0: -13, B: 2601.5},
   177  			want:  TemperatureSensorNtcConf{TC0: -13, B: 2601.5, t0: 260.15, r: 10},
   178  		},
   179  		"B_low_tc0_B": {
   180  			input: TemperatureSensorNtcConf{TC0: -13, B: 5203},
   181  			want:  TemperatureSensorNtcConf{TC0: -13, B: 5203, t0: 260.15, r: 20},
   182  		},
   183  		"B_mid_tc0": {
   184  			input: TemperatureSensorNtcConf{TC0: 25, B: 3950},
   185  			want:  TemperatureSensorNtcConf{TC0: 25, B: 3950, t0: 298.15, r: 13.248364916988095},
   186  		},
   187  		"B_mid_tc0_r0_no_change": {
   188  			input: TemperatureSensorNtcConf{TC0: 25, R0: 1234.5, B: 3950},
   189  			want:  TemperatureSensorNtcConf{TC0: 25, R0: 1234.5, B: 3950, t0: 298.15, r: 13.248364916988095},
   190  		},
   191  		"B_high_tc0": {
   192  			input: TemperatureSensorNtcConf{TC0: 100, B: 3731.5},
   193  			want:  TemperatureSensorNtcConf{TC0: 100, B: 3731.5, t0: 373.15, r: 10},
   194  		},
   195  		"T1_low": {
   196  			input: TemperatureSensorNtcConf{TC0: 25, R0: 2500.0, TC1: -13, R1: 10000},
   197  			want:  TemperatureSensorNtcConf{TC0: 25, R0: 2500.0, TC1: -13, R1: 10000, B: 2829.6355560320544, t0: 298.15, r: 9.490644159087891},
   198  		},
   199  		"T1_high": {
   200  			input: TemperatureSensorNtcConf{TC0: 25, R0: 2500.0, TC1: 100, R1: 371},
   201  			want:  TemperatureSensorNtcConf{TC0: 25, R0: 2500.0, TC1: 100, R1: 371, B: 2830.087381913779, t0: 298.15, r: 9.49215959052081},
   202  		},
   203  	}
   204  	for name, tt := range tests {
   205  		t.Run(name, func(t *testing.T) {
   206  			// arrange
   207  			ntc := tt.input
   208  			// act
   209  			ntc.initialize()
   210  			// assert
   211  			gobottest.Assert(t, ntc, tt.want)
   212  		})
   213  	}
   214  }