/**
  ******************************************************************************
  * @file    BLV_Nor_Dev_CarbonFun.c
  * @author  BLW RCU  development Team
  * @version V1.0.0
  * @date    2024年12月13日
  * @brief   碳达人   
  ******************************************************************************
  */
  
#include "includes.h"

#include "BLV_Nor_DevFun.h"

/*本地函数申明开始*/
void Dev_Carbon_Dis(uint32_t DevAddr);

uint16_t Carbon_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop);
void BLW_Carbon_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start);
uint8_t Dev_Carbon_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType);
/*本地函数申明结束*/


/*******************************************************************************
* Function Name  : BLV_Nor_Dev_Carbon_For_Logic_Init 
* Description    : 碳达人初始化信息来自逻辑文件
* Parameter      : 逻辑数据
*******************************************************************************/
void BLV_Nor_Dev_Carbon_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	NOR_CARBON_INFO DevCarbonInfo;       //服务信息局部变量
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&DevCarbonInfo,0,sizeof(NOR_CARBON_INFO));
	
	BUS_Public.addr = dev_info->addr;			//设备地址
	BUS_Public.type = dev_info->type;		    //设备类型
	BUS_Public.port = dev_info->type;			//设备发送端口
	BUS_Public.baud = 0000;			            //设备发送波特率

	DevCarbonInfo.CarbonLoopValidNum = dev_info->output_num;
	
	DevCarbonInfo.DimValue = DevActionGlobal.DimGlobalValue;
	
	DevCarbonInfo.EnergySaved = 0x02;			//碳达人状态 - 上电默认为关
	
	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_Carbon_Dis;                 
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_Carbon_InType_Get;        
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_Carbon_Control_State;        
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Carbon_Loop_State;   

	SRAM_Write_Byte(DevCarbonInfo.EnergySaved,SRAM_UDP_Report_CarbonSatet);			//给UDP 上报取电状态使用

	
	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&DevCarbonInfo,sizeof(NOR_CARBON_INFO));
}


/**
  * @name  碳达人回路状态得到
  * @param  
  *    devaddr             当前设备的地址
  *    DevOutputLoop       需要查找的回路 从0开始
  * @retval 
  *    ret                 回路状态返回    1 开   2关
  * @brief
  */
uint16_t Carbon_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop)
{	
	NOR_CARBON_INFO DevCarbonInfo;       //服务信息局部变量

	if(devaddr == 0x00)
		return 0x00;
	
	SRAM_DMA_Read_Buff((uint8_t *)&DevCarbonInfo,sizeof(NOR_CARBON_INFO),devaddr+Dev_Privately);	
	
	
	if(DevOutputLoop >= DevCarbonInfo.CarbonLoopValidNum)
	{
		return 0x00;
	}
	
	return DevCarbonInfo.EnergySaved;
}



/*******************************************************************************
* Function Name  : BLW_Carbon_Control_State 
* Description    : 碳达人状态控制函数
* Input          : 
* devaddr	     : 设备信息地址
* Return         : 无
* attention      : temp1非0才会进行保存
*******************************************************************************/
void BLW_Carbon_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start)
{
	uint8_t temp1 = 0; 
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_CARBON_INFO DevCarbonInfo;      
	
	uint8_t ctrlmode = 0, content = 0;
	
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevCarbonInfo,sizeof(NOR_CARBON_INFO),devaddr+Dev_Privately);


	if(DevOutputLoop >= DevCarbonInfo.CarbonLoopValidNum)
	{
		return ;
	}
	
	ctrlmode = start & 0xff;
	content = (start >> 8) & 0xff;
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Carbon Ctrl:%02X Content:%2X",ctrlmode,content);
	
	switch(ctrlmode)
	{
		case 0x01:       //设置节能开或者关
			if((content == 0x01) || (content == 0x02))
			{
				DevCarbonInfo.EnergySaved = content;
				temp1++;
			}
			break;
		case 0x02:       //设置全局亮度
			if(content <= 100)
			{
				DevCarbonInfo.Percentage = content;
				DevCarbonInfo.DimValue = ((DevActionGlobal.DimGlobalValue * DevCarbonInfo.Percentage) / 100);
				DevActionGlobal.DimGlobalValue = DevCarbonInfo.DimValue;
				DevCarbonInfo.DimValue_Flag = 0x01;
				temp1++;
			}
			break;
		case 0x03:       //设置节能温度
			if( ((content & 0x0f) >=1)  && ((content & 0x0f) <=15) )
			{
				DevCarbonInfo.TempInf[((content >> 4) & 0x0f) - 1].Addr = (content >> 4) & 0x0f;
				DevCarbonInfo.TempInf[((content >> 4) & 0x0f) - 1].Temp = content & 0x0f;
				DevCarbonInfo.TempInfFlag[((content >> 4) & 0x0f) - 1] = 0x01;
				temp1++;
			}
			break;
	}
	
	
	if(temp1 != 0x00)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevCarbonInfo, sizeof(NOR_CARBON_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);   /*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevCarbonInfo,sizeof(NOR_CARBON_INFO),devaddr+Dev_Privately);
	}	
}


/**
*@name 碳达人输入检测函数
*@para
*  DevAddr      设备地址
*  DevInputLoop 回路输入地址  范围0~CarbonLoopValidNum  
*  DevInputType 输入类型
*/

