/*************************** (C) COPYRIGHT 2017 CXHY ****************************
 * 文件名  ：DataProcessing.c
 * 描  述  ：数据处理代码     
 * 版  本  ：V1.0
 * 作  者  ：XuZhongPing
 * 日  期  ：2017/03/05 
*********************************************************************************/
#include "my_math.h"
#include "delay.h"
#include "data_typedef.h"	  
#include "gsm_gprs.h" 
#include "gps.h" 
#include "message.h"
#include "tcp_protocol.h"
#include "device.h"

char VersionStr[17];

uint8_t deviceInitFlag = 0;								//设备硬件接口初始化完成标志
uint8_t swKeysVal = 0;
uint8_t adcDevInitOK = 0;
uint8_t synchronousOpenDoorMark = 1;			//同步开门标志（1：为同时开门；0：为独立开门）

uint16_t delayRandomTime = 0;							//网络断开重连后延时发送随机时间

uint8_t water;														//水位

uint8_t optoSwitchStatus = 0;							//光电开关状态
uint8_t switchKey1Status = 0;							//接近开关状态
uint8_t switchKey2Status = 0;							//接近开关状态
uint8_t dropOffPortMark1 = 0;
uint8_t dropOffPortMark2 = 0;

uint8_t getRecycleClearDoorMark = 0;			//硬件获取的收运门状态
uint8_t recycleClearDoorMark = 0;					//收运门状态

uint8_t smokeAlarmStatus = 0;							//烟雾报警状态

uint8_t dropPortClosedStatus1 = 0;				//投放口关闭状态
uint8_t dropPortClosedStatus2 = 0;

uint8_t gpsConnectSta = 0;								//GPS模块硬件连接状态
uint8_t gpsStatus = 0;										//GPS定位状态
uint8_t gpsEW = 0;												//经度方向（0->E-东,W-西）
uint32_t gpsLon = 0;											//经度（已放大1000000倍）
uint8_t  gpsNS = 0;												//纬度方向（0->N-北,1->S-南）
uint32_t gpsLat = 0;											//纬度（已放大1000000倍）
uint8_t getGpsStatus = 0;									//取得GPS信息

uint8_t distance = 255;										//单路测距数据
uint8_t getDistance1 = 255;								//满溢测距值
uint8_t getDistance2 = 255;								
uint8_t getDistance3 = 255;								
uint8_t getDistance4 = 255;		
uint8_t updateDistanceMark = 0;	

uint8_t fullMarkLeft = 0;									//左桶装满标志
uint8_t fullMarkRight = 0;								//右桶装满标志

uint8_t fullMark1 = 0;
uint8_t fullMark2 = 0;
uint8_t fullMark3 = 0;
uint8_t fullMark4 = 0;

uint16_t getWeight1 = 0;									//取得的重量
uint16_t getWeight2 = 0;									
uint16_t getWeight3 = 0;									
uint16_t getWeight4 = 0;

uint16_t getWeightBefore1 = 0;						//取得的第1路回收桶的重量（开门前）
uint16_t getWeightAfter1 = 0;							//取得的第1路回收桶的重量（开门后）
uint16_t getWeightBefore2 = 0;						
uint16_t getWeightAfter2 = 0;							
uint16_t getWeightBefore3 = 0;						
uint16_t getWeightAfter3 = 0;							
uint16_t getWeightBefore4 = 0;					
uint16_t getWeightAfter4 = 0;

uint16_t weightChangeBefore_1 = 0;				//重量变化前的重量
uint16_t weightChangeBefore_2 = 0;							

//异常作弊投放重量
uint16_t exceptionWeight1 = 0;						//检测到的异常作弊投放重量
uint16_t exceptionWeight2 = 0;									
uint16_t exceptionWeight3 = 0;									
uint16_t exceptionWeight4 = 0;		

uint16_t exceptionWeightBefore1 = 0;			//取得的第1路异常投放重量（作弊前）
uint16_t exceptionWeightAfter1 = 0;				//取得的第1路异常投放重量（作弊后）
uint16_t exceptionWeightBefore2 = 0;						
uint16_t exceptionWeightAfter2 = 0;							
uint16_t exceptionWeightBefore3 = 0;						
uint16_t exceptionWeightAfter3 = 0;							
uint16_t exceptionWeightBefore4 = 0;					
uint16_t exceptionWeightAfter4 = 0;	

uint16_t smallBottleCount = 0;						//小塑料瓶数量
uint16_t bigBottleCount = 0;							//大塑料瓶数量
uint16_t cansCount = 0;										//金属易拉罐数量

uint8_t getPutInType = 0;									//接收到的投放类型（按钮、IC卡、二维码、垃圾袋条形码）
uint8_t getDataLength = 0;								//接收到的身份信息长度
uint8_t getDataBuf[256];										//接收到的身份信息缓存

