#include "includes.h"

/*******************************************************************************
* Function Name  : BLV_RS485_EnrgyMonitor_Data_For_Logic_Init 
* Description    : 能耗监测 初始化信息来自逻辑文件
*******************************************************************************/
void BLV_RS485_EnrgyMonitor_Data_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	RS485_ENERGYMONITOR_INFO 	Rs485EnergyInfo;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485EnergyInfo,0,sizeof(RS485_ENERGYMONITOR_INFO));
	
	BUS_Public.addr = dev_info->addr;	  				//设备地址
	BUS_Public.type = dev_info->type;	  				//设备类型
	BUS_Public.port = dev_info->port;	  				//设备发送端口
	BUS_Public.baud = dev_info->baud;			      	//设备发送波特率
	BUS_Public.retry_num = dev_info->retry;		  		//设备数据重发次数
	BUS_Public.wait_time = dev_info->writ_time;		  	//设备数据发送等待回复时间 - 100ms
	BUS_Public.Protocol = dev_info->version;
	
	Rs485EnergyInfo.DevOfflineLast = DEV_IS_ONLINE;	//在线初始化
	Rs485EnergyInfo.DevOffline = Rs485EnergyInfo.DevOfflineLast;
	
	BUS_Public.polling_cf = (uint32_t)&BLV_RS485_Energy_Cycle_Dis;				//对应的端口任务中
	BUS_Public.processing_cf = (uint32_t)&BLV_RS485_Energy_Data_Processing;		
	
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = Dev_Energy_Output_Ctrl;
	
	switch(dev_info->port)
	{
		case Active_Port:   //主动端口
			
			Rs485EnergyInfo.send_flag |= BLV_Energy_SetInfo_Flag;
			//Rs485EnergyInfo.send_flag |= BLV_Energy_ClearStat_Flag;
			Rs485EnergyInfo.send_flag |= BLV_Energy_ReadVer_Flag;
			Rs485EnergyInfo.send_flag |= BLV_Energy_Inquire_Flag;
			Rs485EnergyInfo.periodic_time = 15000;			//定期上报周期
			Rs485EnergyInfo.periodic_enable = 0x01;			//定期上报使能
			
			BUS_Public.port = Active_Port;		//设备发送端口
			Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485EnergyInfo,sizeof(RS485_ENERGYMONITOR_INFO));
			Act485_Info.device_num += 1;
			break;  
		case Polling_Port:   //轮询端口
			Rs485EnergyInfo.send_flag |= BLV_Energy_SetInfo_Flag;
			Rs485EnergyInfo.send_flag |= BLV_Energy_ReadVer_Flag;
			Rs485EnergyInfo.periodic_time = 15000;			//定期上报周期
			Rs485EnergyInfo.periodic_enable = 0x00;			//定期上报使能
			
			BUS_Public.port = Polling_Port;		//设备发送端口
			Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485EnergyInfo,sizeof(RS485_ENERGYMONITOR_INFO));   
			Poll485_Info.device_num += 1;	
			break;
		case Bus_port:    //总线端口
			break;
		default:
			break;
	}
	
}

