
/**
  ********************************************************************************************************************
  * @file    BLV_485_Dev_YangGeLockFun.c
  * @author  BLW RCU  development Team
  * @version V1.0.0
  * @date    2025年12月18日
  * @brief   微信锁驱动总表         
  *          用于杨格蓝牙锁
  *         主要功能：
  *             1、获取门锁上报的身份信息、门锁的动作信息
  *             2、获取门锁上报的门锁电量、门锁的无线模组网络质量
  *             3、控制门锁开锁，当前此门锁端商家反馈为了低功耗考虑，开锁唤醒时间较长，开锁延迟较高
  *         注意：
                0、身份信息需要配合取电设备使用
    1：正常操作情况：用户刷卡，门锁将刷卡人的身份报给RCU，开门，取电，RCU取电时按照身份执行对应场景。
        》》》 当前RCU处理逻辑：OK
        
    2：用户刷卡后，不开门，RCU因为未检测到有人进入，不会取电，但是，门锁仍然会报刷卡操作给RCU。
        》》》 当前RCU处理逻辑：RCU收到刷卡身份后，5分钟内未取电，身份失效 
        
    3：如果房间已经有人，已经处于取电状态，这时如果有人刷卡，会产生身份上报。
        》》》 当前RCU处理逻辑：RCU已取电，忽略本次刷卡操作 
        
    4：用户刷卡，开门，取电，但是门锁的身份延迟上报，在用户取电后才产生上报。
        》》》 当前RCU处理逻辑：RCU取电时没有身份信息，会按照默认身份（住客身份）取电，延迟上报的身份信息会被丢弃               
  ************************************************************************************************************************
  */

#include "includes.h"

#include "BLV_485_Dev_YangGeLockFun.h"
#include "string.h"

uint8_t YangGe_WxLock_Polling_Ctrl(uint32_t dev_addr);
uint8_t YangGe_WxLock_Data_Processing(uint32_t dev_addr,uint32_t data_addr,uint16_t len);

void RS485_YG_LogID_RecAsk(Device_Public_Information_G* BUS_Public, RS485_WxLock_INFOP Rs485WxLockInfo, uint8_t *data, uint16_t DataLen, uint8_t ret);
void RS485_YG_UnLock_Ctrl(Device_Public_Information_G *BUS_Public, RS485_WxLock_INFOP Rs485WxLockInfo);
uint8_t Dev_YangGeLock_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType);
void YG_RS485Lock_IndiCtrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr, uint16_t Addr, uint16_t OnOff);

/*******************************************************************************
* @brief     : 添加设备到链表 - 主动设备
* @param     :
*		BUS_Public		: 设备共有数据结构体
*		Rs485WxLockInfo	: 微信锁私有数据结构体
* @retval    : None
* @attention : None
*******************************************************************************/
void YangGe_RS485_WxLockFun_Init(Device_Public_Information_G *BUS_Public, RS485_WxLock_INFO *Rs485WxLockInfo)
{
	BUS_Public->polling_cf = (uint32_t)&YangGe_WxLock_Polling_Ctrl;	    //微信锁轮询控制函数（用于发送命令控制）
	BUS_Public->processing_cf = (uint32_t)&YangGe_WxLock_Data_Processing;	//微信锁接收处理函数
	BUS_Public->DevFunInfo.Dev_Input_Type_Get = Dev_YangGeLock_InType_Get;  
    BUS_Public->DevFunInfo.Dev_Output_Ctrl = YG_RS485Lock_IndiCtrl;
    
    Rs485WxLockInfo->CJ_kwh = 100;              	//默认电量
    
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"添加杨格微信锁设备,身份有效期:%d ms",Rs485WxLockInfo->YG_IdVldPeriod);
}

