Skip to content

Commit

Permalink
Merge pull request #172 from MiSchroe/MiSchroe/issue171
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSchroe authored Dec 30, 2024
2 parents 0464f3e + 1c21b91 commit 874e368
Show file tree
Hide file tree
Showing 6 changed files with 980 additions and 596 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Placeholder for the next version (at the beginning of the line):
## __WORK IN PROGRESS__
-->

## **WORK IN PROGRESS**

- [#171](https://github.com/MiSchroe/klf-200-api/issues/171) Split each of Limitation Originator and Limitation Time into two separate properties for Min and Max values.

## 5.0.2 (2024-11-11)

- [#165](https://github.com/MiSchroe/klf-200-api/issues/165) Fix logging error when the login throws an error in the TLS stack.
Expand Down
8 changes: 6 additions & 2 deletions src/KLF200-API/GW_COMMAND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ export class LockTime {
return lockTime / 30 - 1;
}

static lockTimeValueToLockTimeForLimitation(lockTimeValue: number): number {
static lockTimeValueToLockTimeForLimitation(lockTimeValue: number): number | undefined {
// Check parameter range
if (lockTimeValue < 0 || lockTimeValue > 253) throw new Error("Lock time value out of range.");
if (lockTimeValue < 0 || lockTimeValue > 255) throw new Error("Lock time value out of range.");

if (lockTimeValue > 253) {
return undefined;
}

return lockTimeValue === 253 ? Infinity : lockTimeValue * 30 + 30;
}
Expand Down
137 changes: 136 additions & 1 deletion src/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ export class Product extends Component {
this._limitationMinRaw = new Array<number>(17).fill(0);
this._limitationMaxRaw = new Array<number>(17).fill(0xc800);
this._limitationOriginator = new Array<CommandOriginator>(17).fill(CommandOriginator.User);
this._limitationOriginatorMin = new Array<CommandOriginator>(17).fill(CommandOriginator.User);
this._limitationOriginatorMax = new Array<CommandOriginator>(17).fill(CommandOriginator.User);
this._limitationTimeRaw = new Array<number>(17).fill(LockTime.lockTimeTolockTimeValueForLimitation(Infinity));
this._limitationTimeRawMin = new Array<number>(17).fill(
LockTime.lockTimeTolockTimeValueForLimitation(Infinity),
);
this._limitationTimeRawMax = new Array<number>(17).fill(
LockTime.lockTimeTolockTimeValueForLimitation(Infinity),
);

this.Connection.on(
async (frame) => {
Expand Down Expand Up @@ -667,6 +675,7 @@ export class Product extends Component {
* A read only array of the limitation originators.
* @readonly
* @type {CommandOriginator[]}
* @deprecated Use {@link LimitationOriginatorMin} or {@link LimitationOriginatorMax} instead.
*/
public get LimitationOriginator(): readonly CommandOriginator[] {
return Array.from(this._limitationOriginator);
Expand All @@ -678,16 +687,60 @@ export class Product extends Component {
*
* @param functionalParameter Paramter for which the limitation originator should be returned.
* @returns The limitation originator.
* @deprecated Use {@link getLimitationOriginatorMin} or {@link getLimitationOriginatorMax} instead.
*/
public getLimitationOriginator(functionalParameter: ParameterActive): CommandOriginator {
return this._limitationOriginator[functionalParameter];
}

private _limitationOriginatorMin: CommandOriginator[];
/**
* A read only array of the limitation originators for the minimum values.
* @readonly
* @type {CommandOriginator[]}
*/
public get LimitationOriginatorMin(): readonly CommandOriginator[] {
return Array.from(this._limitationOriginatorMin);
}

/**
* Returns the limitation originator for a functional parameter for the minimum value.
* You have to call {@link refreshLimitationAsync} to get the latest values first.
*
* @param functionalParameter Paramter for which the limitation originator should be returned.
* @returns The limitation originator.
*/
public getLimitationOriginatorMin(functionalParameter: ParameterActive): CommandOriginator {
return this._limitationOriginatorMin[functionalParameter];
}

private _limitationOriginatorMax: CommandOriginator[];
/**
* A read only array of the limitation originators for the maximum values.
* @readonly
* @type {CommandOriginator[]}
*/
public get LimitationOriginatorMax(): readonly CommandOriginator[] {
return Array.from(this._limitationOriginatorMax);
}

/**
* Returns the limitation originator for a functional parameter for the maximum value.
* You have to call {@link refreshLimitationAsync} to get the latest values first.
*
* @param functionalParameter Paramter for which the limitation originator should be returned.
* @returns The limitation originator.
*/
public getLimitationOriginatorMax(functionalParameter: ParameterActive): CommandOriginator {
return this._limitationOriginatorMax[functionalParameter];
}

private _limitationTimeRaw: number[];
/**
* A read only array of the limitation time raw values.
* @readonly
* @type {number[]}
* @deprecated Use {@link LimitationTimeRawMin} or {@link LimitationTimeRawMax} instead.
*/
public get LimitationTimeRaw(): readonly number[] {
return Array.from(this._limitationTimeRaw);
Expand All @@ -699,6 +752,7 @@ export class Product extends Component {
*
* @param functionalParameter Parameter for which the limitation time raw value should be returned.
* @returns The raw limitation time value.
* @deprecated Use {@link getLimitationTimeRawMin} or {@link getLimitationTimeRawMax} instead.
*/
public getLimitationTimeRaw(functionalParameter: ParameterActive): number {
return this._limitationTimeRaw[functionalParameter];
Expand All @@ -710,11 +764,76 @@ export class Product extends Component {
*
* @param functionalParameter Parameter for which the limitation time should be returned.
* @returns The limitation time in seconds or Infinity.
* @deprecated Use {@link getLimitationTimeMin} or {@link getLimitationTimeMax} instead.
*/
public getLimitationTime(functionalParameter: ParameterActive): number {
public getLimitationTime(functionalParameter: ParameterActive): number | undefined {
return LockTime.lockTimeValueToLockTimeForLimitation(this.getLimitationTimeRaw(functionalParameter));
}

private _limitationTimeRawMin: number[];
/**
* A read only array of the limitation time raw values for the minimum value.
* @readonly
* @type {number[]}
*/
public get LimitationTimeRawMin(): readonly number[] {
return Array.from(this._limitationTimeRawMin);
}

/**
* Returns the raw value of the limitation time of the minimum value for a functional parameter.
* You have to call {@link refreshLimitationAsync} to get the latest values first.
*
* @param functionalParameter Parameter for which the limitation time raw value should be returned.
* @returns The raw limitation time value.
*/
public getLimitationTimeRawMin(functionalParameter: ParameterActive): number {
return this._limitationTimeRawMin[functionalParameter];
}

/**
* Returns the limitation time of the minimum value in seconds for a functional parameter.
* You have to call {@link refreshLimitationAsync} to get the latest values first.
*
* @param functionalParameter Parameter for which the limitation time should be returned.
* @returns The limitation time in seconds or Infinity.
*/
public getLimitationTimeMin(functionalParameter: ParameterActive): number | undefined {
return LockTime.lockTimeValueToLockTimeForLimitation(this.getLimitationTimeRawMin(functionalParameter));
}

private _limitationTimeRawMax: number[];
/**
* A read only array of the limitation time raw values for the maximum value.
* @readonly
* @type {number[]}
*/
public get LimitationTimeRawMax(): readonly number[] {
return Array.from(this._limitationTimeRawMax);
}

/**
* Returns the raw value of the limitation time of the maximum value for a functional parameter.
* You have to call {@link refreshLimitationAsync} to get the latest values first.
*
* @param functionalParameter Parameter for which the limitation time raw value should be returned.
* @returns The raw limitation time value.
*/
public getLimitationTimeRawMax(functionalParameter: ParameterActive): number {
return this._limitationTimeRawMax[functionalParameter];
}

/**
* Returns the limitation time ot the maximum value in seconds for a functional parameter.
* You have to call {@link refreshLimitationAsync} to get the latest values first.
*
* @param functionalParameter Parameter for which the limitation time should be returned.
* @returns The limitation time in seconds or Infinity.
*/
public getLimitationTimeMax(functionalParameter: ParameterActive): number | undefined {
return LockTime.lockTimeValueToLockTimeForLimitation(this.getLimitationTimeRawMax(functionalParameter));
}

private _limitationMinRaw: number[];
/**
* A read only array of the raw limitations' min values.
Expand Down Expand Up @@ -930,12 +1049,28 @@ export class Product extends Component {
this._limitationMinRaw[frame.ParameterID] = frame.LimitationValueMin;
await this.propertyChanged("LimitationMinRaw");
}
if (frame.LimitationOriginator !== this._limitationOriginatorMin[frame.ParameterID]) {
this._limitationOriginatorMin[frame.ParameterID] = frame.LimitationOriginator;
await this.propertyChanged("LimitationOriginatorMin");
}
if (frame.LimitationTime !== this._limitationTimeRawMin[frame.ParameterID]) {
this._limitationTimeRawMin[frame.ParameterID] = frame.LimitationTime;
await this.propertyChanged("LimitationTimeRawMin");
}
}
if (limitationTypes.indexOf(LimitationType.MaximumLimitation) !== -1) {
if (frame.LimitationValueMax !== this._limitationMaxRaw[frame.ParameterID]) {
this._limitationMaxRaw[frame.ParameterID] = frame.LimitationValueMax;
await this.propertyChanged("LimitationMaxRaw");
}
if (frame.LimitationOriginator !== this._limitationOriginatorMax[frame.ParameterID]) {
this._limitationOriginatorMax[frame.ParameterID] = frame.LimitationOriginator;
await this.propertyChanged("LimitationOriginatorMax");
}
if (frame.LimitationTime !== this._limitationTimeRawMax[frame.ParameterID]) {
this._limitationTimeRawMax[frame.ParameterID] = frame.LimitationTime;
await this.propertyChanged("LimitationTimeRawMax");
}
}
if (frame.LimitationOriginator !== this._limitationOriginator[frame.ParameterID]) {
this._limitationOriginator[frame.ParameterID] = frame.LimitationOriginator;
Expand Down
13 changes: 10 additions & 3 deletions test/KLF200-API/GW_COMMAND-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import { expect } from "chai";
import { LockTime, convertPositionRaw, convertPosition } from "../../src/KLF200-API/GW_COMMAND";
import { LockTime, convertPosition, convertPositionRaw } from "../../src/KLF200-API/GW_COMMAND";
import { ActuatorType } from "../../src/KLF200-API/GW_SYSTEMTABLE_DATA";

describe("GW_COMMAND", function () {
Expand Down Expand Up @@ -119,11 +119,18 @@ describe("GW_COMMAND", function () {
expect(() => LockTime.lockTimeValueToLockTimeForLimitation(inputValue)).to.throw();
});

it("should throw on an input value greater than 253", function () {
const inputValue = 254;
it("should throw on an input value greater than 255", function () {
const inputValue = 256;

expect(() => LockTime.lockTimeValueToLockTimeForLimitation(inputValue)).to.throw();
});

it("should return undefined for an input value greater than 253", function () {
const inputValue = 254;
const result = LockTime.lockTimeValueToLockTimeForLimitation(inputValue);

expect(result).to.be.undefined;
});
});

describe("lockTimeTolockTimeValueForLimitation", function () {
Expand Down
Loading

0 comments on commit 874e368

Please sign in to comment.