forked from jeffisfast/TSCU-Wx-Rpi-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppsScriptWebservice-Simple
124 lines (99 loc) · 4.17 KB
/
AppsScriptWebservice-Simple
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
//Replace these with your values
var ssId = '0AjdG0Zk4rfrwdFU1WkY4bFhFeklOM0dFTHZIeVR6Smc'
var myEmailAddress = '[email protected]';
function doGet(e) {
var ss = SpreadsheetApp.openById(ssId)
var sheet = ss.getSheetByName('Sheet1');
var rowContents = [e.parameter.timestamp, e.parameter.tempc, e.parameter.tempf];
sheet.appendRow(rowContents);
// alertEmail(sheet); //Remove double slashes in front to try this out!
// calendarEvent(sheet); //Remove double slashes in front to try this out!
return ContentService.createTextOutput("Success!");
}
//All activities below are optional
//*****Optional Auto Alert Email Activity*****
//Optional extension activity to send email if temperature changes from above to below freezing
function alertEmail(sheet) {
var latestTemp = getLatestTemp(sheet);
var previousTemp = getPreviousTemp(sheet);
//Send alert email if there is a change from above freezing to below freezing
if ((previousTemp>32)&&(latestTemp<32)) {
MailApp.sendEmail(myEmailAddress, 'Urgent: Freezing warning', 'The temperature has just dropped below 40 degrees');
}
}
//*****Optional Auto-Calendar Event Activity*****
//Optional extension activity to create a calendar event and extend it for all times when temp is below freezing
function calendarEvent(sheet) {
var latestDateTime = getLatestTimestamp(sheet)
var previousDateTime = getPreviousTimestamp(sheet);
//Create a new calendar event if the temperature goes from above 32 to below 32
if ((latestTemp<32)&&(previousTemp>32)) {
var myCalendar = CalendarApp.getCalendarById(myEmailAddress);
var newEventId = myCalendar.createEvent("Temp below 40", previousDateTime, latestDateTime).getId();
ScriptProperties.setProperty('latestEventId', newEventId); //store the event ID for future use
}
//Assume a calendar event already exists if both previous and new temp are below 32
if ((latestTemp<32)&&(previousTemp<32)) {
var myCalendar = CalendarApp.getCalendarById('[email protected]');
var eventId = ScriptProperties.getProperty('latestEventId'); //load the event ID of existing event
if (eventId) {
var thisEvent = getEventById(myCalendar, eventId);
if (thisEvent) {
var thisStart = thisEvent.getStartTime();
thisEvent.setTime(thisStart, latestDateTime);
} else { //In case the event isn't found on the calendar, creat a new event
var newEventId = myCalendar.createEvent("Temp below 40", previousDateTime, latestDateTime).getId();
ScriptProperties.setProperty('latestEventId', newEventId);
}
} else { //In case the event ID doesn't exist, create a new event
var newEventId = myCalendar.createEvent("Temp below 40", previousDateTime, latestDateTime).getId();
ScriptProperties.setProperty('latestEventId', newEventId);
}
}
}
function getEventById(calendar, eventId) {
var oneYearAgo = new Date();
oneYearAgo.setYear(oneYearAgo.getYear() - 1);
var oneYearAhead = new Date();
oneYearAhead.setYear(oneYearAgo.getYear() + 1);
var events = calendar.getEvents(oneYearAgo, oneYearAhead);
for (var i=0; i<events.length; i++) {
if (events[i].getId() == eventId) {
return events[i];
}
}
return;
}
//Returns most recently recorded Fahrenheit temperature from spreadsheet
function getLatestTemp(sheet) {
var lastRow = sheet.getLastRow();
var lastTemp = sheet.getRange(lastRow, 3).getValue();
return lastTemp;
}
//Returns previously recorded Fahrenheit temperature from spreadsheet
function getPreviousTemp(sheet) {
var nextToLastRow = sheet.getLastRow() - 1;
if (nextToLastRow > 1) {
var previousTemp = sheet.getRange(nextToLastRow, 3).getValue();
return previousTemp;
}
return;
}
//Returns most recent timestamp
function getLatestTimestamp(sheet) {
var lastRow = sheet.getLastRow();
if (lastRow > 1) {
var latestTimestamp = sheet.getRange(lastRow, 1).getValue();
return latestTimestamp;
}
return;
}
//Returns previously recorded timestamp
function getPreviousTimestamp(sheet) {
var nextToLastRow = sheet.getLastRow() - 1;
if (nextToLastRow > 1) {
var previousTimestamp = sheet.getRange(nextToLastRow, 1).getValue();
return previousTimestamp;
}
return;
}