/**
  ******************************************************************************
  * @file    BLV_Nor_Dev_ServiceFun.c
  * @author  BLW RCU  development Team
  * @version V1.0.0
  * @date    2021年11月01日
  * @brief   服务信息初始化   设备服务信息控制
  * @attention 设备服务信息
  *									输入检测：  有
  *                 输入类型：  有	
  *                 执行函数：  有		
  *            			执行方式：
  *															开 
  *															关
  * @log     
			202205241551   SongHan   SERVICEVALUESET 宏参数用的是整型，整型是uint32_t 需要强转为uint64_t 
			202509011617	曹聪	
				1、修改服务信息回路状态、上一次回路状态、回路改变状态的变量类型
				2、修改服务信息回路上限 24 -> 64
				3、取消服务信息结构体中 Dev_Name_Struct DevNameStruct 服务信息别名，没有使用
				4、增加服务信息 回路1 取电服务 事件以及控制方式
					增加事件：首次取电事件(0x03)
					增加方法：置位首次取电标志位(0x03)、上报服务器播放欢迎词(0x04)
			20250918
  ******************************************************************************
  */
  
#include "includes.h"

#include "BLV_Nor_Dev_ServiceFun.h"
#include "BLV_Nor_DevFun.h"

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

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

/*服务信息新增到普通设备链表*/
void BLV_Nor_Dev_Service_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&DevServiceInfo,0,sizeof(NOR_SERVICE_INFO));
	
	BUS_Public.addr = devaddr;			//设备地址
	BUS_Public.type = Dev_Host_Service;		//设备类型
	BUS_Public.port = NULL;				//设备发送端口
	BUS_Public.baud = 0000;				//设备发送波特率
//	BUS_Public.retry_num = 0x03;		//设备数据重发次数
//	BUS_Public.wait_time = 0x0064;		//设备数据发送等待回复时间
//	BUS_Public.polling_cf = (uint32_t)&BLW_Touch_SwitchCycleDis;
//	BUS_Public.processing_cf = (uint32_t)&BLW_Rs485_Touch_Swi_Check;
	
	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_Service_Dis;                 //
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_Service_InType_Get;        //
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_Service_Control_State;        //
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Service_Loop_State;     //
	
	DevServiceInfo.ServiceLoopValidNum = ServiceNumMAX;
	
//	DevServiceInfo.DevServiceState[Service_Warning] = 0x01;   //默认开启提示音
//	DevServiceInfo.DevServiceState[Service_Dnd] = 0x02;       //默认勿扰为关
//	DevServiceInfo.DevServiceState[Service_Meals] = 0x02;     //关闭9号服务
//	DevServiceInfo.DevServiceState[Service_CheckOut] = 0x02;  //关闭退房
//	DevServiceInfo.DevServiceState[Service_Strong] = 0x02;    //关闭保险箱
//	DevServiceInfo.DevServiceState[Service_Call] = 0x02;      //关闭呼叫
//	DevServiceInfo.DevServiceState[Service_Sos] = 0x02;       //默认关闭SOS服务      2022-06-28
	
	
	//2026-03-11 对默认值进行增加
	DevServiceInfo.DevServiceState[Service_Dnd] = 0x02;              	//默认关闭勿扰
	DevServiceInfo.DevServiceState[Service_Clean] = 0x02;            	//默认关闭清理
	DevServiceInfo.DevServiceState[Service_Call] = 0x02;             	//默认关闭呼叫
	DevServiceInfo.DevServiceState[Service_Wash] = 0x02;             	//默认关闭洗衣
	DevServiceInfo.DevServiceState[Service_CheckOut] = 0x02;         	//默认关闭退房
	DevServiceInfo.DevServiceState[Service_Wait] = 0x02;             	//默认关闭稍后
	DevServiceInfo.DevServiceState[Service_Sos] = 0x02;              	//默认关闭SOS
	DevServiceInfo.DevServiceState[Service_Meals] = 0x02;            	//默认关闭预约待租
	DevServiceInfo.DevServiceState[Service_Food_Plate] = 0x02;       	//默认关闭开锁
	DevServiceInfo.DevServiceState[Service_Luggage] = 0x02;          	//默认关闭行李
	DevServiceInfo.DevServiceState[Service_Strong] = 0x02;           	//默认关闭保险箱 用于无卡取电门口红
	DevServiceInfo.DevServiceState[Service_Door] = 0x02;             	//默认关闭门磁  
	DevServiceInfo.DevServiceState[Service_Warning] = 0x01;          	//默认开启提示音 用于静音开启和关闭
	DevServiceInfo.DevServiceStateLast[Service_Dnd] = 0x02;              	//默认关闭勿扰
	DevServiceInfo.DevServiceStateLast[Service_Clean] = 0x02;            	//默认关闭清理
	DevServiceInfo.DevServiceStateLast[Service_Call] = 0x02;             	//默认关闭呼叫
	DevServiceInfo.DevServiceStateLast[Service_Wash] = 0x02;             	//默认关闭洗衣
	DevServiceInfo.DevServiceStateLast[Service_CheckOut] = 0x02;         	//默认关闭退房
	DevServiceInfo.DevServiceStateLast[Service_Wait] = 0x02;             	//默认关闭稍后
	DevServiceInfo.DevServiceStateLast[Service_Sos] = 0x02;              	//默认关闭SOS
	DevServiceInfo.DevServiceStateLast[Service_Meals] = 0x02;            	//默认关闭预约待租
	DevServiceInfo.DevServiceStateLast[Service_Food_Plate] = 0x02;       	//默认关闭开锁
	DevServiceInfo.DevServiceStateLast[Service_Luggage] = 0x02;          	//默认关闭行李
	DevServiceInfo.DevServiceStateLast[Service_Strong] = 0x02;           	//默认关闭保险箱 用于无卡取电门口红
	DevServiceInfo.DevServiceStateLast[Service_Door] = 0x02;             	//默认关闭门磁  
	DevServiceInfo.DevServiceStateLast[Service_Warning] = 0x01;          	//默认开启提示音 用于静音开启和关闭
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"增加一个服务信息，服务信息私有数据长度:%d",sizeof(NOR_SERVICE_INFO));
	


	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO));
}