uint8_t BLV_Energy_Inquire_SendData(Device_Public_Information_G *BUS_Public, RS485_ENERGYMONITOR_INFO *Rs485EnergyInfo)
{
	uint8_t send_buff[20];
	uint8_t send_len = 0;
	memset(send_buff,0,20);
	
	send_buff[send_len++] = BLV_Energy_DataHeard;						//数据头
	
	if(Rs485EnergyInfo->DevSendCnt == 0x00)
	{
		Rs485EnergyInfo->DevSendSN++;
		if(Rs485EnergyInfo->DevSendSN > 0x0F)	Rs485EnergyInfo->DevSendSN = 0x00;
		send_buff[send_len++] = (Rs485EnergyInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[send_len] = (Rs485EnergyInfo->DevSendSN & 0x0F);
		send_buff[send_len++] |= BUS_Retry_Flag;
	}
	
	send_buff[send_len++] = BLV_Energy_Bus_Type;			//设备类型
	send_buff[send_len++] = BUS_Public->addr;			//设备地址 - C5固定设备地址为0x01
	send_buff[send_len++] = 0x00;						//数据长度
	send_buff[send_len++] = 0x00;						//校验
	send_buff[send_len++] = BLV_Energy_Inquire_CMD;		//查询命令
	send_buff[send_len++] = BLV_Energy_DataEnd;						//数据尾
	
	send_buff[0x04] = send_len;							//本次发送数据长度

	send_buff[0x05] = CheckSum(send_buff, send_len);	//计算校验

	MCU485_SendString(BUS_Public->port,send_buff, send_len);
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLV_Energy_Inquire: ",send_buff,send_len);
	
	/*通讯统计记录*/
	Rs485EnergyInfo->inquire_tick = SysTick_1ms;
	BLV_Communication_Record(&Rs485EnergyInfo->comm_record,0x01,0x00);
	
	
	
	return 0x00;
}

uint8_t BLV_Energy_SetInfo_SendData(Device_Public_Information_G *BUS_Public, RS485_ENERGYMONITOR_INFO *Rs485EnergyInfo)
{
	uint8_t send_buff[20];
	uint8_t send_len = 0;
	memset(send_buff,0,20);
	
	send_buff[send_len++] = BLV_Energy_DataHeard;						//数据头
	
	if(Rs485EnergyInfo->DevSendCnt == 0x00)
	{
		Rs485EnergyInfo->DevSendSN++;
		if(Rs485EnergyInfo->DevSendSN > 0x0F)	Rs485EnergyInfo->DevSendSN = 0x00;
		send_buff[send_len++] = (Rs485EnergyInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[send_len] = (Rs485EnergyInfo->DevSendSN & 0x0F);
		send_buff[send_len++] |= BUS_Retry_Flag;
	}
	
	send_buff[send_len++] = BLV_Energy_Bus_Type;		//设备类型
	send_buff[send_len++] = BUS_Public->addr;			//设备地址 - C5固定设备地址为0x01
	send_buff[send_len++] = 0x00;						//数据长度
	send_buff[send_len++] = 0x00;						//校验
	send_buff[send_len++] = BLV_Energy_SetInfo_CMD;		//设置参数命令
	
	send_buff[send_len++] = 0x02;		//控制位 - 目前只设置上报周期
	
	send_buff[send_len++] = 0x00;		//采集周期 - 暂不设置 需要时增加
	send_buff[send_len++] = 0x00;
	send_buff[send_len++] = 0x00;
	send_buff[send_len++] = 0x00;
	
	send_buff[send_len++] = Rs485EnergyInfo->periodic_enable;			//上报周期使能位
	send_buff[send_len++] = Rs485EnergyInfo->periodic_time & 0xFF;		//设置定期上报时间
	send_buff[send_len++] = (Rs485EnergyInfo->periodic_time >> 8 ) & 0xFF;
	send_buff[send_len++] = (Rs485EnergyInfo->periodic_time >> 16 ) & 0xFF;
	send_buff[send_len++] = (Rs485EnergyInfo->periodic_time >> 24 ) & 0xFF;
	
	send_buff[send_len++] = BLV_Energy_DataEnd;						//数据尾
	
	send_buff[0x04] = send_len;							//本次发送数据长度

	send_buff[0x05] = CheckSum(send_buff, send_len);	//计算校验

	MCU485_SendString(BUS_Public->port,send_buff, send_len);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,send_buff,send_len);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLV_Energy_SetInfo: ",send_buff,send_len);
	
	/*通讯统计记录*/
	Rs485EnergyInfo->inquire_tick = SysTick_1ms;
	BLV_Communication_Record(&Rs485EnergyInfo->comm_record,0x01,0x00);
	
	return 0x00;
}

uint8_t BLV_Energy_ReadVer_SendData(Device_Public_Information_G *BUS_Public, RS485_ENERGYMONITOR_INFO *Rs485EnergyInfo)
{
	uint8_t send_buff[20];
	uint8_t send_len = 0;
	memset(send_buff,0,20);
	
	send_buff[send_len++] = BLV_Energy_DataHeard;						//数据头
	
	if(Rs485EnergyInfo->DevSendCnt == 0x00)
	{
		Rs485EnergyInfo->DevSendSN++;
		if(Rs485EnergyInfo->DevSendSN > 0x0F)	Rs485EnergyInfo->DevSendSN = 0x00;
		send_buff[send_len++] = (Rs485EnergyInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[send_len] = (Rs485EnergyInfo->DevSendSN & 0x0F);
		send_buff[send_len++] |= BUS_Retry_Flag;
	}
	
	send_buff[send_len++] = BLV_Energy_Bus_Type;			//设备类型
	send_buff[send_len++] = BUS_Public->addr;			//设备地址 - C5固定设备地址为0x01
	send_buff[send_len++] = 0x00;						//数据长度
	send_buff[send_len++] = 0x00;						//校验
	send_buff[send_len++] = BLV_Energy_ReadVer_CMD;		//读取软件版本命令
	send_buff[send_len++] = BLV_Energy_DataEnd;						//数据尾
	
	send_buff[0x04] = send_len;							//本次发送数据长度

	send_buff[0x05] = CheckSum(send_buff, send_len);	//计算校验

	MCU485_SendString(BUS_Public->port,send_buff, send_len);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLV_Energy_ReadVer: ",send_buff,send_len);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,send_buff,send_len);
	
	/*通讯统计记录*/
	Rs485EnergyInfo->inquire_tick = SysTick_1ms;
	BLV_Communication_Record(&Rs485EnergyInfo->comm_record,0x01,0x00);
	
	return 0;
}

uint8_t BLV_Energy_ClearStat_SendData(Device_Public_Information_G *BUS_Public, RS485_ENERGYMONITOR_INFO *Rs485EnergyInfo)
{
	uint8_t send_buff[20];
	uint8_t send_len = 0;
	memset(send_buff,0,20);
	
	send_buff[send_len++] = BLV_Energy_DataHeard;						//数据头
	
	if(Rs485EnergyInfo->DevSendCnt == 0x00)
	{
		Rs485EnergyInfo->DevSendSN++;
		if(Rs485EnergyInfo->DevSendSN > 0x0F)	Rs485EnergyInfo->DevSendSN = 0x00;
		send_buff[send_len++] = (Rs485EnergyInfo->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[send_len] = (Rs485EnergyInfo->DevSendSN & 0x0F);
		send_buff[send_len++] |= BUS_Retry_Flag;
	}
	
	send_buff[send_len++] = BLV_Energy_Bus_Type;			//设备类型
	send_buff[send_len++] = BUS_Public->addr;			//设备地址 - C5固定设备地址为0x01
	send_buff[send_len++] = 0x00;						//数据长度
	send_buff[send_len++] = 0x00;						//校验
	send_buff[send_len++] = BLV_Energy_ClearStat_CMD;		//清除周期统计命令
	send_buff[send_len++] = BLV_Energy_DataEnd;						//数据尾
	
	send_buff[0x04] = send_len;							//本次发送数据长度

	send_buff[0x05] = CheckSum(send_buff, send_len);	//计算校验

	MCU485_SendString(BUS_Public->port,send_buff, send_len);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,send_buff,send_len);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLV_Energy_ClearStat: ",send_buff,send_len);
	
	/*通讯统计记录*/
	Rs485EnergyInfo->inquire_tick = SysTick_1ms;
	BLV_Communication_Record(&Rs485EnergyInfo->comm_record,0x01,0x00);
	
	return 0;
}

uint8_t BLV_Energy_PeriodicReport_Reply(Device_Public_Information_G *BUS_Public, uint8_t reply_sn)
{
	uint8_t send_buff[20];
	uint8_t send_len = 0;
	memset(send_buff,0,20);
	
	send_buff[send_len++] = BLV_Energy_DataHeard;						//数据头
	
	send_buff[send_len++] = reply_sn;	//设置发送序号

	send_buff[send_len++] = BLV_Energy_Bus_Type;			//设备类型
	send_buff[send_len++] = BUS_Public->addr;			//设备地址 - C5固定设备地址为0x01
	send_buff[send_len++] = 0x00;						//数据长度
	send_buff[send_len++] = 0x00;						//校验
	send_buff[send_len++] = BLV_Energy_PeriodicReport_CMD;		//清除周期统计命令
	send_buff[send_len++] = BLV_Energy_DataEnd;						//数据尾
	
	send_buff[0x04] = send_len;							//本次发送数据长度

	send_buff[0x05] = CheckSum(send_buff, send_len);	//计算校验

	MCU485_SendString(BUS_Public->port,send_buff, send_len);
	
	return 0;
}

/*******************************************************************************
* Function Name  : BLV_RS485_Energy_Cycle_Dis 
* Description    : 能耗监测 轮询发送函数
* Return         :
	0x00：没问题
	0x01：错误
*******************************************************************************/
uint8_t BLV_RS485_Energy_Cycle_Dis(uint32_t dev_addr)
{
	uint8_t ret = RS485OCCUPYNOTIME;
	Device_Public_Information_G BUS_Public;
	RS485_ENERGYMONITOR_INFO 	Rs485EnergyInfo;
	uint8_t i = 0;
	uint32_t send_data_flag = 0;
	
	uint8_t keepflag = 0x00;
	
	if(dev_addr == NULL)	return 0;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485EnergyInfo,sizeof(RS485_ENERGYMONITOR_INFO),dev_addr+Dev_Privately);
	
	for(i = 0;i < BLV_Energy_Send_Flag_Max; i++)
	{
		if( (Rs485EnergyInfo.send_flag & (0x01 << i)) != 0x00)
		{
			send_data_flag = 0x01 << i;
			break;
		}
	}
	
	if(send_data_flag != 0x00)
	{
		if(Rs485EnergyInfo.DevSendCnt > BUS_Public.retry_num)
		{
			if(Rs485EnergyInfo.DevOffline != DEV_IS_OFFLINE)	//第一次离线记录
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy LogInfo_Device_Offline...........");
				LOG_Device_Online_Record(DEV_RS485_CARD,SRAM_Read_Byte(dev_addr+Dev_Addr),LogInfo_Device_Offline);	//记录设备离线
			}
			
			Rs485EnergyInfo.DevOffline = DEV_IS_OFFLINE;   //判为离线
			if(Rs485EnergyInfo.DevOffline != Rs485EnergyInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				Rs485EnergyInfo.DevOfflineLast = Rs485EnergyInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
		}else {
			switch(send_data_flag)
			{
				case BLV_Energy_SetInfo_Flag:
					BLV_Energy_SetInfo_SendData(&BUS_Public,&Rs485EnergyInfo);
					break;
				case BLV_Energy_ClearStat_Flag:
					BLV_Energy_ClearStat_SendData(&BUS_Public,&Rs485EnergyInfo);
					break;
				case BLV_Energy_ReadVer_Flag:
					BLV_Energy_ReadVer_SendData(&BUS_Public,&Rs485EnergyInfo);
					break;
				case BLV_Energy_Inquire_Flag:
					BLV_Energy_Inquire_SendData(&BUS_Public,&Rs485EnergyInfo);
					break;
			}
			
			ret = RS485OCCUPYTIME;
			Rs485EnergyInfo.DevSendCnt++;   //发送次数累计
		}
		
		keepflag = 0x01;
	}else {
		if(BUS_Public.port == Polling_Port)
		{
			BLV_Energy_Inquire_SendData(&BUS_Public,&Rs485EnergyInfo);
			ret = RS485OCCUPYTIME;
			
			keepflag = 0x01;
		}
	}
	
	if(keepflag == 0x01)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485EnergyInfo, sizeof(RS485_ENERGYMONITOR_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485EnergyInfo,sizeof(RS485_ENERGYMONITOR_INFO),dev_addr+Dev_Privately);
	}
	
	return ret;
}

