/*************************** (C) COPYRIGHT 2017 CXHY ****************************
 * 文件名  ：device.c
 * 描  述  ：外部设备控制检测
 * 版  本  ：V1.0
 * 作  者  ：XuZhongPing
 * 日  期  ：2017/09/04
*********************************************************************************/
#include "deviceSet.h"
#include "device.h"
#include "usart1.h"
#include "usart2.h"
#include "delay.h"
#include "timer.h"
#include "data_typedef.h"	  
#include "tcp_protocol.h"
#include "lcd.h"  
#include "display.h"
#include "stmflash.h"
#include "qr_code.h"
#include "scale.h"


#define VersionNumber "V2.0_200319"

/*
***********************************************************************
*	函 数 名: Show
*	功能说明: 显示回收箱类型
*	形    参：type -> 可回收物类型
*	返 回 值: 无
***********************************************************************
*/
void ShowRecyclingBinsType(uint8_t type)
{
	switch(type)
	{
		case PAPER:
			printf("（纸类） ");
			break;
			
		case PLASTIC:
			printf("（塑料） ");
			break;
			
		case GLASS:
			printf("（玻璃） ");
			break;
			
		case METAL:
			printf("（金属） ");
			break;
			
		case RECYCLED:
			printf("（可回收物） ");
			break;
			
		case KITCHEN:
			printf("（厨余） ");
			break;
			
		case SMALL_BOTTLE:
			printf("（小塑料瓶子） ");
			break;
			
		case BIG_BOTTLE:
			printf("（大塑料瓶子） ");
			break;
			
		case CANS:
			printf("（易拉罐） ");
			break;
			
		case BATTERY:
			printf("（电池） ");
			break;
			
		case FABRIC:
			printf("（织物） ");
			break;
			
		case BOOK:
			printf("（书本） ");
			break;
			
		case NEWSPAPER:
			printf("（报纸） ");
			break;
			
		case KITCHEN_1:
			printf("（厨余1） ");
			break;
			
		case KITCHEN_2:
			printf("（厨余2） ");
			break;
			
		case PLASTIC_2:
			printf("（塑料2） ");
			break;
			
		case OTHER:
			printf("（其它） ");
			break;
	}
}

