@niels-vanhove/home-automation-nielsvanhove
v0.8.1
Published
Exercise about home automation
Downloads
40
Readme
Home Automation
Description
In this example we tried to make a thermostat that is in a house. In version v0.1 we made a simple program. In v0.1 we had to give a wanted temperature, temperature range and the current temperature. If the current temperature was lower than the wanted themperature the heater was on. In cas it was higher the cooling was on.
In v0.2 we upgraded the code a little bit, we had to add a JSON Thermostat class. In v0.2 we had to work with JSON objects, we gave a temperature and a range and then the JSON Thermostat class accepted the settings in a JSON format. In v0.2 we also had to work with a update method, if the temperature changed the update method sends the data to the thermostat and changes it.
In v0.3 we did the same as in v0.2 but we added the units. For example if the thermostat was regulated in Celsius and someone bought it in america and changed it in Fahrenheit. The thermostat then needed to change to Fahrenheit.
In v0.4 we had to make a npm package, cleanup our code, using a linter tool that analyzes the source code, bugs and errors.
In v0.5 we had to make 2 extra classes the HttpTemperature sensor only enables you to fetch a temperature value from a given URL. The HttpThermostat class is a class Thermostat that automatically fetches its current temperature value from a given URL.
In v0.6 we have to make documentation by adding extra commentary and pushing it to a website. In v0.7 we had to refactor the code into the principes of SOLID. In v0.8 we had to use testers so that we can check if some methods are correct without running the code in total.
Installation instructions for a project
The first step is to make a directory in your computer
Then you do this command is for using git to keep our program:
git init
The third step is adding a package.json file with this command:
npm init
If you run this command you have to give up some info like name and version. The most important things are: Package name: (name of the package) version: 0.1.0 entry point: dist/index.js On the end the terminal asks if the generated json file is okay?
You can install npm typescript but this is not necessary:
npm install typescript
Make a git ignore file just type .gitignore, this needs to be in this file:
node_modules/
dist/
node_modules it to prevent the json files to be in a directory node_modules.
Make a src directory to put all the files in.
Installation instructions for NPM
- Create an NPM account.
- Then you have to create a package.json file but it is not necessary if you have done it already.
- Then create and publish scoped public packages with this commando's:
npm init --scope=@my-username
npm publish --access public
Multiple usage instructions and examples
The first thing you have to do is give up the current temperature, unit and the range. You can do this by changing this code in the app.ts:
let settingsToJSON = JSON.stringify({temperature: 68.0, range: 1.8, unit:"fahrenheit"}, null, 2)
If you put this in the output will be:
{
"temperature": 68.0,
"range": 1.8,
"unit" : "fahrenheit"
}
If you want to give the new temperature and the new unit just change this in the app.ts:
let updateToJSONtemperature = JSON.stringify({temperature: 293.2, unit:"kelvin"}, null, 2)
If you have changed it the output of that line will be:
{
"temperature": 68.0,
"unit" : "kelvin"
}
If you gave in the current tempearture, range, unit of the thermostat and the new temperature, unit. The thermostat also needs to cool or heat or do nothing this is done in the code you have to change nothing. If you would gave in the settings as shown above the total output will be:
{
"temperature": 68,
"range": 1.8,
"unit": "fahrenheit"
}
{
"temperature": 293.2,
"unit": "kelvin"
}
{
"heating": false,
"cooling": false
}
Lincence
Differences:
- MIT
- Permissions
- Commercial use
- Distribution
- Modification
- Patent use
- Private use
- Conditions
- License and copyright notice
- Limitations
- Liability
- Warranty
- Permissions
- Apache License 2.0
- Permissions
- Commercial use
- Distribution
- Modification
- Patent use
- Private use
- Conditions
- License and copyright notice
- state changes
- Limitations
- Liability
- Trademark use
- Warranty
- Permissions
- GNU GPLv3
- Permissions
- Commercial use
- Distribution
- Modification
- Patent use
- Private use
- Conditions
- Disclose source
- License and copyright notice
- Same license
- State changes
- Limitations
- Liability
- Warranty
- Permissions
For the license i have chosen for the MIT.
Linter
lint, or a linter, is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs. In order to prevent errors or bugs the project must be checked using a Linter.
Create an .eslintrc file.
Create an .eslintignore file in order to prevent ESlint from linting stuff we don't want it to.
- Put this inside it:
node_modules dist
Update your package.js file and add the following scripts:
"scripts": { "lint": "eslint ./src --ext .ts", "prepublish": "npm run lint", }
execute the command:
npm run lint
prepublish script
- The prepublish script will make sure to run the linter before you package and publish your project to npm. This will prevent publishing any mistakes automatically.
Axios
Axios is a tool to create a HTTP reaquest. Here is how i created this.
async getTemperature() {
const res = await axios.get("http://dummy-sensors.azurewebsites.net/api/sensor/abba5")
return res.data.data
}
You see also here that the method is asynchronous. You need to keep it asynchronous, it's not a good practice to make it behave synchronous. To Read the value of the method you have to use the .then otherwise you won't get the value.
.then
Generating the documentation
TypeDoc is a tool to generate the docs. Install it with:
npm install typedoc
Then change in the package.json:
"scripts": {
"docs": "typedoc"
},
And then run this command:
typedoc --out docs src
SOLID
Solid represents five of the most well known principes of object-oriented design:
Single responsibility
Open-closed
Liskov substitution
Interface segregation
Dependency inversion
In our exercise v0.7 we had to use Single Responsibility and Managing Dependencies. Single responsibility concentrates on how to decide what belongs in a class. The other principles have more emphasis on the messages. Well build objects have a single responsibility. To solve complex tasks it is required that they work together. To collaborate, objects must know things about others. Knowing creates dpendencies. If not managed, dependencies will strangle your application.
TDD - Test Driven Development
Test driven Development is a principle of software development that ensure your code wordk exactly as you want it. It makes debugging easier, it works the way you wanted it. There are 3 types of Test driven:
Unit Test
Integration Test
Acceptance/E2E(end to end) Test
Test driven development cycle consist out of 3 steps: test fails, test passes, refactor.
Testing TypeScript with Jest
Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase. Here you can folow the steps how to get Jest installed and how to use it:
Install dependencies
npm install --save-dev @types/jest jest ts-jest
In your package.json add the following scripts to make it easy to use Jest.
"scripts": { "test": "jest", "test:watch": "jest --coverage --watchAll", "coverage": "jest --coverage", },
Create jest.config.js in the root of your project.
module.exports = { transform: {'^.+\\.ts?$': 'ts-jest'}, testEnvironment: 'node', testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] }
Creating a test (example from exercise)
test('From fahrenheit to fahrenheit', () => { const converter = new UnitConverter(222, "fahrenheit") expect(converter.getUnit()).toBe(222); })
Running the Tests.
npm run test
Extra
To get a preview in visual studio code of the readme.md file press ctrl + shift + v.
Link to webiste
Author information
Author: Niels Vanhove E-mail: [email protected] School: VIVES Brugge