uint8_t BLV_RS485_Energy_Data_Processing(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	uint8_t rev = 0x01;
	uint16_t deal_len = len;
	uint8_t deal_buff[BLV_Energy_RecvData_LenMax];
	uint32_t temp_value = 0,temp_flag = 0x00;
	
	if((dev_addr == NULL) || (data_addr == 0x00) )	return rev;
	
	Device_Public_Information_G BUS_Public;
	RS485_ENERGYMONITOR_INFO Rs485EnergyInfo;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);	
	
	/*1、先判断数据长度对不对，然后在进行数据校验*/
	if( (deal_len > BLV_Energy_RecvData_LenMax) || ( deal_len < BLV_Energy_RecvData_LenMin) )	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy Check_Len Fail - %d!",deal_len);
		return rev;	
	}
	
	memset(deal_buff,0,sizeof(deal_buff));
	SRAM_DMA_Read_Buff(deal_buff,deal_len,data_addr);		//读取处理数据长度
	
	/*校验包头 包尾*/
	if((deal_buff[0] != BLV_Energy_DataHeard) || (deal_buff[deal_len - 1] != BLV_Energy_DataEnd))
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Head Err Not Energy Data");
		return rev;
	}
	
	/*校验数据包长*/
	if(deal_buff[4] != deal_len)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy Data Lens %d Err",deal_buff[4]);
		return rev;
	}
	
	/*校验设备类型 - Dev_Energy_Monitor*/
	if(deal_buff[2] != BLV_Energy_Bus_Type)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy Type %d Err",deal_buff[2]);
		return rev;
	}
	
	/*校验设备地址*/
	if(deal_buff[3] != BUS_Public.addr)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy Addr %d Err",deal_buff[3]);
		return rev;
	}
	
	/*校验校验位的校验和取反*/
    if( deal_buff[5] != CheckSum_Overlook_Check(deal_buff,deal_len,5) )
    {
        Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy Check %02x Err",deal_buff[5]);
        return rev;
    }
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485EnergyInfo,sizeof(RS485_ENERGYMONITOR_INFO),dev_addr+Dev_Privately);
	
	switch(deal_buff[6])
	{
		
		case BLV_Energy_PeriodicReport_CMD:
		case BLV_Energy_SetInfo_CMD:
		case BLV_Energy_ReadVer_CMD:
		case BLV_Energy_ClearStat_CMD:
			
			LOG_Device_COMM_Control_Reply_Record(BUS_Public.port,BUS_Public.baud,deal_buff,deal_len);		//记录日志
		case BLV_Energy_Inquire_CMD:
			rev = 0x00;
			if(Rs485EnergyInfo.DevOffline != DEV_IS_ONLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy DEV_IS_ONLINE");
				LOG_Device_Online_Record(Dev_485_BLE_Music,BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
			}
			Rs485EnergyInfo.DevSendCnt = 0x00;
			Rs485EnergyInfo.DevOffline = DEV_IS_ONLINE;    //设备在线
			if(Rs485EnergyInfo.DevOffline != Rs485EnergyInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				Rs485EnergyInfo.DevOfflineLast = Rs485EnergyInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
			}
			BLV_Communication_Record(&Rs485EnergyInfo.comm_record,0x02,0x01);		//记录通讯成功
			break;
	}
	
	switch(deal_buff[6])
	{
		case BLV_Energy_PeriodicReport_CMD:
			//20250829 - 目前只有一个采样通道
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy PeriodicReport process");
			//Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Energy PeriodicReport process",deal_buff,deal_len);
            temp_flag = deal_buff[7];
            
            if( (temp_flag&0x01) != 0x00 )
            {
                temp_value = deal_buff[9];
                temp_value <<= 8;
                temp_value |= deal_buff[8];			
                Rs485EnergyInfo.voltage = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy voltage %dmV",Rs485EnergyInfo.voltage*10);
            
                temp_value = deal_buff[11];
                temp_value <<= 8;
                temp_value |= deal_buff[10];			
                Rs485EnergyInfo.current = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy current %dmA",Rs485EnergyInfo.current*10);
            
                temp_value = deal_buff[15];
                temp_value <<= 8;
                temp_value |= deal_buff[14];	
                temp_value <<= 8;
                temp_value |= deal_buff[13];
                temp_value <<= 8;
                temp_value |= deal_buff[12];
                Rs485EnergyInfo.active_power = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy active_power %dmW",Rs485EnergyInfo.active_power);
            
                temp_value = deal_buff[19];
                temp_value <<= 8;
                temp_value |= deal_buff[18];	
                temp_value <<= 8;
                temp_value |= deal_buff[17];
                temp_value <<= 8;
                temp_value |= deal_buff[16];		
                Rs485EnergyInfo.phase_energy = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy phase_energy %dWh",Rs485EnergyInfo.phase_energy);
                
                temp_value = deal_buff[23];
                temp_value <<= 8;
                temp_value |= deal_buff[22];	
                temp_value <<= 8;
                temp_value |= deal_buff[21];
                temp_value <<= 8;
                temp_value |= deal_buff[20];			
                Rs485EnergyInfo.total_energy = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy total_energy %dWh",Rs485EnergyInfo.total_energy);
			}
            
            if( (temp_flag&0x02) != 0x00 )
            {
                temp_value = deal_buff[25];
                temp_value <<= 8;
                temp_value |= deal_buff[24];			
                Rs485EnergyInfo.voltage_B = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy voltageB %dmV",Rs485EnergyInfo.voltage_B*10);
            
                temp_value = deal_buff[27];
                temp_value <<= 8;
                temp_value |= deal_buff[26];			
                Rs485EnergyInfo.current_B = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy currentB %dmA",Rs485EnergyInfo.current_B*10);
            
                temp_value = deal_buff[31];
                temp_value <<= 8;
                temp_value |= deal_buff[30];	
                temp_value <<= 8;
                temp_value |= deal_buff[29];
                temp_value <<= 8;
                temp_value |= deal_buff[28];
                Rs485EnergyInfo.active_power_B = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy active_powerB %dmW",Rs485EnergyInfo.active_power_B);
            
                temp_value = deal_buff[35];
                temp_value <<= 8;
                temp_value |= deal_buff[34];	
                temp_value <<= 8;
                temp_value |= deal_buff[33];
                temp_value <<= 8;
                temp_value |= deal_buff[32];		
                Rs485EnergyInfo.phase_energy_B = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy phase_energyB %dWh",Rs485EnergyInfo.phase_energy_B);
                
                temp_value = deal_buff[39];
                temp_value <<= 8;
                temp_value |= deal_buff[38];	
                temp_value <<= 8;
                temp_value |= deal_buff[37];
                temp_value <<= 8;
                temp_value |= deal_buff[36];			
                Rs485EnergyInfo.total_energy_B = temp_value; 
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy total_energyB %dWh",Rs485EnergyInfo.total_energy_B);
            }
            
			BLV_Energy_PeriodicReport_Reply(&BUS_Public,deal_buff[1]);
			
			break;
		case BLV_Energy_Inquire_CMD:
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy Inquire process");
			
            temp_flag = deal_buff[7];
            
            if( (temp_flag&0x01) != 0x00 )
            {
                temp_value = deal_buff[9];
                temp_value <<= 8;
                temp_value |= deal_buff[8];			
                Rs485EnergyInfo.voltage = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy voltage %dmV",Rs485EnergyInfo.voltage*10);
            
                temp_value = deal_buff[11];
                temp_value <<= 8;
                temp_value |= deal_buff[10];			
                Rs485EnergyInfo.current = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy current %dmA",Rs485EnergyInfo.current*10);
            
                temp_value = deal_buff[15];
                temp_value <<= 8;
                temp_value |= deal_buff[14];	
                temp_value <<= 8;
                temp_value |= deal_buff[13];
                temp_value <<= 8;
                temp_value |= deal_buff[12];
                Rs485EnergyInfo.active_power = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy active_power %dmW",Rs485EnergyInfo.active_power);
            
                temp_value = deal_buff[19];
                temp_value <<= 8;
                temp_value |= deal_buff[18];	
                temp_value <<= 8;
                temp_value |= deal_buff[17];
                temp_value <<= 8;
                temp_value |= deal_buff[16];		
                Rs485EnergyInfo.phase_energy = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy phase_energy %dWh",Rs485EnergyInfo.phase_energy);
                
                temp_value = deal_buff[23];
                temp_value <<= 8;
                temp_value |= deal_buff[22];	
                temp_value <<= 8;
                temp_value |= deal_buff[21];
                temp_value <<= 8;
                temp_value |= deal_buff[20];			
                Rs485EnergyInfo.total_energy = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy total_energy %dWh",Rs485EnergyInfo.total_energy);
			}
            
            if( (temp_flag&0x02) != 0x00 )
            {
                temp_value = deal_buff[25];
                temp_value <<= 8;
                temp_value |= deal_buff[24];			
                Rs485EnergyInfo.voltage_B = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy voltage %dmV",Rs485EnergyInfo.voltage*10);
            
                temp_value = deal_buff[27];
                temp_value <<= 8;
                temp_value |= deal_buff[26];			
                Rs485EnergyInfo.current_B = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy current %dmA",Rs485EnergyInfo.current*10);
            
                temp_value = deal_buff[31];
                temp_value <<= 8;
                temp_value |= deal_buff[30];	
                temp_value <<= 8;
                temp_value |= deal_buff[29];
                temp_value <<= 8;
                temp_value |= deal_buff[28];
                Rs485EnergyInfo.active_power_B = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy active_power %dmW",Rs485EnergyInfo.active_power);
            
                temp_value = deal_buff[35];
                temp_value <<= 8;
                temp_value |= deal_buff[34];	
                temp_value <<= 8;
                temp_value |= deal_buff[33];
                temp_value <<= 8;
                temp_value |= deal_buff[32];		
                Rs485EnergyInfo.phase_energy_B = temp_value;
                //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLE Energy phase_energy %dWh",Rs485EnergyInfo.phase_energy);
                
                temp_value = deal_buff[39];
                temp_value <<= 8;
                temp_value |= deal_buff[38];	
                temp_value <<= 8;
                temp_value |= deal_buff[37];
                temp_value <<= 8;
                temp_value |= deal_buff[36];			
                Rs485EnergyInfo.total_energy_B = temp_value; 
            }
			
			Rs485EnergyInfo.send_flag &= ~BLV_Energy_Inquire_Flag;
			break;
		case BLV_Energy_SetInfo_CMD:
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy SetInfo process");
			//Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Energy SetInfo process",deal_buff,deal_len);
			Rs485EnergyInfo.send_flag &= ~BLV_Energy_SetInfo_Flag;
			break;
		case BLV_Energy_ReadVer_CMD:
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy ReadVer process %d-%d-%d",deal_buff[8],deal_buff[9],deal_buff[10]);
			Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Energy ReadVer process",deal_buff,deal_len);
			Rs485EnergyInfo.send_flag &= ~BLV_Energy_ReadVer_Flag;
			break;
		case BLV_Energy_ClearStat_CMD:
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy ClearStat process");
			//Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Energy ClearStat process",deal_buff,deal_len);
			Rs485EnergyInfo.send_flag &= ~BLV_Energy_ClearStat_Flag;
			break;
	}
	
	
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485EnergyInfo, sizeof(RS485_ENERGYMONITOR_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&Rs485EnergyInfo,sizeof(RS485_ENERGYMONITOR_INFO),dev_addr+Dev_Privately);
	
	return rev;
}

