//
//  BTDataAnalysis.m
//  Unity-iPhone
//
//  Created by duowan123 on 2022/1/26.
//

#import "BTDataAnalysis.h"
#import "IOSPlatformSDK.h"
@implementation BTDataAnalysis

#pragma mark ===============================================>> 校验 & 报文数据解析
-(void)analysisCharacteristic:(NSData*)characteristic peripheral:(CBPeripheral*)peripheral{
        
////    NSLog(@"接收到的数据data = %@",characteristic);
//    int frameHead = [AlgorithmTool dataToChar:[characteristic subdataWithRange:NSMakeRange(0, 1)]];//帧头数据aa
//    int messageLength = [AlgorithmTool dataToChar:[characteristic subdataWithRange:NSMakeRange(1, 1)]];//报文长度
//    int messageLengthNegation = ~messageLength;//报文长度取反
//    int dataType = [AlgorithmTool dataToChar:[characteristic subdataWithRange:NSMakeRange(3, 1)]];//cmd类型
//
//      if (dataType == 4){//获取主动推过来的数据
//            //右脚坐标数据
//           int right_X = [AlgorithmTool dataToSwapBigIntToHost:characteristic andRange:NSMakeRange(4, 4)];
//           int right_Y = [AlgorithmTool dataToSwapBigIntToHost:characteristic andRange:NSMakeRange(8, 4)];
//           int right_Z = [AlgorithmTool dataToSwapBigIntToHost:characteristic andRange:NSMakeRange(12, 4)];
//           int  right_pos[3] = {right_X,right_Y,right_Z};
//          //左脚坐标数据
//           int left_X = [AlgorithmTool dataToSwapBigIntToHost:characteristic andRange:NSMakeRange(16, 4)];
//           int left_Y = [AlgorithmTool dataToSwapBigIntToHost:characteristic andRange:NSMakeRange(20, 4)];
//           int left_Z = [AlgorithmTool dataToSwapBigIntToHost:characteristic andRange:NSMakeRange(24, 4)];
//           int  left_pos[3] = {left_X,left_Y,left_Z};
//           //右脚姿势数据
//           short righrPosture_X = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(28, 2)];
//           short righrPosture_Y = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(30, 2)];
//           short righrPosture_Z = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(32, 2)];
//           int  right_att[3] = {righrPosture_X,righrPosture_Y,righrPosture_Z};
//           //左脚姿势数据
//           short leftPosture_X = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(34, 2)];
//           short leftPosture_Y = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(36, 2)];
//           short leftPosture_Z = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(38, 2)];
//           int  left_att[3] = {leftPosture_X,leftPosture_Y,leftPosture_Z};
//           //右脚三维数据
//           short righrAcc_x = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(40, 2)];
//           short righrAcc_Y = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(42, 2)];
//           short righrAcc_z = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(44, 2)];
//           int  righr_acc[3] = {righrAcc_x,righrAcc_Y,righrAcc_z};
//           //左脚三维数据
//           short leftAcc_x = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(46, 2)];
//           short leftAcc_y = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(48, 2)];
//           short leftAcc_z = [AlgorithmTool dataToSwapBigShortToHost:characteristic andRange:NSMakeRange(50, 2)];
//           int  left_acc[3] = {leftAcc_x,leftAcc_y,leftAcc_z};
//           //左脚、右脚、蹲 、跳四个动作信息数据
//           int actionInformation =  [AlgorithmTool dataToChar:[characteristic subdataWithRange:NSMakeRange(52, 1)]];;//0c 16进制字符串
//                       int girlShoes  = actionInformation & 16;
//                       if (girlShoes!=0){
//                           girlShoes=1;
//                       }else{
//                           girlShoes=0;
//                       }
//                       int rightZupt  = actionInformation & 8;
//                       if (rightZupt!=0){
//                           rightZupt=1;
//                       }else{
//                           rightZupt=0;
//                       }
//                       int leftZupt = actionInformation & 4;
//                       if (leftZupt!=0){
//                            leftZupt=1;
//                        }else{
//                            leftZupt=0;
//                        }
//                       int down  = actionInformation & 2;
//                       if (down!=0){
//                            down=1;
//                        }else{
//                            down=0;
//                        }
//                       int jump  = actionInformation & 1;
//                       if (jump!=0){
//                            jump=1;
//                        }else{
//                            jump=0;
//                        }
//
//            int rssi = [AlgorithmTool dataToChar:[characteristic subdataWithRange:NSMakeRange(53, 1)]];//rssi 信号强度
//            int ts = [AlgorithmTool dataToChar:[characteristic subdataWithRange:NSMakeRange(54, 1)]];;//ts 时间戳
//            int right_press = [AlgorithmTool dataToInt:[characteristic subdataWithRange:NSMakeRange(55, 2)]];//3 29新增 右鞋压力
//            int left_press = [AlgorithmTool dataToInt:[characteristic subdataWithRange:NSMakeRange(57, 2)]];//3 29新增 左鞋压力
//            int frameCheck =  [AlgorithmTool dataToChar:[characteristic subdataWithRange:NSMakeRange(59, 1)]];//校验位
//
//              if (deviceType==DEVICETYPE_MAIN){//主设备
//    //                   NSLog(@"主设备 接收到 鞋子动作数据 data = %@",characteristic);
//                  //调取鞋子SDK
//    //                   [self shoseSDKRight_pos:right_pos Right_att:right_att Right_acc:righr_acc LeftPos:left_pos Left_att:left_att Left_acc:left_acc ts:ts rightZupt:rightZupt leftZupt:leftZupt jump:jump down:down rssi:rssi girlShoes:girlShoes right_press:right_press left_press:left_press characteristic:characteristic];
//
//              }else if (deviceType==DEVICETYPE_VICE){//副设备动作数据
//                  //调取鞋子SDK
//    //                   [self viceShoseSDKRight_pos:right_pos Right_att:right_att Right_acc:righr_acc LeftPos:left_pos Left_att:left_att Left_acc:left_acc ts:ts rightZupt:rightZupt leftZupt:leftZupt jump:jump down:down rssi:rssi girlShoes:girlShoes right_press:right_press left_press:left_press characteristic:characteristic];
//              }
//
//        }else if (dataType == -95 && characteristic.length == 26){//获取的是查询的数据 char-->int a1 = -95
           //A1+1 >> 1: 设备数据(左鞋,右鞋)  aa 14 eb a1 01 64 00 1a 0000981d 64 00 17 00000000 f9
    //        NSLog(@"主设备 读取特征的报文 硬件信息 %ld dataType = -95 %@ %ld",(long)deviceType,characteristic,characteristic.length);

                int leftElectricity = [AlgorithmTool dataToInt:[characteristic subdataWithRange:NSMakeRange(5, 1)]];
                int rightElectricity = [AlgorithmTool dataToInt:[characteristic subdataWithRange:NSMakeRange(15, 1)]];//龙哥新协议
                
                if (peripheral==LEManager.peripheral && LEManager.peripheral!=nil){//主设备
//                    NSLog(@"主设备 接收到 鞋子硬件数据: %@  电量 %d %d %@ %@",characteristic,leftElectricity,rightElectricity,LEManager.peripheral.name,LEManager.peripheral.identifier.UUIDString);
                    //主 设备定时器 60秒调一次   /******************ios call unity*****************/
                     [[IOSPlatformSDK sharedInstance] bridgingDeviceAction:DEVICETYPE_MAIN
                                                 name:LEManager.peripheral.name
                                              address:LEManager.peripheral.identifier.UUIDString
                                               status:CONNECT_ED
                                          electricity:leftElectricity<=rightElectricity?leftElectricity:rightElectricity];
                    
                }else if (peripheral==LEManager.vicePeripheral && LEManager.vicePeripheral!=nil){//副设备
//                    NSLog(@"副设备 接收到 鞋子硬件数据: %@  电量 %d %d",characteristic,leftElectricity,rightElectricity);
                    //副 设备定时器 60秒调一次   /******************ios call unity*****************/
                     [[IOSPlatformSDK sharedInstance] bridgingDeviceAction:DEVICETYPE_VICE
                                                 name:LEManager.vicePeripheral.name
                                              address:LEManager.vicePeripheral.identifier.UUIDString
                                               status:CONNECT_ED
                                          electricity:leftElectricity<=rightElectricity?leftElectricity:rightElectricity];
                    
                }
//        }
}


@end
