Repository navigation
Expand file tree
/
Copy pathMessage_Alert.R
More file actions
105 lines (87 loc) · 3.45 KB
/
Copy pathMessage_Alert.R
File metadata and controls
105 lines (87 loc) · 3.45 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
## Loading relevant packages
library(slackr)
library(AirSensor)
library(MazamaSpatialUtils)
library(lubridate)
library(dplyr)
library(twilio)
library(tidygeocoder)
## Loading Spatial data : This requires to create a local folder named Spatial_Data with MazamaSpatialUtils files.
initializeMazamaSpatialUtils("Spatial_Data")
## PurpleAir PAS with 30 days look back time & setting PWFSL as False:
pas <- pas_createNew(countryCodes = "IN",baseUrl = 'https://www.purpleair.com/json?all=true',
lookbackDays = 30,includePWFSL = FALSE)
## Geolocating from the latitude and longitude using open street map
Geolocation <- reverse_geo(
lat = pas$latitude,
long = pas$longitude,
method = "osm", # open street map
full_results = T
)
## Adding more columns in the pas object
pas$City <- as.factor(Geolocation$city)
pas$District <- as.factor(Geolocation$state_district)
pas$State <- as.factor(Geolocation$state)
pas$Sensor_Name <- pas$label
## Converting the last seen Date to India timezone
pas$lastSeen <- strftime(with_tz(pas$lastSeenDate,tz="Asia/Kolkata"),
format="%Y-%m-%d %H:%M",tz="Asia/Kolkata")
## Passing lastSeenDate as object
lastSeenDate <- pas$lastSeen
## Selecting lastSeenDate based Sensor unit and selecting limited columns with respect to states, Sensor_Names.
#Delhi
Inactive_Sensor_DL <- pas %>% filter(!grepl("B",label))%>%
filter(grepl("Delhi", State)) %>%
filter(lastSeen <Sys.Date()-1)%>%
select(Sensor_Name,lastSeen,District,State) %>%
arrange(desc(lastSeen))
#UP
Inactive_Sensor_UP <- pas %>% filter(!grepl("B",label))%>%
filter(grepl("Uttar Pradesh", State)) %>%
filter(lastSeen <Sys.Date()-1)%>%
select(Sensor_Name,lastSeen,District,State) %>%
arrange(desc(lastSeen))
#WB
Inactive_Sensor_WB <- pas %>% filter(!grepl("B",label))%>%
filter(grepl("West Bengal", State)) %>%
filter(lastSeen <Sys.Date()-1)%>%
select(Sensor_Name,lastSeen,District,State) %>%
arrange(lastSeen)
#Punjab
Inactive_Sensor_PB <- pas %>% filter(!grepl("B",label))%>%
filter(grepl("Punjab", State)) %>%
filter(lastSeen <Sys.Date()-1)%>%
select(Sensor_Name,lastSeen,District,State) %>%
arrange(lastSeen)
#Haryana
Inactive_Sensor_HAR <- pas %>% filter(!grepl("B",label))%>%
filter(grepl("Haryana", State)) %>%
filter(lastSeen <Sys.Date()-1)%>%
select(Sensor_Name,lastSeen,District,State) %>%
arrange(lastSeen)
#Karnataka
Inactive_Sensor_KAR <- pas %>% filter(!grepl("B",label))%>%
filter(grepl("Karnataka", State)) %>%
filter(lastSeen <Sys.Date()-1)%>%
select(Sensor_Name,lastSeen,District,State) %>%
arrange(lastSeen)
#chandigarh
Inactive_Sensor_CHAN <- pas %>% filter(!grepl("B",label))%>%
filter(grepl("Chandigarh", State)) %>%
filter(lastSeen <Sys.Date()-1)%>%
select(Sensor_Name,lastSeen,District,State) %>%
arrange(lastSeen)
#add required state as & when necessary
##SMS
## Twilio SSID & Token : This requires to create Twilio account.
Sys.setenv(TWILIO_SID = "xxxxx")
Sys.setenv(TWILIO_TOKEN = "xxxxx")
# Then we're just going to store the phone numbers in some variables
my_phone_number <- "+91xxxxxxxxx"
twilios_phone_number <- "+18179854279"
# Now we can send SMS away!
msg <- tw_send_message(
from = twilios_phone_number, to = my_phone_number, body = paste("Hi, The inactive Sensors as of " , lastSeenDate, "in Delhi are",
Inactive_Sensor_DL, "and in UP is/are", Inactive_Sensor_UP, "Do inform the host.", "Let us Breathe Clean Air- www.ceew.in" )
)
## Add multiple phone numbers & msg format using the above template for all states.