Created
September 22, 2022 04:02
-
-
Save dedSyn4ps3/0c4a73a0a64f07e53d2d66eb78ff225a to your computer and use it in GitHub Desktop.
Simple function to make a backend GET request
This file contains 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
////////////////////////////////////////////////////////////////////////////////// | |
// Make call to Rust backend to query the Pi's flask server for sensor values // | |
////////////////////////////////////////////////////////////////////////////////// | |
callBackend() { | |
let i = localStorage.getItem("pi_address"); | |
if (i == "") { | |
console.log("[!] No sensor address available -> Update device info using the SideDash tab... [!]"); | |
this.setAddressModalVisible(true); | |
} else { | |
invoke('get_data', { address: i.replace(/"/g, ''), endpoint: 'temperature' }).then(value => { this.setState({ temp: parseFloat(value) }) }).catch((e) => console.error(e)); | |
invoke('get_data', { address: i.replace(/"/g, ''), endpoint: 'humidity' }).then(value => { this.setState({ hum: parseFloat(value) }) }).catch((e) => console.error(e)); | |
invoke('get_data', { address: i.replace(/"/g, ''), endpoint: 'reducing' }).then(value => { this.setState({ co: parseFloat(value) }) }).catch((e) => console.error(e)); | |
invoke('get_data', { address: i.replace(/"/g, ''), endpoint: 'ammonia' }).then(value => { this.setState({ nh3: parseFloat(value) }) }).catch((e) => console.error(e)); | |
invoke('get_data', { address: i.replace(/"/g, ''), endpoint: 'pressure' }).then(value => { this.setState({ press: parseFloat(value) }) }).catch((e) => { console.error(e); this.setErrorModalVisible(true) }); | |
this.intervalID = setTimeout(this.callBackend.bind(this), 60000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment