From 17e604a03fcc30c7af0255f56cd7b64e0839f4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Z=C3=ADpek?= Date: Thu, 6 Jun 2024 14:34:20 +0200 Subject: [PATCH] Refactoring --- firmware/dow.cpp | 2 + server/src/app/routes/dow.ts | 106 +++++------------- server/src/lib/dow/constants.ts | 38 +++++++ server/src/lib/dow/defineWidget.ts | 7 ++ server/src/lib/dow/primitives/line.ts | 19 ++++ server/src/lib/dow/primitives/rectangle.ts | 22 ++++ server/src/lib/dow/primitives/text.ts | 25 +++++ .../src/lib/dow/widgets/rectangleWithTitle.ts | 40 +++++++ 8 files changed, 180 insertions(+), 79 deletions(-) create mode 100644 server/src/lib/dow/constants.ts create mode 100644 server/src/lib/dow/defineWidget.ts create mode 100644 server/src/lib/dow/primitives/line.ts create mode 100644 server/src/lib/dow/primitives/rectangle.ts create mode 100644 server/src/lib/dow/primitives/text.ts create mode 100644 server/src/lib/dow/widgets/rectangleWithTitle.ts diff --git a/firmware/dow.cpp b/firmware/dow.cpp index 33d4b5d..dd33c61 100644 --- a/firmware/dow.cpp +++ b/firmware/dow.cpp @@ -50,12 +50,14 @@ void dow_get_text_item_rect(GxEPD2_BW { router.get( '/', @@ -91,7 +67,7 @@ export const dowRoutes = router((router, ctx) => { summary = summary.replace(/_day|_night/, '') } - const widgets: Widget[] = [ + const widgets: DowWidget[] = [ { x: WIDTH / 2, y: 30, @@ -220,31 +196,17 @@ export const dowRoutes = router((router, ctx) => { const tempGraphP = 10 const tempGraphW = tempW - tempGraphP * 2 - widgets.push({ - x: tempX, - y: tempY - 40, - x2: tempW, - y2: tempH, - t: TYPES.RECT, - }) - - widgets.push({ - x: tempX, - y: tempY - 20, - x2: tempX + tempW, - y2: tempY - 20, - t: TYPES.LINE, - }) - - widgets.push({ - x: Math.floor(tempX + tempW / 2), - y: tempY - 30, - t: TYPES.TEXT, - ha: ALIGN.CENTER, - va: ALIGN.CENTER, - c: 'Teplota', - f: 3, - }) + widgets.push( + ...rectangleWithTitle({ + x: tempX, + y: tempY - 40, + width: tempW, + height: tempH, + title: 'Teplota', + titleHeight: 20, + titleFont: 3, + }), + ) for (const [offset, hour] of next12Hours) { const offsetInt = +offset @@ -312,31 +274,17 @@ export const dowRoutes = router((router, ctx) => { const preGraphP = 10 const preGraphW = preW - preGraphP * 2 - widgets.push({ - x: preX, - y: preY - 40, - x2: preW, - y2: preH, - t: TYPES.RECT, - }) - - widgets.push({ - x: preX, - y: preY - 20, - x2: preX + preW, - y2: preY - 20, - t: TYPES.LINE, - }) - - widgets.push({ - x: Math.floor(preX + preW / 2), - y: preY - 30, - t: TYPES.TEXT, - ha: ALIGN.CENTER, - va: ALIGN.CENTER, - c: 'Dest', - f: 3, - }) + widgets.push( + ...rectangleWithTitle({ + x: preX, + y: preY - 40, + width: preW, + height: preH, + title: 'Srazky', + titleHeight: 20, + titleFont: 3, + }), + ) for (const [offset, hour] of next12Hours) { const offsetInt = +offset diff --git a/server/src/lib/dow/constants.ts b/server/src/lib/dow/constants.ts new file mode 100644 index 0000000..ac813db --- /dev/null +++ b/server/src/lib/dow/constants.ts @@ -0,0 +1,38 @@ +export type DowWidget = { + x: number + y: number + x2?: number + y2?: number + c?: string + t: DowWidgetType + va?: DowWidgetAlign + ha?: DowWidgetAlign + f?: number + co?: DowWidgetColor +} + +export type DowWidgetType = (typeof TYPES)[keyof typeof TYPES] + +export const TYPES = { + TEXT: 0, + LINE: 1, + CIRCLE: 2, + CIRCLE_FILL: 3, + RECT: 4, + RECT_FILL: 5, +} as const + +export type DowWidgetAlign = (typeof ALIGN)[keyof typeof ALIGN] + +export const ALIGN = { + START: 0, + CENTER: 1, + END: 2, +} as const + +export type DowWidgetColor = (typeof COLORS)[keyof typeof COLORS] + +export const COLORS = { + BLACK: 0, + WHITE: 1, +} diff --git a/server/src/lib/dow/defineWidget.ts b/server/src/lib/dow/defineWidget.ts new file mode 100644 index 0000000..cc83eaa --- /dev/null +++ b/server/src/lib/dow/defineWidget.ts @@ -0,0 +1,7 @@ +import { DowWidget } from './constants' + +export const defineWidget = ( + factory: (options: TOptions) => TResult, +) => { + return factory +} diff --git a/server/src/lib/dow/primitives/line.ts b/server/src/lib/dow/primitives/line.ts new file mode 100644 index 0000000..a429d45 --- /dev/null +++ b/server/src/lib/dow/primitives/line.ts @@ -0,0 +1,19 @@ +import { DowWidgetColor, TYPES } from '../constants' +import { defineWidget } from '../defineWidget' + +type Options = { + x: number + y: number + x2: number + y2: number + color?: DowWidgetColor +} + +export const line = defineWidget(({ x, y, x2, y2, color }: Options) => ({ + x, + y, + x2, + y2, + t: TYPES.LINE, + co: color, +})) diff --git a/server/src/lib/dow/primitives/rectangle.ts b/server/src/lib/dow/primitives/rectangle.ts new file mode 100644 index 0000000..6766e27 --- /dev/null +++ b/server/src/lib/dow/primitives/rectangle.ts @@ -0,0 +1,22 @@ +import { DowWidgetColor, TYPES } from '../constants' +import { defineWidget } from '../defineWidget' + +type Options = { + x: number + y: number + width: number + height: number + filled?: boolean + color?: DowWidgetColor +} + +export const rectangle = defineWidget( + ({ x, y, width, height, filled, color }: Options) => ({ + x, + y, + x2: width, + y2: height, + t: filled ? TYPES.RECT_FILL : TYPES.RECT, + co: color, + }), +) diff --git a/server/src/lib/dow/primitives/text.ts b/server/src/lib/dow/primitives/text.ts new file mode 100644 index 0000000..213477c --- /dev/null +++ b/server/src/lib/dow/primitives/text.ts @@ -0,0 +1,25 @@ +import { DowWidgetAlign, DowWidgetColor, TYPES } from '../constants' +import { defineWidget } from '../defineWidget' + +type Options = { + x: number + y: number + text: string + font: number + verticalAlign?: DowWidgetAlign + horizontalAlign?: DowWidgetAlign + color?: DowWidgetColor +} + +export const text = defineWidget( + ({ x, y, text, font, verticalAlign, horizontalAlign, color }: Options) => ({ + x, + y, + t: TYPES.TEXT, + c: text, + va: verticalAlign, + ha: horizontalAlign, + f: font, + co: color, + }), +) diff --git a/server/src/lib/dow/widgets/rectangleWithTitle.ts b/server/src/lib/dow/widgets/rectangleWithTitle.ts new file mode 100644 index 0000000..9d64825 --- /dev/null +++ b/server/src/lib/dow/widgets/rectangleWithTitle.ts @@ -0,0 +1,40 @@ +import { ALIGN, DowWidget } from '../constants' +import { defineWidget } from '../defineWidget' +import { line } from '../primitives/line' +import { rectangle } from '../primitives/rectangle' +import { text } from '../primitives/text' + +type Options = { + x: number + y: number + width: number + height: number + title: string + titleHeight: number + titleFont: number +} + +export const rectangleWithTitle = defineWidget( + ({ x, y, width, height, title, titleFont, titleHeight }: Options) => { + const result = [] as DowWidget[] + + result.push(rectangle({ x, y, width, height })) + + result.push( + line({ x, y: y + titleHeight, x2: x + width, y2: y + titleHeight }), + ) + + result.push( + text({ + x: Math.floor(x + width / 2), + y: y + titleHeight / 2, + horizontalAlign: ALIGN.CENTER, + verticalAlign: ALIGN.CENTER, + text: title, + font: titleFont, + }), + ) + + return result + }, +)