-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.gs
More file actions
126 lines (102 loc) · 4.13 KB
/
code.gs
File metadata and controls
126 lines (102 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 2019 enhanced by Rob Shepherd @95point2, Jo Hinchliffe @concreted0g
// - auto create tab based on TTN device ID
// - remove variable length gateways, just concat \n seperate gateways info parts
// - use TTN payload fields in headers and data columns
// 2017 by Daniel Eichhorn, https://blog.squix.org
// Inspired by https://gist.github.com/bmcbride/7069aebd643944c9ee8b
// 1. Create or open an existing Sheet and click Tools > Script editor and enter the code below
// 2. Run > setup
// 3. Publish > Deploy as web app
// - enter Project Version name and click 'Save New Version'
// - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously)
// 4. Copy the 'Current web app URL' and post this in your form/script action
var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service
function doPost(e){
var lock = LockService.getPublicLock();
lock.waitLock(30000); // wait 30 seconds before conceding defeat.
try {
var jsonData = JSON.parse(e.postData.contents);
// next set where we write the data - you could write to multiple/alternate destinations
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
var sheet = doc.getSheetByName(jsonData.dev_id);
if(sheet == null){
doc.insertSheet(jsonData.dev_id)
sheet = doc.getSheetByName(jsonData.dev_id);
}
// we'll assume header is in row 1 but you can override with header_row in GET/POST data
//var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var headerRow = [];
// loop through the header columns
// Update headers
headerRow.push("App ID");
headerRow.push("Device ID");
headerRow.push("Device EUI");
headerRow.push("Port");
headerRow.push("Frame Counter");
headerRow.push("Time");
headerRow.push("Data Rate");
// add headers for payload fields
for (key in jsonData.payload_fields) {
var value = jsonData.payload_fields[key];
if (jsonData.payload_fields.hasOwnProperty(key)) {
headerRow.push(key);
}
}
headerRow.push("Num Gateways");
headerRow.push("Gateway Info");
// set headers, replacing existing
sheet.getRange(1, 1, 1, headerRow.length).setValues([headerRow]);
// add new data row
var row = [];
row.push(jsonData.app_id);
row.push(jsonData.dev_id);
row.push(jsonData.hardware_serial);
row.push(jsonData.port);
row.push(jsonData.counter);
row.push(jsonData.metadata.time);
row.push(jsonData.metadata.data_rate);
// add payload field data
for (key in jsonData.payload_fields) {
var value = jsonData.payload_fields[key];
if (jsonData.payload_fields.hasOwnProperty(key)) {
row.push(value);
}
}
row.push( jsonData.metadata.gateways.length );
// make a gateway info string
var gw_info = ""
for (var i = 0; i < jsonData.metadata.gateways.length; i++) {
var gateway = jsonData.metadata.gateways[i];
gw_info += gateway.gtw_id;
gw_info += ","
gw_info += "rssi=" + gateway.rssi;
gw_info += ","
gw_info += "snr=" + gateway.snr;
gw_info += ","
gw_info += "loc=" + gateway.latitude + "," + gateway.longitude;
if( i < jsonData.metadata.gateways.length-1){
gw_info += "\n"
}
}
row.push(gw_info);
// set data row
// more efficient to set values as [][] array than individually
var nextRow = sheet.getLastRow()+1; // get next row
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
// return json success results
return ContentService
.createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
.setMimeType(ContentService.MimeType.JSON);
} catch(e) {
// if error return this
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
} finally { //release lock
lock.releaseLock();
}
}
function setup() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
SCRIPT_PROP.setProperty("key", doc.getId());
}