#include <stdint.h>
#include "server_common.h"
#include "version.h"
#include <string.h>
#include "charge_logic.h"
#include "fault_detect.h"
#include "rtc_driver.h"
#define GUN_OFFLINE 0
#define GUN_FAULT 1
#define GUN_IDLE 2
#define GUN_CHARGING 3
static uint16_t pack_serial = 0;
#define TO_SERVER_ENERGY(val) (100.0*val/3600)
typedef struct{
uint32_t sec;
uint8_t send_cnt;
uint8_t ack_flag;
PACK_DATA_0X3B bill;
}CURRENT_BILL;
static CURRENT_BILL s_current_bill[2] = {0};
void set_bill_ack_flag(uint8_t *trade_serial)
{
for(uint8_t i = 0;i < 2;i++){
if(!memcmp(s_current_bill[i].bill.trade_serial,trade_serial,TRADE_SERIAL_LENGTH)){
s_current_bill[i].ack_flag = 1;
}
}
}
/*
#define PACK_MONITOR_INTERVAL (30*1000)
static TimerHandle_t pack_monitor_timer = NULL;
static void pack_monitor_timer_proc(void)
{
if(pack_monitor_timer){
}
}
void init_pack_monitor_timer(void)
{
pack_monitor_timer = xTimerCreate("MONITOR_TIMER",PACK_MONITOR_INTERVAL,pdFALSE,( void * ) 0,pack_monitor_timer_proc);
}*/
void charger_to_server_0X01(void)
{
PACK_DATA_0X01 data = {0};
load_charger_serial(data.charger_serial);
data.charger_type = CHARGER_TYPE_AC;
data.gun_num = 2;
data.protocol_ver = SERVER_PROTOCOL_VER;
memcpy(data.software_ver,SW_VERSION,strlen(SW_VERSION));
data.net_conn_type = 0;
memset(data.sim,0x0,sizeof(data.sim));
for(uint8_t i = 0;i < 10;i++){
data.sim[i] = 0x01;
}
data.tele_factory = 0x04;
pack_and_send_server_data(FRAME_TYPE_0X01,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X01));
pack_serial++;
}
void charger_to_server_0X03(uint8_t gun_index,uint8_t gun_status)
{
PACK_DATA_0X03 data = {0};
load_charger_serial(data.charger_serial);
data.gun_index = gun_index;
data.gun_status = gun_status;
pack_and_send_server_data(FRAME_TYPE_0X03,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X03));
pack_serial++;
}
void charger_to_server_0X05(uint8_t num)
{
PACK_DATA_0X05 data = {0};
load_charger_serial(data.charger_serial);
data.fee_model_no = num;
pack_and_send_server_data(FRAME_TYPE_0X05,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X05));
pack_serial++;
}
void charger_to_server_0X09(void)
{
PACK_DATA_0X09 data = {0};
load_charger_serial(data.charger_serial);
pack_and_send_server_data(FRAME_TYPE_0X09,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X09));
pack_serial++;
}
uint8_t get_real_status_for_server(uint8_t idx)
{
uint8_t status = GUN_IDLE;
idx = idx - 1;
if(is_gun_fault_occur(idx)){
status = GUN_FAULT;
}else if((get_user_current_action(idx) == USER_ACTION_START_CHARGE)||(get_user_current_action(idx) == USER_ACTION_BOOK_CHARGE)){
status = GUN_CHARGING;
}else if(get_user_current_action(idx) == USER_ACTION_IDLE){
status = GUN_IDLE;
}else{
}
return status;
}
void charger_to_server_0X13(uint8_t gun_index)
{
PACK_DATA_0X13 data = {0};
CHARGING_GUN_CTRL *gun_ctrl = NULL;
gun_ctrl = get_charge_ctrl(gun_index-1);
load_charger_serial(data.charger_serial);
load_trade_serial(gun_index,data.trade_serial);
data.gun_index = gun_index;
data.status = get_real_status_for_server(gun_index);
data.gun_back = 0x02;
data.gun_is_insert = !!get_gun_status(gun_index-1);
data.out_voltage = gun_ctrl->voltage;//0.1V
data.charge_energy = TO_SERVER_ENERGY(gun_ctrl->charge_energy);//*100/3600;//0.0001kwh
data.out_current = gun_ctrl->current/10;//0.1A
data.charge_time = gun_ctrl->charge_time/60;
data.charge_money = gun_ctrl->charge_money*100;
pack_and_send_server_data(FRAME_TYPE_0X13,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X13));
pack_serial++;
}
void charger_to_server_0X19(uint8_t gun_index)
{
PACK_DATA_0X19 data = {0};
load_charger_serial(data.charger_serial);
load_trade_serial(gun_index,data.trade_serial);
pack_and_send_server_data(FRAME_TYPE_0X19,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X19));
pack_serial++;
}
void charger_to_server_0X31(uint8_t gun_index,uint32_t card_id)
{
PACK_DATA_0X31 data = {0};
load_charger_serial(data.charger_serial);
data.gun_index = gun_index;
memcpy(&data.card_no[4],&card_id,4);
data.start_mode = 0x01;
data.password_flag = 0x0;
//data.password
memset((uint8_t *)&data.vin,0x0,sizeof(data.vin));
pack_and_send_server_data(FRAME_TYPE_0X31,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X31));
pack_serial++;
}
void charger_to_server_0X33(uint8_t gun_index,uint8_t result,uint8_t err_code)
{
PACK_DATA_0X33 data = {0};
load_charger_serial(data.charger_serial);
load_trade_serial(gun_index,data.trade_serial);
data.gun_index = gun_index;
data.result = result;
data.err_code = err_code;
pack_and_send_server_data(FRAME_TYPE_0X33,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X33));
pack_serial++;
}
void charger_to_server_0X35(uint8_t gun_index)
{
PACK_DATA_0X35 data = {0};
load_charger_serial(data.charger_serial);
data.gun_index = gun_index;
pack_and_send_server_data(FRAME_TYPE_0X35,0,pack_serial,(uint8_t *)&data,sizeof(PACK_DATA_0X35));
pack_serial++;
}
void load_charge_bill(uint8_t gun_index,uint8_t stop_reason,PACK_DATA_0X3B *data)
{
CHARGING_GUN_CTRL *gun_ctrl = NULL;
FEE_MODEL *fee = NULL;
gun_ctrl = get_charge_ctrl(gun_index-1);
fee = gun_ctrl->fee_model;
load_trade_serial(gun_index,data->trade_serial);
load_charger_serial(data->charger_serial);
data->gun_index = gun_index;
memcpy(&data->phy_cardid[4],&gun_ctrl->card_id,4);
data->trade_flag = gun_ctrl->start_mode;
get_cp56time_from_sec(data->start_time,gun_ctrl->charge_start_time);
get_cp56time_from_sec(data->end_time,gun_ctrl->charge_time+gun_ctrl->charge_start_time);
get_cp56time_from_sec(data->trade_data,get_current_rtc_sec());
data->shark_price = fee->shark_fee_ratio + fee->shark_service_ratio;
data->shark_energy = TO_SERVER_ENERGY(gun_ctrl->type_energy[0]);//*100/3600;
data->shark_money = 1.0*gun_ctrl->type_energy[0]/3600/1000*data->shark_price;
data->loss_shark_energy = 0;
data->peak_price = fee->peak_fee_ratio + fee->peak_service_ratio;
data->peak_energy = TO_SERVER_ENERGY(gun_ctrl->type_energy[1]);//*100/3600;
data->peak_money = 1.0*gun_ctrl->type_energy[1]/3600/1000*data->peak_price;
data->loss_peak_energy = 0;
data->flat_price = fee->flat_fee_ratio + fee->flat_service_ratio;
data->flat_energy = TO_SERVER_ENERGY(gun_ctrl->type_energy[2]);//*100/3600;
data->flat_money = 1.0*gun_ctrl->type_energy[2]/3600/1000*data->flat_price;
data->loss_flat_energy = 0;
data->valley_price = fee->valley_fee_ratio + fee->valley_service_ratio;
data->valley_energy = TO_SERVER_ENERGY(gun_ctrl->type_energy[3]);//*100/3600;
data->valley_money = 1.0*gun_ctrl->type_energy[3]/3600/1000*data->valley_price;
data->loss_valley_energy = 0;
memset(data->meter_start_value,0,sizeof(data->meter_start_value));
data->meter_end_value[0] = (gun_ctrl->charge_energy*100/3600) & 0xFF;
data->meter_end_value[1] = (gun_ctrl->charge_energy*100/3600 >> 8) & 0xFF;
data->meter_end_value[2] = (gun_ctrl->charge_energy*100/3600 >> 16) & 0xFF;
data->meter_end_value[3] = (gun_ctrl->charge_energy*100/3600 >> 24) & 0xFF;
data->meter_end_value[4] = 0;
data->stop_reason = stop_reason;
data->total_energy = gun_ctrl->charge_energy*100/3600;
data->loss_total_energy = 0;
data->consumption = gun_ctrl->charge_money*100;
}
void charger_to_server_0x3B(uint8_t gun_index,uint32_t card_id,uint8_t trade_flag,uint8_t stop_reason)
{
PACK_DATA_0X3B