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

#include "stm32f10x_gpio.h"
#include "TypeDefine.h"
#include "USARTSetup.h"
#include "FlashSetup.h"
#include "FM31256.h"
#include "History.h"
#include "math.h"
HistoryDataFrame	historyRecord;//历史数据存储
uint16_t	historyRecordQTY = 0;//历史数据存储数量
uint16_t	historyRecordPointer = 0;//历史数据存储指针
uint8_t		historyRefreshFlag = 0;//历史数据刷新标志
uint16_t	FackhistoryRecordQTY = 0;//假历史数据存储数量
uint8_t		historyRecordBuf[HISTORY_DATA_BYTES];//历史数据存储数组

/*************************************************************************************
*  Function:	HistorySendToPC					                                                           
*  Object:		向PC机发送历史数据
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t	tempIndex = 0; 发送数组的临时索引  
*							2、usart1XmitBuffer USART1的发送数组
*							3、向PC发送数据用USART1
**************************************************************************************/
void HistorySendToPC( void )
{
	uint8_t	i;
	uint8_t	tempIndex = 0;
	
	if( TaskSchedulerFlag.historyRequestFlag == TASK_FLAG_SET )//发送历史数据
	{
		if( usart1RecvBuffer[2] == 0x10 )
		{
			usart1XmitBuffer[tempIndex++] = 0xaa;//帧头
			usart1XmitBuffer[tempIndex++] = 23;//数据长度
			usart1XmitBuffer[tempIndex++] = 0x11;//功能码
			for( i = 0 ; i < 16 ; i++ )
			{
				usart1XmitBuffer[tempIndex++] = setParamInfo.deviceID[i];
			}
			usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
			usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
			usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
			usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
			
			usart1XmitBuffer[tempIndex++] = verifyInfo.sickroom;
			usart1XmitBuffer[tempIndex++] = verifyInfo.bedNO;
			
			usart1XmitBuffer[tempIndex++] = 0x55;	
			USART1Send( usart1XmitBuffer, 26);
		}
		else if( usart1RecvBuffer[2] == 0x20 )
		{
			usart1XmitBuffer[tempIndex++] = 0xaa;//帧头
			usart1XmitBuffer[tempIndex++] = 29;//数据长度
			usart1XmitBuffer[tempIndex++] = 0x21;//功能码

			usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0xff000000 ) >> 24 );
			usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x00ff0000 ) >> 16 );
			usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x0000ff00 ) >> 8 );
			usart1XmitBuffer[tempIndex++] = (uint8_t)( ( verifyInfo.hospitalNO & 0x000000ff ) ) ;
			
			usart1XmitBuffer[tempIndex++] = verifyInfo.sickroom;
			usart1XmitBuffer[tempIndex++] = verifyInfo.bedNO;
			
			usart1XmitBuffer[tempIndex++] = historyRecord.totalDose /  256;
			usart1XmitBuffer[tempIndex++] = historyRecord.totalDose % 256;
			
			usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.firstDose;
			usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.continueDose;
			usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.superaddition;
			usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.lockTime;
			
			usart1XmitBuffer[tempIndex++] = historyRecord.limitDose / 256;
			usart1XmitBuffer[tempIndex++] = historyRecord.limitDose % 256;
			
			usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeMonth;
			usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeDate;
			usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeHour;
			usart1XmitBuffer[tempIndex++] = (uint8_t)historyRecord.stopTimeMinute;
			
			usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum1 / 256;
			usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum1 % 256;
			
			usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum2 / 256;
			usart1XmitBuffer[tempIndex++] = historyRecord.additionalNum2 % 256;
			
			usart1XmitBuffer[tempIndex++] = historyRecord.inputed / 256;
			usart1XmitBuffer[tempIndex++] = historyRecord.inputed % 256;
			
			usart1XmitBuffer[tempIndex++] = 0x55;	
			USART1Send( usart1XmitBuffer, tempIndex);//32
		}
		TaskSchedulerFlag.historyRequestFlag = TASK_FLAG_CLEAR;	
	}
}