/*******************************************************************************
* Function Name  : BLV_Nor_Dev_HVout_For_Logic_Init 
* Description    : 继电器初始化信息来自逻辑文件
*******************************************************************************/
void BLV_Nor_Dev_Service_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&DevServiceInfo,0,sizeof(NOR_SERVICE_INFO));
	
	BUS_Public.addr = dev_info->addr;			//设备地址
	BUS_Public.type = Dev_Host_Service;		//设备类型
	BUS_Public.port = dev_info->type;			//设备发送端口
	BUS_Public.baud = 0000;			//设备发送波特率
//	BUS_Public.retry_num = 0x03;		//设备数据重发次数
//	BUS_Public.wait_time = 0x0064;		//设备数据发送等待回复时间
//	BUS_Public.polling_cf = (uint32_t)&BLW_Touch_SwitchCycleDis;
//	BUS_Public.processing_cf = (uint32_t)&BLW_Rs485_Touch_Swi_Check;
	
	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_Service_Dis;                 //
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_Service_InType_Get;        //
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_Service_Control_State;        //
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Service_Loop_State;     //
	
	DevServiceInfo.ServiceLoopValidNum = ServiceNumMAX;
	
	DevServiceInfo.DevServiceState[Service_Dnd] = 0x02;              	//默认关闭勿扰
	DevServiceInfo.DevServiceState[Service_Clean] = 0x02;            	//默认关闭清理
	DevServiceInfo.DevServiceState[Service_Call] = 0x02;             	//默认关闭呼叫
	DevServiceInfo.DevServiceState[Service_Wash] = 0x02;             	//默认关闭洗衣
	DevServiceInfo.DevServiceState[Service_CheckOut] = 0x02;         	//默认关闭退房
	DevServiceInfo.DevServiceState[Service_Wait] = 0x02;             	//默认关闭稍后
	DevServiceInfo.DevServiceState[Service_Sos] = 0x02;              	//默认关闭SOS
	DevServiceInfo.DevServiceState[Service_Meals] = 0x02;            	//默认关闭预约待租
	DevServiceInfo.DevServiceState[Service_Food_Plate] = 0x02;       	//默认关闭开锁
	DevServiceInfo.DevServiceState[Service_Luggage] = 0x02;          	//默认关闭行李
	DevServiceInfo.DevServiceState[Service_Strong] = 0x02;           	//默认关闭保险箱 用于无卡取电门口红
	DevServiceInfo.DevServiceState[Service_Door] = 0x02;             	//默认关闭门磁  
	DevServiceInfo.DevServiceState[Service_Warning] = 0x01;          	//默认开启提示音 用于静音开启和关闭
	DevServiceInfo.DevServiceState[Service_22] = 0x02; 					//电视开关服务状态
	
	DevServiceInfo.DevServiceStateLast[Service_Dnd] = 0x02;           	//默认关闭勿扰
	DevServiceInfo.DevServiceStateLast[Service_Clean] = 0x02;        	//默认关闭清理
	DevServiceInfo.DevServiceStateLast[Service_Call] = 0x02;         	//默认关闭呼叫
	DevServiceInfo.DevServiceStateLast[Service_Wash] = 0x02;          	//默认关闭洗衣
	DevServiceInfo.DevServiceStateLast[Service_CheckOut] = 0x02;      	//默认关闭退房
	DevServiceInfo.DevServiceStateLast[Service_Wait] = 0x02;          	//默认关闭稍后
	DevServiceInfo.DevServiceStateLast[Service_Sos] = 0x02;          	//默认关闭SOS
	DevServiceInfo.DevServiceStateLast[Service_Meals] = 0x02;         	//默认关闭预约待租
	DevServiceInfo.DevServiceStateLast[Service_Food_Plate] = 0x02;    	//默认关闭开锁
	DevServiceInfo.DevServiceStateLast[Service_Luggage] = 0x02;       	//默认关闭行李
	DevServiceInfo.DevServiceStateLast[Service_Strong] = 0x02;       	//默认关闭保险箱 用于无卡取电门口红
	DevServiceInfo.DevServiceStateLast[Service_Door] = 0x02;         	//默认关闭门磁  
	DevServiceInfo.DevServiceStateLast[Service_Warning] = 0x01;       	//默认开启提示音 用于静音开启和关闭
	DevServiceInfo.DevServiceStateLast[Service_22] = 0x02; 				//电视开关服务状态
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"增加一个服务信息，私有数据长度:%d",sizeof(NOR_SERVICE_INFO));
	
	/*RCU根据MCU复位状态进行设备初始化 - 
		保存标志位成立的情况下，如果是看门狗复位、软件复位、外部手动复位，将之前保存的状态进行设备初始化
		否则采用默认状态初始化(默认状态可通过RCU_POWER_Deivce_State_Init 宏定义 修改默认初始化状态)
	*/
	if( (DevActionGlobal.sram_save_flag == 0xA8) && ((SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x01) || (SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x03) || (SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x05)) )
	{
		DevServiceInfo.DevServiceState[Service_Ele] = DevActionGlobal.Last_EleState;   			
		DevServiceInfo.DevServiceStateLast[Service_Ele] = DevActionGlobal.Last_EleState;   		
	}else {

#if RCU_POWER_Deivce_State_Init	
		DevServiceInfo.DevServiceState[Service_Ele] = 0x01;   		//默认开启入住服务 - 开
		DevServiceInfo.DevServiceStateLast[Service_Ele] = 0x01;   	//默认开启入住服务 - 开
#else
		DevServiceInfo.DevServiceState[Service_Ele] = 0x02;   		//默认开启入住服务 - 关
		DevServiceInfo.DevServiceStateLast[Service_Ele] = 0x02;   	//默认开启入住服务 - 关
#endif

	}
	
	SRAM_Write_Byte(DevServiceInfo.DevServiceState[Service_Ele],SRAM_UDP_ELEReport_EleState);			//给UDP 上报取电状态使用
	SRAM_Write_Byte(DevServiceInfo.DevServiceState[Service_Ele],SRAM_UDP_ELEReport_EleState_Last);			//给UDP 上报取电状态使用
	SRAM_Write_DW(SysTick_1ms,SRAM_UDP_ELEReport_EleState_Tick);
	
	DevServiceInfo.is_first_power_on = 0x00;	//上电默认 非首次取电标志位
	
	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO));
}