/*
***********************************************************************
*	函 数 名: DeviceTypeSet
*	功能说明: 设备设备类型设置
*	形    参：setkeyVal -> 拔码开关设置传递过来的值
*	返 回 值: 无
***********************************************************************
*/
void DeviceTypeSet(uint8_t setkeyVal)
{
	uint8_t DeviceTypeBit = 0;
	uint8_t RecyclingTypeBit = 0;
	
	sprintf(VersionStr, "版本:%s", VersionNumber);
	
	printf("\r\n设备类型设置：\r\n");
	
	DeviceTypeBit = (setkeyVal >> 4) & 0x0F;
	RecyclingTypeBit = setkeyVal & 0x0F;
// 	printf("\r\nDeviceTypeBit = %d\r\n", DeviceTypeBit);
// 	printf("\r\nRecyclingTypeBit = %d\r\n", RecyclingTypeBit);
	
	switch(DeviceTypeBit)
	{
		case 0x00:		//默认类型（双桶）
			if((RecyclingTypeBit & 0x08) == 0x08)		//拔码开关值为：00 1000
			{
				printf("\r\n设备类型: “双桶”_(00 1XXX)\r\n");
				deviceType = DOUBLE_BARREL_480L;
				devChannelNumbers = 2;
				
				switch(RecyclingTypeBit & 0x07)		//设置对应投放口的回收类型
				{
					case 0:
						leftBarrelType = KITCHEN;					//厨余
						rightBarrelType = KITCHEN;				//厨余						
						break;
						
					case 1:
						leftBarrelType = PLASTIC;					//塑料
						rightBarrelType = PLASTIC;				//塑料
						break;
						
					case 2:
						leftBarrelType = PAPER;						//纸类
						rightBarrelType = PAPER;					//纸类
						break;
						
					case 3:
						leftBarrelType = FABRIC;					//织物
						rightBarrelType = FABRIC;					//织物
						break;
						
					case 4:
						leftBarrelType = WET_WASTES;			//湿垃圾
						rightBarrelType = DRY_WASTES;			//干垃圾
						break;
						
					case 5:
						leftBarrelType = PAPER;						//纸类
						rightBarrelType = METAL;					//金属
						break;
						
					case 6:
						leftBarrelType = FABRIC;					//织物
						rightBarrelType = METAL;					//金属
						break;
					
					case 7:
						leftBarrelType = DRY_WASTES;			//干垃圾
						rightBarrelType = WET_WASTES;			//湿垃圾
						break;
				
					default:
						leftBarrelType = KITCHEN;					//厨余
						rightBarrelType = KITCHEN;				//厨余	
						break;
				}
			}else{		//拔码开关值为：00 0000
				printf("\r\n设备类型: “双桶”_(00 0XXX)\r\n");
				deviceType = DOUBLE_BARREL_480L;
				devChannelNumbers = 2;
				
				switch(RecyclingTypeBit & 0x07)		//设置对应投放口的回收类型
				{
					case 0:
						leftBarrelType = GLASS;						//玻璃
						rightBarrelType = METAL;					//金属					
						break;
						
					case 1:
						leftBarrelType = RECYCLED;				//可回收物
						rightBarrelType = HARMFUL;				//有害
						break;
						
					case 2:
						leftBarrelType = HARMFUL;					//有害
						rightBarrelType = RECYCLED;				//可回收物
						break;
						
					case 3:
						leftBarrelType = METAL;						//金属	
						rightBarrelType = GLASS;					//玻璃
						break;
						
					case 4:	
						leftBarrelType = KITCHEN;					//厨余
						rightBarrelType = RECYCLED;				//可回收物
						break;
						
					case 5:	
						leftBarrelType = PERISHABLE;			//易腐
						rightBarrelType = OTHER;					//其它
						break;
						
					case 6:	
						leftBarrelType = OTHER;						//其它
						rightBarrelType = PERISHABLE;			//易腐
						break;
						
					case 7:
						leftBarrelType = OTHER;						//其它
						rightBarrelType = OTHER;					//其它
						break;
				
					default:
						leftBarrelType = GLASS;						//玻璃
						rightBarrelType = METAL;					//金属		
						break;
				}
			}
			break;
			
		case 0x02:		//单桶
			printf("\r\n设备类型: “单桶”_(10 XXXX)\r\n");
			deviceType = SINGLE_BARREL_660L;
			devChannelNumbers = 1;
			
			switch(RecyclingTypeBit)		//设置对应投放口的回收类型
			{
				case 0:
					leftBarrelType = PAPER;						//纸类
					break;
					
				case 1:
					leftBarrelType = FABRIC;					//织物
					break;
					
				case 2:
					leftBarrelType = KITCHEN;					//厨余
					break;
					
				case 3:
					leftBarrelType = OTHER;						//其它
					break;
					
				case 4:
					leftBarrelType = RECYCLED;				//可回收
					break;
					
				case 5:
					leftBarrelType = GLASS;						//玻璃
					break;
					
				case 6:
					leftBarrelType = METAL;						//金属
					break;
					
				case 7:
					leftBarrelType = PLASTIC;					//塑料
					break;
					
				case 8:
					leftBarrelType = BOOK;						//书本
					break;
					
				case 9:
					leftBarrelType = NEWSPAPER;				//报纸
					break;
					
				case 10:
					leftBarrelType = HARMFUL;					//有害
					break;
					
				case 11:
					leftBarrelType = BATTERY;					//电池
					break;
					
				case 12:
					leftBarrelType = PERISHABLE;			//易腐
					break;
					
				case 13:
				case 14:
				case 15:
					leftBarrelType = PAPER;						//纸类
					break;
					
				default:
					leftBarrelType = PAPER;						//纸类
					break;
					
			}
			break;
			
		case 0x01:		//双桶
			printf("\r\n设备类型: “双桶”_(01 XXXX)\r\n");
			deviceType = DOUBLE_BARREL_480L;
			devChannelNumbers = 2;
			
			switch(RecyclingTypeBit)		//设置对应投放口的回收类型
			{
				case 0:
					leftBarrelType = GLASS;						//玻璃
					rightBarrelType = METAL;					//金属
					break;
					
				case 1:
					leftBarrelType = PLASTIC;					//塑料
					rightBarrelType = PAPER;					//纸类
					break;
					
				case 2:
					leftBarrelType = KITCHEN;					//厨余
					rightBarrelType = OTHER;					//其它
					break;
					
				case 3:
					leftBarrelType = HARMFUL;					//有害
					rightBarrelType = GLASS;					//玻璃
					break;
					
				case 4:
					leftBarrelType = METAL;						//金属
					rightBarrelType = PLASTIC;				//塑料
					break;
					
				case 5:
					leftBarrelType = PAPER;						//纸类
					rightBarrelType = FABRIC;					//织物
					break;
					
				case 6:
					leftBarrelType = PLASTIC;					//塑料
					rightBarrelType = FABRIC;					//织物
					break;
					
				case 7:
					leftBarrelType = PLASTIC;					//塑料
					rightBarrelType = BOOK;						//书本
					break;
					
				case 8:
					leftBarrelType = PLASTIC;					//塑料
					rightBarrelType = NEWSPAPER;			//报纸
					break;
					
				case 9:
					leftBarrelType = FABRIC;					//织物
					rightBarrelType = NEWSPAPER;			//报纸
					break;
					
				case 10:
					leftBarrelType = FABRIC;					//织物
					rightBarrelType = HARMFUL;				//有害
					break;
					
				case 11:
					leftBarrelType = FABRIC;					//织物
					rightBarrelType = OTHER;					//其它
					break;
					
				case 12:
					leftBarrelType = BOOK;						//书本
					rightBarrelType = NEWSPAPER;			//报纸
					break;
					
				case 13:
					leftBarrelType = GLASS;						//玻璃
					rightBarrelType = PLASTIC;				//塑料
					break;
					
				case 14:
					leftBarrelType = RECYCLED;				//可回收物
					rightBarrelType = OTHER;					//其它
					break;
					
				case 15:
					leftBarrelType = RECYCLED;				//可回收物
					rightBarrelType = KITCHEN;				//厨余
					break;
					
				default:
					leftBarrelType = GLASS;						//玻璃
					rightBarrelType = METAL;					//金属
					break;
					
			}
			break;
			
		case 0x03:		//双桶含瓶类
			printf("\r\n设备类型: “双桶含瓶类”_(11 XXXX)\r\n");
			deviceType = DOUBLE_BARREL_BOTTLE_480L;
			devChannelNumbers = 2;
			
			switch(RecyclingTypeBit)		//设置对应投放口的回收类型
			{
				case 0:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = PLASTIC;				//塑料
					break;
					
				case 1:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = GLASS;					//玻璃
					break;
					
				case 2:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = METAL;					//金属
					break;
					
				case 3:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = PAPER;					//纸类
					break;
					
				case 4:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = FABRIC;					//织物
					break;
					
				case 5:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = RECYCLED;				//可回收物
					break;
					
				case 6:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = BOOK;						//书本
					break;
					
				case 7:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = NEWSPAPER;			//报纸
					break;
					
				case 8:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = OTHER;					//其它
					break;
					
				case 9:
				case 10:
				case 11:
				case 12:
				case 13:
				case 14:
					deviceType = DOUBLE_BARREL_480L;
					devChannelNumbers = 2;
					
					leftBarrelType = METAL;						//金属
					rightBarrelType = FABRIC;					//织物
					break;
					
				case 15:
					deviceType = DOUBLE_BARREL_480L;
					devChannelNumbers = 2;
				
					leftBarrelType = RECYCLED;				//可回收物
					rightBarrelType = FABRIC;					//织物
					break;
					
				default:
					leftBarrelType = SMALL_BOTTLE;		//塑料瓶子
					rightBarrelType = PLASTIC;				//塑料
					break;
					
			}
			
			break;
			
		default:
			break;
	}
	
	printf("\r\n设备类型设置完成！！！\r\n");
}

