Skip to content

Commit

Permalink
Fix response validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aenima4six2 committed Nov 29, 2024
1 parent bb5e81c commit ad28585
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
21 changes: 14 additions & 7 deletions src/gmg-client/GMGClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ class GMGClient {
throw error
}

const command = commands.setGrillTempF(fahrenheit)
const tempF = Number(fahrenheit);
const command = commands.setGrillTempF(tempF)
const result = await this.sendCommand(command)
await this._validateResult(result, newState => newState.desiredGrillTemp === fahrenheit)
await this._validateResult(result, newState => newState.desiredGrillTemp === tempF)
}

async setFoodTemp(fahrenheit) {
Expand All @@ -92,9 +93,10 @@ class GMGClient {
throw error
}

const command = commands.setFoodTempF(fahrenheit)
const tempF = Number(fahrenheit);
const command = commands.setFoodTempF(tempF)
const result = await this.sendCommand(command)
await this._validateResult(result, newState => newState.desiredFoodTemp === fahrenheit)
await this._validateResult(result, newState => newState.desiredFoodTemp === tempF)
}

async discoverGrill({ tries = this.tries } = {}) {
Expand Down Expand Up @@ -249,10 +251,15 @@ class GMGClient {
}

async _validateResult(result, validator) {
// If grill responds with OK, there's nothing else to validate
const response = result.msg.toString()
if (response !== results.OK) {
throw new Error(`Grill responded with non OK status -> ${response}`)
}
if (response === results.OK) return;

// Validate the returned grill state
const newState = new GrillStatus(result.msg)
if (validator(newState)) return

throw new Error(`Grill responded with invalid status -> ${response}`)
}

}
Expand Down
18 changes: 15 additions & 3 deletions src/gmg-server/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach",
"port": 9229,
"request": "attach",
"skipFiles": [
"${workspaceRoot}/node_modules/**/*.js",
"${workspaceRoot}/lib/**/*.js",
"<node_internals>/**"
],
"type": "node"
},

{
"type": "node",
"request": "launch",
Expand All @@ -14,13 +26,13 @@
"skipFiles": [
"${workspaceRoot}/node_modules/**/*.js",
"${workspaceRoot}/lib/**/*.js",
"<node_internals>/**/*.js"
"<node_internals>/**"
],
"env": {
"PORT": "3001",
"PORT": "3000",
"DEBUG": "src:*",
"NODE_ENV": "debug"
},
}
},
]
}

0 comments on commit ad28585

Please sign in to comment.