/*************************************************************************************
*  Function:	ReadHistoryRecordQTY					                                                           
*  Object:		读取历史数据数量
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempRecordBuf[4] = {0};存储数据组合用数组
*							2、uint8_t tempRes = 0;	返回读取状态	             
**************************************************************************************/
void ReadHistoryRecordQTY( void )
{
	uint8_t tempRecordBuf[6] = {0};
//	uint8_t tempRes = 0;
	FlashReadOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
//// 	FlashReadOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
//	tempRes = Read_Multi31256RAM( HISTORY31256_RECORDQTY_ADDR , tempRecordBuf , HISTORY_RECORDQTY_SIZE );
//	if( tempRes != 1 )
//	{
//		return;
//	}
	historyRecordQTY = tempRecordBuf[0];		//历史数据总条数
	historyRecordQTY <<= 8;
	historyRecordQTY += tempRecordBuf[1];
	
	historyRecordPointer = tempRecordBuf[2];	//历史数据存储指针
	historyRecordPointer <<= 8;
	historyRecordPointer += tempRecordBuf[3];
	
	FackhistoryRecordQTY = tempRecordBuf[4];	//假历史数据
	FackhistoryRecordQTY <<= 8;
	FackhistoryRecordQTY += tempRecordBuf[5];
// 	historyRecordQTY = 0;//test
// 	historyRecordPointer = 0;//test
	if( historyRecordQTY > 10 )
	{
		historyRecordQTY = 0;
	}
	
	if( historyRecordPointer > 10 )
	{
		historyRecordPointer = 0;
	}
	if(FackhistoryRecordQTY > 100)
	{
		FackhistoryRecordQTY = 0;
	}
}

/*************************************************************************************
*  Function:	ReadHistoryRecord					                                                           
*  Object:		读取历史数据
*  输入:			读取第几条数据，从0开始到99，共100条
*  输出:			无	                                     
*  备注:			1、uint8_t tempRes = 0;	返回读取状态	   
*							2、uint8_t tempIndex = 0;数组临时索引
*							3、uint16_t tempPointer = 0;读取地址的临时指针
**************************************************************************************/
void ReadHistoryRecord( void )
{
	uint8_t tempIndex = 0;
//	uint8_t tempRes = 0;
	uint16_t tempPointer = 0;
	
	/*if( ( historyRecordPointer + historyRecord.readIndex ) >= 10 )
	{
		tempPointer = ( historyRecordPointer + historyRecord.readIndex - 10 ) * HISTORY_DATA_BYTES;
	}
	else
	{
		tempPointer = ( historyRecordPointer + historyRecord.readIndex ) * HISTORY_DATA_BYTES;
	}*/
	
	tempPointer = (historyRecord.readIndex * HISTORY_DATA_BYTES);	
		
	FlashReadOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + tempPointer ) );
 	//FlashReadOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + historyRecord.readIndex * HISTORY_DATA_BYTES ) );
