#!/bin/bash

# Datos para la conexión

export ENPHASE_REMOTE_API_USERNAME=creylopez@yahoo.es
export ENPHASE_REMOTE_API_PASSWORD=h769jzYDnqpPog
export ENPHASE_REMOTE_API_SERIALNO=122044072645

# obtener un id de sesión válido

sesion=`curl https://enlighten.enphaseenergy.com/login/login.json -d "user[email]=${ENPHASE_REMOTE_API_USERNAME}&user[password]=${ENPHASE_REMOTE_API_PASSWORD}" | jq -r '.session_id'`

# obtener un token usando el id de sesión
token=`curl "https://enlighten.enphaseenergy.com/entrez-auth-token?serial_num=${ENPHASE_REMOTE_API_SERIALNO}" -H "cookie: _enlighten_4_session=$sesion" | jq -r '.token'`

#echo "La sesión es $sesion y el token es $token"

datos=`curl -k -H "Authorization: Bearer $token" https://192.168.1.141/production.json`
#datos=`curl http://envoy.local/production.json 2>/dev/null`
#datos=`curl http://192.168.1.141/production.json 2>/dev/null`

#
# obtiene datos de producción de inverters
#
pi_type=`echo $datos|jq .production[0].type`
pi_activeCount=`echo $datos|jq .production[0].activeCount`
tmp=`echo $datos|jq .production[0].readingTime`
pi_readingTime=`date -d @$tmp +"%Y-%m-%d %H:%M:%S"`
pi_wNow=`echo $datos|jq .production[0].wNow`
pi_whLifetime=`echo $datos|jq .production[0].whLifetime`

#
# Mostrar resultados
#

echo "Type: '$pi_type'"
echo "activeCount: '$pi_activeCount'"
echo "readingTime: '$pi_readingTime'"
echo "wNow: '$pi_wNow'"
echo "whLifetime: '$pi_whLifetime'"

# insertar en la tabla

mysql -h $DB_HOST -u root -p'Dsa-0213' enphase <<EOFMYSQL
insert into production_inverters (type,activeCount,readingTime,wNow,whLifetime) values('$pi_type','$pi_activeCount','$pi_readingTime','$pi_wNow','$pi_whLifetime');
EOFMYSQL

#
# obtiene datos de producción de eim (lo que quiera que sea eso)
#
pe_type=`echo $datos|jq .production[1].type`
pe_activeCount=`echo $datos|jq .production[1].activeCount`
pe_measurementType=`echo $datos|jq .production[1].measurementType`
tmp=`echo $datos|jq .production[1].readingTime`
pe_readingTime=`date -d @$tmp +"%Y-%m-%d %H:%M:%S"`
pe_wNow=`echo $datos|jq .production[1].wNow`
pe_whLifetime=`echo $datos|jq .production[1].whLifetime`
pe_varhLeadLifetime=`echo $datos|jq .production[1].varhLeadLifetime`
pe_varhLagLifetime=`echo $datos|jq .production[1].varhLagLifetime`
pe_vahLifetime=`echo $datos|jq .production[1].vahLifetime`
pe_rmsCurrent=`echo $datos|jq .production[1].rmsCurrent`
pe_rmsVoltage=`echo $datos|jq .production[1].rmsVoltage`
pe_reactPwr=`echo $datos|jq .production[1].reactPwr`
pe_apprntPwr=`echo $datos|jq .production[1].apprntPwr`
pe_pwrFactor=`echo $datos|jq .production[1].pwrFactor`
pe_whToday=`echo $datos|jq .production[1].whToday`
pe_whLastSevenDays=`echo $datos|jq .production[1].whLastSevenDays`
pe_vahToday=`echo $datos|jq .production[1].vahToday`
pe_varhLeadToday=`echo $datos|jq .production[1].varhLeadToday`
pe_varhLagToday=`echo $datos|jq .production[1].varhLagToday`

# insertar en la tabla

mysql  -h $DB_HOST -u root -p'Dsa-0213' enphase <<EOFMYSQL
insert into production_eim (type,activeCount,measurementType,readingTime,wNow,whLifetime,varhLeadLifetime,varhLagLifetime,vahLifetime,rmsCurrent,rmsVoltage,reactPwr,apprntPwr,pwrFactor,whToday,whLastSevenDays,vahToday,varhLeadToday,varhLagToday) values('$pe_type','$pe_activeCount','$pe_measurementType','$pe_readingTime','$pe_wNow','$pe_whLifetime','$pe_varhLeadLifetime','$pe_varhLagLifetime','$pe_vahLifetime','$pe_rmsCurrent','$pe_rmsVoltage','$pe_reactPwr','$pe_apprntPwr','$pe_pwrFactor','$pe_whToday','$pe_whLastSevenDays','$pe_vahToday','$pe_varhLeadToday','$pe_varhLagToday');
EOFMYSQL

