orders

library(zoo)
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(xlsx)
## Loading required package: rJava
## Loading required package: xlsxjars
load("~/git/aprim/sol_production.RData")
 which.peaks <- function(x,partial=TRUE,decreasing=FALSE,lag=1){
     if (decreasing){
         if (partial){
             which(diff(c(FALSE,diff(x,lag=lag)>0,TRUE))>0)
         }else {
             which(diff(diff(x,lag=lag)>0)>0)+1
         }
     }else {
         if (partial){
             which(diff(c(TRUE,diff(x,lag=la)>=0,FALSE))<0)
         }else {
             which(diff(diff(x,lag=lag)>=0)<0)+1
         }
     }
 }

0004A3ECCCAF M222 0004A3ED2831 M220 0004A3ED0E0C M237 0004A3ED46BC M215 0004A3ED5600 M204

load("~/git/aprim/sol_production.RData")
##plot(sol_production$power,type="l")
y=rollmean(sol_production$power,6)
mp=which.peaks(y,decreasing=TRUE,lag=10)
###delete the peaks which the distance is less than 10 indexes
mp[diff(mp,lag=1)>10]+5->p
c(rep(0,5),y)->rollmean_sol
sol_production$rollmean_sol<-rollmean_sol


products<-c()
###calculate the energy consumption from one peak to the next peak###
energyConsumptionPeaks<-function(x,y){
  sum(as.numeric(sol_production[(1+x):y,"dt"])*rollmean(sol_production[x:y,]$rollmean_sol,2))->productEnerge
  productEnerge
}

###calculate the energy consumption for each circle(two products)
for (i in 1:149){
    product<-energyConsumptionPeaks(p[i],p[i+1])
    products<-c(products,product)
}

##each circle corresponds two products(change the units to wh)
products*1000/3600->products
products2<-(products/2)
pdf("hist_machine237_order1568_pignon.pdf",width = 8,height = 8)
hist(products, xlab = "Energy Comsumption(Wh)",main="")
dev.off()
## png 
##   2
hist(products, xlab = "Energy Comsumption(Wh)",main="")

calculate energy consumption and energy price

if(file.exists("electricityPrice.RData")){
  load("electricityPrice.RData")
}else{
read.xlsx("electricity_04_07_2014_04_07_2014.xls",2)->electricity_price
electricity_price[2:25]->electricityPrice
as.data.frame(t(electricityPrice))->electricityPrice
electricityPrice$hour<-c(0:23)
save(electricityPrice,file="eletricityPrice.RData")
}



energyConsumption<-function(dataset=sol_order1568){
   ###calculate energy consumption
  p<-c()
  for(i in 1:(nrow(dataset)-1)){
  (dataset[i,"power"]+dataset[i+1,"power"])*dataset[i+1,"dt"]/2->p[i]
  }
  p*1000/3600->p
  c(0,p)->p
  p
}

  
priceCal<-function(dataset=sol_order1568){
  as.POSIXlt(strptime(dataset$time, "%Y-%m-%dT%H:%M:%S"))->t
  dataset$hour<-t$hour
  merge(as.data.frame(dataset),electricityPrice,by="hour",all.x=TRUE)->dataset
  dataset$powerConsumption<-energyConsumption(dataset)
  dataset$V1*dataset$powerConsumption->price
  dataset$energyPrice<-price
  sum(price)/1000000
}
#########################order 1529-m237 ###
if(file.exists("sol_order1529.RData")){
 load("sol_order1529.RData")
}else{
    deviceid2="0004A3ED0E0C"
  sol_order1529=dailyenergy(deviceid2,"2014-07-08T19:51:00","2014-07-08T22:02:00",0.95)$df
  save(sol_order1529,file="sol_order1529.RData")
}

##energy consumption##
sum(energyConsumption(sol_order1529))
## [1] 8858.992
###co2
sum(energyConsumption(sol_order1529))*0.372
## [1] 3295.545
##
priceCal(sol_order1529)
## [1] 0.529344
######N1474  Nut bearing adjust####
if(file.exists("sol_order1474.RData")){
 load("sol_order1474.RData")
}else{
deviceid="0004A3ED0E0C"
sol_order1474=dailyenergy(deviceid,"2014-06-16T22:02:00","2014-06-17T06:00:00",0.95)$df
save(sol_order1474,file="sol_order1474.RData")
}

##energy consumption##
sum(energyConsumption(sol_order1474))
## [1] 23124.72
###co2
sum(energyConsumption(sol_order1474))*0.372
## [1] 8602.396
##
priceCal(sol_order1474)
## [1] 1.246007
############################N1474 ,Item related to drawing: 8TTAAB001808K####
if(file.exists("sol_order1474_m220.RData")){
 load("sol_order1474_m220.RData")
}else{
deviceid="0004A3ED0E0C"
sol_order1474_m220=dailyenergy(deviceid,"2014-07-03T06:00:00","2014-07-03T14:00:00",0.95)$df
save(sol_order1474_m220,file="sol_order1474_m220.RData")
}

##energy consumption##
sum(energyConsumption(sol_order1474_m220))
## [1] 18826.68
###co2
sum(energyConsumption(sol_order1474_m220))*0.372
## [1] 7003.525
##
priceCal(sol_order1474_m220)
## [1] 1.089057
#########################order 1568 PIGNON FOUR DE 4 EME ###
if(file.exists("sol_order1568.RData")){
  load("sol_order1568.RData")
}else{
  deviceid="0004A3ED0E0C"
sol_order1568=dailyenergy(deviceid,"2014-06-26T10:45:00","2014-06-26T13:51:00",0.95)$df
save(sol_order1568,file="sol_order1568.RData")
}

##energy consumption##
sum(energyConsumption(sol_order1568))
## [1] 10452.27
###co2
sum(energyConsumption(sol_order1568))*0.372
## [1] 3888.245
##price consumption
priceCal(sol_order1568)
## [1] 0.6180429
###order N1537 ENGRANAJE PLANO 4º VEL####
if(file.exists("sol_order1537.RData")){
  load("sol_order1537.RData")
}else{
  deviceid="0004A3ECCCAF"
sol_order1537=dailyenergy(deviceid,"2014-06-25T14:00:00","2014-06-25T22:02:00",0.95)$df
save(sol_order1537,file="sol_order1537.RData")
}

##energy consumption##
sum(energyConsumption(dataset=sol_order1537))
## [1] 31482.57
###co2
sum(energyConsumption(sol_order1537))*0.372
## [1] 11711.52
##price consumption
priceCal(dataset=sol_order1537)
## [1] 1.827919
###order N1537 ENGRA. 5ª VEL.S/P 8TTAB001808K ####
if(file.exists("sol_order1537_m2.RData")){
  load("sol_order1537_m2.RData")
}else{
  deviceid="0004A3ED2831"
sol_order1537_m2=dailyenergy(deviceid,"2014-06-30T06:24:00","2014-06-30T14:00:00",0.95)$df
save(sol_order1537_m2,file="sol_order1537_m2.RData")
}

##energy consumption##
sum(energyConsumption(dataset=sol_order1537_m2))
## [1] 35876.75
###co2
sum(energyConsumption(sol_order1537_m2))*0.372
## [1] 13346.15
##price consumption
priceCal(dataset=sol_order1537_m2)
## [1] 2.061595