#include "common.h"
#include "drv_can.h"
#include "app_rs485_1.h"
#include "app_rs485_2.h"
#include "app_can.h"
#include "app.h"
#include "app_bms_1.h"

BMS_SELF_SEND_STATUS bms_1_self_ss;
BMS_SELF_CUR_VOL_STATUS bms_1_cur_vol_ss;
DELAY_COMMON bms_1_self_stimeout;


void Can_Bms_1_Cur_Vol_Self_Send_Check(CAN_FRAME*can_bms_1_frame)
{
	uint16_t len;
	uint8_t *buf = can_bms_1_frame->data;

	
	do
	{
		if(bms_1_cur_vol_ss.m_cur != sub_bms_info_1.packet_common.m_current/RESOLUTION_DELTA_50M)
			break;
		if(bms_1_cur_vol_ss.m_vol != sub_bms_info_1.packet_common.m_total_vol/RESOLUTION_DELTA_50M)
			break;
		
		return;
	}while(0);

	bms_1_cur_vol_ss.m_cur = sub_bms_info_1.packet_common.m_current/RESOLUTION_DELTA_10M;
	bms_1_cur_vol_ss.m_vol = sub_bms_info_1.packet_common.m_total_vol/RESOLUTION_DELTA_10M;
	len = sizeof(bms_1_cur_vol_ss);
	//
	can_bms_1_frame->head.rsp = FRAME_PT_NO_RSP;
	can_bms_1_frame->head.dest = CTR_ID;
	can_bms_1_frame->head.sour = SELF_ID;
	can_bms_1_frame->head.index = 1;
	can_bms_1_frame->head.total = (len - 1)/8 + 1;
	can_bms_1_frame->head.pro   =FRAME_PRO_D;
	//
	can_bms_1_frame->len = len;
	memcpy(buf,&bms_1_cur_vol_ss,len);
	
	bms_1_self_stimeout.set = 0;
	bms_1_self_stimeout.count = 0;
	g_event &= ~BMS_1_SELF_TIMEOUT_EVENT;
	//
	if(!Send_Data_Can(can_bms_1_frame,BMS_1_CUR_VOL_SELF))
	{
		//g_event |= BMS_1_RESEND_CMD_EVENT;
		//return;
	}

	//
	bms_1_cur_vol_ss.m_cur = sub_bms_info_1.packet_common.m_current/RESOLUTION_DELTA_50M;
	bms_1_cur_vol_ss.m_vol = sub_bms_info_1.packet_common.m_total_vol/RESOLUTION_DELTA_50M;
	
}

void Can_Bms_1_Self_Send_Check(CAN_FRAME*can_bms_1_frame)
{
	uint16_t len;
	uint8_t *buf = can_bms_1_frame->data;

	
	do
	{
		if(bms_1_self_ss.m_percent != sub_bms_info_1.packet_common.m_percent)
			break;
		if(bms_1_self_ss.charge_flag != sub_bms_info_1.packet_common.charge_flag)
			break;
		if(bms_1_self_ss.work_status != sub_bms_info_1.packet_common.work_status)
			break;
		//if(bms_1_self_ss.bms_status != sub_bms_info_1.packet_common.bms_status)
		//	break;
		//Can_Bms_1_Cur_Vol_Self_Send_Check(can_bms_1_frame);
		return;
	}while(0);

	bms_1_self_ss.m_percent = sub_bms_info_1.packet_common.m_percent;
	bms_1_self_ss.charge_flag = sub_bms_info_1.packet_common.charge_flag;
	bms_1_self_ss.work_status = sub_bms_info_1.packet_common.work_status;
	len = sizeof(bms_1_self_ss);
	//
	can_bms_1_frame->head.rsp = FRAME_PT_NEED_RSP;
	can_bms_1_frame->head.dest = CTR_ID;
	can_bms_1_frame->head.sour = SELF_ID;
	can_bms_1_frame->head.index = 1;
	can_bms_1_frame->head.total = (len - 1)/8 + 1;
	can_bms_1_frame->head.pro   =FRAME_PRO_D;
	//
	can_bms_1_frame->len = len;
	memcpy(buf,&bms_1_self_ss,len);
	
	bms_1_self_stimeout.set = 1;
	bms_1_self_stimeout.count = 0;
	//
	if(!Send_Data_Can(can_bms_1_frame,BMS_1_SELF))
	{
		//g_event |= BMS_1_RESEND_CMD_EVENT;
		//return;
	}
	
}
int8_t Handle_Can_Bms_1_CMD(CAN_FRAME*can_bms_1_frame)
{
	uint16_t key;

	memcpy(&key,can_bms_1_frame->data,sizeof(key));

	switch(key)
	{
		case KEY_BMS_1_COMMON:
			break;
		case KEY_BMS_1_READ_INFO:
			break;
		case KEY_BMS_1_READ_VER:
			break;
		case KEY_BMS_1_ALARM_TIMES:
			break;
		case KEY_BMS_1_ALARM_TIMES_1:
			break;
		case KEY_BMS_1_ALARM_TIMES_2:
			break;
		case KEY_BMS_1_CELL_VOL:
			break;
		case KEY_BMS_1_CELL_VOL_1:
			break;
		case KEY_BMS_1_CELL_VOL_2:
			break;
		case KEY_BMS_1_TEMP_OTHER:
			break;
		case KEY_BMS_1_SELF_UP:
			bms_1_self_stimeout.set = 0;
			return 0;	
		default:
			return 0;

	}
	
	return 1;
	
}

