Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions src/handlers/Arduino/codeLedSTATUS.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const CodeLedSTATUS = async (request, event) => {

const arduinoData = await arduinoResponse.json();

console.log(" Response: \n", arduinoResponse);

return { arduinoData, arduinoResponse };
}
}
Expand All @@ -77,9 +79,11 @@ const CodeLedSTATUS = async (request, event) => {

const { arduinoData, arduinoResponse } = await useToken();

console.log(" Data: \n", arduinoData[0].properties[2]);

return new Response(JSON.stringify({
value: `${arduinoData[0].properties[1].last_value}`,
name: `${arduinoData[0].properties[1].name}`,
value: `${arduinoData[0].properties[2].last_value}`,
name: `${arduinoData[0].properties[2].name}`,
status: `${arduinoResponse.status}`,
}
), {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/Arduino/codeLedToggleOFF.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const CodeLedToggleOFF = async (request, event) => {
})
};

const urlTemp = `${propertiesPublishUrl}/${data[0].id}/properties/${data[0].properties[1].id}/publish`;
const urlTemp = `${propertiesPublishUrl}/${data[0].id}/properties/${data[0].properties[2].id}/publish`;
const response = await fetch(urlTemp, options3);

if (!response.ok) {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/Arduino/codeLedToggleON.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const CodeLedToggleON = async (request, event) => {
})
};

const urlTemp = `${propertiesPublishUrl}/${data[0].id}/properties/${data[0].properties[1].id}/publish`;
const urlTemp = `${propertiesPublishUrl}/${data[0].id}/properties/${data[0].properties[2].id}/publish`;
const response = await fetch(urlTemp, options3);

if (!response.ok) {
Expand Down
122 changes: 122 additions & 0 deletions src/handlers/Arduino/webSiteLedToggleON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//Arduino Url
const ArduinoUrl = 'https://api2.arduino.cc/iot/v1/clients/token';
const thingsURL = "https://api2.arduino.cc/iot/v2/things?show_properties=true";
const propertiesPublishUrl = "https://api2.arduino.cc/iot/v2/things";

const webSiteLedToggleON = async (request, event) => {

try {
// Turn on LED on website and board
async function getToken() {
let options = {
method: 'POST',
body: 'grant_type=client_credentials&client_id=' + ARDUINO_CLIENT_ID + '&client_secret=' + ARDUINO_SECRET_TOKEN + '&audience=' + 'https://api2.arduino.cc/iot',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
};

try {
const response = await fetch(ArduinoUrl, options);

console.log(response.status);

if (!response.ok) {
const message = `Bad response: ${response.status}`;
throw new Error(message);
}
else {

const tokenPackage = await response.json();
const token = (tokenPackage['access_token']);

return token;
}
}
catch (error) {
console.error("Failed getting an Arduino access token: " + error);
}
}

async function useToken() {
try {

const token = await getToken();

let options2 = {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
};

try {

const response = await fetch(thingsURL, options2);

if (!response.ok) {
const message = `\n An error has occured: ${response.status}`;

throw new Error(message);
}
else {

const data = await response.json();

try {

let options3 = {
method: 'PUT',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({

'value': true,
})
};

const urlTemp = `${propertiesPublishUrl}/${data[0].id}/properties/${data[0].properties[3].id}/publish`;
const response = await fetch(urlTemp, options3);

if (!response.ok) {
const message = `\n Bad response PUT : ${response.status}`;
throw new Error(message);
}

return response;

}
catch (error) {
console.error("\n Failed POST properties update request: ", error);
}
}
}
catch (error) {
console.error("\n Failed GET things request: ", error);
}

} catch (error) {
console.log("useToken() error occurred: ", error);
}
}

const arduinoResponse = await useToken();

console.log("arduinoResponse:" , arduinoResponse);

return new Response(JSON.stringify("Connection to API is live, LED is on." + arduinoResponse.status), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
'Access-Control-Max-Age': '86400',
}
});
}
catch (error) {
console.error("Failed webSiteLedToggleOn function call : " , error);
//res.status(401).send("Could not find ticker symbol or other issue");
}
};
export default webSiteLedToggleON
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CryptoTicker from './handlers/Financial/cryptoTicker.js';
import NftTicker from './handlers/Financial/nft.js';
import GovData from './handlers/Financial/gov.js';
import CodeLedToggleON from './handlers/Arduino/codeLedToggleON.js';
import WebSiteLedToggleON from './handlers/Arduino/webSiteLedToggleON';
import CodeLedToggleOFF from './handlers/Arduino/codeLedToggleOFF.js';
import CodeLedSTATUS from './handlers/Arduino/codeLedSTATUS.js';
import MongoBackend from './handlers/Mongo/MongoBackend.js';
Expand Down Expand Up @@ -56,6 +57,7 @@ router.get('/api/Gov', GovData );
router.get('/api/Arduino/ToggleON', CodeLedToggleON );
router.get('/api/Arduino/ToggleOFF', CodeLedToggleOFF );
router.get('/api/Arduino/STATUS', CodeLedSTATUS );
router.get('/api/Arduino/TestConnection', WebSiteLedToggleON );

router.get('/api/Mongo', MongoBackend );

Expand Down