Skip to content

Commit

Permalink
moved to seperate pr
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewjeska committed Aug 23, 2023
1 parent 080b9f7 commit 28479e3
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
140 changes: 140 additions & 0 deletions src/lib/util/thermostats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { ClimateSetting } from "lib/zod/climate_setting.ts"

Check failure on line 1 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

All imports in the declaration are only used as types. Use `import type`

Check failure on line 1 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

All imports in the declaration are only used as types. Use `import type`

export const normalizeClimateSettingMode = (cs: Partial<ClimateSetting>) => {

Check failure on line 3 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Missing return type on function

Check failure on line 3 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Missing return type on function

Check failure on line 3 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Missing return type on function
return {
automatic_heating_enabled:
cs.automatic_heating_enabled ??
deriveAutomaticHeatingEnabledFromHvacModeSetting(cs.hvac_mode_setting),
automatic_cooling_enabled:
cs.automatic_cooling_enabled ??
deriveAutomaticCoolingEnabledFromHvacModeSetting(cs.hvac_mode_setting),
hvac_mode_setting:
cs.hvac_mode_setting ??
deriveHvacModeSettingFromFlags({
automatic_cooling_enabled: cs.automatic_cooling_enabled!,

Check failure on line 14 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Forbidden non-null assertion

Check failure on line 14 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Forbidden non-null assertion

Check failure on line 14 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Forbidden non-null assertion
automatic_heating_enabled: cs.automatic_heating_enabled!,

Check failure on line 15 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Forbidden non-null assertion

Check failure on line 15 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Forbidden non-null assertion

Check failure on line 15 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Forbidden non-null assertion
}),
}
}

/**
* @param {ClimateSetting} cs
* @returns {ClimateSetting}
* @description
* This function takes a Partial<ClimateSetting> and returns a new ClimateSetting with all fields filled in
*
*/
export const normalizeClimateSetting = (
cs: Partial<ClimateSetting>
): ClimateSetting => {
if (cs.manual_override_allowed === undefined)
throw new Error("manual_override_allowed is required")

let normalized_climate_setting: ClimateSetting = {
...normalizeClimateSettingMode(cs),
manual_override_allowed: cs.manual_override_allowed,
}

// if the climate setting calls for cooling set points, we set them
if (normalized_climate_setting.automatic_cooling_enabled) {
normalized_climate_setting = {
...normalized_climate_setting,
...normalizeCoolingSetPoints(cs),
}
}

// likewise for heating
if (normalized_climate_setting.automatic_heating_enabled) {
normalized_climate_setting = {
...normalized_climate_setting,
...normalizeHeatingSetPoints(cs),
}
}

return normalized_climate_setting
}

export const deriveAutomaticHeatingEnabledFromHvacModeSetting = (

Check failure on line 57 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Missing return type on function

Check failure on line 57 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Missing return type on function

Check failure on line 57 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Missing return type on function
hvac_mode_setting?: ClimateSetting["hvac_mode_setting"]
) => hvac_mode_setting === "heat" || hvac_mode_setting === "heatcool"

export const deriveAutomaticCoolingEnabledFromHvacModeSetting = (

Check failure on line 61 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Missing return type on function

Check failure on line 61 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Missing return type on function

Check failure on line 61 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Missing return type on function
hvac_mode_setting?: ClimateSetting["hvac_mode_setting"]
) => hvac_mode_setting === "cool" || hvac_mode_setting === "heatcool"

export const deriveHvacModeSettingFromFlags = ({
automatic_cooling_enabled,
automatic_heating_enabled,
}: {
automatic_cooling_enabled: boolean
automatic_heating_enabled: boolean
}): ClimateSetting["hvac_mode_setting"] => {
if (automatic_cooling_enabled && automatic_heating_enabled) return "heatcool"
if (automatic_cooling_enabled) return "cool"
if (automatic_heating_enabled) return "heat"
return "off"
}