/*******************************************************************************
* @brief    : 杨格锁控制函数，用于发送命令控制
* @param    : 
*		dev_addr : 设备信息地址
* @retval   : 
* @note     : 1、开锁指令的发送和重发
*******************************************************************************/
uint8_t YangGe_WxLock_Polling_Ctrl(uint32_t dev_addr)
{
	uint8_t Ret = 0x00;
	Device_Public_Information_G BUS_Public;
	RS485_WxLock_INFO  Rs485WxLockInfo;


	SRAM_Read_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), dev_addr);  
	SRAM_Read_Buff((uint8_t *)&Rs485WxLockInfo, sizeof(RS485_WxLock_INFO), dev_addr+Dev_Privately);

    //开锁指令发送
    if(0x01 == Rs485WxLockInfo.WeixinAskLockFlag)  //微信锁控制标志位
    {
        if(SysTick_1ms - Rs485WxLockInfo.YG_Send_Tick>= 500)  //超过时间   
        {
            Rs485WxLockInfo.YG_Send_Tick = SysTick_1ms;
            if(Rs485WxLockInfo.YG_SendCnt > WxLockCtrlSendMax)
            {
                Rs485WxLockInfo.WeixinAskLockFlag = 0x00;
                Rs485WxLockInfo.YG_SendCnt = 0x00;
            }
            else
            {
                Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"杨格锁开锁指令:");
                RS485_YG_UnLock_Ctrl(&BUS_Public, &Rs485WxLockInfo);//控制开锁，控制开锁标志，在保证主机接收到微信锁的回复才会清0  
                Rs485WxLockInfo.YG_SendCnt++;             
            }
            Ret = 0x01;
        }
        
		/*通讯统计*/
		BLV_Communication_Record(&Rs485WxLockInfo.comm_record,0x01,0x00);
        
        if(Rs485WxLockInfo.DevSendCnt > WxLockCtrlSendMax)
        {
            if(Rs485WxLockInfo.DevOffline != DEV_IS_OFFLINE)
            {
                Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"YGLock DEV_IS_OFFLINE");
                LOG_Device_Online_Record(DEV_RS485_WxLock,SRAM_Read_Byte(dev_addr+Dev_Addr),LogInfo_Device_Offline);	//记录设备离线
            }
            Rs485WxLockInfo.DevOffline = DEV_IS_OFFLINE;   //判为离线
            if(Rs485WxLockInfo.DevOffline != Rs485WxLockInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
            {
                Rs485WxLockInfo.DevOfflineLast = Rs485WxLockInfo.DevOffline;	//更新上一次在线状态
                Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
            }
            Ret = 0x01;
        }
        else
        {
            Rs485WxLockInfo.DevSendCnt++;
            Ret = 0x01;
        }        
        
    }

    
    ////门锁设备上报身份,YYW,2025-12-20，杨格锁设备
    if(DevActionGlobal.DevActionU64Cond.EleState == 0x01)           //取电状态
    {
        if( (Rs485WxLockInfo.YG_haveID_Flag != 0)||(Rs485WxLockInfo.CJ_identity != CARD_Anonymous_Identity) )
        {
            Rs485WxLockInfo.YG_haveID_Flag = 0;
            Rs485WxLockInfo.CJ_identity = CARD_Anonymous_Identity;  //已经取电，门锁身份无效 
        //    DevActionGlobal.LockType = CARD_Anonymous_Identity;
            Ret = 0x01; 
        }

    }
    else if(DevActionGlobal.DevActionU64Cond.EleState == 0x02)      //断电状态
    {
        //有门锁上报身份
        if( (Rs485WxLockInfo.YG_haveID_Flag == 1)&&(SysTick_1ms - Rs485WxLockInfo.YG_IdVldPeriod_Tick >= Rs485WxLockInfo.YG_IdVldPeriod) )
        {
            Rs485WxLockInfo.YG_IdVldPeriod_Tick = SysTick_1ms;
            Rs485WxLockInfo.CJ_identity = CARD_Anonymous_Identity;
            
            Rs485WxLockInfo.YG_haveID_Flag = 0;
            Rs485WxLockInfo.YG_LockAction = CARD_Anonymous_Identity;
            Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"杨格锁上报身份-有效期到期, have_flag:%d, Time:%d",Rs485WxLockInfo.YG_haveID_Flag,Rs485WxLockInfo.YG_IdVldPeriod);
             Ret = 0x01; 
        }
        else if( (Rs485WxLockInfo.YG_haveID_Flag == 1)&&(Rs485WxLockInfo.CJ_identity != CARD_Anonymous_Identity) )
        {
            Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"杨格锁上报身份-赋值到全局门锁变量,LockType:%d, have_flag:%d ",Rs485WxLockInfo.CJ_identity,Rs485WxLockInfo.YG_haveID_Flag);
            
            Rs485WxLockInfo.YG_LockAction = Rs485WxLockInfo.CJ_identity;      //产生身份事件
            Rs485WxLockInfo.CJ_identity = CARD_Anonymous_Identity;
            
            Ret = 0x01; 
        }
        
    }
	
	if(0x01 == Ret)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485WxLockInfo, sizeof(RS485_WxLock_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485WxLockInfo,sizeof(RS485_WxLock_INFO),dev_addr+Dev_Privately);
	}
	
	return Ret;
}