/**
  * @name  指定服务信息回路状态得到
  * @param  
  *    devaddr             当前设备的地址
  *    DevOutputLoop       需要查找的服务信息回路 从0开始
  * @retval 
  *    ret                 回路状态返回    1 开   2关
  * @brief

  */
uint16_t Service_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop)
{	
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量

	if(devaddr == 0x00)
		return 0x00;
	
	SRAM_DMA_Read_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),devaddr+Dev_Privately);	
	
	if(DevOutputLoop >= DevServiceInfo.ServiceLoopValidNum)
	{
		return 0x00;
	}
	
	return DevServiceInfo.DevServiceState[DevOutputLoop];   

	
}

/*******************************************************************************
* Function Name : BLW_Service_Control_State 
* Description   : BLW服务信息状态控制函数
* Input         : 
	devaddr	: 设备信息地址
    loop    ：回路      回路都是从0
	start   ：状态    0x01开  0x02关 0x04清除首次标志位 0x04
* Return        : 无
* attention     : temp1非0才会进行保存
*******************************************************************************/
void BLW_Service_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start)
{
//	uint16_t DataLen = 0;
	uint8_t temp1 = 0;  //crc_val = 0,
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),devaddr+Dev_Privately);

	if(DevOutputLoop >= DevServiceInfo.ServiceLoopValidNum)
	{
		return ;
	}
	
	switch(start)
	{
		case 0x01:
		case 0x02:
			if( DevServiceInfo.DevServiceState[DevOutputLoop] != start )
			{
				if((DevOutputLoop == LOOPCH16) && (start == 0x01))		//
				{
					if(0x01 == DevActionGlobal.DevActionU64Cond.NeightFlag)   //夜灯存在
					{
						if(NightModeStart != DevActionGlobal.DevActionU64Cond.NeightState)
						{
							Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"设备输出场景控制，有场景控制睡眠，进入起夜");    //有场景控制
							DevActionGlobal.DevActionU64Cond.NeightState = NightModeStart;     //进入起夜  
						}
					}
				}
				
				if((DevOutputLoop == LOOPCH02)&& (start == 0x01))          //2023-10-31    清理和勿扰服务互锁
				{
					DevServiceInfo.DevServiceState[DevOutputLoop+1] = 0x02;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"SERVICE loop:%d,start:%d",(DevOutputLoop+1),0x02);
				}
				else if((DevOutputLoop == LOOPCH03)&& (start == 0x01))
				{
					DevServiceInfo.DevServiceState[DevOutputLoop-1] = 0x02;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"SERVICE loop:%d,start:%d",(DevOutputLoop-1),0x02);
				}
				else if((DevOutputLoop == LOOPCH24) && (start == 0x01))     //2023-12-05     静音开
				{
					DevServiceInfo.DevServiceState[LOOPCH14] = 0x02;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"SERVICE loop:%d,start:%d",LOOPCH14,0x02);
				}
				else if((DevOutputLoop == LOOPCH24) && (start == 0x02))     //2023-12-05     静音关
				{
					DevServiceInfo.DevServiceState[LOOPCH14] = 0x01;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"SERVICE loop:%d,start:%d",LOOPCH14,0x01);
				}
				else if((DevOutputLoop == LOOPCH01)&& (start == 0x01))    //2024-04-29
				{
					DevActionGlobal.CardInFlag = 0x01;
				}
				
				if(DevOutputLoop == LOOPCH01){
					/*取电服务信息 - 增加日志记录 2025-02-19*/
					
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"SERVICE loop:%d,start:%d\r\n",DevOutputLoop,start);
					LOG_LogicInfo_DebugRecord("DevService:loop:%d,start:%d",DevOutputLoop,start);
				}
				
				DevServiceInfo.DevServiceState[DevOutputLoop] = start;//当前动作清0
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"SERVICE loop:%d,start:%d",DevOutputLoop,start);
				temp1++;
			}
			break;
		case 0x03:	//首次取电标志位 置位 目前只针对于取电服务
			if( (DevOutputLoop == Service_Ele) && (DevServiceInfo.is_first_power_on == 0x00) )
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Service_Ele Set first_power_on");
				DevServiceInfo.is_first_power_on = 0x01;
				temp1++;
			}
			break;
		case 0x04:	//仅取电
			/*
				1、如果当前首次取电标志位还存在，便触发首次取电事件
				2、如果当前首次取电标志位不存在，仅修改取电状态，不触发取电事件，且触发“仅取电”事件
			*/
			
			if( (DevOutputLoop == Service_Ele) && (DevServiceInfo.is_first_power_on == 0x01) )
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"仅取电 首次取电");
				DevActionGlobal.CardInFlag = 0x01;
				DevServiceInfo.DevServiceState[DevOutputLoop] = 0x01;
				temp1++;
			}else if( DevOutputLoop == Service_Ele ){
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"仅取电 非首次取电");
				DevActionGlobal.CardInFlag = 0x01;
				DevServiceInfo.DevServiceState[DevOutputLoop] = 0x01;
				DevServiceInfo.DevServiceStateLast[DevOutputLoop] = 0x01;
				DevServiceInfo.DevChangeFlag[DevOutputLoop] = 0x04;		//产生仅取电事件
				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 *)&DevServiceInfo, sizeof(NOR_SERVICE_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);   /*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),devaddr+Dev_Privately);
	}	
}


/**
*@name 服务信息输入检测函数
*@para
*  DevAddr      设备地址
*  DevInputLoop 回路输入地址  范围0~ServiceLoopValidNum  
*  DevInputType 输入类型
*/