/*
***********************************************************************
*	函 数 名: SetDefaultOverflowAlarmParam
*	功能说明: 设置满溢报警默认参数
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void SetDefaultOverflowAlarmParam(void)
{
	switch(deviceType)
	{
		case DOUBLE_BARREL_480L:		//双桶480L
			//设置装满报警参数
			distance_L_DepthVal = 100;
			distance_L_MaxVal = 155;
			distance_L_AlarmPercent = 100;
			
			distance_R_DepthVal = 100;
			distance_R_MaxVal = 155;
			distance_R_AlarmPercent = 100;
			
			printf("\r\n设备类型: “双桶480L”\r\n");
			break;
			
		case DOUBLE_BARREL_BOTTLE_480L:		//双桶480L含瓶类
			//设置装满报警参数
			distance_L_DepthVal = 100;
			distance_L_MaxVal = 110;
			distance_L_AlarmPercent = 85;
			
			distance_R_DepthVal = 100;
			distance_R_MaxVal = 155;
			distance_R_AlarmPercent = 100;
			
			printf("\r\n设备类型: “双桶480L含瓶类”\r\n");
			break;
			
		case SINGLE_BARREL_660L:		//单桶660L
			//设置装满报警参数
			distance_L_DepthVal = 100;
			distance_L_MaxVal = 155;
			distance_L_AlarmPercent = 100;
			printf("\r\n设备类型: “单桶660L”\r\n");
			break;
			
		default:
			break;
	}
	
// 	printf("\r\n设置默认满溢参数: h1 = %d; H1 = %d; A1 = %d; h2 = %d; H2 = %d; A2 = %d;\r\n", 
// 						setDistance_L_DepthVal, setDistance_L_MaxVal, setDistance_L_AlarmPercent,
// 						setDistance_R_DepthVal, setDistance_R_MaxVal, setDistance_R_AlarmPercent);
}

/*
***********************************************************************
*	函 数 名: DeviceSetParamInit
*	功能说明: 设备设置参数初始化
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void DeviceSetParamInit(void)
{	
	setDeviceType = deviceType;															//设置的设备类型
	setDevChannelNumbers = devChannelNumbers;								//设置时取得的投放口通道数量
	setLeftBarrelType = leftBarrelType;											//设置的设备左桶类型
	setRightBarrelType = rightBarrelType;										//设置的设备右桶类型

	setPassword = password;																	//要设置的设备操作密码
	setCustomerID = customerID;															//客户号（用于控制谁可以打开收运清理门）

	setDistance_L_DepthVal = distance_L_DepthVal;						//设置左桶测距桶的深度（即内桶的高度）
	setDistance_L_MaxVal = distance_L_MaxVal;								//设置左桶测距最大值（即安装最高点的值）
	setDistance_L_AlarmPercent = distance_L_AlarmPercent;		//设置左桶测距装满报警百分比

	setDistance_R_DepthVal = distance_R_DepthVal;						//设置右桶测距桶的深度（即内桶的高度）
	setDistance_R_MaxVal = distance_R_MaxVal;								//设置右桶测距最大值（即安装最高点的值）
	setDistance_R_AlarmPercent = distance_R_AlarmPercent;		//设置右桶测距装满报警百分比
	
	setDisableICCardFunStatus = disableICCardFunStatus;				//设置“禁用IC卡用户”功能标志位
	setEnableCleanQRCodeStatus = enableCleanQRCodeStatus;			//设置“启用收运卡二维码开门”标志
	
	printf("\r\n设备设置参数初始化完成！！！\r\n");
	printf("\r\nSet P=%d; ID=%d; Type=%d; Cnt=%d; T1=%d, h1=%d, H1=%d, A1=%d; T2=%d, h2=%d, H2=%d, A2=%d, ICE=%d, ICQR=%d\r\n",
						setPassword,
						setCustomerID,
						setDeviceType,
						setDevChannelNumbers,
						setLeftBarrelType, setDistance_L_DepthVal, setDistance_L_MaxVal, setDistance_L_AlarmPercent,
						setRightBarrelType, setDistance_R_DepthVal, setDistance_R_MaxVal, setDistance_R_AlarmPercent,
						setDisableICCardFunStatus,
						setEnableCleanQRCodeStatus
					);
}

/*
***********************************************************************
*	函 数 名: DeviceParamInit
*	功能说明: 设备参数初始化
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void DeviceParamInit(void)
{	
	password = setPassword;
	customerID = setCustomerID;
	
// 	deviceType = setDeviceType;
// 	devChannelNumbers = setDevChannelNumbers;
// 	leftBarrelType = setLeftBarrelType;
// 	rightBarrelType = setRightBarrelType;
	
	distance_L_DepthVal = setDistance_L_DepthVal;
	distance_L_MaxVal = setDistance_L_MaxVal;
	distance_L_AlarmPercent = setDistance_L_AlarmPercent;
	
	distance_R_DepthVal = setDistance_R_DepthVal;
	distance_R_MaxVal = setDistance_R_MaxVal;
	distance_R_AlarmPercent = setDistance_R_AlarmPercent;
	
	disableICCardFunStatus = setDisableICCardFunStatus;
	enableCleanQRCodeStatus = setEnableCleanQRCodeStatus;
	
	printf("\r\nDevice Param Init OK！！！\r\n");
	printf("\r\nCur P=%d; ID=%d; Type=%d; Cnt=%d; T1=%d, h1=%d, H1=%d, A1=%d; T2=%d, h2=%d, H2=%d, A2=%d, ICE=%d, ICQR=%d\r\n",
						password,
						customerID,
						deviceType,
						devChannelNumbers,
						leftBarrelType, distance_L_DepthVal, distance_L_MaxVal, distance_L_AlarmPercent,
						rightBarrelType, distance_R_DepthVal, distance_R_MaxVal, distance_R_AlarmPercent,
						disableICCardFunStatus,
						enableCleanQRCodeStatus
					);
}


//-----------------------------------------------------------------------------------
//通过二维码设置设备参数

/*
***********************************************************************
*	函 数 名: StrToInt
*	功能说明: 字符串转整型数据（带格式校验）
*	形    参：str -> 待转换的字符串
*	返 回 值: 转换后的整型数据，当格式不正确时返回0xFFFFFFFF
***********************************************************************
*/
uint32_t StrToInt(char *str)
{
	uint32_t res = 0xFFFFFFFF;
	
	if(strlen(str) < 10)		//要转换的内容长度小于10位数
	{
		if(strspn(str, "0123456789") == strlen(str))		//校验内容是不是都由数字组成
		{
			res = atoi(str);
		}else{
			res = 0xFFFFFFFF;
			printf("\r\n内容格式错误\r\n");
		}
	}
	return res;
}