/*******************************************************************************
* @brief    :杨格微信锁接收处理函数
* @param    :
*		dev_addr	:	设备信息地址
*		data_addr	:	485数据包地址
*		DataLen		:	数据包长度
* @retval   : 0-是杨格锁数据，1-不是杨格锁数据
* @note     :	先校验数据的最大、最小长度是否正确
*******************************************************************************/
uint8_t YangGe_WxLock_Data_Processing(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
    uint8_t data[YG_RECEIVE_MAX_LEN];	    //局部变量  接收的数据缓冲，协议决定
    uint8_t checksum = 0x00;
    uint8_t ret = 0x01;                     //默认不是杨格锁数据
    uint16_t udpdata = 0x00;               //上报服务器
    
    //1、接收的数据长度不对
	if( (len < 7)||(len > YG_RECEIVE_MAX_LEN) )	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"YangGe_WxLock Lens Err");
		return ret;
	}

    Device_Public_Information_G BUS_Public;	//共有数据结构体
	RS485_WxLock_INFO Rs485WxLockInfo;		//微信锁私有数据结构体
    
 	SRAM_Read_Buff(data, len, data_addr);   //将485数据读取出来

    //2、接收的数据长度不对
 	if( len != data[YG_DATA_LEN] )	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"YangGe_WxLock DataLens Err");
		return ret;
	}   
    
    //3、设备类型校验
    if(data[YG_DEVICE_TYPE] != DEV_RS485_WxLock)    
    {
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"YangGe_WxLock TYPE Err");
		return ret;        
    }    
 
     //4、计算校验和
    checksum = CheckSum_Overlook_Check_2(data,len,YG_CHECK);
    if(data[YG_CHECK] != checksum)
    {
        Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"YangGe_WxLock SumCheck Err:%02x,%02x",data[YG_CHECK],checksum);
        return ret;     //校验和错误，不是有效杨格锁数据
    }
	SRAM_Read_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), dev_addr);			//读取门锁地址的共有数据
	SRAM_Read_Buff((uint8_t *)&Rs485WxLockInfo, sizeof(RS485_WxLock_INFO), dev_addr+Dev_Privately);	//读取门锁地址的私有数据  

    //5、发送方地址校验
    if(data[YG_OUR_ADD] != BUS_Public.addr)    
    {
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"YangGe_WxLock ADDR Err");
		return ret;        
    }    
    //6、更新设备在线状态
	if(Rs485WxLockInfo.DevOffline != DEV_IS_ONLINE)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"杨格锁在线！");
		LOG_Device_Online_Record(BUS_Public.type, BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
	}
    
    Rs485WxLockInfo.DevOffline = DEV_IS_ONLINE;    	//设备在线
    if(Rs485WxLockInfo.DevOffline != Rs485WxLockInfo.DevOfflineLast)	    //当前在线状态不等于上一次在线状态
    {
        Rs485WxLockInfo.DevOfflineLast = Rs485WxLockInfo.DevOffline;	    //更新上一次在线状态
        Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
    }    
    BLV_Communication_Record(&Rs485WxLockInfo.comm_record,0x02,0x01);		//记录通讯成功
    Rs485WxLockInfo.DevSendCnt = 0x00;	//进入数据回复，清发送次数
    Rs485WxLockInfo.YG_SendCnt = 0x00;
      
    //7、验证命令是否为杨格锁支持的命令
    switch(data[YG_COMMAND])
    {
        case YG_CARD_ID:     //身份信息、门锁动作上报
            if( Rs485WxLockInfo.YG_RecvSN != (data[YG_SEND_TYPE]&0x0F) )        //发送序号递增
            {
                Rs485WxLockInfo.YG_RecvSN = (data[YG_SEND_TYPE]&0x0F);
                
                RS485_YG_LogID_RecAsk(&BUS_Public,&Rs485WxLockInfo,data,len,0x01); //回复
                // 处理身份信息和门锁动作
                if( (data[YG_PARAM] & 0x01) != 0x00)    
                {
                    if(data[YG_PARAM+1] != CARD_Anonymous_Identity)
                    {
                        Rs485WxLockInfo.CJ_identity = data[YG_PARAM+1];     //门锁私有变量、身份信息
                        Rs485WxLockInfo.YG_IdVldPeriod_Tick = SysTick_1ms;  //身份上报记录时间戳   
                        Rs485WxLockInfo.YG_haveID_Flag = 1;
                    }

                    Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"杨格锁身份上报,ID:%d, have_flag:%d ",Rs485WxLockInfo.CJ_identity,Rs485WxLockInfo.YG_haveID_Flag);
                }
                
                //门锁动作上报控制位
                if( (data[YG_PARAM] & 0x02) != 0x00)    
                {
                    Rs485WxLockInfo.CJ_status = data[YG_PARAM+6];           //门锁私有变量、门锁动作
                    
                    //门锁动作上报服务器
                    udpdata = (0x80) + ((Rs485WxLockInfo.CJ_status&0xff)<<8);
                    Udp_Addtion_Roomstate(DEV_RS485_WxLock,BUS_Public.addr,0x0001,udpdata);
                    
                    Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"杨格锁动作上报:%02x",Rs485WxLockInfo.CJ_status);
                }                   
            }
            else if( (data[YG_SEND_TYPE]&YG_RETRY_FLAG) == YG_RETRY_FLAG )      //重发数据,回复但不处理
            {
                RS485_YG_LogID_RecAsk(&BUS_Public,&Rs485WxLockInfo,data,len,0x01);              
            }  

            /*日志记录*/
            LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,data,len);            
            ret = 0x00;   //是杨格锁数据
            break;
        case YG_BATMon:     //电量、无线模块网络质量上报
            if( Rs485WxLockInfo.YG_RecvSN != (data[YG_SEND_TYPE]&0x0F) )        //发送序号递增
            {
                Rs485WxLockInfo.YG_RecvSN = (data[YG_SEND_TYPE]&0x0F);                
                //回复
                RS485_YG_LogID_RecAsk(&BUS_Public,&Rs485WxLockInfo,data,len,0x01);
            
                Rs485WxLockInfo.CJ_kwh = data[YG_PARAM];                        // 处理电量信息    
                Rs485WxLockInfo.CJ_signal = data[YG_PARAM+1];                   // 处理网络质量
                
                //电量上报服务器
                //if(Rs485WxLockInfo.CJ_kwh < Rs485WxLockInfo.YG_kwh_LowThr)
                {
                    udpdata = (0x40)+ ((Rs485WxLockInfo.CJ_kwh&0xff)<<8);
                    Udp_Addtion_Roomstate(DEV_RS485_WxLock,BUS_Public.addr,0x0001,udpdata);
                }
                
                Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"杨格锁电量上报:%d，%02x",Rs485WxLockInfo.CJ_kwh,Rs485WxLockInfo.CJ_signal);
            }
            else if( (data[YG_SEND_TYPE]&YG_RETRY_FLAG) == YG_RETRY_FLAG )      //重发数据,回复但不处理
            {
                RS485_YG_LogID_RecAsk(&BUS_Public,&Rs485WxLockInfo,data,len,0x01);             
            }           
            
            /*日志记录*/
            LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,data,len);              
            ret = 0x00;   //是杨格锁数据
            break;
        case YG_UNLOCKING:   //下发开锁控制后设备的响应
            if( Rs485WxLockInfo.YG_SendSN != (data[YG_SEND_TYPE]&0x0F) )        //发送序号递增
            {
                Rs485WxLockInfo.YG_SendSN = (data[YG_SEND_TYPE]&0x0F);                   
                // 处理开锁响应
                Rs485WxLockInfo.WeixinAskLockFlag = 0;
                Rs485WxLockInfo.WeixinCtrlSendCnt = 0;
                Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"杨格锁开锁指令回复成功");
            }
            
            /*日志记录*/
            LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,data,len);              
            ret = 0x00;   //是杨格锁数据
            break;
        default:
            break;
    }

    
    // 如果是有效的杨格锁数据，更新私有数据到SRAM
    if(ret == 0x00)
    {
        BUS_Public.check = 0x00;	//校验和先置0
        BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485WxLockInfo, sizeof(RS485_WxLock_INFO));	//计算检验和        
        SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);	//将共有数据保存
        SRAM_Write_Buff((uint8_t *)&Rs485WxLockInfo, sizeof(RS485_WxLock_INFO), dev_addr+Dev_Privately);
    }
    
    return ret;
}


