Last active
January 7, 2023 00:25
-
-
Save nayelyzarazua-bluetrail/b3c0ea173812e686df1a7fd61c023c44 to your computer and use it in GitHub Desktop.
GoogleSheetLogger
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
'use strict'; | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const SmartApp = require('@smartthings/smartapp'); | |
const axios = require('axios'); | |
require('dotenv').config(); | |
const server = module.exports = express(); | |
server.use(bodyParser.json()); | |
const app = new SmartApp() | |
/* Handles lifecycle events from SmartThings */ | |
server.post('/', async (req, res) => { | |
app.handleHttpCallback(req, res); | |
}); | |
/* Defines the SmartApp .enableEventLogging() */ | |
app.enableEventLogging(2).appId("location-room-request") | |
.page('mainPage', (context, page, configData) => { | |
page.section('contacts', section => { | |
section.deviceSetting('conSensor').capabilities(['contactSensor']).required(false); | |
}); | |
page.section('googleSheet', section => { | |
//This doesn't require a defaultValue, it was used just for the tests | |
section.textSetting('urlKey').defaultValue("AKfycbwkNjHobma8Z62C8Zo-MunAqjjh_Z9pm2E2bmRLy...").required(true); | |
}); | |
}) | |
.updated(async (context, updateData) => { | |
await context.api.subscriptions.unsubscribeAll(); | |
return Promise.all([ | |
context.api.subscriptions.subscribeToDevices(context.config.conSensor, 'contactSensor', 'contact', 'contactSensorEventHandler') | |
]) | |
}) | |
.uninstalled((context, uninstallData) =>{ | |
//... | |
}) | |
.subscribedDeviceHealthEventHandler('contactSensorEventHandler', async (context, deviceEvent) => { | |
//Request to get the info from the device (label) to use identify it easily in the sheet | |
//By default, the subscription event only includes the deviceID, not its display name (label) | |
let deviceinfo= await context.api.devices.get(deviceEvent.deviceId) | |
//Encode the values to be sent to the sheet because they are query parameters | |
//(this is what the original script of the app expects) | |
let columnName = encodeURIComponent(deviceinfo.label+" "+deviceEvent.capability) | |
let rowValue = encodeURIComponent(deviceEvent.value) | |
//context.config.urlKey[0].stringConfig gets the value we set as default in line 28 | |
sendEvent(columnName,rowValue,context.config.urlKey[0].stringConfig.value) | |
}) | |
function sendEvent (columnName, rowValue, urlKey) { | |
let url = "https://script.google.com/" | |
var config = { | |
method: 'GET', | |
url: `https://script.google.com/macros/s/${urlKey}/exec?${columnName}=${rowValue}`, | |
headers: { } | |
}; | |
axios(config) | |
.then(function (response) { | |
console.log(response.status); | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}); | |
//Note: For this to execute correctly, you need to make sure your script (Google Sheet) is authorized, | |
//otherwise, the response will return a login page | |
} | |
/* Starts the server */ | |
let port = process.env.PORT; | |
server.listen(port); | |
console.log(`Open: http://127.0.0.1:${port}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment