/*--------------------------------------------------------------------------------------
*  @file     ZigbeeDataHandle.c
*  @author   ZhangJing
*  @version  base on stm32f0x   
*  @date     2015.09.11
*  @brief    Zigbee数据处理
---------------------------------------------------------------------------------------*/

#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "TypeDefine.h"
#include "Driver.h"
#include "USARTSetup.h"
#include "DrawLCDGUI.h"
#include "SystemAlarm.h"
#include "ParameterSet.h"
#include "ZigbeeDataHandle.h"
//#include "ZigbeeNet.h"
#include "ControlSystem.h"
#include "lorawan.h"

#include <stdlib.h> 
#include <stdio.h> 
uint8_t zigbeeXmitCounts = 0;//zigbee发送累计
extern uint8_t Recive_lorawanID_flag;  //接收LORAWAN ID号
extern uint8_t mcu_id[8];              //mcu ID号缓冲区
extern uint8_t lorawan_send_data_flag;////数据发送标志
extern uint8_t Start_send  ; //开始发送标志
//uint16_t random_cont=0;                //随机数种子值
//uint32_t random_data=0;                //随机数
uint8_t RunToLorawawn_flag=0;          //在运行时，发送lorawan数据的标志
uint8_t StartToRun_flag=0;             //当按下运行按键的时候，置位该标志
uint8_t Running_flag=0;                //在正在输注状态下，置位该标志
uint8_t check_succeed=0;               //收到上位发送的数据，校验成功后，置位该标
uint8_t send_number=0;                 //数据发送次数
uint16_t FailureSendNumber =0;			//长包发送失败次数
uint8_t lorawan_to_sleep=0;            //lorawan模块进入睡眠标志
uint8_t StartToRunSend_flag = 0;		//发送数据完成标志
uint8_t PauseSend_flag = 0; 			//暂停发送数据完成标志
/***************************************************************************************
*  Function:	CommandToZigbeeHandle						                                                           
*  Object:		发给zigbee模块的命令
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempIndex = 0;发送赋值索引
*								 uint8_t tempXOR = 0;计算校验值    
*							2、USART2Send发送
****************************************************************************************/
void CommandToZigbeeHandle( void )
{
	uint8_t tempIndex = 0;
	uint8_t tempXOR = 0;
	uint8_t i;
	
	//读设备ID
	usart2XmitBuffer[tempIndex++] = 0xfc;
	usart2XmitBuffer[tempIndex++] = 0x03;
	usart2XmitBuffer[tempIndex++] = 0x05;
	usart2XmitBuffer[tempIndex++] = 0x00;
	usart2XmitBuffer[tempIndex++] = 0x00;
	usart2XmitBuffer[tempIndex++] = 0x00;

	for( i = 0; i < 6; i++ )
	{
		tempXOR ^= usart2XmitBuffer[i];
	}
	usart2XmitBuffer[tempIndex++] = tempXOR;//校验
	
	USART2Send( usart2XmitBuffer, tempIndex ); 
}

/***************************************************************************************
*  Function:	CommandToZigbeeReadNet						                                                           
*  Object:		发给zigbee模块的读网络命令
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempIndex = 0;发送赋值索引   
*							2、USART2Send发送           
****************************************************************************************/
void CommandToZigbeeReadNet( void )
{
	uint8_t tempIndex = 0;
	
	usart2XmitBuffer[tempIndex++] = 0xfc;
	usart2XmitBuffer[tempIndex++] = 0x03;
	usart2XmitBuffer[tempIndex++] = 0x05;
	usart2XmitBuffer[tempIndex++] = 0x00;
	usart2XmitBuffer[tempIndex++] = 0x00;
	usart2XmitBuffer[tempIndex++] = 0x00;
	usart2XmitBuffer[tempIndex++] = 0xfa;//校验
	USART2Send( usart2XmitBuffer, tempIndex ); 
}

/***************************************************************************************
*  Function:	CommandToZigbeeReadFatherADDR						                                                           
*  Object:		发给zigbee模块的读父节点MAC地址命令
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempIndex = 0;发送赋值索引
*								 uint8_t tempXOR = 0;计算校验值    
*							2、USART2Send发送		              
****************************************************************************************/
void CommandToZigbeeReadFatherADDR( void )
{
	uint8_t tempIndex = 0;
	uint8_t tempXOR = 0;
	uint8_t i;
	
	//读设备ID
	usart2XmitBuffer[tempIndex++] = 0xfc;
	usart2XmitBuffer[tempIndex++] = 0x03;
	usart2XmitBuffer[tempIndex++] = 0x1d;
	usart2XmitBuffer[tempIndex++] = 0x00;
	usart2XmitBuffer[tempIndex++] = 0x00;
	usart2XmitBuffer[tempIndex++] = 0x00;
	for( i = 0; i < 6; i++ )
	{
		tempXOR ^= usart2XmitBuffer[i];
	}
	usart2XmitBuffer[tempIndex++] = tempXOR;//校验
	
	USART2Send( usart2XmitBuffer, tempIndex ); 
}

/***************************************************************************************
*  Function:	DataFromZigbeeHandle						                                                           
*  Object:		处理来自zigbee的数据
*  输入:			uint8_t *recvDataBuf  待处理的数据缓冲; 
*							uint8_t recvDataLen		待处理的数据长度;
*  输出:			无	                                     
*  备注:			              
****************************************************************************************/
void DataFromZigbeeHandle( uint8_t *recvDataBuf, uint8_t recvDataLen )
{
	
}