/************************************************
* @brief      : 对门锁上报数据的回复数据组包
* @param      :
*           BUS_Public        : 设备公共结构体
*           Rs485WxLockInfo   : 锁设备私有结构体
*           data      : 上报数据缓存
*           DataLen   : 上报数据长度
*           ret       : 回复数据处理结果
* @retval     : 无
* @note       : 默认回复 结果 0x01
  *************************************************/
void RS485_YG_LogID_RecAsk(Device_Public_Information_G* BUS_Public, RS485_WxLock_INFOP Rs485WxLockInfo, uint8_t *data, uint16_t DataLen, uint8_t ret)
{
    uint16_t SdLens = 0x08;
    uint8_t SdData[0x08];
    uint8_t SN = 0x00;
  
	Rs485WxLockInfo->YG_RecvSN++;	                    
	if(Rs485WxLockInfo->YG_RecvSN > 15) Rs485WxLockInfo->YG_RecvSN = 1;
	SN |= (Rs485WxLockInfo->YG_RecvSN & 0x0F);	        //发送类型    
       
    
    SdData[YG_OUR_ADD] = data[YG_RECE_ADD];
    SdData[YG_SEND_TYPE] = SN;
    SdData[YG_DEVICE_TYPE] = data[YG_DEVICE_TYPE];
    SdData[YG_RECE_ADD] = data[YG_OUR_ADD];
    SdData[YG_DATA_LEN] = SdLens;
    SdData[YG_CHECK] = 0x00;
    SdData[YG_COMMAND] = data[YG_COMMAND];
    SdData[YG_PARAM] = ret;
    
    SdData[YG_CHECK] = SOR_CRC(SdData,SdLens);
    
    MCU485_SendString(BUS_Public->port,SdData,SdLens);
}


/************************************************
* @brief      : 门锁开锁控制发送函数
* @param      : BUS_Public       : 设备公共结构体
* @param      : Rs485WxLockInfo  : 锁设备私有结构体
* @retval     : 无
* @note       : 发送开锁指令
  *************************************************/
void RS485_YG_UnLock_Ctrl(Device_Public_Information_G *BUS_Public, RS485_WxLock_INFOP Rs485WxLockInfo)
{
    uint16_t SdLens = 0x08;
	uint8_t SendBuf[8];
    uint8_t SN = 0x00;
    
	if(Rs485WxLockInfo->YG_SendCnt == 0x00)	//首发
	{
		Rs485WxLockInfo->YG_SendSN++;	//发送序号递增
		if(Rs485WxLockInfo->YG_SendSN > 15) Rs485WxLockInfo->YG_SendSN = 1;
		SN |= (Rs485WxLockInfo->YG_SendSN & 0x0F);	//发送类型
	}
	else	//重发
	{
		SN |= (Rs485WxLockInfo->YG_SendSN & 0x0F);	//发送类型
		SN |= YG_RETRY_FLAG;
	}
	
	SendBuf[YG_OUR_ADD] = 0x00;     //主机地址
	SendBuf[YG_SEND_TYPE] = SN;
	SendBuf[YG_DEVICE_TYPE] = BUS_Public->type;   
	SendBuf[YG_RECE_ADD] = BUS_Public->addr;
	SendBuf[YG_DATA_LEN] = SdLens;
	SendBuf[YG_CHECK] = 0x00;
	SendBuf[YG_COMMAND] = YG_UNLOCKING;
	SendBuf[YG_PARAM] = 0x01;

    SendBuf[YG_CHECK] = SOR_CRC(SendBuf,SdLens);    //和校验
	
    MCU485_SendString(BUS_Public->port,SendBuf,SdLens);

    /*日志记录*/
    LOG_Device_COMM_Send_Control_Record(BUS_Public->port, BUS_Public->baud, SendBuf, SdLens);
		
}