int8_t Rsp_Can_Bms_1_CMD(CAN_FRAME*can_bms_1_frame)
{
	uint16_t key,len;
	uint8_t *buf = can_bms_1_frame->data,*bt;
	uint16_t temp;

	memcpy(&key,can_bms_1_frame->data,sizeof(key));

	switch(key)
	{
		case KEY_BMS_1_COMMON:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			memcpy(&buf[len],&sub_bms_info_1.packet_common,sizeof(sub_bms_info_1.packet_common) - 1);
			if(sheng_yu_li_cheng.sy_yu_ji_ke_xing_shi_li_cheng)
			{
				if(sheng_yu_li_cheng.sy_percent_total == sub_bms_info_1.packet_common.m_percent)
					buf[3] = sheng_yu_li_cheng.sy_yu_ji_ke_xing_shi_li_cheng;
				else
					#if 0
					//bang zi ce shi start
					buf[3] = sheng_yu_li_cheng.sy_yu_ji_ke_xing_shi_li_cheng*sub_bms_info_1.packet_common.m_percent/50;
					//bang zi ce shi start
					#else
					{
						if(Is_Sub_BMS_1_Normal())
						{
							temp = (uint16_t)(sub_bms_info_1.packet_common.m_percent*sheng_yu_li_cheng.sy_ss_parm.neng_hao_bi*10/1000);
							if(temp%10 >= 5)
								buf[3] = temp/10 + 1;
							else
								buf[3] = temp/10;
							
						}
						else
							buf[3] = 0;
					}
					#endif
			}
			len += sizeof(sub_bms_info_1.packet_common) - 1;
			break;
		case KEY_BMS_1_READ_INFO:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			memcpy(&buf[len],sub_bms_info_1.bat_dev_info.sn,sizeof(sub_bms_info_1.bat_dev_info.sn));
			len += sizeof(sub_bms_info_1.bat_dev_info.sn);
			break;	
		case KEY_BMS_1_READ_VER:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			memcpy(&buf[len],sub_bms_info_1.bat_dev_info.soft_ver,sizeof(sub_bms_info_1.bat_dev_info.soft_ver));
			len += sizeof(sub_bms_info_1.bat_dev_info.soft_ver);
			break;
		case KEY_BMS_1_ALARM_TIMES:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			//memcpy(&buf[len],&sub_bms_info_1.bat_times,sizeof(sub_bms_info_1.bat_times));
			//len += sizeof(sub_bms_info_1.bat_times);
			bt = (uint8_t*)(&sub_bms_info_1.bat_times);
			memcpy(&buf[len],bt,14);
			len += 14;
			break;
		case KEY_BMS_1_ALARM_TIMES_1:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			bt = (uint8_t*)(&sub_bms_info_1.bat_times);
			memcpy(&buf[len],&bt[14],16);
			len += 16;
			break;
		case KEY_BMS_1_ALARM_TIMES_2:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			bt = (uint8_t*)(&sub_bms_info_1.bat_times);
			memcpy(&buf[len],&bt[30],12);
			len += 12;
			break;
		case KEY_BMS_1_CELL_VOL:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			bt = (uint8_t*)(&sub_bms_info_1.cell_vol);
			memcpy(&buf[len],bt,13);
			len += 13;
			break;
		case KEY_BMS_1_CELL_VOL_1:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			buf[len++] = 0;
			bt = (uint8_t*)(&sub_bms_info_1.cell_vol);	
			memcpy(&buf[len],&bt[13],12);
			len += 12;
			break;
		case KEY_BMS_1_CELL_VOL_2:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			buf[len++] = 0;
			bt = (uint8_t*)(&sub_bms_info_1.cell_vol);
			memcpy(&buf[len],&bt[25],6);
			len += 6;
			//
			memset(&buf[len],0x00,6);
			len += 6;
			break;
		case KEY_BMS_1_TEMP_OTHER:
			if((can_bms_1_frame->head.rsp != FRAME_PT_NEED_RSP) || bms_1_self_stimeout.set)
				return 1;
			len = 2;
			buf[len++] = 0;
			bt = (uint8_t*)(&sub_bms_info_1.temp_other);
			memcpy(&buf[len],bt,5);
			len += 5;
			break;
		case KEY_BMS_1_SELF_UP:
			return 0;
		default:
			return 0;
	}
	//
	can_bms_1_frame->head.rsp = FRAME_PT_RSP;
	can_bms_1_frame->head.dest = can_bms_1_frame->head.sour;
	can_bms_1_frame->head.sour = SELF_ID;
	can_bms_1_frame->head.index = 1;
	can_bms_1_frame->head.total = (len - 1)/8 + 1;
	can_bms_1_frame->head.pro   =FRAME_PRO_D;
	//
	can_bms_1_frame->len = len;

	//
	if(!Send_Data_Can(can_bms_1_frame,BMS_1_RSP))
	{
		g_event |= BMS_1_RESEND_CMD_EVENT;
		return 0;
	}
	
	return 1;

}