/*
***********************************************************************
*	函 数 名: SetParamDecode
*	功能说明: 设置参数解码
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void SetParamDecode(uint8_t *pSetParamStr)
{
	uint32_t temp = 0xFFFFFFFF;
	char paramBuf[35];
	uint8_t paramData[10][10];
	uint8_t i = 0;
	uint8_t len = 0;
	static uint8_t preSetParamCmd = 0;							//之前的命令码
	uint8_t setParamCmd = 0;
	uint8_t paramCunt = 0;
	uint8_t decodeOkMark = 0;
	
	char *p;
	char *pParam;
	char *token;
	
	p = (char*)strstr((const char*)pSetParamStr, ":");
	pParam = p+1;
	strcpy(paramBuf, pParam);												//先备份设置的参数内容
	
	//分解出命令码
	token = strtok(pParam, ",");
	if(token != NULL)
	{
		printf("\r\ntoken = %s\r\n", token);
		
		temp = StrToInt(token);											//将字符串转换成整型数据
		if(temp != 0xFFFFFFFF)
		{
			setParamCmd = temp;						
			decodeOkMark = 1;
			
			if((preSetParamCmd != setParamCmd) && (setParamCmd != 1))
			{
				if(setParamCmd != 1)
				{
					setParamChangeMark = 1;
					printf("\r\n指令有变化！\r\n");
				}
				preSetParamCmd = setParamCmd;
				
			}
			printf("\r\ncmd = %d\r\n", setParamCmd);
		}else{
			decodeOkMark = 0;
			printf("\r\n命令错误\r\n");
		}
	}
	
	//判断是否为设置命令
	if(decodeOkMark == 1)
	{
		switch(setParamCmd)
		{
			case 1:		//管理密码确认
				printf("\r\nSET<>管理密码确认！\r\n");
				i = 0;
				token = strtok(paramBuf, ",");
				while (token != NULL)
				{
// 					printf("\r\nparam%d %s\r\n", i, token);
					if(i == 1)
					{
// 						printf("\r\n密码为：%s\r\n", token);
						temp = StrToInt(token);													//将字符串转换成整型数据
						if(temp != 0xFFFFFFFF)
						{
							getConfirmPassword = temp;									
							getConfirmPasswordMark = 1;										//标志已经取得待确认的密码
							printf("\r\n密码为：%d\r\n", getConfirmPassword);
						}else{
							getConfirmPasswordMark = 0;
							printf("\r\n确认密码内容格式错误\r\n");
							break;
						}
					}
					
					token = strtok(NULL, ",");
					i++;
				}
				break;
				
			case 2:		//密码修改
				printf("\r\nSET<>密码修改！\r\n");
				i = 0;
				token = strtok(paramBuf, ",");
				while (token != NULL)
				{
// 					printf("\r\nparam%d %s\r\n", i, token);
					if(i == 1)
					{
// 						printf("\r\n密码为：%s\r\n", token);
						temp = StrToInt(token);													//将字符串转换成整型数据
						if(temp != 0xFFFFFFFF)
						{
							getPassword = temp;										
							getPasswordMark = 1;													//标志已经取得待确认的密码
							printf("\r\n密码为：%d\r\n", getPassword);
						}else{
							getPasswordMark = 0;
							printf("\r\n设置密码内容格式错误\r\n");
							break;
						}
					}
					
					token = strtok(NULL, ",");
					i++;
				}
				break;
				
			case 3:		//设备类型设置
				printf("\r\nSET<>设备类型设置！\r\n");
// 				i = 0;
// 				token = strtok(paramBuf, ",");
// 				while (token != NULL)
// 				{
// // 					printf("\r\nparam%d %s\r\n", i, token);
// 					
// 					if(i == 1){		//设备类型					
// 						temp = StrToInt(token);													//将字符串转换成整型数据
// 						if(temp != 0xFFFFFFFF)
// 						{
// 							getDeviceType = temp;											
// 							printf("\r\n设备类型为：%d\r\n", getDeviceType);
// 						}else{
// 							getDeviceTypeMark = 0;
// 							printf("\r\n设置类型内容格式错误\r\n");
// 							break;
// 						}
// 					}else if(i == 2){		//投口通道数量
// 						temp = StrToInt(token);													//将字符串转换成整型数据
// 						if(temp != 0xFFFFFFFF)
// 						{
// 							getDevChannelNumbers = temp;											
// 							printf("\r\n设备投口通道数量为：%d\r\n", getDevChannelNumbers);
// 						}else{
// 							getDeviceTypeMark = 0;
// 							printf("\r\n设备投口通道数量内容格式错误\r\n");
// 							break;
// 						}

// 					}else if(i == 3){
// // 						printf("\r\n回收类型1：%s\r\n", token);
// 						temp = StrToInt(token);													//将字符串转换成整型数据
// 						if(temp != 0xFFFFFFFF)
// 						{
// 							getLeftBarrelType = temp;										
// 							if(getDeviceType == 1)
// 							{
// 								getDeviceTypeMark = 1;											//单投口设备则标志解码成功
// 							}
// 							printf("\r\n回收类型1：%d\r\n", getLeftBarrelType);
// 						}else{
// 							getDeviceTypeMark = 0;
// 							printf("\r\n设置类型内容格式错误\r\n");
// 							break;
// 						}
// 					}else if(i == 4){
// // 						printf("\r\n回收类型2：%s\r\n", token);
// 						temp = StrToInt(token);													//将字符串转换成整型数据
// 						if(temp != 0xFFFFFFFF)
// 						{
// 							getRightBarrelType = temp;
// 							if((getDeviceType == 2) || (getDeviceType == 3))
// 							{
// 								getDeviceTypeMark = 1;											//双投口设备则标志解码成功
// 							}
// 							printf("\r\n回收类型2：%d\r\n", getRightBarrelType);							
// 						}else{
// 							getDeviceTypeMark = 0;
// 							printf("\r\n设置类型内容格式错误\r\n");
// 							break;
// 						}
// 					}
// 					
// 					token = strtok(NULL, ",");
// 					i++;
// 				}
				break;
				
			case 4:		//设备客户号设置
				printf("\r\nSET<>设备客户号设置！\r\n");
				i = 0;
				token = strtok(paramBuf, ",");
				while (token != NULL)
				{
					if(i == 1)
					{
						temp = StrToInt(token);													//将字符串转换成整型数据
						if(temp != 0xFFFFFFFF)
						{
							getCustomerID = temp;	
							getCustomerIDMark = 1;													//标志已经取得待确认的密码
							printf("\r\n客户号为：%d\r\n", getCustomerID);
							break;
						}else{
							getCustomerIDMark = 0;
							printf("\r\n设置客户号内容格式错误\r\n");
							break;
						}
					}
					
					token = strtok(NULL, ",");
					i++;
				}
				break;
				
			case 5:
				break;
				
			case 6:		//启用/停用“禁用的IC卡不能开门”功能设置
				printf("\r\nSET<>（禁用的IC卡不能开门）功能设置！\r\n");
				i = 0;
				token = strtok(paramBuf, ",");
				while (token != NULL)
				{
					if(i == 1)
					{
						temp = StrToInt(token);													//将字符串转换成整型数据
						if(temp != 0xFFFFFFFF)
						{
							getDisableICCardFunStatus = temp;	
							getDisableICCardFunMark = 1;													//标志已经取得待确认的密码
							printf("\r\n状态为：%d\r\n", getDisableICCardFunStatus);
							break;
						}else{
							getDisableICCardFunMark = 0;
							printf("\r\n设置（禁用的IC卡不能开门）内容格式错误\r\n");
							break;
						}
					}
					
					token = strtok(NULL, ",");
					i++;
				}
				break;
				
			case 7:		//启用/停用“收运二维码开门”功能设置
				printf("\r\nSET<>（收运二维码开门）功能设置！\r\n");
				i = 0;
				token = strtok(paramBuf, ",");
				while (token != NULL)
				{
					if(i == 1)
					{
						temp = StrToInt(token);													//将字符串转换成整型数据
						if(temp != 0xFFFFFFFF)
						{
							getEnableCleanQRCodeStatus = temp;	
							getEnableCleanQRCodeMark = 1;													//标志已经取得待确认的密码
							printf("\r\n状态为：%d\r\n", getEnableCleanQRCodeStatus);
							break;
						}else{
							getEnableCleanQRCodeMark = 0;
							printf("\r\n设置（收运二维码开门）内容格式错误\r\n");
							break;
						}
					}
					
					token = strtok(NULL, ",");
					i++;
				}
				break;
				
			case 30:		//称盘校准
				printf("\r\nSET<>称盘校准！\r\n");
				i = 0;
				token = strtok(paramBuf, ",");
				while (token != NULL)
				{
// 					printf("\r\nparam%d %s\r\n", i, token);
					if(i == 1)
					{
// 						printf("\r\ntoken：%s\r\n", token);
						temp = StrToInt(token);													//将字符串转换成整型数据
						if(temp != 0xFFFFFFFF)
						{
							getScaleNum = temp;									
							getScaleCalibrationMark = 1;										//标志已经取得待确认的密码
							printf("\r\n称盘编号为：%d\r\n", getScaleNum);
						}else{
							getConfirmPasswordMark = 0;
							printf("\r\n校准称盘操作内容格式错误\r\n");
							break;
						}
					}
					
					token = strtok(NULL, ",");
					i++;
				}
				break;
				
			case 31:		//临时开收运清理门二维码
				printf("\r\nSET<>临时开门！\r\n");
				i = 0;
				token = strtok(paramBuf, ",");
				while (token != NULL)
				{
// 					printf("\r\nparam%d %s\r\n", i, token);
					if(i == 1)
					{
// 						printf("\r\ntoken：%s\r\n", token);
						temp = StrToInt(token);													//将字符串转换成整型数据
						if(temp != 0xFFFFFFFF)
						{							
							getBoxOpenMark = 1;										//标志已经取得待确认的密码
// 							printf("\r\n开门编号为：%d\r\n", getScaleNum);
						}else{
							getBoxOpenMark = 0;
							printf("\r\n临时开门操作内容格式错误\r\n");
							break;
						}
					}
					
					token = strtok(NULL, ",");
					i++;
				}
				break;
				
			case 56:		//密码强制修改
				printf("\r\nSET<>密码强制修改！\r\n");
				i = 0;
				token = strtok(paramBuf, ",");
				while (token != NULL)
				{
					if(i == 1)
					{
// 						printf("\r\n密码为：%s\r\n", token);
						temp = StrToInt(token);													//将字符串转换成整型数据
						if(temp != 0xFFFFFFFF)
						{
							getPassword = temp;										
							getForceChangeMark = 1;											//标志已经取得待确认的密码
							printf("\r\n密码为：%d\r\n", getPassword);
						}else{
							getForceChangeMark = 0;
							printf("\r\n强制设置密码内容格式错误\r\n");
							break;
						}
					}
					
					token = strtok(NULL, ",");
					i++;
				}
				break;
				
			default:
				break;
		}
	}
}

/*
***********************************************************************
*	函 数 名: PasswordSetTask
*	功能说明: 密码修改设置任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void PasswordSetTask(void)
{
	static uint8_t setStep = 0x00;
	
	switch(setStep)
	{
		case 0x00:
			if(getPasswordMark == 1)
			{
				getPasswordMark = 0;
				getConfirmPasswordMark = 0;
				setParamChangeMark = 0;
				setStep = 0x01;
			}
			break;
			
		case 0x01:
			if(getConfirmPasswordMark == 1)
			{
				getConfirmPasswordMark = 0;
				if(password == getConfirmPassword)
				{
					printf("\r\n密码校验正确！\r\n");
					setPassword = getPassword;
					printf("\r\n密码修改成功！\r\n");
					setParamSaveMark = 1;														//标志有参数进行了修改，需要进行保存
					setStep = 0x00;
					break;
				}else{
					printf("\r\n密码校验错误！\r\n");
					break;
				}
			}
			
			if(setParamChangeMark == 1)
			{
				setParamChangeMark = 0;
				setStep = 0x00;
				printf("\r\n参数设置命令有切换！！\r\n");
				break;
			}
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: PasswordCastSetTask
*	功能说明: 密码强制修改设置任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void PasswordCastSetTask(void)
{
	static uint8_t setStep = 0x00;
	
	switch(setStep)
	{
		case 0x00:
			if(getForceChangeMark == 1)
			{
				getForceChangeMark = 0;
				setPassword = getPassword;
				setParamSaveMark = 1;														//标志有参数进行了修改，需要进行保存
				printf("\r\n密码强制修改成功！\r\n");
				setStep = 0x00;
			}
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: DeviceTypeSetTask
*	功能说明: 设备类型设置任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void DeviceTypeSetTask(void)
{
	static uint8_t setStep = 0x00;
	
	switch(setStep)
	{
		case 0x00:
			if(getDeviceTypeMark == 1)
			{
				getDeviceTypeMark = 0;
				getConfirmPasswordMark = 0;
				setParamChangeMark = 0;
				setStep = 0x01;
			}
			break;
			
		case 0x01:
			if(getConfirmPasswordMark == 1)
			{
				getConfirmPasswordMark = 0;
				if(password == getConfirmPassword)
				{
					printf("\r\n密码校验正确！\r\n");
					setDeviceType = getDeviceType;
					setDevChannelNumbers = getDevChannelNumbers;
					setLeftBarrelType = getLeftBarrelType;
					setRightBarrelType = getRightBarrelType;
					SetDefaultOverflowAlarmParam();									//当设备类型有变化时则要重新设置满溢报警参数
					setParamSaveMark = 1;														//标志有参数进行了修改，需要进行保存
					setStep = 0x00;
					break;
				}else{
					printf("\r\n密码校验错误！\r\n");
					break;
				}
			}
			
			if(setParamChangeMark == 1)
			{
				setParamChangeMark = 0;
				setStep = 0x00;
				printf("\r\n参数设置命令有切换！！\r\n");
				break;
			}
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: CustomerIdSetTask
*	功能说明: 设备客户号设置任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void CustomerIdSetTask(void)
{
	static uint8_t setStep = 0x00;
	
	switch(setStep)
	{
		case 0x00:
			if(getCustomerIDMark == 1)
			{
				getCustomerIDMark = 0;
				getConfirmPasswordMark = 0;
				setParamChangeMark = 0;
				setStep = 0x01;
			}
			break;
			
		case 0x01:
			if(getConfirmPasswordMark == 1)
			{
				getConfirmPasswordMark = 0;
				if(password == getConfirmPassword)
				{
					printf("\r\n密码校验正确！\r\n");
					setCustomerID = getCustomerID;
					printf("\r\n客户号修改成功！\r\n");
					setParamSaveMark = 1;														//标志有参数进行了修改，需要进行保存
					setStep = 0x00;
					break;
				}else{
					printf("\r\n密码校验错误！\r\n");
					break;
				}
			}
			
			if(setParamChangeMark == 1)
			{
				setParamChangeMark = 0;
				setStep = 0x00;
				printf("\r\n参数设置命令有切换！！\r\n");
				break;
			}
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: DisableICCardFunSetTask
*	功能说明: 启用/停用“禁用的IC卡不能开门”功能设置
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void DisableICCardFunSetTask(void)
{
	static uint8_t setStep = 0x00;
	
	switch(setStep)
	{
		case 0x00:
			if(getDisableICCardFunMark == 1)
			{
				getDisableICCardFunMark = 0;
				getConfirmPasswordMark = 0;
				setParamChangeMark = 0;
				setStep = 0x01;
			}
			break;
			
		case 0x01:
			if(getConfirmPasswordMark == 1)
			{
				getConfirmPasswordMark = 0;
				if(password == getConfirmPassword)
				{
					printf("\r\n密码校验正确！\r\n");
					setDisableICCardFunStatus = getDisableICCardFunStatus;
					printf("\r\n“禁用的IC卡不能开门”功能修改成功！\r\n");
					if(setDisableICCardFunStatus == 1)
					{
						printf("\r\n停用！\r\n");
					}else{
						printf("\r\n启用！\r\n");
					}
					setParamSaveMark = 1;														//标志有参数进行了修改，需要进行保存
					setStep = 0x00;
					break;
				}else{
					printf("\r\n密码校验错误！\r\n");
					break;
				}
			}
			
			if(setParamChangeMark == 1)
			{
				setParamChangeMark = 0;
				setStep = 0x00;
				printf("\r\n参数设置命令有切换！！\r\n");
				break;
			}
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: DisableICCardFunSetTask
*	功能说明: 启用/停用“收运二维码开门”功能设置
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void EnableCleanQRCodeSetTask(void)
{
	static uint8_t setStep = 0x00;
	
	switch(setStep)
	{
		case 0x00:
			if(getEnableCleanQRCodeMark == 1)
			{
				getEnableCleanQRCodeMark = 0;
				getConfirmPasswordMark = 0;
				setParamChangeMark = 0;
				setStep = 0x01;
			}
			break;
			
		case 0x01:
			if(getConfirmPasswordMark == 1)
			{
				getConfirmPasswordMark = 0;
				if(password == getConfirmPassword)
				{
					printf("\r\n密码校验正确！\r\n");
					setEnableCleanQRCodeStatus = getEnableCleanQRCodeStatus;
					printf("\r\n“收运二维码开门”功能修改成功！\r\n");
					if(setEnableCleanQRCodeStatus == 1)
					{
						printf("\r\n启用！\r\n");
					}else{
						printf("\r\n停用！\r\n");
					}
					setParamSaveMark = 1;														//标志有参数进行了修改，需要进行保存
					setStep = 0x00;
					break;
				}else{
					printf("\r\n密码校验错误！\r\n");
					break;
				}
			}
			
			if(setParamChangeMark == 1)
			{
				setParamChangeMark = 0;
				setStep = 0x00;
				printf("\r\n参数设置命令有切换！！\r\n");
				break;
			}
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: ScaleCalibrationTask
*	功能说明: 称盘校准任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void ScaleCalibrationTask(void)
{
	static uint8_t setStep = 0x00;
	static timer waitingTimer;
	static timer waitingTimer1S;
	static timer waitingExitTimer;
	
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&waitingTimer, CLOCK_SECOND*3);
		timer_set(&waitingTimer1S, CLOCK_SECOND*1);
		timer_set(&waitingExitTimer, CLOCK_SECOND*20);
	}
	
	switch(setStep)
	{
		case 0x00:		//初始状态
			if(getScaleCalibrationMark == 1)
			{
				getScaleCalibrationMark = 0;
				getConfirmPasswordMark = 0;				//确认密码标志清零
				setParamChangeMark = 0;						//指令切换标志清零
				setStep = 0x01;
			}
			break;
			
		case 0x01:		//校验密码
			if(getConfirmPasswordMark == 1)
			{
				getConfirmPasswordMark = 0;
				if(password == getConfirmPassword)
				{
					printf("\r\n密码校验正确！\r\n");
					
					printf("\r\n开启称盘校准！\r\n");
					setStep = 0x02;
					break;
				}else{
					printf("\r\n密码校验错误！\r\n");
					break;
				}
			}
			
			if(setParamChangeMark == 1)
			{
				setParamChangeMark = 0;
				setStep = 0x00;
				printf("\r\n参数设置命令有切换！！\r\n");
				break;
			}
			break;
			
		case 0x02:		//正在启动进入校准
			Screen_ScaleCalibraEnter(getScaleNum);		//显示正在校准
			switch(getScaleNum)
			{
				case 1:
					SendCalibraCmd(1);
					getCalibraZeroMark1 = 0;
					timer_restart(&waitingExitTimer);
					setStep = 0x03;
					break;
					
				case 2:
					SendCalibraCmd(2);
					getCalibraZeroMark2 = 0;
					timer_restart(&waitingExitTimer);
					setStep = 0x03;
					break;
					
				default:
					setStep = 0x00;
					break;
			}
			break;
			
		case 0x3:		//等待称重模块回应取得零点			
			if(timer_expired(&waitingExitTimer))
			{
				printf("\r\n取得校准零点超时退出！\r\n");
				setStep = 0x10;
			}
			
			switch(getScaleNum)
			{
				case 1:		//称盘1
					if(getCalibraZeroMark1 == 1)
					{
						printf("\r\n收到称1校准零点！\r\n");
						Screen_ScaleCalibraStart(getScaleNum);
						getCalibraParamOkMark1 = 0;
						timer_restart(&waitingExitTimer);
						setStep = 0x04;
						break;
					}
					break;
					
				case 2:		//称盘2
					if(getCalibraZeroMark2 == 1)
					{
						printf("\r\n收到称2校准零点！\r\n");
						Screen_ScaleCalibraStart(getScaleNum);
						getCalibraParamOkMark2 = 0;
						timer_restart(&waitingExitTimer);
						setStep = 0x04;
						break;
					}
					break;
					
				default:
					setStep = 0x00;
					break;
			}
			break;
			
		case 0x04:		//等待校准完成
			if(timer_expired(&waitingExitTimer))
			{
				printf("\r\n取得校准零点超时退出！\r\n");
				setStep = 0x10;
			}
			
			switch(getScaleNum)
			{
				case 1:		//称盘1
					if(getCalibraParamOkMark1 == 1)
					{
						printf("\r\n收到称1校准完成！\r\n");
						Screen_ScaleCalibraFinish(getScaleNum);
						timer_restart(&waitingTimer);
						setStep = 0x05;
						break;
					}
					break;
					
				case 2:		//称盘2
					if(getCalibraParamOkMark2 == 1)
					{
						printf("\r\n收到称2校准完成！\r\n");
						Screen_ScaleCalibraFinish(getScaleNum);
						timer_restart(&waitingTimer);
						setStep = 0x05;
						break;
					}
					break;
					
				default:
					setStep = 0x00;
					break;
			}
			break;
			
		case 0x05:		//退出
			if(timer_expired(&waitingTimer))
			{
				if(deviceWorkStatus == 1)
				{
					Screen_Default();
				}else{
					Screen_DeviceDisable();
				}
				setStep = 0x00;
			}
			break;
			
		case 0x10:		//校准失败重启称重模块电源
			ScalePowerOFF(1);
			ScalePowerOFF(2);
			timer_restart(&waitingTimer1S);
			setStep = 0x11;
			break;
			
		case 0x11:		//校准失败重启称重模块电源
			if(timer_expired(&waitingTimer1S))
			{
				ScalePowerON(1);
				ScalePowerON(2);
				setStep = 0x05;
			}
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: BoxOpenSetTask
*	功能说明: 临时打开收运清理门操作任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/

void BoxOpenSetTask(void)
{
	static uint8_t setStep = 0x00;
	
	switch(setStep)
	{
		case 0x00:
			if(getBoxOpenMark == 1)
			{
				getBoxOpenMark = 0;
				boxOpenMark = 1;
				printf("\r\n准备打开收运门！\r\n");
				setStep = 0x00;
			}
			break;
			
		default:
			break;
	}
}

// void BoxOpenSetTask(void)
// {
// 	static uint8_t setStep = 0x00;
// 	
// 	switch(setStep)
// 	{
// 		case 0x00:
// 			if(getBoxOpenMark == 1)
// 			{
// 				getBoxOpenMark = 0;
// 				getConfirmPasswordMark = 0;
// 				setParamChangeMark = 0;
// 				setStep = 0x01;
// 			}
// 			break;
// 			
// 		case 0x01:
// 			if(getConfirmPasswordMark == 1)
// 			{
// 				getConfirmPasswordMark = 0;
// 				if(password == getConfirmPassword)
// 				{
// 					printf("\r\n密码校验正确！\r\n");
// 					boxOpenMark = 1;
// 					printf("\r\n准备打开收运门！\r\n");
// 					setStep = 0x00;
// 					break;
// 				}else{
// 					printf("\r\n密码校验错误！\r\n");
// 					break;
// 				}
// 			}
// 			
// 			if(setParamChangeMark == 1)
// 			{
// 				setParamChangeMark = 0;
// 				setStep = 0x00;
// 				printf("\r\n参数设置命令有切换！！\r\n");
// 				break;
// 			}
// 			break;
// 			
// 		default:
// 			break;
// 	}
// }

/*
***********************************************************************
*	函 数 名: ParamSaveTask
*	功能说明: 设备参数保存任务（有参数修改确认，则需要进行保存）
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void ParamSaveTask(void)
{
	static uint8_t setStep = 0x00;
	
	static timer waitingTimer;
	
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&waitingTimer, CLOCK_SECOND*2);			//创建1个定时器，用于电磁锁开启时间控制
	}
	
	switch(setStep)
	{
		case 0x00:
			if(setParamSaveMark == 1)
			{
				setParamSaveMark = 0;
					
				SaveDeviceConfig();
				DeviceParamInit();										//参数有变动，则重新初始化设备参数
				
				DeviceInfoShow();
				setStep = 0x01;
				timer_restart(&waitingTimer);
			}
			break;
			
		case 0x01:
			if(timer_expired(&waitingTimer))
			{
				if(deviceWorkStatus == 1)
				{
					Screen_Default();
				}else{
					Screen_DeviceDisable();
				}
				setStep = 0x00;
			}
			break;
			
		case 0x02:
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: ParamSetTask
*	功能说明: 参数设置任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void ParamSetTask(void)
{
	if(devSetQrCodeState == 1)
	{
		printf("\r\n进入参数设置操作---->\r\n");
		devSetQrCodeState = 0;
		SetParamDecode(codeBuf);									//设置二维码参数解码
	}
	
	PasswordSetTask();												//密码修改任务
	PasswordCastSetTask();										//强制修改密码任务
// 	DeviceTypeSetTask();											//设备类型设置任务
	CustomerIdSetTask();											//客户号设置任务
	
	DisableICCardFunSetTask();								//启用/停用“禁用的IC卡不能开门”功能设置任务
	EnableCleanQRCodeSetTask();								//启用/停用“收运二维码开门”功能设置任务
	
	ScaleCalibrationTask();										//称盘校准操作任务
	BoxOpenSetTask();													//临时打开收运清理门操作任务
	
	ParamSaveTask();													//设备参数保存任务
}


/******************** (C) COPYRIGHT 2017 CXHY *****END OF FILE*******************/