#
# obtiene datos de consumption total-consumption
#
tc_type=`echo $datos|jq .consumption[0].type`
tc_activeCount=`echo $datos|jq .consumption[0].activeCount`
tc_measurementType=`echo $datos|jq .consumption[0].measurementType`
tmp=`echo $datos|jq .consumption[0].readingTime`
tc_readingTime=`date -d @$tmp +"%Y-%m-%d %H:%M:%S"`
tc_wNow=`echo $datos|jq .consumption[0].wNow`
tc_whLifetime=`echo $datos|jq .consumption[0].whLifetime`
tc_varhLeadLifetime=`echo $datos|jq .consumption[0].varhLeadLifetime`
tc_varhLagLifetime=`echo $datos|jq .consumption[0].varhLagLifetime`
tc_vahLifetime=`echo $datos|jq .consumption[0].vahLifetime`
tc_rmsCurrent=`echo $datos|jq .consumption[0].rmsCurrent`
tc_rmsVoltage=`echo $datos|jq .consumption[0].rmsVoltage`
tc_reactPwr=`echo $datos|jq .consumption[0].reactPwr`
tc_apprntPwr=`echo $datos|jq .consumption[0].apprntPwr`
tc_pwrFactor=`echo $datos|jq .consumption[0].pwrFactor`
tc_whToday=`echo $datos|jq .consumption[0].whToday`
tc_whLastSevenDays=`echo $datos|jq .consumption[0].whLastSevenDays`
tc_vahToday=`echo $datos|jq .consumption[0].vahToday`
tc_varhLeadToday=`echo $datos|jq .consumption[0].varhLeadToday`
tc_varhLagToday=`echo $datos|jq .consumption[0].varhLagToday`

# insertar en la tabla

mysql  -h $DB_HOST -u root -p'Dsa-0213' enphase <<EOFMYSQL
insert into consumption_total (type,activeCount,measurementType,readingTime,wNow,whLifetime,varhLeadLifetime,varhLagLifetime,vahLifetime,rmsCurrent,rmsVoltage,reactPwr,apprntPwr,pwrFactor,whToday,whLastSevenDays,vahToday,varhLeadToday,varhLagToday) values('$tc_type','$tc_activeCount','$tc_measurementType','$tc_readingTime','$tc_wNow','$tc_whLifetime','$tc_varhLeadLifetime','$tc_varhLagLifetime','$tc_vahLifetime','$tc_rmsCurrent','$tc_rmsVoltage','$tc_reactPwr','$tc_apprntPwr','$tc_pwrFactor','$tc_whToday','$tc_whLastSevenDays','$tc_vahToday','$tc_varhLeadToday','$tc_varhLagToday');
EOFMYSQL

#
# obtiene datos de consumption net-consumption
#
nc_type=`echo $datos|jq .consumption[1].type`
nc_activeCount=`echo $datos|jq .consumption[1].activeCount`
nc_measurementType=`echo $datos|jq .consumption[1].measurementType`
tmp=`echo $datos|jq .consumption[1].readingTime`
nc_readingTime=`date -d @$tmp +"%Y-%m-%d %H:%M:%S"`
nc_wNow=`echo $datos|jq .consumption[1].wNow`
nc_whLifetime=`echo $datos|jq .consumption[1].whLifetime`
nc_varhLeadLifetime=`echo $datos|jq .consumption[1].varhLeadLifetime`
nc_varhLagLifetime=`echo $datos|jq .consumption[1].varhLagLifetime`
nc_vahLifetime=`echo $datos|jq .consumption[1].vahLifetime`
nc_rmsCurrent=`echo $datos|jq .consumption[1].rmsCurrent`
nc_rmsVoltage=`echo $datos|jq .consumption[1].rmsVoltage`
nc_reactPwr=`echo $datos|jq .consumption[1].reactPwr`
nc_apprntPwr=`echo $datos|jq .consumption[1].apprntPwr`
nc_pwrFactor=`echo $datos|jq .consumption[1].pwrFactor`
nc_whToday=`echo $datos|jq .consumption[1].whToday`
nc_whLastSevenDays=`echo $datos|jq .consumption[1].whLastSevenDays`
nc_vahToday=`echo $datos|jq .consumption[1].vahToday`
nc_varhLeadToday=`echo $datos|jq .consumption[1].varhLeadToday`
nc_varhLagToday=`echo $datos|jq .consumption[1].varhLagToday`

# insertar en la tabla

mysql -h $DB_HOST  -u root -p'Dsa-0213' enphase <<EOFMYSQL
insert into consumption_net (type,activeCount,measurementType,readingTime,wNow,whLifetime,varhLeadLifetime,varhLagLifetime,vahLifetime,rmsCurrent,rmsVoltage,reactPwr,apprntPwr,pwrFactor,whToday,whLastSevenDays,vahToday,varhLeadToday,varhLagToday) values('$nc_type','$nc_activeCount','$nc_measurementType','$nc_readingTime','$nc_wNow','$nc_whLifetime','$nc_varhLeadLifetime','$nc_varhLagLifetime','$nc_vahLifetime','$nc_rmsCurrent','$nc_rmsVoltage','$nc_reactPwr','$nc_apprntPwr','$nc_pwrFactor','$nc_whToday','$nc_whLastSevenDays','$nc_vahToday','$nc_varhLeadToday','$nc_varhLagToday');
EOFMYSQL

#
# obtiene datos de storage
#
st_type=`echo $datos|jq .storage[0].type`
st_activeCount=`echo $datos|jq .storage[0].activeCount`
tmp=`echo $datos|jq .storage[0].readingTime`
st_readingTime=`date -d @$tmp +"%Y-%m-%d %H:%M:%S"`
st_wNow=`echo $datos|jq .storage[0].wNow`
st_whNow=`echo $datos|jq .storage[0].whNow`
st_state=`echo $datos|jq .storage[0].state`

# insertar en la tabla

mysql -h $DB_HOST  -u root -p'Dsa-0213' enphase <<EOFMYSQL
insert into storage (type,activeCount,readingTime,wNow,whNow,state) values('$st_type','$st_activeCount','$st_readingTime','$st_wNow','$st_whNow','$st_state');
EOFMYSQL