uint8_t sendPutInType = 0;								//发送投放类型（按钮、IC卡、二维码、垃圾袋条形码）
uint8_t sendDataLength = 0;								//发送身份信息的长度
uint8_t sendDataBuf[256];									//发送身份信息缓存

uint8_t pcRequestOpenDoorMark = 0;				//上位机请求打开投放口
uint8_t openDoorNumber = 0;								//请求打开的投口编号

uint8_t rspPCDoorNumber = 0;							//应答上位机投口编号
uint8_t rspPCWeightNumber = 0;						//应答上位机秤盘编号
uint8_t rspPCDistanceNumber = 0;					//应答上位机测距编号		
uint8_t rspPCTemperatureNumber = 0;				//应答上位机温度编号

uint8_t pcRequestClooseDoorMark = 0;			//上位机请求关闭投放口

//收运用户信息上传
uint8_t collectorSendAuthType = 0;				//发送投放类型（按钮、IC卡、二维码、垃圾袋条形码）
uint8_t collectorSendDataLength = 0;			//发送身份信息的长度
uint8_t collectorSendDataBuf[35];					//发送身份信息缓存

uint8_t get_IC_ID[16];											//接收到的IC卡号
uint8_t send_IC_ID[16];										//将要发送到后台的IC卡号
uint8_t getCardNumberBit[32];							//卡号分离出来的17位十进制数据


//后台操作应答相关变量
uint8_t userInfoQueryStatus = 0;					//从后台获取用户信息返回状态
uint8_t getUserInfoMark = 0;							//取得用户信息标志
uint8_t putInUploadSuccessMark = 0;				//投放上传成功标志
uint8_t getSetDistanceParamMark = 0;			//取得后台设置满溢参数
uint8_t getDeviceWorkStatusMark = 0;			//取得后台设置的设备工作状态标志

uint8_t deviceStatus = 0;									//心跳包返回设备状态

uint8_t  isAuth;													//是否通过验证
uint32_t userID;													//用户ID
uint32_t accountNum;											//用户账号
uint8_t  nickNameLength;									//昵称数据长度
uint8_t  nickName[64];										//昵称数据
uint32_t capital = 0;											//资金
uint32_t capitalAfter = 0;								//资金（投放后的余额）
uint8_t  residue;													//提货码
uint8_t  businessNum[8];									//交易流水号 
uint16_t receiveBusinessNum;							//后台返回的交易流水号
uint8_t  isOutput;												//是否可以出货

uint8_t  barcodeLength = 0;								//垃圾袋二维码数据长度
uint8_t  barcode[32];											//垃圾袋二维码数据

//收运门相关
uint8_t getBoxOpenAckMark = 0;						//取得后台返回打开收运门状态标志
uint8_t boxOpenAckStatus = 0;							//后台应答的收运门打开标志	

//投放过程中作用的变量
uint8_t putInProgress = 0;								//投放进行中标志
uint8_t isOpenDoor = 0;
uint8_t keyOpenDoorRequest = 0;						//按键开门请求

uint8_t isOpenDoorRequest_1 = 0;					//请求开门标志
uint8_t isOpenDoorRequest_2 = 0;
uint8_t isOpenDoorRequest_3 = 0;
uint8_t isOpenDoorRequest_4 = 0;
uint8_t isOpenDoor_1 = 0;									//开门标志
uint8_t isOpenDoor_2 = 0;
uint8_t isOpenDoor_3 = 0;
uint8_t isOpenDoor_4 = 0;

uint8_t isCloseDoorMark_1 = 0;						//请求关门标志
uint8_t isCloseDoorMark_2 = 0;
uint8_t isCloseDoorMark_3 = 0;
uint8_t isCloseDoorMark_4 = 0;

uint8_t openDoorCtrlEndMark_1 = 0;				//开关门控制结束标志
uint8_t openDoorCtrlEndMark_2 = 0;
uint8_t openDoorCtrlEndMark_3 = 0;
uint8_t openDoorCtrlEndMark_4 = 0;

uint8_t putInEndMark = 0;									//设备运转控制结束标志

uint8_t delayCloseDoorStatus = 0;					//接收到延迟关门
uint8_t delayCloseDoorStatus2 = 0;				//接收到延迟关门

uint8_t disablePutInMark = 0;							//禁止投放完成标志
uint8_t putInOKMark = 0;									//投放完成标志

uint8_t getRemainingTimerStatus = 0;			//取得倒计时时间状态
uint8_t remainingTimer = 0; 							//剩余关门时间

uint8_t faceEntryFlag = 0;								//人脸录入操作标志

