Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions smart-home-sensors/background/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
let Wappsto = require("wapp-api");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use const


let wappsto = new Wappsto();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use const


let wappstoConsole = require("wapp-api/console");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could rewrite to the following form:
.then(collection) => {
...
}

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" });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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) +

@mareklap mareklap Feb 12, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 }
);
}
}
});
}
}
5 changes: 5 additions & 0 deletions smart-home-sensors/background/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"wapp-api": "^1.2.2"
}
}
Loading