Skip to content

Commit

Permalink
- Version to 0.6.0
Browse files Browse the repository at this point in the history
- Added config speed_range_step and speed_range_max to customize the speed ranges. Default depend on output speed unit.
  • Loading branch information
aukedejong committed Mar 3, 2023
1 parent 416c133 commit 11027b5
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 187 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,27 @@ 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 |
| 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 |
| 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`. |
| speed_range_step | number | depends on output speed unit | - | Sets the speed range step to use. Not possible for output speed unit bft (Beaufort) . |
| speed_range_max | number | depends on output speed unit | - | Sets the speed range max to use. Not possible for output speed unit bft (Beaufort). For example: step 5, max 20 creates ranges: 0-5, 5-10, 10-15, 15-20, 20-infinity |
| 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
2 changes: 2 additions & 0 deletions src/CardConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface CardConfig {
wind_direction_unit: string;
input_speed_unit: string;
output_speed_unit: string;
speed_range_step: number;
speed_range_max: number;

direction_compensation: number;
windspeed_bar_location: string;
Expand Down
39 changes: 39 additions & 0 deletions src/CardConfigWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class CardConfigWrapper {
windDirectionUnit: string;
inputSpeedUnit: string;
outputSpeedUnit: string;
speedRangeStep: number | undefined;
speedRangeMax: number | undefined;
matchingStrategy: string;
directionSpeedTimeDiff: number;

Expand All @@ -41,6 +43,8 @@ export class CardConfigWrapper {
wind_direction_unit: GlobalConfig.defaultWindDirectionUnit,
input_speed_unit: GlobalConfig.defaultInputSpeedUnit,
output_speed_unit: GlobalConfig.defaultOutputSpeedUnit,
speed_range_step: undefined,
speed_range_max: undefined,
direction_compensation: 0,
cardinal_direction_letters: GlobalConfig.defaultCardinalDirectionLetters,
matching_strategy: GlobalConfig.defaultMatchingStategy,
Expand All @@ -63,6 +67,9 @@ export class CardConfigWrapper {
this.windDirectionUnit = this.checkWindDirectionUnit();
this.inputSpeedUnit = this.checkInputSpeedUnit();
this.outputSpeedUnit = this.checkOutputSpeedUnit();
this.speedRangeStep = this.checkSpeedRangeStep();
this.speedRangeMax = this.checkSpeedRangeMax();
this.checkSpeedRangeCombi();
this.matchingStrategy = this.checkMatchingStrategy();
this.directionSpeedTimeDiff = this.checkDirectionSpeedTimeDiff();
this.filterEntitiesQueryParameter = this.createEntitiesQueryParameter();
Expand Down Expand Up @@ -204,6 +211,38 @@ export class CardConfigWrapper {
return GlobalConfig.defaultOutputSpeedUnit;
}

private checkSpeedRangeStep(): number | undefined {
if (this.cardConfig.speed_range_step && isNaN(this.cardConfig.speed_range_step)) {
throw new Error('WindRoseCard: Invalid speed_range_step, should be a positive number.');
} else if (this.cardConfig.max_width <= 0) {
throw new Error('WindRoseCard: Invalid speed_range_step, should be a positive number.')
} else if (this.cardConfig.speed_range_step) {
return this.cardConfig.speed_range_step;
}
return undefined;
}

private checkSpeedRangeMax(): number | undefined {
if (this.cardConfig.speed_range_max && isNaN(this.cardConfig.speed_range_max)) {
throw new Error('WindRoseCard: Invalid speed_range_max, should be a positive number.');
} else if (this.cardConfig.max_width <= 0) {
throw new Error('WindRoseCard: Invalid speed_range_max, should be a positive number.')
} else if (this.cardConfig.speed_range_max) {
return this.cardConfig.speed_range_max;
}
return undefined;
}

private checkSpeedRangeCombi(): void {
if (this.outputSpeedUnit === 'bft' && (this.speedRangeStep || this.speedRangeMax)) {
throw new Error("WindRoseCard: speed_range_step and/or speed_range_max should not be set when using output " +
"speed unit Beaufort (bft). Beaufort uses fixed speed ranges.");
}
if ((this.speedRangeStep && !this.speedRangeMax) || (!this.speedRangeStep && this.speedRangeMax)) {
throw new Error("WindRoseCard: speed_range_step and speed_range_max should both be set.")
}
}

private checkMatchingStrategy(): string {
if (this.cardConfig.matching_strategy) {
if (this.cardConfig.matching_strategy !== 'direction-first' && this.cardConfig.matching_strategy !== 'speed-first') {
Expand Down
14 changes: 7 additions & 7 deletions src/WindBarCalculator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {SpeedUnits, WindSpeedConverter} from "./WindSpeedConverter";
import {WindSpeedConverter} from "./WindSpeedConverter";
import {WindBarData} from "./WindBarData";
import {WindBarConfig} from "./WindBarConfig";

export class WindBarCalculator {

readonly windSpeedConverter = new WindSpeedConverter();
readonly windSpeedConverter: WindSpeedConverter;
readonly config: WindBarConfig;
speeds: number[] = [];
modified = false;
Expand All @@ -15,12 +15,12 @@ export class WindBarCalculator {
speedConverterFunction: (speed: number) => number;
rangeCount: number;

constructor(config: WindBarConfig) {
constructor(config: WindBarConfig, windSpeedConverter: WindSpeedConverter) {
this.config = config;
this.speedRangeFunction = this.windSpeedConverter.getRangeFunction(this.config.outputUnit);
this.speedConverterFunction = this.windSpeedConverter.getSpeedConverter(this.config.inputUnit,
this.config.outputUnit);
this.rangeCount = SpeedUnits.getSpeedUnit(this.config.outputUnit).speedRanges.length;
this.windSpeedConverter = windSpeedConverter;
this.speedRangeFunction = this.windSpeedConverter.getRangeFunction()
this.speedConverterFunction = this.windSpeedConverter.getSpeedConverter();
this.rangeCount = this.windSpeedConverter.getRangeCount();
}

addSpeeds(speeds: number[]) {
Expand Down
16 changes: 9 additions & 7 deletions src/WindBarCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import {ColorUtil} from "./ColorUtil";
import {WindBarData} from "./WindBarData";
import {WindBarConfig} from "./WindBarConfig";
import {GlobalConfig} from "./GlobalConfig";
import {SpeedRange, SpeedUnit, SpeedUnits} from "./WindSpeedConverter";
import {SpeedRange, WindSpeedConverter} from "./WindSpeedConverter";

export class WindBarCanvas {

readonly colorUtil: ColorUtil;
readonly config: WindBarConfig;
readonly speedUnit: SpeedUnit;
readonly windSpeedConverter: WindSpeedConverter;
readonly outputUnitName: string;
readonly speedRanges: SpeedRange[];

constructor(config: WindBarConfig) {
constructor(config: WindBarConfig, windSpeedConverter: WindSpeedConverter) {
this.config = config;
this.speedUnit = SpeedUnits.getSpeedUnit(this.config.outputUnit)
this.speedRanges = this.speedUnit.speedRanges;
this.windSpeedConverter = windSpeedConverter;
this.outputUnitName = this.windSpeedConverter.getOutputSpeedUnit().name;
this.speedRanges = this.windSpeedConverter.getOutputSpeedUnit().speedRanges;
this.colorUtil = new ColorUtil(this.speedRanges.length);
}

Expand Down Expand Up @@ -124,7 +126,7 @@ export class WindBarCanvas {
canvasContext.textAlign = 'center';
canvasContext.textBaseline = 'bottom';
canvasContext.fillStyle = GlobalConfig.getTextColor();
canvasContext.fillText(this.speedUnit.name, this.config.posX + (this.config.height / 2), this.config.posY - this.config.length - 2);
canvasContext.fillText(this.outputUnitName, this.config.posX + (this.config.height / 2), this.config.posY - this.config.length - 2);
canvasContext.fill();
}

Expand Down Expand Up @@ -187,7 +189,7 @@ export class WindBarCanvas {
canvasContext.textAlign = 'right';
canvasContext.textBaseline = 'bottom';
canvasContext.fillStyle = GlobalConfig.getTextColor();
canvasContext.fillText(this.speedUnit.name, this.config.posX + this.config.length, this.config.posY);
canvasContext.fillText(this.outputUnitName, this.config.posX + this.config.length, this.config.posY);
canvasContext.fill();
}

Expand Down
11 changes: 6 additions & 5 deletions src/WindDirectionCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {WindDirectionData} from "./WindDirectionData";
import {WindSpeedConverter} from "./WindSpeedConverter";

export class WindDirectionCalculator {
readonly windSpeedConverter = new WindSpeedConverter();
readonly windSpeedConverter: WindSpeedConverter;
data = new WindDirectionData();
speeds: number[] = [];
speedRangeCounts: number[] = [];
Expand All @@ -12,12 +12,13 @@ export class WindDirectionCalculator {
speedRangeFunction: (speed: number) => number;
speedConverterFunction: (speed: number) => number;

constructor(minDegrees: number, centerDegrees: number, maxDegrees: number, config: WindRoseConfig) {
constructor(minDegrees: number, centerDegrees: number, maxDegrees: number, config: WindRoseConfig,
windSpeedConverter: WindSpeedConverter) {
this.data.centerDegrees = centerDegrees;
this.config = config;
this.speedRangeFunction = this.windSpeedConverter.getRangeFunction(this.config.outputUnit);
this.speedConverterFunction = this.windSpeedConverter.getSpeedConverter(this.config.inputUnit,
this.config.outputUnit);
this.windSpeedConverter = windSpeedConverter;
this.speedRangeFunction = this.windSpeedConverter.getRangeFunction();
this.speedConverterFunction = this.windSpeedConverter.getSpeedConverter();
if (minDegrees < 0) {
this.data.minDegrees = minDegrees + 360;
} else {
Expand Down
Loading

0 comments on commit 11027b5

Please sign in to comment.