uint8_t Dev_Service_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType)
{
	Device_Public_Information_G BUS_Public;   //共用
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	uint8_t Ret = CtrlInvalid;         //控制无效

	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),DevAddr+Dev_Privately);	/*拷贝设备私有信息*/
	
	if(DevInputLoop >= DevServiceInfo.ServiceLoopValidNum)
	{
		return Ret;
	}

	if(DevInputType == DevServiceInfo.DevChangeFlag[DevInputLoop])   //输入类型对的上 1开 2关
	{
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"服务信息动作 %d - %d 清零",DevInputLoop,DevInputType);

		DevServiceInfo.DevChangeFlag[DevInputLoop] = 0x00;		//当前动作清0
		Ret = CtrlValid;
	}
	
	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 *)&DevServiceInfo, sizeof(NOR_SERVICE_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),DevAddr+Dev_Privately);
	}
	
	return Ret;
	
}

/*服务条件同步*/
void Dev_Action_CondService_Get(NOR_SERVICE_INFO *DevServiceInfo)
{
	/* 取电服务状态改变时，记录时间戳 */
	if( DevActionGlobal.DevActionU64Cond.EleState != DevServiceInfo->DevServiceState[Service_Ele] )
	{
		SRAM_Write_DW(SysTick_1ms,SRAM_UDP_ELEReport_EleState_Tick);
	}
	
	DevActionGlobal.DevActionU64Cond.EleState = DevServiceInfo->DevServiceState[Service_Ele];
	DevActionGlobal.DevActionU64Cond.DndState = DevServiceInfo->DevServiceState[Service_Dnd];
	DevActionGlobal.DevActionU64Cond.CleanState = DevServiceInfo->DevServiceState[Service_Clean];
	DevActionGlobal.DevActionU64Cond.CallState = DevServiceInfo->DevServiceState[Service_Call];
	DevActionGlobal.DevActionU64Cond.WashState = DevServiceInfo->DevServiceState[Service_Wash];
	DevActionGlobal.DevActionU64Cond.CheckOutState = DevServiceInfo->DevServiceState[Service_CheckOut];
	DevActionGlobal.DevActionU64Cond.WaitState = DevServiceInfo->DevServiceState[Service_Wait];
	DevActionGlobal.DevActionU64Cond.SosState = DevServiceInfo->DevServiceState[Service_Sos];
	DevActionGlobal.DevActionU64Cond.RentState = DevServiceInfo->DevServiceState[Service_Meals];
	DevActionGlobal.DevActionU64Cond.LockState = DevServiceInfo->DevServiceState[Service_Food_Plate];
	DevActionGlobal.DevActionU64Cond.LuggageState = DevServiceInfo->DevServiceState[Service_Luggage];
	DevActionGlobal.DevActionU64Cond.StrongState = DevServiceInfo->DevServiceState[Service_Strong];
	DevActionGlobal.DevActionU64Cond.DoorState = DevServiceInfo->DevServiceState[Service_Door];
	DevActionGlobal.DevActionU64Cond.WarningState = DevServiceInfo->DevServiceState[Service_Warning];
	DevActionGlobal.Service_16 = DevServiceInfo->DevServiceState[Service_16];
	
	SRAM_Write_Byte(DevActionGlobal.DevActionU64Cond.EleState,SRAM_UDP_ELEReport_EleState);			//给UDP 上报取电状态使用
	
}

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