/***************************************************************************************
*  Function:	RTDToZigbeeHandle						                                                           
*  Object:		实时数据发送到zigbee的数据
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempIndex = 0;发送赋值索引
*								 uint8_t tempXOR = 0;计算校验值    
*							2、USART2Send发送			              
****************************************************************************************/
void RTDToZigbeeHandle( void )
{
//	uint8_t tempIndex = 0;
	uint8_t i;
	uint16_t tempCRC = 0;
	
	usart2XmitBuffer[0] = 33;
	usart2XmitBuffer[1] = 0x15;//包类型 
	
	for( i = 0 ; i <8 ; i++ )//泵号
	{
		usart2XmitBuffer[i+2] = setParamInfo.deviceID[i];
	}
	
	/*usart2XmitBuffer[tempIndex++] = (uint8_t)( displayTimeBuf.year - 2015 );//时间
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.month;
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.date;
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.hour;
	
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.minute;
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.seconds;*/
	
	usart2XmitBuffer[10] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
	usart2XmitBuffer[11] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
	usart2XmitBuffer[12] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
	usart2XmitBuffer[13] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
	//usart2XmitBuffer[14] = 0;//住院号	
	
	usart2XmitBuffer[14] = (uint8_t)( verifyInfo.sickroom & 0x00ff );
	usart2XmitBuffer[15] = (uint8_t)( ( verifyInfo.sickroom &0xff00 ) >> 8 );//病区号
	
	usart2XmitBuffer[16] = (uint8_t)( verifyInfo.bedNO & 0x00ff );
	usart2XmitBuffer[17] = (uint8_t)( ( verifyInfo.bedNO &0xff00 ) >> 8 );//病床号
	
	//usart2XmitBuffer[tempIndex++] = (uint8_t)( realTimeData.surplusDose & 0x00ff );	
	//usart2XmitBuffer[tempIndex++] = (uint8_t)( ( realTimeData.surplusDose & 0xff00 ) >> 8 );//剩余量	
	
	usart2XmitBuffer[18] = (uint8_t)( setParamInfo.continueDose1 & 0x00ff );	
	usart2XmitBuffer[19] = (uint8_t)( ( setParamInfo.continueDose1 & 0xff00 ) >> 8 );//持续量	
	
//	usart2XmitBuffer[tempIndex++] = (uint8_t)( EngineeringModeValue.selfcontrol & 0x00ff );
//	usart2XmitBuffer[tempIndex++] = (uint8_t)( ( EngineeringModeValue.selfcontrol & 0xff00 ) >> 8 );//自控量	
	
	usart2XmitBuffer[20] = (uint8_t)( setParamInfo.lockTime1 & 0x00ff );
	usart2XmitBuffer[21] = (uint8_t)( ( setParamInfo.lockTime1 & 0xff00 ) >> 8 );//锁时	
	
	usart2XmitBuffer[22] = (uint8_t)( setParamInfo.limitDose1 & 0x00ff );
	usart2XmitBuffer[23] = (uint8_t)( ( setParamInfo.limitDose1 & 0xff00 ) >> 8 );//极限量		
	
	//usart2XmitBuffer[tempIndex++] = (uint8_t)( realTimeData.inputDose & 0x00ff );
	//usart2XmitBuffer[tempIndex++] = (uint8_t)( ( realTimeData.inputDose & 0xff00 ) >> 8 );//输入量	
	
	usart2XmitBuffer[24] = setParamInfo.firstDose;//首次量
	
	usart2XmitBuffer[25] = (uint8_t)( setParamInfo.superaddition1 & 0x00ff );
	usart2XmitBuffer[26] = (uint8_t)( ( setParamInfo.superaddition1 & 0xff00 ) >> 8 );//追加量
	
	usart2XmitBuffer[27] = (uint8_t)( FailureSendNumber & 0x00ff );
	usart2XmitBuffer[28] = (uint8_t)( ( FailureSendNumber & 0xff00 ) >> 8 );//发送失败条数	
	
/*	usart2XmitBuffer[29] = (uint8_t)( realTimeData.validCount & 0x00ff );
	usart2XmitBuffer[30] = (uint8_t)( ( realTimeData.validCount & 0xff00 ) >> 8 );//有效次数	
	
	usart2XmitBuffer[31] = (uint8_t)( realTimeData.invalidCount & 0x00ff );
	usart2XmitBuffer[32] = (uint8_t)( ( realTimeData.invalidCount & 0xff00 ) >> 8 );//无效次数	*/
	
	//usart2XmitBuffer[tempIndex++] = (uint8_t)( realTimeData.validCount + realTimeData.invalidCount );//总按次数
	
	if(realTimeData.stateRun!=Poweroff)
	{
		if( sysAlarmFlag.BubbleFault == ALARM_ON )
		{
			usart2XmitBuffer[29] = (( usart2XmitBuffer[29] & 0xfc) | 0x01);//气泡或无液故障 0000 0001
		}
		else if( sysAlarmFlag.JamFault == ALARM_ON )
		{
			usart2XmitBuffer[29] = (( usart2XmitBuffer[29] & 0xfc) | 0x02);//堵塞故障 0000 0010
		}	
		else if( sysAlarmFlag.NonePillCase == ALARM_ON )
		{
			usart2XmitBuffer[29] = (( usart2XmitBuffer[29] & 0xfc) | 0x03);//未装药盒故障 0000 0011
		}
		else
		{
			usart2XmitBuffer[29] &= 0xfc;// 1111 1100
		}
		
		/*if( sysAlarmFlag.InstallFalt == ALARM_ON )
		{
			usart2XmitBuffer[tempIndex++] = 1;//未装夹到位故障
		}
		else
		{
			usart2XmitBuffer[tempIndex++] = 0;
		}*/
		
		if( sysAlarmFlag.MaxFault == ALARM_ON )//极限量状态
		{
			usart2XmitBuffer[29] = (( usart2XmitBuffer[29] & 0xc7) | 0x08);// 0000 1000
		}
		else if( ( sysAlarmFlag.InputEnding == ALARM_ON ) && ( sysAlarmFlag.InputEnd == ALARM_OFF ) )//输液将结束
		{
			usart2XmitBuffer[29] = (( usart2XmitBuffer[29] & 0xc7) | 0x10);// 0001 0000
		}
		else if( ( sysAlarmFlag.InputEnding == ALARM_OFF ) && ( sysAlarmFlag.InputEnd == ALARM_ON ) )//输液结束
		{
			usart2XmitBuffer[29] = (( usart2XmitBuffer[29] & 0xc7) | 0x20);// 0010 0000
		}
		else
		{
			usart2XmitBuffer[29] &= 0xc7;
		}
		
		if( sysAlarmFlag.VoltLowest == ALARM_ON )//电池电量偏低状态
		{
			usart2XmitBuffer[29] = (( usart2XmitBuffer[29] & 0x3f) | 0x40);// 0100 0000
		}
		else if( sysAlarmFlag.VoltLowest == ALARM_PREPARE )//电池电量低预报警
		{
			usart2XmitBuffer[29] = (( usart2XmitBuffer[29] & 0x3f) | 0x80);// 1000 0000
		}
		else 
		{
			usart2XmitBuffer[29] &= 0x3f;
		}
	}
	else 
	{
		usart2XmitBuffer[29] &= 0x00;
	}
	
	if( sysAlarmFlag.MotorFalt == ALARM_ON )//电机失控
	{
		usart2XmitBuffer[30] = (( usart2XmitBuffer[30] & 0xfc) | 0x01);// 0000 0001;
	}
	
	else if( ( sysAlarmFlag.MechanicalFault == ALARM_ON ) || ( sysAlarmFlag.MechanicalFault == ALARM_CONTINUE ) )//机械故障
	{
		usart2XmitBuffer[30] = (( usart2XmitBuffer[30] & 0xfc) | 0x02);// 0000 0010; 
	}
	else
	{
		usart2XmitBuffer[30] &= 0xfc;
	}
	/*
	if( TaskSchedulerFlag.callFunctionFlag == TASK_FLAG_SET )//呼叫功能
	{
		usart2XmitBuffer[34] = (( usart2XmitBuffer[34] & 0xfb) | 0x04);// 0000 0100; 
	}
	else
	{
		usart2XmitBuffer[34] &= 0xfb;
	}
	
	
	if( sysAlarmFlag.Zhentongxiaoguo == ALARM_ON )//镇痛效果
	{
		usart2XmitBuffer[34] |= 0x08;  //0000 1000
	}
	else
	{
		usart2XmitBuffer[34] &= 0xf7;
	}*/

	
	
	switch( realTimeData.stateRun )
	{
		case FirstRun:
		case MainRun:
		case Airout:
		case PCARun:
			usart2XmitBuffer[30] = ((usart2XmitBuffer[30] & 0x0f) | 0x20);//运行状态 0010,xxxx
			break;
		case Pause:
			usart2XmitBuffer[30] = ((usart2XmitBuffer[30] & 0x0f) | 0x30);//暂停状态 0011,xxxx
			break;
		case Poweroff:
			usart2XmitBuffer[30] &= 0x0f;//关机状态 0000,xxxx
			break;
		case Poweron:
			usart2XmitBuffer[30] = ((usart2XmitBuffer[30] & 0x0f) | 0x10);//开机状态 0001,xxxx
			break;
		default:
			usart2XmitBuffer[30] = ((usart2XmitBuffer[30] & 0x0f) | 0x40);//待机状态，现在没有待机状态，全按开机传 0100,xxxx
			break;
	}
	
	if( realTimeData.batteryVolt > 100 )//电池电量，发送电量百分比
	{
		usart2XmitBuffer[31] = 100;
	}
	else
	{
		usart2XmitBuffer[31] = realTimeData.batteryVolt;
	}
	tempCRC = CalCRC16(&usart2XmitBuffer[1], 31 );
	usart2XmitBuffer[32] = tempCRC % 256;
	usart2XmitBuffer[33] = tempCRC / 256;
 //	USART2Send( usart2XmitBuffer, USART2_XMIT_LEN ); 
	transparent_send_data(usart2XmitBuffer,USART2_XMIT_LEN); //通过LORAWAN模块发送数据
}

/***************************************************************************************
*  Function:	RunToLorawawn						                                                           
*  Object:		实时数据发送到zigbee的数据
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempIndex = 0;发送赋值索引
*								 uint8_t tempXOR = 0;计算校验值    
*							2、USART2Send发送			              
****************************************************************************************/
void RunToLorawawn( void )
{
//	uint8_t tempIndex = 0;
//	uint8_t i;
	uint16_t tempCRC = 0;
	
	usart2XmitBuffer[0] = 15;
	usart2XmitBuffer[1] = 0x13;//包类型 

/*	
	for( i = 0 ; i <8 ; i++ )//泵号
	{
		usart2XmitBuffer[i+2] = setParamInfo.deviceID[i];
	}*/
	
	usart2XmitBuffer[2] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ; //住院号	
	usart2XmitBuffer[3] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
	usart2XmitBuffer[4] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
	usart2XmitBuffer[5] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
	
	
	usart2XmitBuffer[6] = (uint8_t)( setParamInfo.totalDose & 0x00ff );
	usart2XmitBuffer[7] = (uint8_t)( ( setParamInfo.totalDose & 0xff00 ) >> 8 );//总量	
		
	
	usart2XmitBuffer[8] = (uint8_t)( realTimeData.inputDose & 0x00ff );
	usart2XmitBuffer[9] = (uint8_t)( ( realTimeData.inputDose & 0xff00 ) >> 8 );//输入量	
	
	
	
	
	if( realTimeData.batteryVolt > 100 )//电池电量，发送电量百分比
	{
		usart2XmitBuffer[10] = 100;
	}
	else
	{
		usart2XmitBuffer[10] = realTimeData.batteryVolt;
	}
	
  usart2XmitBuffer[11] = (uint8_t)( realTimeData.validCount & 0x00ff );  //有效次数
	usart2XmitBuffer[12] = (uint8_t)( realTimeData.invalidCount & 0x00ff );//无效次数	
	
	if( TaskSchedulerFlag.callFunctionFlag == TASK_FLAG_SET )//呼叫功能
	{
		usart2XmitBuffer[13] = (( usart2XmitBuffer[13] & 0xfe) | 0x01);// 0000 0001; 
	}
	else
	{
		usart2XmitBuffer[13] &= 0xfe;
	}
	if( sysAlarmFlag.Zhentongxiaoguo == ALARM_ON )//镇痛效果
	{
		usart2XmitBuffer[13] = (( usart2XmitBuffer[13] & 0xfd) | 0x02);  //0000 0010
	}
	else
	{
		usart2XmitBuffer[13] &= 0xfd;
	}

  if( sysAlarmFlag.VoltLowest == ALARM_PREPARE )//电池电量低预报警
	{
		usart2XmitBuffer[13] = (( usart2XmitBuffer[13] & 0xfb) | 0x04);// 0000 0100
	}
	else 
	{
		usart2XmitBuffer[13] &= 0xfb;
	}	
	
	tempCRC = CalCRC16(&usart2XmitBuffer[1], 13 );
	usart2XmitBuffer[14] = tempCRC % 256;
	usart2XmitBuffer[15] = tempCRC / 256;
	transparent_send_data(usart2XmitBuffer,16); //通过LORAWAN模块发送数据
}



