1 GreenFeed API
Developed by Jameson Brennan, Ira Parsons, and Hector Menendez Department of Animal Science, South Dakota State University
The objectives of this markdown document is to automate the download and visualization of Greenfeed data provided by C-Lock. Users will need to provide their own login credentials to run the code. This markdown can be used to quickly visualize usage of Greenfeed machines and preliminary data.
1.1 API
The first thing you need to do is to enter your variables for the code to run. In the quotations below you will need to change the username, password, FID (or greenfeed ID), and the start and end times for the data you want to download.
Two things to note are 1) you can enter multiple greenfeeds assocated with an account and 2) the start time and end time must be in the same format. The chunk below sets the end time to the computer time for the most recent downloads.
#change to login user name
USER <- "username"
#Change to login password
PASS <- 'Password'
#Change to greenfeed id or id's. The second line can be uncommented out for multiple greenfeeds
FID <- "541"
#FID <- "541,539,535,534"
#enter the start date (ST) and end date (ET) for the data you want to download
ST <- "yyyy-mm-dd"
ET <- "yyyy-mm-dd"
1.1.1 Data Download
This code chunk will pull the data from the cloud based on the specifications of what was provided above.
# Spaces must be replaced with %20
library(httr) # Must first install httr with: install.packages("httr")
library(stringr)
# First Authenticate to receive token:
req <- POST("https://portal.c-lockinc.com/api/login", body=list(user=USER, pass=PASS))
stop_for_status(req)
TOK <- trimws(content(req))
#URL="https://portal.c-lockinc.com/api/getemissions?d=visits&fids=297,298&st=2023-06-06%2000:00:00&et=2023-06-14%2012:00:00"
# Now get data using the login token
URL=paste0("https://portal.c-lockinc.com/api/getemissions?d=visits&fids=", FID, "&st=", ST, "&et=", ET, "%2012:00:00")
req <- POST(URL, body=list(token=TOK))
stop_for_status(req)
a <- content(req)
#Split the lines
perline <- str_split(a, "\\n")[[1]]
#Split the commas into a dataframe, while getting rid of the "Parameters" line and the headers line
df <- do.call("rbind", str_split(perline[3:length(perline)], ","))
df=as.data.frame(df)
colnames(df)=c('FeederID','AnimalName','RFID','StartTime','EndTime','GoodDataDuration',
'CO2GramsPerDay','CH4GramsPerDay','O2GramsPerDay','H2GramsPerDay','H2SGramsPerDay',
'AirflowLitersPerSec','AirflowCf','WindSpeedMetersPerSec','WindDirDeg','WindCf',
'WasInterrupted','InterruptingTags','TempPipeDegreesCelsius','IsPreliminary','RunTime')
1.2 Data Visualization
This next section is used to generate a series of quick plots to summarize visits and data from the greenfeeds to check usage rates.
#convert start time to date
df$Date=as.Date(df$StartTime)
df$RFID=stringr:: str_sub(df$RFID,-6,-1)
#get a count on the number of good observations by each greenfeed by day
library(dplyr)
daily_good_data=df %>% count(FeederID, Date, sort = TRUE)
daily_good_data=na.omit(daily_good_data)
ggplot(daily_good_data,aes(x=Date,y=n))+
geom_bar(stat = 'identity')+
scale_x_date(breaks = '2 day')+
facet_wrap(~FeederID)+
ggtitle('Number of good observations by GF by day')+
ylab('Number of daily observations')+
theme_light()+
theme(axis.text.x = element_text(angle = -45))
#get the number of unique animals visiting by day
rfid_day=df %>% # Applying group_by & summarise
group_by(Date,FeederID) %>%
summarise(count = n_distinct(RFID))
## `summarise()` has grouped output by 'Date'. You can override using the `.groups` argument.
rfid_day=na.omit(rfid_day)
ggplot(rfid_day,aes(x=Date,y=count))+
geom_bar(stat = 'identity')+
facet_wrap(~FeederID)+
scale_x_date(breaks = '2 day')+
labs(title = 'Number of Unique animals per day')+
theme_light()+
theme(axis.text.x = element_text(angle = -45))
df=na.omit(df)
df$CH4GramsPerDay=as.numeric(df$CH4GramsPerDay)
ggplot(df,aes(y=CH4GramsPerDay,x=Date))+
geom_point(aes(color = RFID))+
geom_smooth(method = 'lm', se = F)+
ylim(0,300)+
labs(title = 'Grams CH4 per day, colors individual animals ')+
theme(legend.position = "none",
axis.text.x = element_text(angle = -45))+
theme_light()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 11 rows containing non-finite outside the scale range (`stat_smooth()`).
## Warning: Removed 11 rows containing missing values or values outside the scale range (`geom_point()`).
df %>%
filter(Date > (max(df$Date) - 7)) %>%
dplyr::group_by(Date, RFID) %>%
dplyr::summarise(CH4GramsPerDay = mean(as.numeric(CH4GramsPerDay), na.rm = T)) %>%
ggplot(aes(x=Date,y=CH4GramsPerDay, fill = RFID))+
geom_boxplot()+
# facet_wrap(~Assignment)+
theme_classic()+
theme(panel.grid.major = element_line(color = 'black', linewidth = 0.1),
panel.grid.minor = element_line(color = 'grey', linewidth = 0.05),
axis.text.x = element_text(angle = 45, hjust = 1),
plot.caption = element_text(hjust = 0),
legend.position = "right")
## `summarise()` has grouped output by 'Date'. You can override using the `.groups` argument.