void Dev_Service_Dis(uint32_t DevAddr)
{
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	uint8_t KeepFlag = 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 *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),DevAddr+Dev_Privately); 
	
	if( DevActionGlobal.SleepMode_State != 0x01 )      //2024-10-21       起夜关闭夜灯开启按键背光，同时调用16号服务进行暂停助眠音乐
	{
		DevServiceInfo.DevServiceState[Service_16] = 0x02;
		KeepFlag = 0x01;
	}
	
	for(uint16_t i = 0; i < DevServiceInfo.ServiceLoopValidNum; i++)
	{
		if(DevServiceInfo.DevServiceStateLast[i] != DevServiceInfo.DevServiceState[i])
		{
			KeepFlag = 0x01;
			DevServiceInfo.DevServiceStateLast[i] = DevServiceInfo.DevServiceState[i];
			
			if( (i == Service_Ele) && ( DevServiceInfo.DevServiceState[i] == 0x01 ) && (DevServiceInfo.is_first_power_on == 0x01) )
			{
				DevServiceInfo.is_first_power_on = 0x00;
				DevServiceInfo.DevChangeFlag[i] = 0x03;		//产生首次取电事件
			}else {
				DevServiceInfo.DevChangeFlag[i] = DevServiceInfo.DevServiceState[i];
			}

			Udp_Addtion_Roomstate(Dev_Host_Service,0x00, i+1, DevServiceInfo.DevServiceState[i]);	     //上报服务信息
			
			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"准备同步服务信息的状态，回路:%d 状态:%d", i, DevServiceInfo.DevChangeFlag[i]);
		}
	}

	if(0x01 == KeepFlag)
	{
		Dev_Action_CondService_Get(&DevServiceInfo); //服务信息有变化，进行状态同步
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevServiceInfo, sizeof(NOR_SERVICE_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),DevAddr+Dev_Privately);		
	}
}

