Skip to content

Commit

Permalink
Added wind_direction_unit configuration option.
Browse files Browse the repository at this point in the history
  • Loading branch information
aukedejong committed Jan 24, 2023
1 parent b220684 commit 5720c3e
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 20 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ 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) |
| 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` |
| 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 |
| 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` |
| wind_direction_unit | string | degrees | - | Wind direction unit, options: `degrees`, `letters`. Where letters being N, NE upto 32 directions. |
| 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 |

#### 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 @@ -7,6 +7,7 @@ export interface CardConfig {
max_width: number;
wind_direction_entity: string;
windspeed_entities: {entity: string, name: string}[];
wind_direction_unit: string;

direction_compensation: number;
windspeed_bar_location: string;
Expand Down
17 changes: 16 additions & 1 deletion src/CardConfigWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class CardConfigWrapper {
windspeedBarLocation: string;
cardinalDirectionLetters: string;
windDirectionCount: number;
windDirectionUnit: string;

entities: string[];
filterEntitiesQueryParameter: string;
Expand All @@ -31,6 +32,7 @@ export class CardConfigWrapper {
name: ''
}
],
wind_direction_unit: GlobalConfig.defaultWindDirectionUnit,
direction_compensation: 0,
cardinal_direction_letters: GlobalConfig.defaultCardinalDirectionLetters
};
Expand All @@ -47,6 +49,7 @@ export class CardConfigWrapper {
this.windspeedBarLocation = this.checkWindspeedBarLocation();
this.cardinalDirectionLetters = this.checkCardinalDirectionLetters();
this.windDirectionCount = this.checkWindDirectionCount();
this.windDirectionUnit = this.checkWindDirectionUnit();
this.filterEntitiesQueryParameter = this.createEntitiesQueryParameter();
this.entities = this.createEntitiesArray();
}
Expand Down Expand Up @@ -111,7 +114,7 @@ export class CardConfigWrapper {
if (this.cardConfig.windspeed_bar_location) {

if (this.cardConfig.windspeed_bar_location !== 'bottom' && this.cardConfig.windspeed_bar_location !== 'right') {
throw new Error('WindRoseCard: Invalid windspeed bar location ' + this.cardConfig.windspeed_bar_location +
throw new Error('Invalid windspeed bar location ' + this.cardConfig.windspeed_bar_location +
'. Valid options: bottom, right');
}
return this.cardConfig.windspeed_bar_location;
Expand Down Expand Up @@ -140,6 +143,18 @@ export class CardConfigWrapper {
return GlobalConfig.defaultWindDirectionCount;
}

private checkWindDirectionUnit(): string {
if (this.cardConfig.wind_direction_unit) {
if (this.cardConfig.wind_direction_unit !== 'degrees'
&& this.cardConfig.wind_direction_unit !== 'letters') {
throw new Error('Invalid wind direction unit configured: ' + this.cardConfig.wind_direction_unit +
'. Valid options: degrees, letters');
}
return this.cardConfig.wind_direction_unit;
}
return GlobalConfig.defaultWindDirectionUnit;
}

private createEntitiesQueryParameter() {
return this.windDirectionEntity + ',' + this.windspeedEntities
.map(config => config.entity)
Expand Down
1 change: 1 addition & 0 deletions src/GlobalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class GlobalConfig {
static defaultHoursToShow = 4;
static defaultRefreshInterval = 300;
static defaultWindDirectionCount = 16;
static defaultWindDirectionUnit = 'degrees';

static verticalBarHeight = 16;
static horizontalBarHeight = 15;
Expand Down
47 changes: 47 additions & 0 deletions src/WindDirectionConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

export class WindDirectionConverter {

directions: Record<string, number>;

constructor() {
this.directions = {
N: 0,
NXE: 11.25,
NNE: 22.5,
NEXN: 33.75,
NE: 45,
NEXE: 56.25,
ENE: 67.5,
EXN: 78.75,
E: 90,
EXS: 101.25,
ESE: 112.50,
SEXE: 123.75,
SE: 135,
SEXS: 146.25,
SSE: 157.50,
SXE: 168.75,
S: 180,
SXW: 191.25,
SSW: 202.5,
SWXS: 213.75,
SW: 225,
SWxW: 236.25,
WSW: 247.5,
WXS: 258.75,
W: 270,
WXN: 281.25,
WNW: 292.50,
NWXW: 303.75,
NW: 315,
NWXN: 326.25,
NNW: 337.5,
NXW: 348.5
};
}

getDirection(designation: string): number {
return this.directions[designation.toUpperCase()];
}
}

27 changes: 20 additions & 7 deletions src/WindRoseCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import {WindRoseConfig} from "./WindRoseConfig";
import {WindDirectionCalculator} from "./WindDirectionCalculator";
import {WindRoseData} from "./WindRoseData";
import {WindSpeedConverter} from "./WindSpeedConverter";
import {WindDirectionConverter} from "./WindDirectionConverter";

export class WindRoseCalculator {

readonly windSpeedConverter = new WindSpeedConverter();
readonly windDirectionConverter = new WindDirectionConverter();

data = new WindRoseData();
windDirections: WindDirectionCalculator[] = [];
config: WindRoseConfig;
Expand Down Expand Up @@ -35,17 +39,26 @@ export class WindRoseCalculator {
}
}

addDataPoint(direction: number, speed: number): void {
addDataPoint(direction: number | string, speed: number): void {
let degrees = 0;
if (this.config.windDirectionUnit === 'letters') {
degrees = this.windDirectionConverter.getDirection(direction as string);
if (isNaN(degrees)) {
throw new Error("Could not convert direction " + direction + " to degrees.");
}
} else {
degrees = direction as number;
}
if (this.config.directionCompensation !== 0) {
direction = +direction + this.config.directionCompensation;
if (direction < 0) {
direction = 360 + direction;
} else if (direction >= 360) {
direction = direction - 360;
degrees = +degrees + this.config.directionCompensation;
if (degrees < 0) {
degrees = 360 + degrees;
} else if (degrees >= 360) {
degrees = degrees - 360;
}
}
for(const windDirection of this.windDirections) {
if (windDirection.checkDirection(direction)) {
if (windDirection.checkDirection(degrees)) {
windDirection.addSpeed(speed);
this.totalMeasurements++;
}
Expand Down
1 change: 1 addition & 0 deletions src/WindRoseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class WindRoseConfig {
public centerX: number,
public centerY: number,
public windDirectionCount: number,
public windDirectionUnit: string,
public leaveArc: number,
public cardinalDirectionLetters: string,
public directionCompensation: number) {
Expand Down
1 change: 1 addition & 0 deletions src/WindRoseConfigFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class WindRoseConfigFactory {
this.roseCenterX,
this.roseCenterY,
this.cardConfig.windDirectionCount,
this.cardConfig.windDirectionUnit,
(360 / this.cardConfig.windDirectionCount) - 5,
this.cardConfig.cardinalDirectionLetters,
this.cardConfig.directionCompensation);
Expand Down

0 comments on commit 5720c3e

Please sign in to comment.