//	tempRes = Read_Multi31256RAM( ( HISTORY31256_DATA_START_ADDR + tempPointer ) ,historyRecordBuf,HISTORY_DATA_BYTES );
//	if( tempRes != 1 )
//	{
//		return;
//	}
	historyRecord.totalDose = historyRecordBuf[tempIndex++];	//总量
	historyRecord.totalDose <<= 8;
	historyRecord.totalDose += historyRecordBuf[tempIndex++];
	if( historyRecord.totalDose > 999 )
	{
		historyRecord.totalDose = 0;
	}
	
	historyRecord.firstDose = historyRecordBuf[tempIndex++];	//首次量
	historyRecord.firstDose <<= 8;
	historyRecord.firstDose += historyRecordBuf[tempIndex++];
	if( historyRecord.firstDose > 99 )
	{
		historyRecord.firstDose = 0;
	}
	
	historyRecord.continueDose = historyRecordBuf[tempIndex++];	//持续量
	historyRecord.continueDose <<= 8;
	historyRecord.continueDose += historyRecordBuf[tempIndex++];
	if( historyRecord.continueDose > 999 )
	{
		historyRecord.continueDose = 0;
	}
	
	historyRecord.superaddition = historyRecordBuf[tempIndex++];	//追加量
	historyRecord.superaddition <<= 8;
	historyRecord.superaddition += historyRecordBuf[tempIndex++];
	if( historyRecord.superaddition > 999 )
	{
		historyRecord.superaddition = 0;
	}
	
	historyRecord.lockTime = historyRecordBuf[tempIndex++];	//锁时
	historyRecord.lockTime <<= 8;
	historyRecord.lockTime += historyRecordBuf[tempIndex++];
	if( historyRecord.lockTime > 99 )
	{
		historyRecord.lockTime = 0;
	}
	
	historyRecord.limitDose = historyRecordBuf[tempIndex++];	//极限值
	historyRecord.limitDose <<= 8;
	historyRecord.limitDose += historyRecordBuf[tempIndex++];
	if( historyRecord.limitDose > 90 )
	{
		historyRecord.limitDose = 0;
	}
	
	historyRecord.inputed = historyRecordBuf[tempIndex++];	//已输入
	historyRecord.inputed <<= 8;
	historyRecord.inputed += historyRecordBuf[tempIndex++];
	if( historyRecord.inputed > 9999 )
	{
		historyRecord.inputed = 0;
	}
	
	historyRecord.additionalNum1 = historyRecordBuf[tempIndex++];
	historyRecord.additionalNum1 <<= 8;
	historyRecord.additionalNum1 += historyRecordBuf[tempIndex++];
	if( historyRecord.additionalNum1 > 999 )
	{
		historyRecord.additionalNum1 = 0;
	}
	
	historyRecord.additionalNum2 = historyRecordBuf[tempIndex++];
	historyRecord.additionalNum2 <<= 8;
	historyRecord.additionalNum2 += historyRecordBuf[tempIndex++];
	if( historyRecord.additionalNum2 > 999 )
	{
		historyRecord.additionalNum2 = 0;
	}
	
	historyRecord.stopTimeMonth = historyRecordBuf[tempIndex++];
	if( historyRecord.stopTimeMonth > 12 )
	{
		historyRecord.stopTimeMonth = 1;
	}
	
	historyRecord.stopTimeDate = historyRecordBuf[tempIndex++];
	if( historyRecord.stopTimeDate > 31 )
	{
		historyRecord.stopTimeDate = 1;
	}
	
	historyRecord.stopTimeHour = historyRecordBuf[tempIndex++];
	if( historyRecord.stopTimeHour > 23 )
	{
		historyRecord.stopTimeHour = 0;
	}
	
	historyRecord.stopTimeMinute = historyRecordBuf[tempIndex++];	
	if( historyRecord.stopTimeMinute > 59 )
	{
		historyRecord.stopTimeMinute = 0;
	}
	
	historyRecord.alarmTimeMonth = historyRecordBuf[tempIndex++];
	if( historyRecord.alarmTimeMonth > 12 )
	{
		historyRecord.alarmTimeMonth = 1;
	}
	
	historyRecord.alarmTimeDate = historyRecordBuf[tempIndex++];
	if( historyRecord.alarmTimeDate > 31 )
	{
		historyRecord.alarmTimeDate = 1;
	}
	
	historyRecord.alarmTimeHour = historyRecordBuf[tempIndex++];
	if( historyRecord.alarmTimeHour > 23 )
	{
		historyRecord.alarmTimeHour = 0;
	}
	
	historyRecord.alarmTimeMinute = historyRecordBuf[tempIndex++];	
	if( historyRecord.alarmTimeMinute > 59 )
	{
		historyRecord.alarmTimeMinute = 0;
	}
	historyRecord.alarmType = historyRecordBuf[tempIndex++];	
	if( historyRecord.alarmType > 8 )//报警类型值>8则为非法值
	{
		historyRecord.alarmType = 0;
	}
}

/*************************************************************************************
*  Function:	WriteHistoryRecordQTY					                                                           
*  Object:		写入历史数据数量
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempRecordBuf[4] = {0};写数据用数组         
**************************************************************************************/
void WriteHistoryRecordQTY( void )
{
	uint8_t tempRecordBuf[6] = {0};
	if( ( historyRecordQTY > 10 ) && ( historyRecordQTY < 200 ) )//防止读出来的第一条数据为255
	{
		historyRecordQTY = 10;
		historyRecordPointer++;
		if( historyRecordPointer >= 10 )
		{
			historyRecordPointer = 0;
		}
	}
	else if( historyRecordQTY >= 200 )//防止读出来的第一条数据为255//
	{
		historyRecordQTY = 0;
	}
	if(FackhistoryRecordQTY > 100)
	{
		FackhistoryRecordQTY = 100;
	}
	tempRecordBuf[0] = historyRecordQTY / 256;
	tempRecordBuf[1] = historyRecordQTY % 256;	
	tempRecordBuf[2] = historyRecordPointer / 256;
	tempRecordBuf[3] = historyRecordPointer % 256;
	tempRecordBuf[4] = FackhistoryRecordQTY / 256;
	tempRecordBuf[5] = FackhistoryRecordQTY % 256;
	FlashWriteOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
// 	FlashWriteOperate( tempRecordBuf,HISTORY_RECORDQTY_SIZE,HISTORY_RECORDQTY_ADDR );
//	Write_Multi31256RAM( HISTORY31256_RECORDQTY_ADDR , tempRecordBuf , HISTORY_RECORDQTY_SIZE );
  
}

