Skip to content

Commit

Permalink
hmlib: change sysvar structure and output
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommi2Day committed Feb 18, 2024
1 parent 473dba3 commit bd65fc7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Go Library

## [v1.11.4 - 2024-02-18]
### Changed
- hmlib: use plain url insead of httpclient query params encoded strings
- hmlib: change sysvar structure and output

## [v1.11.3 - 2024-02-16]
### Changed
- dblib: use bitnami/openldap as test container
Expand Down
56 changes: 51 additions & 5 deletions hmlib/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package hmlib
import (
"encoding/xml"
"fmt"
"strings"

"github.com/tommi2day/gomodules/common"

Expand All @@ -25,7 +24,7 @@ type SysvarListResponse struct {
// SysVarEntry is a single system variable in SysvarListResponse
type SysVarEntry struct {
XMLName xml.Name `xml:"systemVariable"`
Name string `xml:"Name,attr"`
Name string `xml:"name,attr"`
Variable string `xml:"variable,attr"`
Value string `xml:"value,attr"`
ValueList string `xml:"value_list,attr"`
Expand All @@ -39,6 +38,7 @@ type SysVarEntry struct {
Timestamp string `xml:"timestamp,attr"`
ValueName0 string `xml:"value_name_0,attr"`
ValueName1 string `xml:"value_name_1,attr"`
Info string `xml:"info,attr"`
}

const pmTrue = "true"
Expand All @@ -48,15 +48,61 @@ var SysVarIDMap = map[string]SysVarEntry{}

// String returns a string representation of the system variable list
func (e SysVarEntry) String() string {
return fmt.Sprintf("ID:%s, %s= %s (%s), ts %s\n", e.IseID, e.Name, e.Value, e.Unit, common.FormatUnixtsString(e.Timestamp, "2006-01-02 15:04:05"))
ts := ""
if len(e.Timestamp) > 0 && e.Timestamp != "0" {
ts = fmt.Sprintf(", Since:%s", common.FormatUnixtsString(e.Timestamp, "2006-01-02 15:04:05"))
}
v := e.GetValue()
if e.Unit != "" {
v = fmt.Sprintf("%s %s", v, e.Unit)
}
i := ""
if e.Info != "" {
i = fmt.Sprintf(", INFO: %s", e.Info)
}
output := fmt.Sprintf("ID:%s, Name: %s, Value: %s%s%s", e.IseID, e.Name, v, i, ts)
return output
}

// GetValue returns the value of the system variable depending on the type
func (e SysVarEntry) GetValue() string {
v := e.Value
switch e.Type {
case "2":
// boolean
switch e.Subtype {
case "2":
// logical
if e.Value == "true" {
v = e.ValueName1
} else {
v = e.ValueName0
}
case "6":
// Alarm
if len(e.Value) > 0 {
v = e.ValueName1
} else {
v = e.ValueName0
}
}
case "4":
// number
case "16":
// Liste
v = e.ValueText
case "20":
// string
}
return v
}

// GetSysvar returns a single system variable
func GetSysvar(sysvarIDs []string, text bool) (result SysvarListResponse, err error) {
func GetSysvar(sysvarIDs string, text bool) (result SysvarListResponse, err error) {
log.Debug("sysvars called")
var parameter = make(map[string]string)
if len(sysvarIDs) > 0 {
parameter["ise_id"] = strings.Join(sysvarIDs, ",")
parameter["ise_id"] = common.RemoveSpace(sysvarIDs)
}
if text {
parameter["text"] = pmTrue
Expand Down
2 changes: 1 addition & 1 deletion hmlib/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestSysvar(t *testing.T) {
httpmock.RegisterResponderWithQuery(
"GET", fakeURL, queryVar,
httpmock.NewStringResponder(200, SysVarEmptyTest))
l, err := GetSysvar([]string{"4711"}, false)
l, err := GetSysvar("4711", false)
assert.NoErrorf(t, err, "GetSysvar should not return an error:%s", err)
assert.NotNil(t, l, "GetSysvar should return a list")
assert.Equal(t, 0, len(l.SysvarEntry), "GetSysvar should return 0 entry")
Expand Down

0 comments on commit bd65fc7

Please sign in to comment.