void Dev_Energy_Output_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t DevAddr, uint16_t DevOutputLoop, uint16_t DevOutputType)
{
	Device_Public_Information_G BUS_Public;
	RS485_ENERGYMONITOR_INFO Rs485EnergyInfo; 
	
	uint8_t keepflag = 0x00;
	
	uint8_t ctrlmode = 0x00;
	uint8_t ctrlstate = 0x00;
	
	if(DevAddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485EnergyInfo,sizeof(RS485_ENERGYMONITOR_INFO),DevAddr+Dev_Privately);/*拷贝设备私有信息*/
	
	ctrlmode = DevOutputType & 0xff;
	ctrlstate = (DevOutputType >> 8) & 0xff;
	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy Ctrl Mode:%02X  State:%02X",ctrlmode,ctrlstate);
	
	switch(ctrlmode)
	{
		case 0x01:
			//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Energy Ctrl ClearStat");
			Rs485EnergyInfo.send_flag |= BLV_Energy_ClearStat_Flag;
		
			keepflag = 0x01;
			break;
	}
	
	if(keepflag == 0x01)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485EnergyInfo, sizeof(RS485_ENERGYMONITOR_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485EnergyInfo,sizeof(RS485_ENERGYMONITOR_INFO),DevAddr+Dev_Privately);
	}

}