uint8_t Dev_Carbon_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType)
{
	Device_Public_Information_G BUS_Public;   //共用
	NOR_CARBON_INFO DevCarbonInfo;      
	uint8_t Ret = CtrlInvalid;                //控制无效

	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevCarbonInfo,sizeof(NOR_CARBON_INFO),DevAddr+Dev_Privately);	/*拷贝设备私有信息*/
	
//	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Dev_Carbon_InType_Get Loop:%d  Ctrl:%04X",DevInputLoop,DevInputType);
	
	if(DevInputLoop >= DevCarbonInfo.CarbonLoopValidNum)
	{
		return Ret;
	}

	switch(DevInputType)
	{
		case 0x01:
		case 0x02:
			if(DevInputType == DevCarbonInfo.DevAction)
			{
				DevCarbonInfo.DevAction = NULL;
				Ret = CtrlValid;
			}
			break;
	}
	

	if(CtrlValid == Ret)
	{
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"碳达人动作清空");

		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevCarbonInfo, sizeof(NOR_CARBON_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevCarbonInfo,sizeof(NOR_CARBON_INFO),DevAddr+Dev_Privately);
			
	}
	
	return Ret;
	
}


/**
*@name 回路扫描函数
*@para
*  DevAddr      设备地址
*@brief 拿到回路的状态，并转换为动作
*/