/***************************************************************************************
*  Function:	EvaluateToZigbeeHandle						                                                           
*  Object:		随访数据发送到zigbee的数据
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempIndex = 0;发送赋值索引   
*							2、USART2Send发送		              
****************************************************************************************/
void EvaluateToZigbeeHandle( void )
{
	uint8_t tempIndex = 0;
	uint8_t i;
	
	//usart2XmitBuffer[tempIndex++] = 0xaa;
	//usart2XmitBuffer[tempIndex++] = 0x55;
	usart2XmitBuffer[tempIndex++] = 26;
	usart2XmitBuffer[tempIndex++] = 3;//包类型

	for( i = 0 ; i < 8 ; i++ )
	{
		usart2XmitBuffer[tempIndex++] = setParamInfo.deviceID[i];
	}
	
	usart2XmitBuffer[tempIndex++] = (uint8_t)( displayTimeBuf.year - 2015 );//时间
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.month;
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.date;
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.hour;
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.minute;
	usart2XmitBuffer[tempIndex++] = displayTimeBuf.seconds;
	
	usart2XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
	usart2XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
	usart2XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
	usart2XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
	//usart2XmitBuffer[tempIndex++] = 0;//住院号	
	
	usart2XmitBuffer[tempIndex++] = (uint8_t)( verifyInfo.sickroom & 0x00ff );
	usart2XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.sickroom &0xff00 ) >> 8 );//病区号
	
	usart2XmitBuffer[tempIndex++] = (uint8_t)( verifyInfo.bedNO & 0x00ff );
	usart2XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.bedNO &0xff00 ) >> 8 );//病床号
	
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.Static;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.Activity;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.CACSI;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.PONV;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.pruritus;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.LeftArm;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.RightArm;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.LeftLeg;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.RightLeg;
	usart2XmitBuffer[tempIndex++] = evaluateTransfuseValue.Valuer;
	
	usart2XmitBuffer[tempIndex++] = 0;//CRC
	usart2XmitBuffer[tempIndex++] = 0;//CRC
	
	/*for( i = tempIndex; i < USART2_XMIT_LEN ; i++ )
	{
		usart2XmitBuffer[tempIndex++] = 0xff;
	}*/
 //	USART2Send( usart2XmitBuffer, USART2_XMIT_LEN ); 
	transparent_send_data(usart2XmitBuffer,27); //通过LORAWAN模块发送数据
}

