Skip to content

Commit

Permalink
- Version to 0.5.0
Browse files Browse the repository at this point in the history
- Added config matching_strategy, speed or direction first matching
  • Loading branch information
aukedejong committed Feb 17, 2023
1 parent ff7867f commit 416c133
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 32 deletions.
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,25 @@ Else, if you prefer the graphical editor, use the menu to add the resource:

### Card options

| Name | Type | Default | Required | Description |
|----------------------------|:-------:|:-------:|:--------:|-------------------------------------------------------------------------------------------------------|
| type | string | | x | `custom:windrose-card`. |
| title | string | | - | The card title. |
| wind_direction_entity | string | | x | The wind direction entity, having directing in degrees as the state. |
| windspeed_entities | object | | x | One are more windspeed entities. Only the first is used for the windrose. (for now) |
| refresh_interval | number | 300 | - | Refresh interval in seconds |
| hours_to_show | number | 4 | - | Show winddata for the last number of hours. |
| max_width | number | null | - | Use to limit the with (and height) of the windrose. |
| windspeed_bar_location | string | bottom | - | Location of the speed bar graph: `bottom`, `right` |
| windspeed_bar_full | boolean | true | - | When true, renders all wind ranges, when false, doesn't render the speed range without measurements. |
| wind_direction_unit | string | degrees | - | Wind direction unit, options: `degrees`, `letters`. Where letters being N, NE upto 32 directions. |
| input_speed_unit | string | mps | - | Windspeed unit of measurement, options: `mps`, `kph`, `mph`, `knots`. |
| output_speed_unit | string | bft | - | Windspeed unit used on card, options: `mps`, `kph`, `mph`, `knots`, `bft`. |
| direction_compensation | number | 0 | - | Compensate the measured direction in degrees. |
| cardinal_direction_letters | string | NESW | - | The cardinal letters used in the windrose. |
| wind_direction_count | string | 16 | - | How many wind direction the windrose can display, min. 4 max. 32 |
| direction_speed_time_diff | string | 1 | - | How many seconds a speed measurement time can be earlier or later then the direction measurement time |
| Name | Type | Default | Required | Description |
|----------------------------|:-------:|:---------------:|:--------:|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| type | string | | x | `custom:windrose-card`. |
| title | string | | - | The card title. |
| wind_direction_entity | string | | x | The wind direction entity, having directing in degrees as the state. |
| windspeed_entities | object | | x | One are more windspeed entities. Only the first is used for the windrose. (for now) |
| refresh_interval | number | 300 | - | Refresh interval in seconds |
| hours_to_show | number | 4 | - | Show winddata for the last number of hours. |
| max_width | number | null | - | Use to limit the with (and height) of the windrose. |
| windspeed_bar_location | string | bottom | - | Location of the speed bar graph: `bottom`, `right` |
| windspeed_bar_full | boolean | true | - | When true, renders all wind ranges, when false, doesn't render the speed range without measurements. |
| wind_direction_unit | string | degrees | - | Wind direction unit, options: `degrees`, `letters`. Where letters being N, NE upto 32 directions. |
| input_speed_unit | string | mps | - | Windspeed unit of measurement, options: `mps`, `kph`, `mph`, `knots`. |
| output_speed_unit | string | bft | - | Windspeed unit used on card, options: `mps`, `kph`, `mph`, `knots`, `bft`. |
| direction_compensation | number | 0 | - | Compensate the measured direction in degrees. |
| cardinal_direction_letters | string | NESW | - | The cardinal letters used in the windrose. |
| wind_direction_count | string | 16 | - | How many wind direction the windrose can display, min. 4 max. 32 |
| matching_strategy | string | direction-first | - | How to match direction and speed measurements. Find a speed with each direction or a direction with each speed measurement. Options: `direction-frist`, `speed-first` |
| direction_speed_time_diff | string | 1 | - | How many seconds a speed measurement time can be earlier or later then the direction measurement time. Or the other way around, depending on thie matching_strategy |

#### Object windspeed_entities