/****************************************************************
* @brief    : 杨格微信锁控制接口函数        
* @param    :
*    Rs485WxLockInfo : 微信锁数据结构体     
*    Addr  		     : 无
*    OnOff           : 无
* @retval   : 无
* @brief    : 指定微信锁打开
* @nota     :   1、杨格微信锁只控制开，不控制关
                2、门锁身份信息取电控制
*****************************************************************/
void YG_RS485Lock_IndiCtrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr, uint16_t Addr, uint16_t OnOff)
{
	RS485_WxLock_INFO Rs485WxLockInfo;
	uint8_t temp1 = 0;  //crc_val = 0,

	Device_Public_Information_G BUS_Public;
	
	if(NULL == devaddr)
	{
		return ;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485WxLockInfo,sizeof(RS485_WxLock_INFO),devaddr+Dev_Privately);
	SRAM_Read_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), devaddr);  
	switch(OnOff&0x00ff)
	{
		case 0x01:  //开锁
		case 0x02:  //关锁

			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"杨格锁,控制和锁请求标志置位");		//
			Rs485WxLockInfo.WeixinAskLockFlag = 0x01;   //请求开锁标志置位
			Rs485WxLockInfo.WeixinCtrlFlag = 0x01; 
			Rs485WxLockInfo.WeixinCtrlSendCnt = WxLockCtrlSendMax;
			Rs485WxLockInfo.WeixinCtrlState = OnOff&0x00ff;
            Rs485WxLockInfo.YG_SendCnt = 0x00;
			temp1 = 0x01;
			break;
        case 0x03:
            
            Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"杨格锁,门锁取电标志");		//
            Rs485WxLockInfo.YG_LockAction_Flag = 0x01;      //门锁事件触发启用标志

            temp1 = 0x01;
            break;

		default:
 
			break;
	}
	if(0x00 != temp1)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485WxLockInfo, sizeof(RS485_WxLock_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485WxLockInfo,sizeof(RS485_WxLock_INFO),devaddr+Dev_Privately);
				
	}	
	
}

/****************************************************************
* @brief    : 杨格微信锁输出接口     
* @param    :
*    DevAddr        : 设备地址     
*    DevInputLoop   : 回路
*    DevInputType   : 内容
* @retval   :   CtrlInvalid   : 控制无效
                CtrlValid     :v控制有效
* @brief    : 微信锁执行事件
* @nota     : 杨格微信锁执行事件
*****************************************************************/
uint8_t Dev_YangGeLock_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType)
{
    Device_Public_Information_G BUS_Public;	//共有数据结构体
	RS485_WxLock_INFO Rs485WxLockInfo;		//微信锁私有数据结构体
	uint8_t Ret = CtrlInvalid;              //控制无效
	uint8_t Keep1 = 0x00;
    
	if(DevAddr == 0x00)	return Ret;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485WxLockInfo,sizeof(RS485_WxLock_INFO),DevAddr+Dev_Privately);	/*拷贝设备私有信息*/
	
 //   Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"杨格锁取电事件:%d, %d",Rs485WxLockInfo.YG_LockAction_Flag,(Rs485WxLockInfo.YG_LockAction+1));
    if( (Rs485WxLockInfo.YG_LockAction + 1) == DevInputType)
    {
        if(Rs485WxLockInfo.YG_LockAction_Flag == 0x01)
        {
            Rs485WxLockInfo.YG_LockAction_Flag = 0x00;
            Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"杨格锁身份对应动作:%d ",(Rs485WxLockInfo.YG_LockAction + 1));
            
            Rs485WxLockInfo.YG_LockAction = 0x00;

            Ret = CtrlValid;
            Keep1 = 0x01;
        }
    }
    
    
    
	if(0x00 != Keep1)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485WxLockInfo, sizeof(RS485_WxLock_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485WxLockInfo,sizeof(RS485_WxLock_INFO),DevAddr+Dev_Privately);
	}
	
	return Ret;
}






