-
Notifications
You must be signed in to change notification settings - Fork 5
Added background functionality #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
78975e4
b2908bd
17c7f49
6895bfe
ce71810
a6e9f74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| let Wappsto = require("wapp-api"); | ||
|
|
||
| let wappsto = new Wappsto(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use const |
||
|
|
||
| let wappstoConsole = require("wapp-api/console"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use const or require("wapp-api/console").start() |
||
| wappstoConsole.start(); | ||
|
|
||
| let device, sensors, led, data; | ||
|
|
||
| wappsto | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could try to use async/await |
||
| .get( | ||
| "network", | ||
| { | ||
| name: "Smart Home" | ||
| }, | ||
| { | ||
| quantity: 1, | ||
| expand: 5, | ||
| subscribe: true | ||
| } | ||
| ) | ||
| .then(function(collection) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could rewrite to the following form: |
||
| device = collection.get("0.device"); | ||
| sensors = device.find({ name: "Sensors" }).get("value"); | ||
| led = device.find({ name: "LED" }).get("value"); | ||
|
|
||
| initSensorListerners(); | ||
| initButtonListener(); | ||
| initBrightnessListener(); | ||
| }) | ||
| .catch(console.error); | ||
|
|
||
| wappsto | ||
| .get( | ||
| "data", | ||
| {}, | ||
| { | ||
| expand: 5, | ||
| subscribe: true | ||
| } | ||
| ) | ||
| .then(function(collection) { | ||
| data = collection.first(); | ||
|
|
||
| data.on("change", function() { | ||
| updateSensorData(data.get("sensorToUpdate")); | ||
| }); | ||
| }) | ||
| .catch(console.error); | ||
|
|
||
| function initSensorListerners() { | ||
| if (sensors) { | ||
| sensors.each(sensorValue => { | ||
| let sensorState = sensorValue.get("state").find({ type: "Report" }); | ||
|
|
||
| sensorState.on("change:data", function() { | ||
| let currentlySelectedSensor = data.get("sensorToUpdate"); | ||
| let sensorName = sensorValue.get("name"); | ||
|
|
||
| if (currentlySelectedSensor === sensorName) { | ||
| updateSensorData(currentlySelectedSensor); | ||
| } else if (currentlySelectedSensor === "temperature-CO2") { | ||
| updateSensorData(currentlySelectedSensor); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| function updateSensorData(sensorValueToBeUpdated) { | ||
| let panel = led | ||
| .find({ name: "LED Panel" }) | ||
| .get("state") | ||
| .find({ type: "Control" }); | ||
|
|
||
| if (sensors) { | ||
| if (sensorValueToBeUpdated === "temperature-CO2") { | ||
| let temp_value = sensors.find({ name: "Temperature" }); | ||
| let temp_data = temp_value | ||
| .get("state") | ||
| .find({ type: "Report" }) | ||
| .get("data"); | ||
|
|
||
| let CO2_value = sensors.find({ name: "CO2" }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. either you use const or change it to a small letter |
||
| let CO2_data = CO2_value.get("state") | ||
| .find({ type: "Report" }) | ||
| .get("data"); | ||
|
|
||
| panel.save( | ||
| { | ||
| data: | ||
| +Number(temp_data).toFixed(2) + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the end it will be converted to a string anyway since you're concatenating. You could maybe use ${...} (string interpolation). |
||
| " " + | ||
| temp_value.get("number.unit") + | ||
| " " + | ||
| +Number(CO2_data).toFixed(2) + | ||
| " " + | ||
| CO2_value.get("number.unit") | ||
| }, | ||
| { patch: true } | ||
| ); | ||
| } else { | ||
| let foundSensorValue = sensors.find({ name: sensorValueToBeUpdated }); | ||
|
|
||
| if (foundSensorValue) { | ||
| let data = foundSensorValue | ||
| .get("state") | ||
| .find({ type: "Report" }) | ||
| .get("data"); | ||
|
|
||
| panel.save({ | ||
| data: | ||
| +Number(data).toFixed(2) + | ||
| " " + | ||
| foundSensorValue.get("number.unit") + | ||
| " " | ||
| }); | ||
| } else { | ||
| console.log("Sensor could not be found"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function initButtonListener() { | ||
| if (led) { | ||
| let button = led | ||
| .find({ name: "Button" }) | ||
| .get("state") | ||
| .find({ type: "Report" }); | ||
| let brightness = led | ||
| .find({ name: "Brightness" }) | ||
| .get("state") | ||
| .find({ type: "Control" }); | ||
|
|
||
| button.on("change:data", function(model) { | ||
| let isButtonOn = model.get("data"); | ||
| let userBrightnessOption = data.get("brightnessOption"); | ||
| let setting = 0; | ||
|
|
||
| if (isButtonOn === "1") { | ||
| setting = userBrightnessOption; | ||
| } | ||
|
|
||
| brightness.save({ data: setting.toString() }, { patch: true }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| function initBrightnessListener() { | ||
| if (led) { | ||
| let button = led | ||
| .find({ name: "Button" }) | ||
| .get("state") | ||
| .find({ type: "Report" }); | ||
| let brightnessState = led | ||
| .find({ name: "Brightness" }) | ||
| .get("state") | ||
| .find({ type: "Report" }); | ||
| let userBrightnessOption = data.get("brightnessOption"); | ||
|
|
||
| brightnessState.on("change:data", function() { | ||
| let currentBrightnessOption = brightnessState.get("data"); | ||
| let currentButtonOption = button.get("data"); | ||
|
|
||
| if (currentButtonOption === "1") { | ||
| if (currentBrightnessOption !== userBrightnessOption) { | ||
| data.save( | ||
| { brightnessOption: currentBrightnessOption }, | ||
| { patch: true } | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "dependencies": { | ||
| "wapp-api": "^1.2.2" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use const