/**
*
* @param cs
* @returns {Partial<ClimateSetting>}
* This will look at what cooling set point is on the climate setting, and set the other temperature scale if neccessary.
* For example, if a request comes in with a cooling_set_point_ceslius, we will set the cooling_set_point_fahrenheit as well.
*
*/
const normalizeCoolingSetPoints = (cs: Partial<ClimateSetting>) => {

Check failure on line 86 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Missing return type on function

Check failure on line 86 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Missing return type on function

Check failure on line 86 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Missing return type on function
const setPoints: Partial<ClimateSetting> = {}

if (cs.cooling_set_point_celsius !== undefined) {
setPoints.cooling_set_point_celsius = cs.cooling_set_point_celsius
} else if (cs.cooling_set_point_fahrenheit !== undefined) {
setPoints.cooling_set_point_celsius = convertToCelsius(
cs.cooling_set_point_fahrenheit
)
}

if (cs.cooling_set_point_fahrenheit !== undefined) {
setPoints.cooling_set_point_fahrenheit = cs.cooling_set_point_fahrenheit
} else if (cs.cooling_set_point_celsius !== undefined) {
setPoints.cooling_set_point_fahrenheit = convertToFahrenheit(
cs.cooling_set_point_celsius
)
}
return setPoints
}

/**
*
* @param cs
* @returns {Partial<ClimateSetting>}
* This will look at what heating set point is on the climate setting, and set the other temperature scale.
* For example, if a request comes in with a heating_set_point_ceslius, we will set the heating_set_point_fahrenheit as well.
*
*/
const normalizeHeatingSetPoints = (cs: Partial<ClimateSetting>) => {

Check failure on line 115 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Missing return type on function

Check failure on line 115 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Missing return type on function

Check failure on line 115 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Missing return type on function
const setPoints: Partial<ClimateSetting> = {}

if (cs.heating_set_point_celsius !== undefined) {
setPoints.heating_set_point_celsius = cs.heating_set_point_celsius
} else if (cs.heating_set_point_fahrenheit !== undefined) {
setPoints.heating_set_point_celsius = convertToCelsius(
cs.heating_set_point_fahrenheit
)
}

if (cs.heating_set_point_fahrenheit !== undefined) {
setPoints.heating_set_point_fahrenheit = cs.heating_set_point_fahrenheit
} else if (cs.heating_set_point_celsius !== undefined) {
setPoints.heating_set_point_fahrenheit = convertToFahrenheit(
cs.heating_set_point_celsius
)
}

return setPoints
}

export const convertToFahrenheit = (celsius: number) => (celsius * 9) / 5 + 32

Check failure on line 137 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Missing return type on function

Check failure on line 137 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Missing return type on function

Check failure on line 137 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Missing return type on function

export const convertToCelsius = (fahrenheit: number) =>

Check failure on line 139 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Format code

Missing return type on function

Check failure on line 139 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v16)

Missing return type on function

Check failure on line 139 in src/lib/util/thermostats.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Missing return type on function
(fahrenheit - 32) * (5 / 9)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { withRouteSpec } from "lib/middleware/with-route-spec.ts"
import { climate_setting } from "lib/zod/climate_setting.ts"
import { climate_setting_schedule } from "lib/zod/climate_setting_schedule.ts"
import { timestamp } from "lib/zod/common.ts"
import { normalizeClimateSetting } from "lib/util/thermostats.ts"

const jsonBody = z
.object({
Expand Down Expand Up @@ -53,7 +54,7 @@ export default withRouteSpec({
name: name ?? `Schedule ${randomUUID().slice(5)}`,
schedule_starts_at: new Date(schedule_starts_at).toISOString(),
schedule_ends_at: new Date(schedule_ends_at).toISOString(),
...climate_setting_for_schedule,
...normalizeClimateSetting(climate_setting_for_schedule),
})

res.status(200).json({
Expand Down

0 comments on commit 28479e3

Please sign in to comment.