/***************************************************************************************
*  Function:	ZigbeeXmitHandle						                                                           
*  Object:		发送zigbee数据
*  输入:			无
*  输出:			无	                                     
*  备注:			1、开机后先读取zigbee地址作为设备ID号，然后根据 TaskSchedulerFlag.zigbeeXmitFlag标志位发送实时数据  
*							2、为节省功耗，每次发送完数据需将zigbee电源关掉
*							3、if( TaskSchedulerFlag.zigbeeETXmitFlag == TASK_FLAG_WAIT )//发送前8s做发送准备
								{
									ZigbeePWRCtl( DRIVER_ON );//zigbee电源开
								}
****************************************************************************************/
void ZigbeeXmitHandle( void )
{

	if( EngineeringModeValue.networkState ==  ENGINEERINGMODE_ON )
	{		
	/*	if( TaskSchedulerFlag.zigbeeETXmitFlag != TASK_FLAG_CLEAR )//zigbee发送随访信息
		{	
			if( TaskSchedulerFlag.zigbeeETXmitFlag == TASK_FLAG_SET )
			{
				if( speakerWorkStep != emSpeakerNoneWork)
					return;
					
					EvaluateToZigbeeHandle();
					emEvaluateTransfuseChoose = UploadFinshed;	
					if( emCurrentPicture == EvaluateUploadEnterDisp )
					{
						emDisplayPicture = EvaluateUploadEnterDisp;
					}
					TaskSchedulerFlag.zigbeeETXmitFlag = TASK_FLAG_CLEAR;
			}
		}*/
		if(Start_send == 1)
		{
			if( realTimeData.stateRun == Poweroff )
			{
				TaskSchedulerFlag.sysPowerOffFlag = TASK_FLAG_SET;
			}
			return;
		}						
		if(EngineeringModeValue.test == 0)
		{
			if( realTimeData.stateRun == Poweroff )
			{
				TaskSchedulerFlag.sysPowerOffFlag = TASK_FLAG_SET;
			}			
			else if(realTimeData.stateRun == MainRun)
			{
				if(TaskSchedulerFlag.zigbeeXmitFlag == TASK_FLAG_SET)
				{			
					RunToLorawawn();
					TaskSchedulerTimer.rtdXmitTimer = TWENTY_SECOND_TIMER;//20s发送一次
					TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_CLEAR;
				}
			}
			
		}
		else 
		{
			if((( TaskSchedulerFlag.zigbeeXmitFlag != TASK_FLAG_CLEAR ) && (realTimeData.stateRun != MainRun) && (realTimeData.stateRun != FirstRun) && (realTimeData.stateRun != PCARun) ) || (StartToRun_flag ==1))//zigbee发送实时信息
			{		
				if(( TaskSchedulerFlag.zigbeeXmitFlag == TASK_FLAG_SET ) || (StartToRun_flag == 1))
				{
				   if( speakerWorkStep != emSpeakerNoneWork)
						return;
					 /* if(check_succeed == 1)//首先判断是否收到上位机的握手信号，收到了，直接退出，不再发生信息
					 {
						 StartToRun_flag=0;//接收到上位机发送的数据，清除该标志						 
						 check_succeed=0;
						 send_number=0;
						 TaskSchedulerTimer.rtdXmitTimer = TWO_MINUTE_TIMER;  //接收到上位机发送的数据，开始定时20分钟
						 lorawan_to_sleep=1;//接收到上位机发送的数据，让模块进入睡眠状态
						 TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_CLEAR;
						 return;
					 }*/

					 if(Running_flag == 1)//在输注状态下，出现报警或者暂停等情况，先发送一次输注信息，然后再延时发送报警信息
					 {					 
						 if(TaskSchedulerFlag.StartToRunDontSend == SendSuceed)	
						 {							 
							 TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_CLEAR;
							 StartToRun_flag=0;
							 TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;  //定时发送数据开始 10s
							 if((check_succeed == 1) || (send_number >= 3))
							 {
								 check_succeed=0;
								 send_number=0;
								 Running_flag=0; //接收到上位机发送的数据，清除该标志
								 lorawan_to_sleep=1;//接收到上位机发送的数据，让模块进入睡眠状态
							 }
							 else
							 {			
								RunToLorawawn();//调用发送数据包
								 send_number++;
							 }
						}
						 else
						 {
							Running_flag = 0;
						 }

						 return ;
					 }
									 					  											
					if( realTimeData.stateRun == Poweroff )
					{
						if( zigbeeXmitCounts < 2 )//增加发送两条，2s后发一次
						{	
							TaskSchedulerTimer.rtdXmitTimer = TWO_SECOND_TIMER;
							TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_WAIT;
							RTDToZigbeeHandle();	
						}
						if( zigbeeXmitCounts >= 3 )
						{
							 zigbeeXmitCounts = 0;
							 TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_WAIT;//如果不加此条语句，会重复发送多次	
							 TaskSchedulerFlag.sysPowerOffFlag = TASK_FLAG_SET;
							speakerWorkStep = emSpeakerNoneWork;
						}
						 else
						{
							zigbeeXmitCounts++;
						}
					}								
					else
					{
						 zigbeeXmitCounts = 0;
						 //TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_CLEAR;
						 if(( TaskSchedulerFlag.zigbeeXmitFlag == TASK_FLAG_SET )&&(StartToRun_flag ==1)) // 判断这次发送的数据是否是按下运行按键发送的，如果是，重新给定时寄存器写入时间
						 {
							 TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_CLEAR;
							 RunToLorawawn_flag=0;//把运行发送的标志清零
							 TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;  //定时10s再发送一次,重新给定时寄存器写入时间 
							 if(((check_succeed == 1) || (send_number >= 3))&&(StartToRunSend_flag == 1))
							 {
								 if(check_succeed == 1)
								 {
									 TaskSchedulerFlag.StartToRunDontSend = SendSuceed;//运行长数据包发送成功
								 }
								 else if(send_number >= 3)
								 {
									FailureSendNumber ++;//长包发送失败次数
									TaskSchedulerFlag.StartToRunDontSend = SendFailure;//运行长数据包发送失败
								 }
								 StartToRunSend_flag=0;
								 StartToRun_flag=0;//接收到上位机发送的数据，清除该标志
								 check_succeed=0;
								 send_number=0;
								 lorawan_to_sleep=1;//接收到上位机发送的数据，让模块进入睡眠状态
							 }
							 else
							 {		
								 StartToRunSend_flag=1;
								 RTDToZigbeeHandle();
								 send_number++; 
								 
							 }
							 
						 }
						else if( TaskSchedulerFlag.zigbeeXmitFlag == TASK_FLAG_SET )
						{
							TaskSchedulerFlag.zigbeeXmitFlag = TASK_FLAG_CLEAR;
							if(((check_succeed == 1) || (send_number >= 3))&&(PauseSend_flag == 1))
							{
								 PauseSend_flag = 0;
								 check_succeed=0;
								 send_number=0;
								 TaskSchedulerTimer.rtdXmitTimer = TWENTY_MINUTE_TIMER;  //接收到上位机发送的数据，开始定时20分钟
								 lorawan_to_sleep=1;//接收到上位机发送的数据，让模块进入睡眠状态
								if( realTimeData.stateRun == Poweron)
								{
									realTimeData.stateRun = WaitState;//开机发送数据后状态切换为待机状态
								}
							 }
							 else
							 {
								 PauseSend_flag = 1;
								 RTDToZigbeeHandle();
								 send_number++;
								 TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;  //没有接收到上位机发送的数据，开始定时30s
								 
							 }
						}
				   }
									
				}
			}	
			
			else if(( RunToLorawawn_flag == 1) && ((realTimeData.stateRun == MainRun) || (realTimeData.stateRun == FirstRun) || (realTimeData.stateRun == PCARun) )) //在主运行、首次量、PCA正常运行情况发送这个协议包，第一次进入发送一次，以后就是定时发送+随机发送
			{

				RunToLorawawn_flag=0;
				Running_flag=1;//在输注状态下，置位该标志
				if(TaskSchedulerFlag.StartToRunDontSend == SendSuceed)
				{
					if(setParamInfo.continueDose1 == 500)//持续量为50ml时，长包发送成功后，运行过程中30s发送一次短包
					{
						if((check_succeed == 1) || (send_number >= 3))
						{
							check_succeed=0;
							send_number=0;
							TaskSchedulerTimer.rtdXmitTimer = TWENTY_SECOND_TIMER;  //接收到上位机发送的数据，开始定时30秒
							lorawan_to_sleep=1;//接收到上位机发送的数据，让模块进入睡眠状态
						}
						else
						{
							RunToLorawawn();//调用发送数据包
							send_number++;
							TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;  //没有接收到上位机发送的数据，开始定时10s	 
						}
					}
					else
					{
						if((check_succeed == 1) || (send_number >= 3))
						{
							check_succeed=0;
							send_number=0;
							TaskSchedulerTimer.rtdXmitTimer = TWENTY_MINUTE_TIMER;  //接收到上位机发送的数据，开始定时20分钟
							lorawan_to_sleep=1;//接收到上位机发送的数据，让模块进入睡眠状态
						}
						else
						{
							RunToLorawawn();//调用发送数据包
							send_number++;
							TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;  //没有接收到上位机发送的数据，开始定时10s
							 
						}
					}
				 }
				else if(TaskSchedulerFlag.StartToRunDontSend == SendFailure)
				{
					if((check_succeed == 1) || (send_number >= 3))
					 {
						 if(check_succeed == 1)
						 {
							 TaskSchedulerFlag.StartToRunDontSend = SendSuceed;//运行长数据包发送成功
						 }
						 else if(send_number >= 3)
						 {
							FailureSendNumber ++;//长包发送失败次数
							TaskSchedulerFlag.StartToRunDontSend = SendFailure;//运行长数据包发送失败
						 }
						 check_succeed=0;
						 send_number=0;   
						 TaskSchedulerTimer.rtdXmitTimer = TWENTY_MINUTE_TIMER;  //接收到上位机发送的数据，开始定时20分钟
						 lorawan_to_sleep=1;//接收到上位机发送的数据，让模块进入睡眠状态
					 }
					 else
					 {
						 RTDToZigbeeHandle();
						 send_number++;
						 TaskSchedulerTimer.rtdXmitTimer = TEN_SECOND_TIMER;  //没有接收到上位机发送的数据，开始定时10s
						 
					 }
				}
			}

		}
	}
		
		else
		{
			// 网络没开的情况下，进入了随访上传界面，则显示上传失败 
/*			if( TaskSchedulerFlag.zigbeeETXmitFlag == TASK_FLAG_SET )//zigbee发送随访信息
			{
				emEvaluateTransfuseChoose = UploadFall;	
				emDisplayPicture = EvaluateUploadEnterDisp;
				TaskSchedulerFlag.zigbeeETXmitFlag = TASK_FLAG_CLEAR;
			}*/
			if( realTimeData.stateRun == Poweroff )
			{
				TaskSchedulerFlag.sysPowerOffFlag = TASK_FLAG_SET;
			}
		}
	}