Expand Down
1 change: 1 addition & 0 deletions src/CardConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export interface CardConfig {
windspeed_bar_full: boolean;
cardinal_direction_letters: string;
wind_direction_count: number;
matching_strategy: string;
direction_speed_time_diff: number;
}
26 changes: 15 additions & 11 deletions src/CardConfigWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class CardConfigWrapper {
windDirectionUnit: string;
inputSpeedUnit: string;
outputSpeedUnit: string;
matchingStrategy: string;
directionSpeedTimeDiff: number;

entities: string[];
Expand All @@ -29,7 +30,7 @@ export class CardConfigWrapper {
max_width: 400,
refresh_interval: GlobalConfig.defaultRefreshInterval,
windspeed_bar_location: GlobalConfig.defaultWindspeedBarLocation,
windspeed_bar_full: 'true',
windspeed_bar_full: GlobalConfig.defaultWindspeedBarFull,
wind_direction_entity: '',
windspeed_entities: [
{
Expand All @@ -42,6 +43,7 @@ export class CardConfigWrapper {
output_speed_unit: GlobalConfig.defaultOutputSpeedUnit,
direction_compensation: 0,
cardinal_direction_letters: GlobalConfig.defaultCardinalDirectionLetters,
matching_strategy: GlobalConfig.defaultMatchingStategy,
direction_speed_time_diff: GlobalConfig.defaultDirectionSpeedTimeDiff
};
}
Expand All @@ -61,6 +63,7 @@ export class CardConfigWrapper {
this.windDirectionUnit = this.checkWindDirectionUnit();
this.inputSpeedUnit = this.checkInputSpeedUnit();
this.outputSpeedUnit = this.checkOutputSpeedUnit();
this.matchingStrategy = this.checkMatchingStrategy();
this.directionSpeedTimeDiff = this.checkDirectionSpeedTimeDiff();
this.filterEntitiesQueryParameter = this.createEntitiesQueryParameter();
this.entities = this.createEntitiesArray();
Expand Down Expand Up @@ -135,14 +138,6 @@ export class CardConfigWrapper {

private checkWindspeedBarFull(): boolean {
return this.cardConfig.windspeed_bar_full;
// if (this.cardConfig.windspeed_bar_full) {
// // if (this.cardConfig.windspeed_bar_full !== 'true' && this.cardConfig.windspeed_bar_full !== 'false') {
// // throw new Error('Invalid windspeed bar full config ' + this.cardConfig.windspeed_bar_full +
// // '. Valid options: true, false');
// // }
// return this.cardConfig.windspeed_bar_full === 'true';
// }
// return GlobalConfig.defaultWindspeedBarFull;
}

private checkCardinalDirectionLetters(): string {
Expand Down Expand Up @@ -209,6 +204,17 @@ export class CardConfigWrapper {
return GlobalConfig.defaultOutputSpeedUnit;
}

private checkMatchingStrategy(): string {
if (this.cardConfig.matching_strategy) {
if (this.cardConfig.matching_strategy !== 'direction-first' && this.cardConfig.matching_strategy !== 'speed-first') {
throw new Error('Invalid matching stategy ' + this.cardConfig.matching_strategy +
'. Valid options: direction-first, speed-first');
}
return this.cardConfig.matching_strategy;
}
return GlobalConfig.defaultMatchingStategy;
}

private checkDirectionSpeedTimeDiff(): number {
if (this.cardConfig.direction_speed_time_diff) {
if (isNaN(this.cardConfig.direction_speed_time_diff)) {
Expand All @@ -232,6 +238,4 @@ export class CardConfigWrapper {
entities.push(this.windDirectionEntity);
return entities.concat(this.windspeedEntities.map(config => config.entity));
}


}
1 change: 1 addition & 0 deletions src/GlobalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class GlobalConfig {
static defaultInputSpeedUnit = 'mps';
static defaultOutputSpeedUnit = 'bft';
static defaultWindspeedBarFull = true;
static defaultMatchingStategy = 'direction-first';
static defaultDirectionSpeedTimeDiff = 1;

static verticalBarHeight = 30;
Expand Down
43 changes: 42 additions & 1 deletion src/MeasurementMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,48 @@ export class MeasurementMatcher {
private readonly timeDiff: number) {
}

match(): DirectionSpeed[] {
match(matchingStrategy: string) {
if (matchingStrategy == 'speed-first') {
return this.matchSpeedLeading()

} else if (matchingStrategy === 'direction-first') {
return this.matchDirectionLeading();
}
throw Error('Unkown matchfing strategy: ' + matchingStrategy);
}

private matchSpeedLeading(): DirectionSpeed[] {
const matchedData: DirectionSpeed[] = [];
this.speedData.forEach((speed) => {
const direction = this.findMatchingDirection(speed.lu);
if (direction) {
if (direction.s === '' || direction.s === null || isNaN(+direction.s)) {
console.log("Speed " + speed.s + " at timestamp " + speed.lu + " is not a number.");
} else {
matchedData.push(new DirectionSpeed(direction.s, +speed.s));
}
} else {
console.log('No matching direction found for speed ' + speed.s + " at timestamp " + speed.lu);
}
})
return matchedData;
}

private findMatchingDirection(timestamp: number): HistoryData | undefined {
const selection = this.directionData.filter(
(direction) => direction.lu >= timestamp - this.timeDiff && direction.lu <= timestamp + 1);

if (selection.length == 1) {
return selection[0];
} else if (selection.length > 1) {
selection.sort((a, b) => b.lu - a.lu);
return selection[0];
}
return undefined;
}


private matchDirectionLeading(): DirectionSpeed[] {
const matchedData: DirectionSpeed[] = [];
this.directionData.forEach((direction) => {
const speed = this.findMatchingSpeed(direction.lu);
Expand Down
4 changes: 2 additions & 2 deletions src/WindRoseCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {MeasurementMatcher} from "./MeasurementMatcher";

/* eslint no-console: 0 */
console.info(
`%c WINROSE-CARD %c Version 0.4.3 `,
`%c WINROSE-CARD %c Version 0.5.0 `,
'color: orange; font-weight: bold; background: black',
'color: white; font-weight: bold; background: dimgray',
);
Expand Down Expand Up @@ -173,7 +173,7 @@ export class WindRoseCard extends LitElement {
const directionData = history[this.cardConfig.windDirectionEntity];
const firstSpeedData = history[this.cardConfig.windspeedEntities[0].entity];
const directionSpeedData = new MeasurementMatcher(directionData, firstSpeedData,
this.cardConfig.directionSpeedTimeDiff).match();
this.cardConfig.directionSpeedTimeDiff).match(this.cardConfig.matchingStrategy);

this.windRoseCalculator.clear();
for (const directionSpeed of directionSpeedData) {
Expand Down

0 comments on commit 416c133

Please sign in to comment.