This is an example of an implementation in NodeJS of a backend service used to control an appliance such as a washing machine.
First create a database called "appliance" and import the strucure from the file "appliance.sql" found in the root folder of the project.
Modify the ./config/mysql.config.js file according to your MySQL environment, setting the host address, the user and the password.
const parameters={
host: 'localhost',
user: 'root',
password: '',
database: 'appliance'
};
Check if you are running npm in development environment
$ npm config get production
It should return false, if not set it with:
$ npm config set -g production false
Install the dependencies and start the server, it will run on localhost:3000
$ cd Appliance
$ npm install
$ npm start
Run the unit test from another console window
$ npm test
It should pass 43 tests.
For executing the API calls manually, I suggest using Postman
- Reset database to its initial state:
- List of available washing machines models:
- List of available programs for a certain model
- List of valid centrifuge rpm parameters (for all models):
- List of instantiated units:
- Power ON an individual unit:
- Power OFF an individual unit:
- Set state to "running" of an individual unit:
- Set state to "paused" of an individual unit:
- Set state to "waiting" of an individual unit:
- Add a new unit of a certain model:
- POST http://localhost:3000/units/
- Headers ("Content-Type: application/x-www-form-urlencoded")
- Params ("model: idModel") (a valid model id can be seen from previous request (GET http://localhost:3000/models) )
- POST http://localhost:3000/units/
- Set program to an individual unit:
- PATCH http://localhost:3000/units/program/:id
- Headers ("Content-Type: application/x-www-form-urlencoded")
- Params ("program: idProgram") (a valid program id can be seen from previous request (GET http://localhost:3000/models/:modelName) )
- PATCH http://localhost:3000/units/program/:id
- Set timer to an individual unit:
- PATCH http://localhost:3000/units/timer/:id
- Headers ("Content-Type: application/x-www-form-urlencoded")
- Params ("minutes: amountOfMinutes")
- PATCH http://localhost:3000/units/timer/:id
- Toggle "intensive" of an individual unit:
- Delete the instance of an individual unit: