Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added InternalTemp, LineFrequency and OutputVoltage #4

Merged
merged 2 commits into from
Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func testClient(t *testing.T, fn func() [][]byte) (*Client, func()) {
wg.Done()
}()

c, err := Dial("tcp", l.Addr().String())
c, err := Dial("tcp", dialAddr(l))
if err != nil {
t.Fatalf("failed to dial Client: %v", err)
}
Expand All @@ -157,3 +157,11 @@ func kvBytes(kv string) ([]byte, []byte) {

return lenb, []byte(kv)
}

// dialAddr returns a valid net.Dial address to dial using network "tcp".
func dialAddr(ln net.Listener) string {
addr := ln.Addr().String()
addr = strings.TrimPrefix(addr, "[::]")
addr = strings.TrimPrefix(addr, "0.0.0.0")
return addr
}
90 changes: 51 additions & 39 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type Status struct {
NominalPower int
Firmware string
EndAPC time.Time
InternalTemp float64
OutputVoltage float64
LineFrequency float64
}

// parseKV parses an input key/value string in "key : value" format, and
Expand Down Expand Up @@ -119,45 +122,48 @@ func (s *Status) parseKV(kv string) error {

// List of keys sent by a NIS, used to map values to Status fields.
const (
keyAPC = "APC"
keyDate = "DATE"
keyHostname = "HOSTNAME"
keyVersion = "VERSION"
keyUPSName = "UPSNAME"
keyCable = "CABLE"
keyDriver = "DRIVER"
keyUPSMode = "UPSMODE"
keyStartTime = "STARTTIME"
keyModel = "MODEL"
keyStatus = "STATUS"
keyLineV = "LINEV"
keyLoadPct = "LOADPCT"
keyBCharge = "BCHARGE"
keyTimeLeft = "TIMELEFT"
keyMBattChg = "MBATTCHG"
keyMinTimeL = "MINTIMEL"
keyMaxTime = "MAXTIME"
keySense = "SENSE"
keyLoTrans = "LOTRANS"
keyHiTrans = "HITRANS"
keyAlarmDel = "ALARMDEL"
keyBattV = "BATTV"
keyLastXfer = "LASTXFER"
keyNumXfers = "NUMXFERS"
keyXOnBat = "XONBATT"
keyTOnBatt = "TONBATT"
keyCumOnBatt = "CUMONBATT"
keyXOffBat = "XOFFBATT"
keyLastStest = "LASTSTEST"
keySelftest = "SELFTEST"
keyStatFlag = "STATFLAG"
keySerialNo = "SERIALNO"
keyBattDate = "BATTDATE"
keyNomInV = "NOMINV"
keyNomBattV = "NOMBATTV"
keyNomPower = "NOMPOWER"
keyFirmware = "FIRMWARE"
keyEndAPC = "END APC"
keyAPC = "APC"
keyDate = "DATE"
keyHostname = "HOSTNAME"
keyVersion = "VERSION"
keyUPSName = "UPSNAME"
keyCable = "CABLE"
keyDriver = "DRIVER"
keyUPSMode = "UPSMODE"
keyStartTime = "STARTTIME"
keyModel = "MODEL"
keyStatus = "STATUS"
keyLineV = "LINEV"
keyLoadPct = "LOADPCT"
keyBCharge = "BCHARGE"
keyTimeLeft = "TIMELEFT"
keyMBattChg = "MBATTCHG"
keyMinTimeL = "MINTIMEL"
keyMaxTime = "MAXTIME"
keySense = "SENSE"
keyLoTrans = "LOTRANS"
keyHiTrans = "HITRANS"
keyAlarmDel = "ALARMDEL"
keyBattV = "BATTV"
keyLastXfer = "LASTXFER"
keyNumXfers = "NUMXFERS"
keyXOnBat = "XONBATT"
keyTOnBatt = "TONBATT"
keyCumOnBatt = "CUMONBATT"
keyXOffBat = "XOFFBATT"
keyLastStest = "LASTSTEST"
keySelftest = "SELFTEST"
keyStatFlag = "STATFLAG"
keySerialNo = "SERIALNO"
keyBattDate = "BATTDATE"
keyNomInV = "NOMINV"
keyNomBattV = "NOMBATTV"
keyNomPower = "NOMPOWER"
keyFirmware = "FIRMWARE"
keyEndAPC = "END APC"
keyITemp = "ITEMP"
keyOutV = "OUTPUTV"
keyLineFrequency = "LINEFREQ"
)

// parseKVString parses a simple string into the appropriate Status field.
Expand Down Expand Up @@ -229,6 +235,12 @@ func (s *Status) parseKVFloat(k string, v string) (bool, error) {
s.NominalInputVoltage, err = parse()
case keyNomBattV:
s.NominalBatteryVoltage, err = parse()
case keyITemp:
s.InternalTemp, err = parse()
case keyOutV:
s.OutputVoltage, err = parse()
case keyLineFrequency:
s.LineFrequency, err = parse()
default:
return false, nil
}
Expand Down
21 changes: 21 additions & 0 deletions status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ func TestStatus_parseKV(t *testing.T) {
AlarmDel: 0,
},
},
{
desc: "OK ITEMP",
kv: "ITEMP : 35.1 C",
s: &Status{
InternalTemp: 35.1,
},
},
{
desc: "OK OUTPUTV",
kv: "OUTPUTV : 230.4 Volts",
s: &Status{
OutputVoltage: 230.4,
},
},
{
desc: "OK LINEFREQ",
kv: "LINEFREQ : 50.0 Hz",
s: &Status{
LineFrequency: 50.0,
},
},
}

for _, tt := range tests {
Expand Down