-
Notifications
You must be signed in to change notification settings - Fork 5
Weather station branch #4
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
Open
MadalinaKhaji
wants to merge
8
commits into
Wappsto:master
Choose a base branch
from
MadalinaKhaji:weather-station-branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
78975e4
Added background functionality
MadalinaKhaji b2908bd
Added weather station
MadalinaKhaji 3771e9e
Moved background functionality to foreground
MadalinaKhaji a547da9
Revert "Added background functionality"
MadalinaKhaji 882e070
Revert "Moved background functionality to foreground"
MadalinaKhaji 05d23be
update weather station
MadalinaKhaji 76b1802
refactor weather station background
MadalinaKhaji ca2a9c3
improve weather station foreground
MadalinaKhaji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const config = { | ||
| clientId: "your_client_id", | ||
| clientSecret: "your_client_secret", | ||
| deviceId: "your_device_id", | ||
| username: "your_username", | ||
| password: "your_password" | ||
| }; | ||
|
|
||
| module.exports = config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,358 @@ | ||
| let Wappsto = require("wapp-api"); | ||
| let axios = require("axios"); | ||
| let config = require("./config"); | ||
| let networkInfo = require("./networkInfo.json"); | ||
|
|
||
| let wappsto = new Wappsto({ | ||
| baseUrl: "https://wappsto.com/services", | ||
| session: "68c355e7-d255-4f4c-b912-d35b12f709c7" | ||
| }); | ||
|
|
||
| let network, data; | ||
| let unreacheableDevices = []; | ||
| // Timer used for updating data | ||
| let updateTimer; | ||
| // 3 min | ||
| let timeInterval = 180000; | ||
|
|
||
| let statusMessage = { | ||
| warning_user_login: | ||
| "Please login into your Netatmo account to grant permissions to this app", | ||
| success_user_granted_netatmo_data_access: "Permission granted", | ||
| error_user_denied_netatmo_data_access: | ||
| "Permission to access Netatmo account data denied", | ||
| success_retrieve_wappsto_data: "Succesfully retrieved Wappsto data", | ||
| error_retrieve_wappsto_data: "Failed to retrieve Wappsto data" | ||
| }; | ||
|
|
||
| wappsto.get( | ||
| "data", | ||
| {}, | ||
| { | ||
| expand: 1, | ||
| subscribe: true, | ||
| success: function(collection) { | ||
| data = collection.first(); | ||
|
|
||
| checkNetwork(); | ||
| }, | ||
| error: function(error) { | ||
| console.log(error); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| // Check if network exists; if not then create it | ||
| let checkNetwork = function() { | ||
| wappsto.get( | ||
| "network", | ||
| {}, | ||
| { | ||
| expand: 5, | ||
| subscribe: true, | ||
| success: function(collection) { | ||
| if (collection.length > 0) { | ||
| network = collection.first(); | ||
|
|
||
| setUpdateTimer(); | ||
| } else { | ||
| network = createNetwork(); | ||
| // get the users station data with which to populate the network | ||
| getStationData(); | ||
| } | ||
| }, | ||
| error: function(error) { | ||
| console.log(error); | ||
| } | ||
| } | ||
| ); | ||
| }; | ||
|
|
||
| // Update station data | ||
| let updateStationData = function() { | ||
| unreacheableDevices = []; | ||
| // Remove previous unreacheable devices | ||
| updateWappstoData({ lostDevices: unreacheableDevices }); | ||
|
|
||
| if (updateTimer) { | ||
| clearInterval(updateTimer); | ||
| } | ||
| // Destroy existing network and then create new network using new data | ||
| network | ||
| .destroy() | ||
SamiCharfeddine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .catch(function(error) { | ||
| // console.log(error); | ||
| }) | ||
| .then(function() { | ||
| network = createNetwork(); | ||
|
|
||
| getStationData(); | ||
| }); | ||
| }; | ||
|
|
||
| let setUpdateTimer = function() { | ||
| updateTimer = setInterval(function() { | ||
| updateStationData(); | ||
| }, timeInterval); | ||
| }; | ||
|
|
||
| // Create and return network | ||
| let createNetwork = function() { | ||
| let newNetwork = new wappsto.models.Network(); | ||
|
|
||
| newNetwork.set("name", networkInfo.name); | ||
|
|
||
| return newNetwork; | ||
| }; | ||
|
|
||
| // Create and return device | ||
| let createDevice = function(deviceData) { | ||
| let newDevice = new wappsto.models.Device(); | ||
| // If a device is not reachable it will be skipped | ||
| if (deviceData.reachable === false) { | ||
| unreacheableDevices.push(deviceData.module_name); | ||
| return null; | ||
| } | ||
| // Device type is used to differentiate between the Main Module and the other modules | ||
| // Thus the right attributes can be set for each case | ||
| if (deviceData.type === "NAMain") { | ||
| newDevice.set({ | ||
| name: deviceData.module_name, | ||
| description: networkInfo.device[0].description, | ||
| manufacturer: networkInfo.device[0].manufacturer, | ||
| communication: networkInfo.device[0].communication | ||
| }); | ||
| } else { | ||
| newDevice.set({ | ||
| name: deviceData.module_name, | ||
| description: "Module device", | ||
| manufacturer: networkInfo.device[0].manufacturer, | ||
| communication: networkInfo.device[0].communication | ||
| }); | ||
| } | ||
| return newDevice; | ||
| }; | ||
|
|
||
| // Create and return device value | ||
| let createValue = function(dataType, device) { | ||
| let newValue = new wappsto.models.Value(); | ||
|
|
||
| networkInfo.device[0].value.forEach(function(value) { | ||
| if (value.param === dataType) { | ||
| newValue.set({ | ||
| name: value.name, | ||
| type: value.type, | ||
| permission: value.permission, | ||
| dataType: value.dataType, | ||
| // all the values are of type number | ||
| number: { | ||
| min: value.min ? parseInt(value.min) : -999, | ||
| max: value.max ? parseInt(value.max) : 999, | ||
| step: value.step ? parseInt(value.step) : 1, | ||
| unit: value.unit | ||
| }, | ||
| description: value.description | ||
| }); | ||
|
|
||
| if (newValue) { | ||
| // get the state data | ||
| let stateData = device.dashboard_data[value.param]; | ||
| // all the value permissions are of type Report | ||
| let reportState = createState("Report", stateData); | ||
|
|
||
| newValue.get("state").push(reportState); | ||
| } | ||
| } | ||
| }); | ||
| return newValue; | ||
| }; | ||
|
|
||
| // Create and return value state | ||
| let createState = function(type, data) { | ||
| let newState = new wappsto.models.State(); | ||
|
|
||
| let timestamp = new Date().toISOString(); | ||
|
|
||
| newState.set({ | ||
| type: type, | ||
| data: data + "", | ||
| timestamp: timestamp + "" | ||
| }); | ||
|
|
||
| return newState; | ||
| }; | ||
|
|
||
| // Save network and set update timer | ||
| let saveNetwork = function() { | ||
| network.save( | ||
| {}, | ||
| { | ||
| subscribe: true, | ||
| success: function() { | ||
| if (updateTimer) { | ||
| clearInterval(updateTimer); | ||
| } | ||
|
|
||
| setUpdateTimer(); | ||
| }, | ||
| error: function(error) { | ||
| console.log(error); | ||
| } | ||
| } | ||
| ); | ||
| }; | ||
|
|
||
| // Save and update data to wappsto data model | ||
| let updateWappstoData = function(dataToUpdate) { | ||
| data.set(dataToUpdate); | ||
| data.save(dataToUpdate, { | ||
| patch: true | ||
| }); | ||
| }; | ||
|
|
||
| // By default, axios serializes JavaScript objects to JSON. | ||
| // To send data in the application/x-www-form-urlencoded format instead, use querystring to stringify nested objects! | ||
| const querystring = require("querystring"); | ||
|
|
||
| // Get access token with client credentials grant type - use only for development and testing | ||
| let getAccessToken = function() { | ||
| axios({ | ||
| method: "POST", | ||
| headers: { | ||
| Host: "api.netatmo.com", | ||
| "Content-type": "application/x-www-form-urlencoded;charset=UTF-8" | ||
| }, | ||
| url: "/oauth2/token", | ||
| baseURL: "https://api.netatmo.com/", | ||
| data: querystring.stringify({ | ||
| grant_type: "password", | ||
| client_id: config.clientId, | ||
| client_secret: config.clientSecret, | ||
| username: config.username, | ||
| password: config.password, | ||
| scope: "read_station" | ||
| }) | ||
| }) | ||
| .then(function(response) { | ||
| updateWappstoData({ | ||
| accessToken: response.data.access_token, | ||
| refreshToken: response.data.refresh_token, | ||
| expiresIn: response.data.expires_in | ||
| }); | ||
|
|
||
| getStationData(); | ||
| }) | ||
| .catch(function(error) { | ||
| console.log(error); | ||
| }); | ||
| }; | ||
|
|
||
| // Send request to refresh token and update tokens with new values | ||
| let getRefreshToken = function() { | ||
| let refreshToken = data.get("refreshToken"); | ||
|
|
||
| axios({ | ||
| method: "POST", | ||
| headers: { | ||
| Host: "api.netatmo.com", | ||
| "Content-type": "application/x-www-form-urlencoded;charset=UTF-8" | ||
| }, | ||
| url: "/oauth2/token", | ||
| baseURL: "https://api.netatmo.com/", | ||
| data: querystring.stringify({ | ||
| grant_type: "refresh_token", | ||
| refresh_token: refreshToken, | ||
| client_id: config.clientId, | ||
| client_secret: config.clientSecret | ||
| }) | ||
| }) | ||
| .then(function(response) { | ||
| updateWappstoData({ | ||
| accessToken: response.data.access_token, | ||
| refreshToken: response.data.refresh_token, | ||
| expiresIn: response.data.expires_in | ||
| }); | ||
|
|
||
| getStationData(); | ||
| }) | ||
| .catch(function(error) { | ||
| console.log(error); | ||
| }); | ||
| }; | ||
|
|
||
| // Use device data to create device, values and state and then add device to the network | ||
| let addDevicesToNetwork = function(deviceData) { | ||
| deviceData.forEach(function(device) { | ||
| let deviceToAdd = createDevice(device); | ||
|
|
||
| if (deviceToAdd) { | ||
| let deviceDataTypes = device.data_type; | ||
|
|
||
| deviceDataTypes.forEach(function(dataType) { | ||
| let valueToAdd = createValue(dataType, device); | ||
|
|
||
| deviceToAdd.get("value").push(valueToAdd); | ||
| }); | ||
| network.get("device").push(deviceToAdd); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| // Get the users station data | ||
| let getStationData = function() { | ||
| let accessToken = data.get("accessToken"); | ||
|
|
||
| if (!accessToken) { | ||
| getAccessToken(); | ||
| } | ||
|
|
||
| axios({ | ||
SamiCharfeddine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| method: "GET", | ||
| headers: { | ||
| Host: "api.netatmo.com", | ||
| Authorization: "Bearer " + accessToken | ||
| }, | ||
| url: "/getstationsdata", | ||
| baseURL: "https://api.netatmo.com/api/", | ||
| data: querystring.stringify({ | ||
| device_id: config.deviceId, | ||
| get_favorites: false | ||
| }) | ||
| }) | ||
| .then(function(response) { | ||
| // Data of the Main Module - every station has this device | ||
| let deviceData = response.data.body.devices; | ||
| // Saving station name to display in the FG | ||
| let stationName = deviceData[0].station_name; | ||
|
|
||
| if (data.get("stationName") !== stationName) { | ||
| updateWappstoData({ stationName: stationName }); | ||
| } | ||
|
|
||
| addDevicesToNetwork(deviceData); | ||
| // Data of the modules associated with the Main Module | ||
| let moduleData = response.data.body.devices[0].modules; | ||
|
|
||
| addDevicesToNetwork(moduleData); | ||
| // Saving network | ||
| saveNetwork(); | ||
| // Save unreachable devices if any | ||
| if (unreacheableDevices.length > 0) { | ||
| updateWappstoData({ | ||
SamiCharfeddine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| status_message: statusMessage.success_retrieve_wappsto_data, | ||
| lostDevices: unreacheableDevices | ||
| }); | ||
| } else { | ||
| updateWappstoData({ | ||
| status_message: statusMessage.success_retrieve_wappsto_data | ||
| }); | ||
| } | ||
| }) | ||
| .catch(function(error) { | ||
| updateWappstoData({ | ||
| status_message: statusMessage.error_retrieve_wappsto_data | ||
| }); | ||
|
|
||
| getRefreshToken(); | ||
| }); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.