/*************************************************************************************
*  Function:	WriteHistoryRecord					                                                           
*  Object:		写入历史数据
*  输入:			无
*  输出:			无	                                     
*  备注:			1、uint8_t tempIndex = 0;数组临时索引
*							2、uint16_t tempPointer = 0;写入地址的临时指针             
**************************************************************************************/
void WriteHistoryRecord( void )
{
	uint8_t tempIndex = 0;
	uint16_t tempPointer = 0;
	historyRecordBuf[tempIndex++] = setParamInfo.totalDose /  256;
	historyRecordBuf[tempIndex++] = setParamInfo.totalDose % 256;

	historyRecordBuf[tempIndex++] = setParamInfo.firstDose / 256;
	historyRecordBuf[tempIndex++] = setParamInfo.firstDose % 256;
	
	historyRecordBuf[tempIndex++] = runParamInfo.continueDose / 256;
	historyRecordBuf[tempIndex++] = runParamInfo.continueDose % 256;
	
	historyRecordBuf[tempIndex++] = runParamInfo.superaddition / 256;
	historyRecordBuf[tempIndex++] = runParamInfo.superaddition % 256;
	
	historyRecordBuf[tempIndex++] = runParamInfo.lockTime / 256;
	historyRecordBuf[tempIndex++] = runParamInfo.lockTime % 256;
	
	historyRecordBuf[tempIndex++] = runParamInfo.limitDose / 256;
	historyRecordBuf[tempIndex++] = runParamInfo.limitDose % 256;

	historyRecordBuf[tempIndex++] = realTimeData.inputDose / 256;
	historyRecordBuf[tempIndex++] = realTimeData.inputDose % 256;
	
	historyRecordBuf[tempIndex++] = realTimeData.validCount / 256;
	historyRecordBuf[tempIndex++] = realTimeData.validCount % 256;
	
	historyRecordBuf[tempIndex++] = realTimeData.invalidCount / 256;
	historyRecordBuf[tempIndex++] = realTimeData.invalidCount % 256;
	
	historyRecordBuf[tempIndex++] = displayTimeBuf.month;//historyRecord.stopTimeMonth;
	historyRecordBuf[tempIndex++] = displayTimeBuf.date;//historyRecord.stopTimeDate;	
	historyRecordBuf[tempIndex++] = displayTimeBuf.hour;//(uint8_t)historyRecord.stopTimeHour;	
	historyRecordBuf[tempIndex++] = displayTimeBuf.minute;//(uint8_t)historyRecord.stopTimeMinute;	
	
	historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeMonth;
	historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeDate;
	historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeHour;	
	historyRecordBuf[tempIndex++] = alarmRecord.alarmTimeMinute;	
	historyRecordBuf[tempIndex++] = alarmRecord.alarmType;	
	
	for( ; tempIndex < 32; tempIndex++ )
	{
		historyRecordBuf[tempIndex] = 0;
	}

	if( historyRecordQTY >= 10 )//当存储条数小于10条时存储地址根据存储条数变化，否则根据存储指针变化
	{
		if( historyRecordPointer == 0 )
		{
			tempPointer = ( historyRecordQTY - 1 ) * HISTORY_DATA_BYTES;
		}
		else
		{
//			tempPointer = ( historyRecordPointer - 1 + historyRecordQTY - 10 ) * HISTORY_DATA_BYTES;//删除当前指针的前一条数据，写入新数据
			tempPointer = ( historyRecordPointer - 1 ) * HISTORY_DATA_BYTES;//删除当前指针的前一条数据，写入新数据
		}
	}
	else
	{
		tempPointer = ( historyRecordQTY - 1 ) * HISTORY_DATA_BYTES;
	}
	 FlashWriteOperate( historyRecordBuf,100,( HISTORY_DATA_START_ADDR + tempPointer ) );
 	//FlashWriteOperate( historyRecordBuf,HISTORY_DATA_BYTES,( HISTORY_DATA_START_ADDR + historyRecordQTY * HISTORY_DATA_BYTES ) );
//	Write_Multi31256RAM( ( HISTORY31256_DATA_START_ADDR + tempPointer ) ,historyRecordBuf,HISTORY_DATA_BYTES );
}

/*************************************************************************************
*  Function:	HistoryQtyAdd					                                                           
*  Object:		历史数据数量自加
*  输入:			无
*  输出:			无	                                     
*  备注:			1、            
**************************************************************************************/
void HistoryQtyAdd()
{
	historyRecordQTY++;//历史数据数量自加
	FackhistoryRecordQTY ++;//假历史数据数量自加
	if(FackhistoryRecordQTY > 100)
	{
		FackhistoryRecordQTY = 100;
	}
}