uint8_t devChannel1FaultMark = 0;					//故障标志
uint8_t devChannel2FaultMark = 0;					//故障标志
uint8_t devChannel3FaultMark = 0;					//故障标志
uint8_t devChannel4FaultMark = 0;					//故障标志
uint8_t deviceFault = 0;									//设备故障
uint8_t faultLedFlashMark = 0;						//故障时灯闪烁标志
uint8_t faultCode_1 = 0;									//故障码
uint8_t faultCode_2 = 0;
uint8_t faultCode_3 = 0;
uint8_t faultCode_4 = 0;
uint8_t limitKeyExceptionMark = 0;				//限位开关异常标志
uint8_t displayFaultCode = 0;							//显示的故障码

uint8_t exitMark = 0;											//退出标志

uint8_t boxOpenRecordMark = 0;						//收运清理门开门记录标志（标志是否已经开过，用于临时二维码开门）
uint8_t boxOpenMark = 0;									//取得临时二维码开收运清理门标志
uint8_t boxOpenQRMark = 0;								//取得临时二维码开收运清理门标志

//设备相关参数
//当前使用的参数变量
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
uint8_t deviceType = FOUR_CLASSIFICATION;		//设备的类型（默认四分类）
uint8_t devChannelNumbers = 4;						//投放口通道数量
uint8_t leftBarrelType = GLASS;						//设备左桶类型
uint8_t rightBarrelType = METAL;					//设备右桶类型

uint8_t devChannel1Type = GLASS;					//设备通道类型
uint8_t devChannel2Type = METAL;
uint8_t devChannel3Type = PLASTIC;
uint8_t devChannel4Type = PAPER;

uint32_t password = 123321;								//设备操作密码
uint32_t customerID = 000000;							//客户号（用于控制谁可以打开收运清理门）

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

uint8_t distance_R_MaxVal;					//设置右桶测距最大值（即安装最高点的值）
uint8_t distance_R_DepthVal;				//设置右桶测距桶的深度（即内桶的高度）
uint8_t distance_R_AlarmPercent;		//设置右桶测距装满报警百分比

//设备通道满溢参数
uint8_t distanceParamAtStart = 0;		//开始检测距离的起点距离参数
uint8_t PerCentSetVal = 30;					//开始检测满溢的百分比设置值

uint8_t devChannel1DisDepthVal = 100;				//设置测距桶的深度（即内桶的高度）
uint8_t devChannel1DisMaxVal = 155;					//设置测距最大值（即安装最高点的值）
uint8_t devChannel1DisAlarmPercent = 90;		//设置测距装满报警百分比

uint8_t devChannel2DisDepthVal = 100;				
uint8_t devChannel2DisMaxVal = 155;
uint8_t devChannel2DisAlarmPercent = 90;

uint8_t devChannel3DisDepthVal = 100;
uint8_t devChannel3DisMaxVal = 160;
uint8_t devChannel3DisAlarmPercent = 90;

uint8_t devChannel4DisDepthVal = 100;
uint8_t devChannel4DisMaxVal = 160;
uint8_t devChannel4DisAlarmPercent = 90;

uint8_t disableICCardFunStatus = 0;				//启用/停用“禁用的IC卡不能开门”功能标志位（1：停用功能；其它启用功能）默认启用功能
uint8_t enableCleanQRCodeStatus = 0;			//启用/停用“收运二维码开门”功能标志位（1：启用功能；其它停用功能）默认停用功能

uint8_t deviceIdLength = 0;								//设备编号长度
uint8_t deviceIdBuf[33] = {0};						//设备编号

uint8_t ipAddr0 = 255, ipAddr1 = 255, ipAddr2 = 255, ipAddr3 = 255;
uint16_t ipPort = 8080;

//取得的待修改后保存的参数变量（从flash中取出或者通过二维码取得的待保存和使用的参数变量）
uint8_t setDeviceType = 0;								//设置的设备类型
uint8_t setDevChannelNumbers = 0;					//设置时取得的投放口通道数量
uint8_t setDevChannel1Type = 0;						//设置设备通道回收类型
uint8_t setDevChannel2Type = 0;
uint8_t setDevChannel3Type = 0;
uint8_t setDevChannel4Type = 0;

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

uint8_t setDevChannel1DisDepthVal = 0;		//设置桶的深度距离（即内桶的高度）
uint8_t setDevChannel1DisMaxVal = 0;			//设置传感器安装高度距离最大值（即安装最高点的值）
uint8_t setDevChannel1DisAlarmPercent = 0;//设置满溢距离报警百分比

uint8_t setDevChannel2DisDepthVal = 0;
uint8_t setDevChannel2DisMaxVal = 0;
uint8_t setDevChannel2DisAlarmPercent = 0;

uint8_t setDevChannel3DisDepthVal = 0;
uint8_t setDevChannel3DisMaxVal = 0;
uint8_t setDevChannel3DisAlarmPercent = 0;