/*服务信息 - 清楚首次取电标志位*/
uint8_t ServiceInfo_Set_first_power_on(uint8_t state)
{
	uint32_t dev_addr = Find_AllDevice_List_Information(Dev_Host_Service,0x00);
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	
	if(dev_addr == 0x00)	return 0x01;	//未找到设备
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),dev_addr+Dev_Privately);
	
	if(DevServiceInfo.is_first_power_on != state)
	{
		DevServiceInfo.is_first_power_on = state;
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevServiceInfo, sizeof(NOR_SERVICE_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);   /*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),dev_addr+Dev_Privately);
	}
	
	return 0x00;
}

/*服务信息 - 获取全部回路的开关状态*/
uint8_t ServiceInfo_Get_ALL_Loop_State(uint8_t *read_buff)
{
	uint32_t dev_addr = Find_AllDevice_List_Information(Dev_Host_Service,0x00);
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	uint8_t loop_offset = 0;
	uint8_t loop_ide = 0;
	
	if(dev_addr == 0x00)	return 0x01;	//未找到设备
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),dev_addr+Dev_Privately);
	
	if( DevServiceInfo.ServiceLoopValidNum >= ServiceNumMAX )	DevServiceInfo.ServiceLoopValidNum = ServiceNumMAX;
	
	for(uint8_t i=0;i<DevServiceInfo.ServiceLoopValidNum;i++)
	{
		loop_ide = i / 8;
		loop_offset = i % 8;
		if(DevServiceInfo.DevServiceState[i] == 0x01)
		{
			read_buff[loop_ide] |= 0x01 << loop_offset;
		}
	}
	
	return 0x00;
}

