diff --git a/go.mod b/go.mod index f0c1004..1622c8a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( github.com/gorilla/websocket v1.4.0 github.com/prometheus/client_golang v0.9.2 github.com/rakyll/statik v0.1.5 + github.com/stretchr/testify v1.3.0 github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 golang.org/x/sys v0.0.0-20190312061237-fead79001313 // indirect ) diff --git a/go.sum b/go.sum index 621c6f4..e817b0c 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,15 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.2 h1:awm861/B8OKDd2I/6o1dy3ra4BamzKhYOiGItCeZ740= github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8= @@ -16,6 +20,9 @@ github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nL github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/rakyll/statik v0.1.5 h1:Ly2UjURzxnsSYS0zI50fZ+srA+Fu7EbpV5hglvJvJG0= github.com/rakyll/statik v0.1.5/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/mk2driver/mk2_test.go b/mk2driver/mk2_test.go new file mode 100644 index 0000000..871132b --- /dev/null +++ b/mk2driver/mk2_test.go @@ -0,0 +1,141 @@ +/** +write out: []byte{0x04, 0xff, 0x41, 0x01, 0x00, 0xbb, } +read byte: []byte{0x04, } +read byte: []byte{0xff, } +read unlocked: []byte{0x41, 0x01, 0x00, 0xbb, } +2019/03/17 16:24:17 Locked + + + +write out: []byte{0x04, 0xff, 0x41, 0x01, 0x00, 0xbb, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x00, 0x00, 0x6f, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x01, 0x00, 0x6e, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x02, 0x00, 0x6d, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x03, 0x00, 0x6c, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x04, 0x00, 0x6b, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x05, 0x00, 0x6a, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x06, 0x00, 0x69, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x07, 0x00, 0x68, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x08, 0x00, 0x67, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x09, 0x00, 0x66, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x0a, 0x00, 0x65, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x0b, 0x00, 0x64, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x0c, 0x00, 0x63, } +write out: []byte{0x05, 0xff, 0x57, 0x36, 0x0d, 0x00, 0x62, } +write out: []byte{0x03, 0xff, 0x46, 0x00, 0xb8, } +write out: []byte{0x03, 0xff, 0x46, 0x01, 0xb7, } +write out: []byte{0x02, 0xff, 0x4c, 0xb3, } +write out: []byte{0x05, 0xff, 0x57, 0x30, 0x0d, 0x00, 0x68, } +write out: []byte{0x03, 0xff, 0x46, 0x00, 0xb8, } +write out: []byte{0x03, 0xff, 0x46, 0x01, 0xb7, } +write out: []byte{0x02, 0xff, 0x4c, 0xb3, } +write out: []byte{0x05, 0xff, 0x57, 0x30, 0x0d, 0x00, 0x68, } +*/ + +package mk2driver_test + +import ( + "bytes" + "io" + "testing" + + "github.com/diebietse/invertergui/mk2driver" + "github.com/stretchr/testify/assert" +) + +var knownWrites = []byte{ + 0x04, 0xff, 0x41, 0x01, 0x00, 0xbb, + 0x05, 0xff, 0x57, 0x36, 0x00, 0x00, 0x6f, + 0x05, 0xff, 0x57, 0x36, 0x01, 0x00, 0x6e, + 0x05, 0xff, 0x57, 0x36, 0x02, 0x00, 0x6d, + 0x05, 0xff, 0x57, 0x36, 0x03, 0x00, 0x6c, + 0x05, 0xff, 0x57, 0x36, 0x04, 0x00, 0x6b, + 0x05, 0xff, 0x57, 0x36, 0x05, 0x00, 0x6a, + 0x05, 0xff, 0x57, 0x36, 0x06, 0x00, 0x69, + 0x05, 0xff, 0x57, 0x36, 0x07, 0x00, 0x68, + 0x05, 0xff, 0x57, 0x36, 0x08, 0x00, 0x67, + 0x05, 0xff, 0x57, 0x36, 0x09, 0x00, 0x66, + 0x05, 0xff, 0x57, 0x36, 0x0a, 0x00, 0x65, + 0x05, 0xff, 0x57, 0x36, 0x0b, 0x00, 0x64, + 0x05, 0xff, 0x57, 0x36, 0x0c, 0x00, 0x63, + 0x05, 0xff, 0x57, 0x36, 0x0d, 0x00, 0x62, + 0x03, 0xff, 0x46, 0x00, 0xb8, + 0x03, 0xff, 0x46, 0x01, 0xb7, + 0x02, 0xff, 0x4c, 0xb3, + 0x05, 0xff, 0x57, 0x30, 0x0d, 0x00, 0x68, +} + +var writeBuffer = bytes.NewBuffer(nil) + +type testIo struct { + io.Reader + io.Writer +} + +func NewIOStub(readBuffer []byte) io.ReadWriter { + return &testIo{ + Reader: bytes.NewBuffer(readBuffer), + Writer: writeBuffer, + } +} + +// Test a know sequence as reference as extracted from Mk2 +func TestSync(t *testing.T) { + knownReadBuffer := []byte{ + //Len Cmd + 0x04, 0xff, 0x41, 0x01, 0x00, 0xbb, 0x07, 0xff, 0x56, 0x96, 0x3e, 0x11, 0x00, 0x00, 0xbf, + 0x08, 0xff, 0x57, 0x8e, 0x9c, 0x7f, 0x8f, 0x00, 0x00, 0x6a, + 0x08, 0xff, 0x57, 0x8e, 0x64, 0x80, 0x8f, 0x00, 0x00, 0xa1, + 0x08, 0xff, 0x57, 0x8e, 0x9c, 0x7f, 0x8f, 0x00, 0x00, 0x6a, + 0x08, 0xff, 0x57, 0x8e, 0x9c, 0x7f, 0x8f, 0x00, 0x00, 0x6a, + 0x08, 0xff, 0x57, 0x8e, 0x9c, 0x7f, 0x8f, 0x00, 0x00, 0x6a, + 0x08, 0xff, 0x57, 0x8e, 0x64, 0x80, 0x8f, 0x00, 0x00, 0xa1, + 0x08, 0xff, 0x57, 0x8e, 0x9c, 0x7f, 0x8f, 0x00, 0x00, 0x6a, + 0x08, 0xff, 0x57, 0x8e, 0x57, 0x78, 0x8f, 0x00, 0x01, 0xb5, + 0x08, 0xff, 0x57, 0x8e, 0x2f, 0x7c, 0x8f, 0x00, 0x00, 0xda, + 0x08, 0xff, 0x57, 0x8e, 0x64, 0x80, 0x8f, 0x00, 0x00, 0xa1, + 0x08, 0xff, 0x57, 0x8e, 0x04, 0x00, 0x8f, 0x00, 0x80, 0x01, + 0x08, 0xff, 0x57, 0x8e, 0x01, 0x00, 0x8f, 0x00, 0x80, 0x04, + 0x08, 0xff, 0x57, 0x8e, 0x02, 0x00, 0x8f, 0x00, 0x80, 0x03, + 0x08, 0xff, 0x57, 0x8e, 0x38, 0x7f, 0x8f, 0x00, 0x00, 0xce, + 0x07, 0xff, 0x56, 0x96, 0x3e, 0x11, 0x00, 0x00, 0xbf, + 0x0f, 0x20, 0xf3, 0x00, 0xc8, 0x02, 0x0c, 0xa1, 0x05, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x88, 0xb2, + 0x0f, 0x20, 0x01, 0x01, 0xca, 0x09, 0x08, 0xaa, 0x58, 0xab, 0x00, 0xaa, 0x58, 0x9a, 0x00, 0xc3, 0xe8, + 0x06, 0xff, 0x4c, 0x03, 0x00, 0x00, 0x00, 0xac, + 0x05, 0xff, 0x57, 0x85, 0xc8, 0x00, 0x58, + } + + expectedLEDs := map[mk2driver.Led]mk2driver.LEDstate{ + mk2driver.LedMain: mk2driver.LedOn, + mk2driver.LedAbsorption: mk2driver.LedOn, + mk2driver.LedBulk: mk2driver.LedOff, + mk2driver.LedFloat: mk2driver.LedOff, + mk2driver.LedInverter: mk2driver.LedOff, + mk2driver.LedOverload: mk2driver.LedOff, + mk2driver.LedLowBattery: mk2driver.LedOff, + mk2driver.LedTemperature: mk2driver.LedOff, + } + testIO := NewIOStub(knownReadBuffer) + mk2, err := mk2driver.NewMk2Connection(testIO) + assert.NoError(t, err, "Could not open MK2") + + event := <-mk2.C() + mk2.Close() + + assert.Equal(t, 0, bytes.Compare(writeBuffer.Bytes(), knownWrites), "Expected writes did not match received writes") + assert.True(t, event.Valid, "data not valid") + assert.Equal(t, uint32(2736), event.Version, "Invalid version decoded") + assert.Equal(t, 0, len(event.Errors), "Reported errors not empty") + assert.Equal(t, expectedLEDs, event.LEDs, "Reported LEDs incorrect") + + epsilon := 0.00000001 + assert.InEpsilon(t, 14.41, event.BatVoltage, epsilon, "BatVoltage conversion failed") + assert.InEpsilon(t, -0.4, event.BatCurrent, epsilon, "BatCurrent conversion failed") + assert.InEpsilon(t, 226.98, event.InVoltage, epsilon, "InVoltage conversion failed") + assert.InEpsilon(t, 1.71, event.InCurrent, epsilon, "InCurrent conversion failed") + assert.InEpsilon(t, 50.10256410256411, event.InFrequency, epsilon, "InFrequency conversion failed") + assert.InEpsilon(t, 226.980, event.OutVoltage, epsilon, "OutVoltage conversion failed") + assert.InEpsilon(t, 1.54, event.OutCurrent, epsilon, "OutCurrent conversion failed") + assert.InEpsilon(t, 50.025510204081634, event.OutFrequency, epsilon, "OutFrequency conversion failed") + assert.InEpsilon(t, 1, event.ChargeState, epsilon, "ChargeState conversion failed") +}