uint8_t setDevChannel4DisDepthVal = 0;
uint8_t setDevChannel4DisMaxVal = 0;
uint8_t setDevChannel4DisAlarmPercent = 0;

uint8_t setDisableICCardFunStatus = 0;		//设置启用/停用“禁用的IC卡不能开门”功能标志位（1：停用功能；其它启用功能）
uint8_t setEnableCleanQRCodeStatus = 0;		//设置启用/停用“收运二维码开门”功能标志位（1：启用功能；其它停用功能）

uint8_t setDeviceIdLength = 0;						//设置的设备编号长度
uint8_t setDeviceIdBuf[33];								//设置的设备编号

uint8_t setIpAddr0 = 0, setIpAddr1 = 0, setIpAddr2 = 0, setIpAddr3 = 0;
uint16_t setIpPort = 0;

//用户设置取得的参数变量
uint8_t getDeviceType = 0;								//取得的设备类型
uint8_t getDevChannelNumbers = 0;					//设置时取得的投放口通道数量
uint8_t getDevChannel1Type = 0;						//取得的设备通道回收类型
uint8_t getDevChannel2Type = 0;
uint8_t getDevChannel3Type = 0;
uint8_t getDevChannel4Type = 0;

uint32_t getConfirmPassword = 0;					//取得确认密码
uint32_t getPassword = 0;									//取得的设备操作密码
uint32_t getCustomerID = 0;								//取得的客户号

uint8_t getDevChannel1DisDepthVal = 0;		//设置桶的深度距离（即内桶的高度）
uint8_t getDevChannel1DisMaxVal = 0;			//设置传感器安装高度距离最大值（即安装最高点的值）
uint8_t getDevChannel1DisAlarmPercent = 0;//设置满溢距离报警百分比

uint8_t getDevChannel2DisDepthVal = 0;
uint8_t getDevChannel2DisMaxVal = 0;
uint8_t getDevChannel2DisAlarmPercent = 0;

uint8_t getDevChannel3DisDepthVal = 0;
uint8_t getDevChannel3DisMaxVal = 0;
uint8_t getDevChannel3DisAlarmPercent = 0;

uint8_t getDevChannel4DisDepthVal = 0;
uint8_t getDevChannel4DisMaxVal = 0;
uint8_t getDevChannel4DisAlarmPercent = 0;

uint8_t getDisableICCardFunStatus = 0;		//取得启用/停用“禁用的IC卡不能开门”功能标志位（1：停用功能；其它启用功能）
uint8_t getEnableCleanQRCodeStatus = 0;		//取得启用/停用“收运二维码开门”功能标志位（1：启用功能；其它停用功能）

uint8_t getDeviceIdLength = 0;						//取得的设备编号长度
uint8_t getDeviceIdBuf[33];								//取得的设备编号

uint8_t getIpAddr0 = 0, getIpAddr1 = 0, getIpAddr2 = 0, getIpAddr3 = 0;
uint16_t getIpPort = 0;

uint8_t getScaleNum = 0;									//取得校准称盘的编号

//有取得相关参数标志
uint8_t setParamSaveMark = 0;							//设置参数需要进行保存标志
uint8_t setParamChangeMark = 0;						//设置参数有变化标志

uint8_t getConfirmPasswordMark = 0;				//取得待确认密码标志
uint8_t getPasswordMark = 0;							//取得要设置的密码标志
uint8_t getForceChangeMark = 0;						//取得强制修改密码标志
uint8_t getDeviceTypeMark = 0;						//取得设置设备类型标志
uint8_t getCustomerIDMark = 0;						//取得设置客户号标志

uint8_t getDisableICCardFunMark = 0;			//取得启用/停用“禁用的IC卡不能开门”功能设置标志
uint8_t getEnableCleanQRCodeMark = 0;			//取得启用/停用“收运二维码开门”功能设置标志

uint8_t getOverflowSetMark = 0;						//取得设置满溢参数标志

uint8_t getScaleCalibrationMark = 0;			//取得称盘校准标志
uint8_t getBoxOpenMark = 0;								//取得临时二维码开收运清理门标志

uint8_t getDeviceParamResetMark = 0;			//取得恢复出厂设置
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

uint8_t deviceWorkStatus = 1;							//设备工作状态（正常和停用）
uint8_t deviceWorkStatusPre = 1;					//设备工作状态切换前的状态
uint8_t getDeviceWorkStatusVal = 0;				//取得后台设置的工作状态值

uint8_t getMcu2SensorDataMark = 0;				//取得MCU2发送的传感器数据标志

uint16_t netStatus = 0;										//网络状态
uint16_t devRunnigStatus = 0;							//设备启用状态

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