/*******************************************************************************
* Function Name  : ServiceInfo_Set_RoomState
* Description    : 服务信息 - 设置房态对应的回路状态		2025-10-27
	针对服务器对于PMS推送的房态进行下发后，改变对应的服务信息回路状态，用于触发对应的事件
		服务信息回路25 - 出租
		服务信息回路26 - 退房
		服务信息回路27 - 待租
		服务信息回路28 - 空房
	这4个服务信息回路存在互斥关系，有且仅有一个状态为开。
* Input          : state        -0x01:出租、0x02:退房、0x03:待租、0x04:空房
* Return         : 0x00:处理结果成功，其他值:处理失败
*******************************************************************************/
uint8_t ServiceInfo_Set_RoomState(uint8_t state)
{
	uint32_t dev_addr = Find_AllDevice_List_Information(Dev_Host_Service,0x00);
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	uint8_t keep_flag = 0;
	
	if(dev_addr == 0x00)	return 0x01;	//未找到设备
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),dev_addr+Dev_Privately);
	
	switch(state)
	{
		case 0x01:		//出租
			DevServiceInfo.DevServiceState[Service_PMS_Rented] = 0x01;
			DevServiceInfo.DevServiceState[Service_PMS_CheckOut] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_Waiting] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_Vacant] = 0x02;
			
			if(DevServiceInfo.is_first_power_on != 0x01)
			{
				DevServiceInfo.is_first_power_on = 0x01;
			}
			keep_flag = 0x01;
			break;
		case 0x02:		//退房
			DevServiceInfo.DevServiceState[Service_PMS_Rented] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_CheckOut] = 0x01;
			DevServiceInfo.DevServiceState[Service_PMS_Waiting] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_Vacant] = 0x02;
			
			if(DevServiceInfo.is_first_power_on != 0x01)
			{
				DevServiceInfo.is_first_power_on = 0x01;
			}
			keep_flag = 0x01;
			break;
		case 0x03:		//待租
			DevServiceInfo.DevServiceState[Service_PMS_Rented] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_CheckOut] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_Waiting] = 0x01;
			DevServiceInfo.DevServiceState[Service_PMS_Vacant] = 0x02;
			keep_flag = 0x01;
			break;
		case 0x04:		//空房
			DevServiceInfo.DevServiceState[Service_PMS_Rented] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_CheckOut] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_Waiting] = 0x02;
			DevServiceInfo.DevServiceState[Service_PMS_Vacant] = 0x01;
			keep_flag = 0x01;
			break;
	}
	
	if(keep_flag == 0x01)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevServiceInfo, sizeof(NOR_SERVICE_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);   /*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),dev_addr+Dev_Privately);
	}
	
	return 0x00;
}

/*******************************************************************************
* Function Name  : ServiceInfo_Set_VirtualCard_LoopState
* Description    : 服务信息 - 设置无卡取电对应的回路状态		2026-02-26
	该函数主要用于设置无卡取电对应的服务信息回路状态
	无卡取电 回路1	->	门磁服务 		回路号13
	无卡取电 回路2	->	门口红外服务	回路号32
	无卡取电 回路3	->	卫红外服务		回路号33
	无卡取电 回路4	->	浴红外服务		回路号34
	无卡取电 回路5	->	床头红外服务	回路号35
	无卡取电 回路6	->	镜前红外服务	回路号36
	无卡取电 回路7	->	休闲红外服务	回路号37
	无卡取电 回路8	->	客厅红外服务	回路号38
	无卡取电 回路9	->	厅卫红外服务	回路号39
	无卡取电 回路10	->	浴缸红外服务	回路号40

	给无卡取电中初始化IO状态时，并同步对应的服务信息状态。-- 仅用于初始化同步状态

* Input          : 
	loop		 - 回路范围：1~10
	state        - 0x01:开、0x02:关、
* Return         : 0x00:处理结果成功，其他值:处理失败
*******************************************************************************/
uint8_t ServiceInfo_Set_VirtualCard_LoopState(uint8_t loop,uint8_t state)
{
	uint32_t dev_addr = Find_AllDevice_List_Information(Dev_Host_Service,0x00);
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_SERVICE_INFO DevServiceInfo;       //服务信息局部变量
	
	if(dev_addr == 0x00)	return 0x01;	//未找到设备
	if( (loop == 0x00) || (loop > 10) )		return 0x02;	//回路不在设置范围内
	if( (state != 0x01) && (state != 0x02) )	return 0x03;	//设置状态错误
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),dev_addr+Dev_Privately);
	
	if(loop == 0x01)
	{
		loop = Service_Door;
		
		/*门磁对应的服务状态需要翻转一下
			门磁 低电平对应触发 -> 关门磁服务
			门磁 高电平对应释放 -> 开门磁服务
		*/
		if( state == 0x01 )
		{
			DevServiceInfo.DevServiceState[loop] = 0x02;
			DevServiceInfo.DevServiceStateLast[loop] = 0x02;
		}else {
			DevServiceInfo.DevServiceState[loop] = 0x01;
			DevServiceInfo.DevServiceStateLast[loop] = 0x01;
		}
	}else {
		loop = loop + Service_30;
		
		DevServiceInfo.DevServiceState[loop] = state;
		DevServiceInfo.DevServiceStateLast[loop] = state;
	}
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"%s Loop:%d - 0x%x", __func__, loop, DevServiceInfo.DevServiceState[loop]);
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevServiceInfo, sizeof(NOR_SERVICE_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);   /*将数据保存进入SRAM中*/
	SRAM_DMA_Write_Buff((uint8_t *)&DevServiceInfo,sizeof(NOR_SERVICE_INFO),dev_addr+Dev_Privately);
	
	return 0x00;
}