void Bms_1_Self_Send_Timeout(void)
{
	if(bms_1_self_stimeout.set)
	{
		++bms_1_self_stimeout.count;
		if(bms_1_self_stimeout.count >= 300)
		{
			memset(&bms_1_self_stimeout,0x00,sizeof(bms_1_self_stimeout));
			g_event |= BMS_1_SELF_TIMEOUT_EVENT;
		}
		else if(bms_1_self_stimeout.count == 200)
			g_event |= BMS_1_SELF_TIMEOUT_EVENT;
		else if(bms_1_self_stimeout.count == 100)
			g_event |= BMS_1_SELF_TIMEOUT_EVENT;
	}
	else
		bms_1_self_stimeout.count = 0;
	
}

#define RS485_PRINT_ENABLE   (0)
void RS485_Print(uint8_t dest,uint8_t * dbuf,uint16_t dbuf_len)
{

#if RS485_PRINT_ENABLE
	CAN_FRAME can_bms_1_frame;
	
	can_bms_1_frame.head.rsp = FRAME_PT_NO_RSP;
	can_bms_1_frame.head.dest = dest;
	can_bms_1_frame.head.sour = SELF_ID;
	can_bms_1_frame.head.index = 1;
	can_bms_1_frame.head.total = (dbuf_len - 1)/8 + 1;
	can_bms_1_frame.head.pro   =FRAME_PRO_D;
	//
	can_bms_1_frame.len = dbuf_len;
	memcpy(can_bms_1_frame.data,dbuf,dbuf_len);

	//
	if(!Send_Data_Can(&can_bms_1_frame,OTHER_SEND))
	{
		//g_event |= BMS_1_RESEND_CMD_EVENT;
		//return;
	}
#endif

}