void Dev_Carbon_Dis(uint32_t DevAddr)
{
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_CARBON_INFO DevCarbonInfo;       //服务信息局部变量
	uint8_t KeepFlag = 0x00;
	
	Device_Public_Information_G BUS_PublicOut;   //共用
	RS485_TEMP_INFO Rs485Temp;
	
	uint8_t data[7];	                 //保存设置的底板型号
	
	uint32_t dev_addr = 0x00;
	
	if(NULL == DevAddr)
	{
		return ;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevCarbonInfo,sizeof(NOR_CARBON_INFO),DevAddr+Dev_Privately); 
	
	
	if(DevCarbonInfo.EnergySaved != DevCarbonInfo.EnergySaved_Last)
	{
		DevCarbonInfo.EnergySaved_Last = DevCarbonInfo.EnergySaved;   //有变化
		DevCarbonInfo.DevAction = DevCarbonInfo.EnergySaved;          //产生动作
		
		if(DevCarbonInfo.EnergySaved == 0x01)
		{
			DevCarbonInfo.CloseSource = 0x00;
		}
		
		SRAM_Write_Byte(DevCarbonInfo.EnergySaved,SRAM_UDP_Report_CarbonSatet);
		Udp_Addtion_Roomstate(DEV_Carbon_Saved,BUS_Public.addr, 0x00, ( (DevCarbonInfo.EnergySaved | (DevCarbonInfo.CloseSource << 4)) << 8 ) | 0x01);	     //上报状态
		KeepFlag = 0x01;
	}

	if(DevCarbonInfo.DimValue_Flag == 0x01)
	{
		DevCarbonInfo.DimValue_Flag = 0x00;
		
		uint8_t ctrl_flag = 0x00, ctrl_num = 0x00;
		uint16_t ctrl_loop_state[1] = {0};
		
		
		ctrl_flag |= (0x01 << 0);
		ctrl_num = 1;
		
		memset(data,0,7);	//初始化变量
		Flash_Read(&data[0],7,FLASH_MCU_Control_Revision_ADDRESS);	//读底板信息
		if((strcmp((const char *)data,"BLV-C12") == 0) || (strcmp((const char *)data,"BLV-V9") == 0))	//C12  V9
		{
			dev_addr = Find_AllDevice_List_Information(DEV_RS485_PWM,0x00);
			ctrl_loop_state[0] = ( 0x30 + (DevCarbonInfo.DimValue << 8) );
			if(ctrl_loop_state[0] > 0)
			{
				BLW_LED_Group_Ctrl(0x00, 0x00,dev_addr,ctrl_flag,ctrl_num,ctrl_loop_state);
			}
			
		}
		#if C8_TYPE
		else if(strcmp((const char *)data,"BLV-C8") == 0)
		{
			dev_addr = Find_AllDevice_List_Information2(Bus_port,Dev_Rs485_PB20,0x01);
			ctrl_loop_state[0] = ( 0x0A + (DevCarbonInfo.DimValue << 8) );
			if(ctrl_loop_state[0] > 0)
			{
				BLV_PB20_Group_Control_State(0x00, 0x00,dev_addr,ctrl_flag,ctrl_num,ctrl_loop_state);
			}
		}
		#endif
		
		if(DevCarbonInfo.Percentage != DevCarbonInfo.PercentageLast)
		{
			DevCarbonInfo.PercentageLast = DevCarbonInfo.Percentage;
			Udp_Addtion_Roomstate(DEV_Carbon_Saved,BUS_Public.addr, 0x00, (DevCarbonInfo.Percentage << 8) | 0x02);	     //上报状态
		}
		
		
		KeepFlag = 0x01;
	}
	
	
	for(uint8_t i = 0; i < TEMP_LIST_MAX; i++)
	{
		if(DevCarbonInfo.TempInfFlag[i] == 0x01)
		{
			DevCarbonInfo.TempInfFlag[i] = 0x00;
			
			uint8_t temp_ctrl = 0x00;
			uint16_t temp = 0x00;
			
			dev_addr = Find_AllDevice_List_Information(DEV_RS485_TEMP,DevCarbonInfo.TempInf[i].Addr);
			
			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"碳达人 控制空调：%d  Addr:%04X",DevCarbonInfo.TempInf[i].Addr,dev_addr);
			
			if(dev_addr != NULL)
			{
				SRAM_DMA_Read_Buff((uint8_t *)&BUS_PublicOut,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据	
				SRAM_DMA_Read_Buff((uint8_t *)&Rs485Temp,sizeof(RS485_TEMP_INFO),dev_addr+Dev_Privately);
				if(Rs485Temp.TemState.mode == TEMP_COLD)
				{
					if((Rs485Temp.TemState.set_t != 0x00) && ((DevCarbonInfo.TempInf[i].Temp  + Rs485Temp.TemState.set_t) <= 32))
					{
						temp = DevCarbonInfo.TempInf[i].Temp + Rs485Temp.TemState.set_t;
						temp_ctrl = 0x01;
						DevCarbonInfo.TempInf[i].SetTemp = temp;
					}
				}
				else if(Rs485Temp.TemState.mode == TEMP_HOT)
				{
					if((Rs485Temp.TemState.set_t != 0x00) && (Rs485Temp.TemState.set_t - DevCarbonInfo.TempInf[i].Temp >= 16))
					{
						temp = Rs485Temp.TemState.set_t - DevCarbonInfo.TempInf[i].Temp;
						temp_ctrl = 0x01;
					}
					else if(Rs485Temp.TemState.set_t == 0x00)
					{
						temp = 32 - DevCarbonInfo.TempInf[i].Temp;
						temp_ctrl = 0x01;
					}
					DevCarbonInfo.TempInf[i].SetTemp = temp;
				}
				
				if(temp_ctrl == 0x01)
				{
					//直接修改温控器的温度，而不是调用Dev_TEMPCTRL_Ctrl()函数	2025-11-20
					Rs485Temp.TemStateCtrl.set_t = temp;
					Rs485Temp.Carbon_Set_Temp = temp;
					
					BUS_PublicOut.check = 0x00;
					BUS_PublicOut.check = DoubleData_CheckSum((uint8_t *)&BUS_PublicOut, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485Temp, sizeof(RS485_TEMP_INFO));
					SRAM_DMA_Write_Buff((uint8_t *)&BUS_PublicOut, sizeof(Device_Public_Information_G),dev_addr);
					SRAM_DMA_Write_Buff((uint8_t *)&Rs485Temp,sizeof(RS485_TEMP_INFO),dev_addr+Dev_Privately);
					
					//Dev_TEMPCTRL_Ctrl(0x00, 0x00, dev_addr, 0x00, temp);
				}
			}
			
			if(DevCarbonInfo.TempInf[i].Temp != DevCarbonInfo.TempInfLast[i].Temp)
			{
				DevCarbonInfo.TempInfLast[i].Temp = DevCarbonInfo.TempInf[i].Temp;
				Udp_Addtion_Roomstate(DEV_Carbon_Saved,BUS_Public.addr, 0x00, (((DevCarbonInfo.TempInfLast[i].Addr << 4) | DevCarbonInfo.TempInf[i].Temp) << 8) | 0x03);	     //上报状态
			}
			
			KeepFlag = 0x01;
		}
	}
	
	/*全局亮度高于节能亮度则自动退出节能模式*/
	if((DevCarbonInfo.DevAction == NULL) && (DevActionGlobal.DimGlobalValue > DevCarbonInfo.DimValue) && (DevCarbonInfo.EnergySaved == 0x01) && (DevCarbonInfo.EnergySaved_Last == 0x01))
	{
		DevCarbonInfo.EnergySaved = 0x02;
		DevCarbonInfo.CloseSource = 0x01;
		
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Carbon Ctrl Close global:%d  carbondim:%d",DevActionGlobal.DimGlobalValue,DevCarbonInfo.DimValue);
		KeepFlag = 0x01;
	}
	
	if((DevCarbonInfo.DevAction == NULL) && (DevCarbonInfo.EnergySaved == 0x01) && (DevCarbonInfo.EnergySaved_Last == 0x01))
	{
		for(uint8_t i = 0; i < TEMP_LIST_MAX; i++)
		{
			dev_addr = Find_AllDevice_List_Information(DEV_RS485_TEMP,DevCarbonInfo.TempInf[i].Addr);
			if(dev_addr != NULL)
			{
				SRAM_DMA_Read_Buff((uint8_t *)&Rs485Temp,sizeof(RS485_TEMP_INFO),dev_addr+Dev_Privately);
				if(Rs485Temp.Carbon_Set_Temp == Rs485Temp.TemState.set_t)
				{
					if(Rs485Temp.TemState.mode == TEMP_COLD)
					{
						if((Rs485Temp.TemState.set_t != 0x00) && (Rs485Temp.TemState.set_t < DevCarbonInfo.TempInf[i].SetTemp))
						{
							DevCarbonInfo.EnergySaved = 0x02;
							Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Carbon Temp:%d Cold Ctrl Close set:%d  carbonset:%d",\
							DevCarbonInfo.TempInf[i].Addr,Rs485Temp.TemState.set_t,DevCarbonInfo.TempInf[i].SetTemp);
							DevCarbonInfo.CloseSource = 0x02;
							KeepFlag = 0x01;
							break;
						}
					}
					else if(Rs485Temp.TemState.mode == TEMP_HOT)
					{
						if((Rs485Temp.TemState.set_t != 0x00) && (Rs485Temp.TemState.set_t > DevCarbonInfo.TempInf[i].SetTemp))
						{
							DevCarbonInfo.EnergySaved = 0x02;
							Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Carbon Temp:%d Hot Ctrl Close set:%d  carbonset:%d",\
							DevCarbonInfo.TempInf[i].Addr,Rs485Temp.TemState.set_t,DevCarbonInfo.TempInf[i].SetTemp);
							DevCarbonInfo.CloseSource = 0x02;
							KeepFlag = 0x01;
							break;
						}
						else if((Rs485Temp.TemState.set_t == 0x00) && (32 >= DevCarbonInfo.TempInf[i].SetTemp))
						{
							DevCarbonInfo.EnergySaved = 0x02;
							Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Carbon Temp:%d Hot Ctrl Close set:%d  carbonset:%d",\
							DevCarbonInfo.TempInf[i].Addr,Rs485Temp.TemState.set_t,DevCarbonInfo.TempInf[i].SetTemp);
							DevCarbonInfo.CloseSource = 0x02;
							KeepFlag = 0x01;
							break;
						}
					}
				}
			}
		}
	}

	if(0x01 == KeepFlag)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevCarbonInfo, sizeof(NOR_CARBON_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevCarbonInfo,sizeof(NOR_CARBON_INFO),DevAddr+Dev_Privately);		
	}
	
}