/***************************************************************************************
*  Function:	ZigbeeRecvHandle						                                                           
*  Object:		接收zigbee数据
*  输入:			无
*  输出:			无	                                     
*  备注:			处理从zibgee通信接收到的数据              
****************************************************************************************/
void ZigbeeRecvHandle( void )
{
	uint8_t i;
	uint8_t j = 7;
	
	//收到ZIGBEE 数据(ZIGBEE 模块对命令的响应)
	if( Recive_lorawanID_flag == TASK_FLAG_SET )
	{
		Recive_lorawanID_flag = TASK_FLAG_CLEAR;
		
	//	if( usart2RecvBuffer[0] == 0xfc )
		{
	//		if( ( usart2RecvBuffer[1] == 0x03 ) && ( usart2RecvBuffer[2] == 0x05 ) )
			{
				for( i = 0 ; i < 8 ; i++ )
				{
					setParamInfo.deviceID[j] = mcu_id[j];
					j--;
				}
				
				//接收到数据ID完成在此处保存一下
				WriteParameterRecord();				
			}
		}
	}
}
/***************************************************************************************
*  Function:	Uart2RecvHandle						                                                           
*  Object:		接收zigbee数据
*  输入:			无
*  输出:			无	                                     
*  备注:			处理从zibgee通信接收到的数据              
****************************************************************************************/
void Uart2RecvHandle( void )
{
	uint8_t i,j=0;

	uint8_t check_hospitalNO[10];
	
	check_hospitalNO[0] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
	check_hospitalNO[1] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
	check_hospitalNO[2] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
	check_hospitalNO[3] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
	//if(lorawan_send_data_flag != 1)//没有发送数据的时候，直接退出
//		return;
	if( TaskSchedulerFlag.zigbeeRecvFlag == TASK_FLAG_SET )
	{
		TaskSchedulerFlag.zigbeeRecvFlag = TASK_FLAG_CLEAR;
		
		if(( usart2RecvBuffer[0] == 0xad ) && ( usart2RecvBuffer[1] == 0xfc ))
		{
			
			
				for( i = 0 ; i < 4 ; i++ )
				{
					if(usart2RecvBuffer[i+2] == check_hospitalNO[i])
					{
						j++;						
					}
				
				}
				if(j>=4) 
				{
					check_succeed=1;//所有位都校验对了，置位该标志
				  //USART1Send(usart2RecvBuffer,10);
				}			
			
		}
	}
}
