From 7b597fc5267f03cd9e291aca53270031c5c19f1f Mon Sep 17 00:00:00 2001 From: Michael Parker Date: Sat, 6 Nov 2021 23:08:56 -0500 Subject: [PATCH 1/3] update deviceinfo function a dirty fix for the device info potentially being a 0/1 instead of false/true --- internal/pkg/models/system.go | 88 +++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/internal/pkg/models/system.go b/internal/pkg/models/system.go index c4f97a6..3604ed0 100644 --- a/internal/pkg/models/system.go +++ b/internal/pkg/models/system.go @@ -1,5 +1,7 @@ package models +import "encoding/json" + type DstInformation struct { Enable bool `json:"enable"` EndHour int `json:"endHour"` @@ -48,6 +50,92 @@ type DeviceInformation struct { Wifi bool `json:"wifi"` } +func (d *DeviceInformation) UnmarshalJSON(b []byte) error { + var deviceInformation struct { + B485 int `json:"B485"` + IoInputNumber int `json:"IOInputNum"` + IoOutputNumber int `json:"IOOutputNum"` + AudioNumber int `json:"AudioNum"` + BuildDay string `json:"buildDay"` + ConfigVersion string `json:"cfgVer"` + ChannelNumber int `json:"channelNum"` + Detail string `json:"detail"` + DiskNumber int `json:"diskNum"` + FirmwareVersion string `json:"firmVer"` + HardwareVersion string `json:"hardVer"` + Model string `json:"model"` + Name string `json:"name"` + Serial string `json:"serial"` + Type string `json:"type"` + Wifi int `json:"wifi"` + } + + if err := json.Unmarshal(b, &deviceInformation); err != nil { + var deviceInfo struct { + B485 int `json:"B485"` + IoInputNumber int `json:"IOInputNum"` + IoOutputNumber int `json:"IOOutputNum"` + AudioNumber int `json:"AudioNum"` + BuildDay string `json:"buildDay"` + ConfigVersion string `json:"cfgVer"` + ChannelNumber int `json:"channelNum"` + Detail string `json:"detail"` + DiskNumber int `json:"diskNum"` + FirmwareVersion string `json:"firmVer"` + HardwareVersion string `json:"hardVer"` + Model string `json:"model"` + Name string `json:"name"` + Serial string `json:"serial"` + Type string `json:"type"` + Wifi bool `json:"wifi"` + } + if err := json.Unmarshal(b, &deviceInfo); err != nil { + return err + } else { + d.B485 = deviceInfo.B485 + d.IoInputNumber = deviceInfo.IoInputNumber + d.IoOutputNumber = deviceInfo.IoOutputNumber + d.AudioNumber = deviceInfo.AudioNumber + d.BuildDay = deviceInfo.BuildDay + d.ConfigVersion = deviceInfo.ConfigVersion + d.ChannelNumber = deviceInfo.ChannelNumber + d.DiskNumber = deviceInfo.DiskNumber + d.FirmwareVersion = deviceInfo.FirmwareVersion + d.HardwareVersion = deviceInfo.HardwareVersion + d.Model = deviceInfo.Model + d.Name = deviceInfo.Name + d.Serial = deviceInfo.Serial + d.Type = deviceInfo.Type + d.Wifi = deviceInfo.Wifi + return nil + } + } + + switch deviceInformation.Wifi { + case 1: + d.Wifi = true + default: + d.Wifi = false + } + + d.B485 = deviceInformation.B485 + d.IoInputNumber = deviceInformation.IoInputNumber + d.IoOutputNumber = deviceInformation.IoOutputNumber + d.AudioNumber = deviceInformation.AudioNumber + d.BuildDay = deviceInformation.BuildDay + d.ConfigVersion = deviceInformation.ConfigVersion + d.ChannelNumber = deviceInformation.ChannelNumber + d.DiskNumber = deviceInformation.DiskNumber + d.FirmwareVersion = deviceInformation.FirmwareVersion + d.HardwareVersion = deviceInformation.HardwareVersion + d.Model = deviceInformation.Model + d.Name = deviceInformation.Name + d.Serial = deviceInformation.Serial + d.Type = deviceInformation.Type + + return nil +} + type DevicePerformanceInformation struct { CodecRate int `json:"codecRate"` CpuUsed int `json:"cpuUsed"` From ac1a53a446b053569cf6803a24b6fb7c23a720a6 Mon Sep 17 00:00:00 2001 From: Michael Parker Date: Sat, 6 Nov 2021 23:12:16 -0500 Subject: [PATCH 2/3] code cleanup get rid of lines that aren't needed. --- internal/pkg/models/system.go | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/internal/pkg/models/system.go b/internal/pkg/models/system.go index 3604ed0..94746a2 100644 --- a/internal/pkg/models/system.go +++ b/internal/pkg/models/system.go @@ -92,30 +92,15 @@ func (d *DeviceInformation) UnmarshalJSON(b []byte) error { if err := json.Unmarshal(b, &deviceInfo); err != nil { return err } else { - d.B485 = deviceInfo.B485 - d.IoInputNumber = deviceInfo.IoInputNumber - d.IoOutputNumber = deviceInfo.IoOutputNumber - d.AudioNumber = deviceInfo.AudioNumber - d.BuildDay = deviceInfo.BuildDay - d.ConfigVersion = deviceInfo.ConfigVersion - d.ChannelNumber = deviceInfo.ChannelNumber - d.DiskNumber = deviceInfo.DiskNumber - d.FirmwareVersion = deviceInfo.FirmwareVersion - d.HardwareVersion = deviceInfo.HardwareVersion - d.Model = deviceInfo.Model - d.Name = deviceInfo.Name - d.Serial = deviceInfo.Serial - d.Type = deviceInfo.Type d.Wifi = deviceInfo.Wifi - return nil } - } - - switch deviceInformation.Wifi { - case 1: - d.Wifi = true - default: - d.Wifi = false + } else { + switch deviceInformation.Wifi { + case 1: + d.Wifi = true + default: + d.Wifi = false + } } d.B485 = deviceInformation.B485 From 051595d2e66f52d6fe34f3da683150f5de6331b3 Mon Sep 17 00:00:00 2001 From: Michael Parker Date: Sat, 6 Nov 2021 23:15:42 -0500 Subject: [PATCH 3/3] add comments add code comments explaining why I did what I did. --- internal/pkg/models/system.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/pkg/models/system.go b/internal/pkg/models/system.go index 94746a2..76bb67a 100644 --- a/internal/pkg/models/system.go +++ b/internal/pkg/models/system.go @@ -70,7 +70,9 @@ func (d *DeviceInformation) UnmarshalJSON(b []byte) error { Wifi int `json:"wifi"` } + // try to unmarshal with wifi as an int if err := json.Unmarshal(b, &deviceInformation); err != nil { + // if it fails to parse because the value is not an int var deviceInfo struct { B485 int `json:"B485"` IoInputNumber int `json:"IOInputNum"` @@ -89,12 +91,17 @@ func (d *DeviceInformation) UnmarshalJSON(b []byte) error { Type string `json:"type"` Wifi bool `json:"wifi"` } + if err := json.Unmarshal(b, &deviceInfo); err != nil { + // return the error if neither an int or bool return err } else { + // set wifi value to wifi status d.Wifi = deviceInfo.Wifi } } else { + // if wifi was an int + // set wifi value to wifi status switch deviceInformation.Wifi { case 1: d.Wifi = true @@ -103,6 +110,7 @@ func (d *DeviceInformation) UnmarshalJSON(b []byte) error { } } + // set the rest of the values as expected. d.B485 = deviceInformation.B485 d.IoInputNumber = deviceInformation.IoInputNumber d.IoOutputNumber = deviceInformation.IoOutputNumber @@ -118,6 +126,7 @@ func (d *DeviceInformation) UnmarshalJSON(b []byte) error { d.Serial = deviceInformation.Serial d.Type = deviceInformation.Type + // return values return nil }