Skip to content

Commit

Permalink
Refactor endpoints to support request bodies (#37)
Browse files Browse the repository at this point in the history
* update openapi to reflect inteded changes

* update api to handle request body

* add timer endpoint

* update FE to new post request bodies

* refactor timerEnabled handling in FE

* tweak loading bar

* move cancel button

* bump FE version
  • Loading branch information
mwood77 authored Feb 26, 2024
1 parent f10942c commit 6592d1b
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 160 deletions.
Binary file modified data/main.js.gz
Binary file not shown.
178 changes: 139 additions & 39 deletions openapi.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.1.0
info:
title: Winderoo API
version: 0.1.0
version: 1.0.0
servers:
- url: http://winderoo.local/api/
description: local development and deployed devices
Expand All @@ -11,58 +11,68 @@ tags:
- name: Modify
description: Modify the state of Winderoo
paths:
/update:
/timer:
post:
tags:
- Modify
description: Change the state of Winderoo
summary: Change the state of Winderoo
parameters:
- in: query
name: tpd
schema:
type: integer
description: how many turns are required
example: 330
- in: query
name: hour
schema:
type: integer
description: At what hour winderoo should begin winding at
example: 14
- in: query
name: minutes
schema:
type: integer
description: At what minute winderoo should begin winding at
example: 50
- in: query
name: timerEnabled
schema:
type: integer
description: Whether Winderoo should enable alarm-start winding; number represents a boolean where 0 == off and 1 == on
example: 0, 1
- in: query
name: action
schema:
type: string
description: Whether Winderoo should start or stop winding
example: START, STOP
- in: query
name: rotationDirection
schema:
type: string
description: The winding direction
example: CW, CCW, BOTH
description: Whether Winderoo should enable alarm-start winding; number represents a boolean where 0 == off and 1 == on.
example: 1
responses:
'204':
description: Successful opeation
'400':
description: Missing required field in request body
content:
text/plain:
schema:
type: string
examples:
- "Missing required field: 'tpd'"
'500':
description: Something went wrong when writing to memory or during deserialization
content:
text/plain:
schema:
type: string
examples:
- Failed to deserialize request body
/update:
post:
tags:
- Modify
summary: Change the state of Winderoo
requestBody:
$ref: '#/components/requestBodies/UpdateBody'
responses:
'204':
description: Successful opeation
'400':
description: Missing required field in request body
content:
text/plain:
schema:
type: string
examples:
- "Missing required field: 'tpd'"
'500':
description: Something went wrong when writing to memory
description: Something went wrong when writing to memory or during deserialization
content:
text/plain:
schema:
type: string
examples:
- Failed to deserialize request body
/status:
get:
tags:
- Status
description: Get the current status of Winderoo
summary: Get the current status of Winderoo
responses:
'200':
description: Service is alive with current winder state
Expand All @@ -74,15 +84,33 @@ paths:
post:
tags:
- Modify
description: Toggle whether Winderoo is on or off (hard off state)
summary: Toggle whether Winderoo is on or off (hard off state)
requestBody:
$ref: '#/components/requestBodies/PowerBody'
responses:
'204':
description: State toggled succesfully
'400':
description: Missing required field in request body
content:
text/plain:
schema:
type: string
examples:
- "Missing required field: 'winderEnabled'"
'500':
description: Something went wrong when writing to memory or during deserialization
content:
text/plain:
schema:
type: string
examples:
- Failed to deserialize request body
/reset:
get:
tags:
- Modify
description: Resets Winderoo's network settings; re-initializes winderoo with setup access point
summary: Resets Winderoo's network settings; re-initializes winderoo with setup access point
responses:
'200':
description: State toggled succesfully
Expand All @@ -91,7 +119,79 @@ paths:
schema:
$ref: '#/components/schemas/Resetting'
components:
requestBodies:
UpdateBody:
description: a JSON object containing winderoo information
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Update'
example:
tpd: "330"
hour: "14"
minutes: "50"
timerEnabled: "0"
action: "START"
rotationDirection: "BOTH"
PowerBody:
description: a JSON object containing winderoo power information
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Power'
example:
winderEnabled: "1"
schemas:
Power:
type: object
propertries:
winderEnabled:
type: string
description:
Whether Winderoo should enable alarm-start winding; number represents a boolean where 0 == off and 1 == on.
examples:
- 0
- 1
Update:
type: object
propertries:
tpd:
type: string
description: how many turns are required
examples:
- 330
hour:
type: string
description: At what hour winderoo should begin winding at
examples:
- 14
minutes:
type: string
description: At what minute winderoo should begin winding at
examples:
- 50
timerEnabled:
type: string
description:
Whether Winderoo should enable alarm-start winding; number represents a boolean where 0 == off and 1 == on.
examples:
- 0
- 1
action:
type: string
description: Whether Winderoo should start or stop winding
examples:
- START
- STOP
rotationDirection:
type: string
description: The winding direction
examples:
- CW
- CCW
- BOTH
Status:
type: object
properties:
Expand Down
4 changes: 2 additions & 2 deletions src/angular/osww-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/angular/osww-frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "osww-frontend",
"version": "0.2.0",
"version": "1.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
53 changes: 18 additions & 35 deletions src/angular/osww-frontend/src/app/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';


export interface Update {
action?: string
rotationDirection?: string,
tpd?: number,
hour?: string;
minutes?: string;
timerEnabled?: number;
action: string
rotationDirection: string,
tpd: number,
hour: string;
minutes: string;
timerEnabled: number;
}

export interface Status {
Expand All @@ -34,8 +34,6 @@ export interface Status {
})
export class ApiService {

DEFUALT_URL = 'http://winderoo.local';

isWinderEnabled$ = new BehaviorSubject(0);
shouldRefresh$ = new BehaviorSubject(false);

Expand All @@ -55,49 +53,34 @@ export class ApiService {

updatePowerState(powerState: boolean) {
let powerStateToNum;
const baseURL = ApiService.constructURL();
const baseURL = ApiService.constructURL() + 'power';

if (powerState) {
powerStateToNum = 1;
} else {
powerStateToNum = 0;
}

const powerBody = {
winderEnabled: powerStateToNum
}

const constructedURL = baseURL
+ "power?"
+ "winderEnabled=" + powerStateToNum;

return this.http.post(constructedURL, null, { observe:'response' });
return this.http.post(baseURL, powerBody, { observe:'response' });
}

updateTimerState(timerState: boolean) {
let timerStateToNum;
updateTimerState(timerState: number) {
const baseURL = ApiService.constructURL();
console.log(timerState)
if (timerState) {
timerStateToNum = 1;
} else {
timerStateToNum = 0;
}

const constructedURL = baseURL
+ "update?"
+ "timerEnabled=" + timerStateToNum;
+ "timer?"
+ "timerEnabled=" + timerState;

return this.http.post(constructedURL, null, { observe: 'response' });
}

updateState(update: Update) {
const baseURL = ApiService.constructURL();

const constructedURL = baseURL
+ 'update?action=' + update.action + '&'
+ 'rotationDirection=' + update.rotationDirection + '&'
+ 'tpd=' + update.tpd + '&'
+ 'hour=' + update.hour + '&'
+ 'minutes=' + update.minutes;

return this.http.post(constructedURL, null, { observe: 'response' });
const baseURL = ApiService.constructURL() + 'update';
return this.http.post(baseURL, update, { observe: 'response' });
}

resetDevice() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ <h4>
</h4>

<div mat-dialog-content>

<button aria-label="Close dialog window" class="pure-css-button" style="background-color: white; color: black" (click)="closeDialog()">
{{ "HEADER.DIALOG.CANCEL" | translate }}
</button>


<ol>
<li>
{{ "HEADER.DIALOG.POINT_1" | translate }}
Expand All @@ -37,6 +32,10 @@ <h4>
</ul>
</div>

<button aria-label="Close dialog window" class="pure-css-button" style="background-color: white; color: black; margin-bottom: 1rem;" (click)="closeDialog()">
{{ "HEADER.DIALOG.CANCEL" | translate }}
</button>

<button mat-stroked-button aria-label="Reset winder" class="pure-css-button" (click)="confirmReset()">
{{ "HEADER.DIALOG.CONFIRM_RESET" | translate }}
</button>
Expand Down
Loading

0 comments on commit 6592d1b

Please sign in to comment.