DebugView.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // DebugView.m
  3. // Unity-iPhone
  4. //
  5. // Created by duowan123 on 2021/12/22.
  6. //
  7. #import "DebugView.h"
  8. #import "pub.h"
  9. @interface DebugView ()
  10. /*****测试*******/
  11. @property (nonatomic,strong)UILabel * testLabel;//调试框
  12. @property (nonatomic,strong)UIButton * uploadButton;//上传
  13. @property (nonatomic,strong)UIButton * clearnButton;//上传
  14. //当前的时间戳+丢包
  15. @property (nonatomic,strong)NSString * tempStepString;//剑波要的字符串数据 缓存plist
  16. @property(nonatomic,assign) int currentTS;//颖嘉要的测试数据
  17. @property (nonatomic,strong)NSString *current_game_date;
  18. @end
  19. @implementation DebugView
  20. #pragma mark ===============================================>> 静态的初始化方法 init的时候(建立蓝牙中心管理类,设置代理)
  21. //单例静态
  22. static DebugView* instance = nil;
  23. +(instancetype)sharedInstance{
  24. // NSLog(@"创建单例一次 1");
  25. return [[self alloc] init];
  26. }
  27. + (instancetype)allocWithZone:(struct _NSZone *)zone{
  28. // NSLog(@"创建单例一次 2");
  29. static dispatch_once_t onceToken;
  30. dispatch_once(&onceToken, ^{
  31. instance = [super allocWithZone:zone];
  32. });
  33. return instance;
  34. }
  35. - (instancetype)init{
  36. // NSLog(@"创建单例一次 3");
  37. static dispatch_once_t onceToken;
  38. dispatch_once(&onceToken, ^{
  39. instance = [super init];
  40. });
  41. return instance;
  42. }
  43. #pragma mark ===============================================>> DEBUG
  44. -(void)notificationStartGame{
  45. // //建立socket链接
  46. // SocketRocketUtility * install = [SocketRocketUtility sharedInstance];
  47. // [install SRWebSocketOpen];
  48. //初始化实时时间戳
  49. self.currentTS = -1;
  50. if (self.testLabel!=nil){
  51. [self.testLabel removeFromSuperview];
  52. }
  53. if (self.uploadButton!=nil){
  54. [self.uploadButton removeFromSuperview];
  55. }
  56. if (self.clearnButton!=nil){
  57. [self.clearnButton removeFromSuperview];
  58. }
  59. self.clearnButton = [UIButton buttonWithType:UIButtonTypeCustom];
  60. self.clearnButton.frame = CGRectMake(30, SCREEN_HEIGHT-40-10, SCALEoefficient(60), SCALEoefficient(25));
  61. self.clearnButton.backgroundColor = [UIColor redColor];
  62. [self.clearnButton addTarget: self action: @selector (button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
  63. [self.clearnButton addTarget: self action: @selector (button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
  64. [self.clearnButton setFont: [UIFont boldSystemFontOfSize:SCALEoefficient(13)]];
  65. [self.clearnButton setTitle:@"清理缓存" forState:UIControlStateNormal];
  66. [self.clearnButton addTarget:self action:@selector(clearCacheWithFilePath) forControlEvents:UIControlEventTouchUpInside];
  67. self.uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
  68. self.uploadButton.frame = CGRectMake(30+SCALEoefficient(60)+20, SCREEN_HEIGHT-40-10, SCALEoefficient(60), SCALEoefficient(25));
  69. self.uploadButton.backgroundColor = [UIColor redColor];
  70. [self.uploadButton addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
  71. [self.uploadButton addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
  72. [self.uploadButton setFont: [UIFont boldSystemFontOfSize:SCALEoefficient(13)]];
  73. [self.uploadButton setTitle:@"上传" forState:UIControlStateNormal];
  74. [self.uploadButton addTarget:self action:@selector(uploadpPlistFile) forControlEvents:UIControlEventTouchUpInside];
  75. self.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCALEoefficient(20), -20, [UIScreen mainScreen].bounds.size.width-100, SCALEoefficient(140))];
  76. self.testLabel.textColor = [UIColor redColor];
  77. self.testLabel.font = [UIFont boldSystemFontOfSize:SCALEoefficient(14)];
  78. self.testLabel.backgroundColor = [UIColor clearColor];
  79. self.testLabel.textAlignment = NSTextAlignmentLeft;
  80. self.testLabel.numberOfLines = 0;
  81. //
  82. if ([UIApplication sharedApplication].keyWindow != nil){
  83. // [[UIApplication sharedApplication].keyWindow addSubview:self.clearnButton];
  84. // [[UIApplication sharedApplication].keyWindow addSubview:self.uploadButton];
  85. // [[UIApplication sharedApplication].keyWindow addSubview:self.testLabel];
  86. }
  87. //jb
  88. // [self clearCacheWithFilePath];
  89. //缓存当局游戏的起始时间
  90. self.current_game_date = [self getNowTime];
  91. }
  92. //button普通状态下的背景色
  93. - (void )button1BackGroundNormal:(UIButton *)sender
  94. {
  95. sender.backgroundColor = [UIColor redColor];
  96. }
  97. // button高亮状态下的背景色
  98. - (void )button1BackGroundHighlighted:(UIButton *)sender
  99. {
  100. sender.backgroundColor = [UIColor orangeColor];
  101. }
  102. #pragma mark ===============================================>> 存储和上传数据
  103. //清理缓存
  104. - (void)clearCacheWithFilePath{
  105. NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Log"];
  106. NSError *error = nil;
  107. //删除子文件夹
  108. [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
  109. if (error){
  110. NSLog(@"清理缓存失败 %@",error);
  111. [PopupView showCusHUDA:@"清理失败"];
  112. }else{
  113. NSLog(@"清理缓存成功");
  114. [PopupView showCusHUDA:@"清理成功"];
  115. }
  116. }
  117. //数据写入沙河
  118. -(void)writeFileToplist{
  119. NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Log"];
  120. NSFileManager *fileManager = [NSFileManager defaultManager];
  121. BOOL isDirExist = [fileManager fileExistsAtPath:path];
  122. if(!(isDirExist)){//文件夹不存在
  123. BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  124. if(!bCreateDir){
  125. NSLog(@"文件夹不存在,创建文件夹失败!");
  126. }else{
  127. NSLog(@"文件夹不存在,创建文件夹成功,文件路径%@",path);
  128. }
  129. }else{//文件夹存在
  130. NSString * filePath = [[path stringByAppendingPathComponent:self.current_game_date] stringByAppendingPathExtension:@"txt"];
  131. // NSLog(@"-------文件夹存在件---------- 文件路径%@",path);
  132. if ([fileManager fileExistsAtPath:filePath]){
  133. // NSLog(@"-------文件存在,追加文件---------- 文件路径%@",filePath);
  134. NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
  135. [fileHandle seekToEndOfFile]; //将节点跳到文件的末尾
  136. NSData* stringData = [self.tempStepString dataUsingEncoding:NSUTF8StringEncoding];
  137. [fileHandle writeData:stringData]; //追加写入数据
  138. [fileHandle closeFile];
  139. }else{
  140. NSLog(@"-------文件不存在,第一次写入文件---------- 文件路径%@",filePath);
  141. NSError *error;
  142. if([self.tempStepString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]){
  143. NSLog(@"------写入文件------success");
  144. }else{
  145. NSLog(@"------写入文件------fail,error==%@",error);
  146. };
  147. }
  148. }
  149. }
  150. -(void)uploadpPlistFile{
  151. //这个方法获取出的结果是一个数组.因为有可以搜索到多个路径.
  152. NSString *documentsDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Log"];
  153. //拼接文件路径
  154. NSString *filePathName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",self.current_game_date]];
  155. //如果保存的是一个数组.那就通过数组从文件当中加载.
  156. NSString *string = [NSString stringWithContentsOfFile:filePathName encoding:NSUTF8StringEncoding error:nil];
  157. // NSLog(@"%@",string);
  158. //4.设置请求体
  159. NSData *upData = [string dataUsingEncoding:NSUTF8StringEncoding];
  160. // NSLog(@"uploadpPlistFile upData = %@",upData);
  161. if (upData==nil){
  162. return;
  163. }
  164. //
  165. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  166. manager.requestSerializer.timeoutInterval=15;
  167. [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
  168. manager.requestSerializer = [AFJSONRequestSerializer serializer];
  169. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/html",@"application/json",@"text/javascript",@"image/jpeg",@"image/png",@"application/octet-stream",@"application/x-www-form-urlencoded", nil];
  170. // NSLog(@"请求URL ===================>> %@",urlString);
  171. NSLog(@"******************* 请求头参数 = %@ ***************************",manager.requestSerializer.HTTPRequestHeaders);
  172. NSString * gameType;
  173. NSString * bundleIdentifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
  174. if ([bundleIdentifier isEqualToString:@"com.Oujia.Run"]){
  175. gameType = @"run";
  176. }else if ([bundleIdentifier isEqualToString:@"com.Oujia.Dance"]){
  177. gameType = @"Dance";
  178. }else{
  179. gameType = @"Tricycle";
  180. }
  181. AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:nil];
  182. [sessionManager POST:@"http://172.16.14.127:8080/upload" parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData){
  183. [formData appendPartWithFileData:upData name:@"file" fileName:[NSString stringWithFormat:@"ios_%@_%s_%@_%@.txt",gameType,GAME_VERSION,[AlgorithmTool returnTimestamp],LEManager.peripheral.name] mimeType:@"text/plain"];
  184. }progress:^(NSProgress * _Nonnull uploadProgress){
  185. NSLog(@"测试数据 上传进度 = %@",uploadProgress);
  186. }success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject){
  187. NSLog(@"测试数据 上传成功");
  188. }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
  189. for (NSString * key in error.userInfo){
  190. if ([key isEqualToString:@"com.alamofire.serialization.response.error.data"]){
  191. id errorObject = [NSJSONSerialization JSONObjectWithData:error.userInfo[key] options:1 error:nil];
  192. NSLog(@"测试数据 上传失败 ===>> %@",errorObject);
  193. if (errorObject !=nil && [errorObject isKindOfClass:[NSDictionary class]]){
  194. NSString * result = errorObject[@"result"];
  195. if ([result intValue] ==1){
  196. [PopupView showCusHUDA:@"测试数据 上传成功"];
  197. }
  198. }
  199. }
  200. }
  201. }];
  202. }
  203. -(void)debugMessage:(NSString*)message shoesInfo:(ShoesInfo*)shoesInfo{
  204. if (message.length <1){
  205. return;
  206. }
  207. /********************调试窗数据 *****************/
  208. NSArray *aArray = [message componentsSeparatedByString:@","];
  209. if (aArray == nil || [aArray isKindOfClass:[NSNull class]] || aArray.count<30){
  210. return;
  211. }
  212. //上传即使动作数据
  213. // [[SocketRocketUtility sharedInstance] sendData:nil withRequestURI:message];
  214. if (LEManager.RSSI == nil){
  215. LEManager.RSSI = @0;
  216. }
  217. //缓存动作数据plist文件
  218. message = [NSString stringWithFormat:@"%@,%@",message,LEManager.RSSI];
  219. self.tempStepString = [NSString stringWithFormat:@"%@\n",message];
  220. if (self.tempStepString!=nil && self.tempStepString.length>1){
  221. [self writeFileToplist];
  222. }
  223. // NSLog(@"aArray == %@",aArray);
  224. // dispatch_async(dispatch_get_main_queue(), ^{
  225. if (self.testLabel!=nil){
  226. self.testLabel.text = [NSString stringWithFormat:@"right:[%@,%@,%@] [%@,%@,%@] [%@,%@,%@] [%@,%@,%@] \nleft:[%@,%@,%@] [%@,%@,%@] [%@,%@,%@] [%@,%@,%@] \nrssi:[%@ %@] ts:%@ \n步数:[%@ %@][%d %d] \n步频:[%d:%d %d:%d] \nsdk版本:%s ",aArray[0],aArray[1],aArray[2],aArray[3],aArray[4],aArray[5],aArray[6],aArray[7],aArray[8],aArray[9],aArray[10],aArray[11],aArray[12],aArray[13],aArray[14],aArray[15],aArray[16],aArray[17],aArray[18],aArray[19],aArray[20],aArray[21],aArray[22],aArray[23],LEManager.RSSI,aArray[26],aArray[27],aArray[28],aArray[29],shoesInfo.rightStepCount,shoesInfo.leftStepCount,shoesInfo.rightStepStatus,shoesInfo.rightStepFreq,shoesInfo.leftStepStatus,shoesInfo.leftStepFreq,GAME_VERSION];
  227. }
  228. // });
  229. /********************颖嘉+的ts丢包数据 *****************/
  230. NSString * tsString = aArray[27];
  231. // NSLog(@"剑波 getGameDataStr 时间戳 ==============>> %@\n",tsString);
  232. int tsInt = [tsString intValue];
  233. int total;
  234. if (self.currentTS != -1){//颖嘉初始值未知 ios初始值==1000
  235. //1....255
  236. if (tsInt > self.currentTS){
  237. total = tsInt - self.currentTS - 1;
  238. }
  239. //255...1
  240. if (tsInt < self.currentTS){
  241. if (tsInt ==0){
  242. total = (255-self.currentTS) + tsInt + 1 - 1;
  243. }else{
  244. total = (255-self.currentTS) + tsInt - 1;
  245. }
  246. }
  247. if (total > 30){
  248. NSLog(@"getGameDataStr 丢包 ================================>> %d",total);
  249. dispatch_async(dispatch_get_main_queue(), ^{
  250. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  251. // [PopupView showCusHUDA:[NSString stringWithFormat:@"丢包数: %d",total]];
  252. });
  253. }
  254. }
  255. // NSLog(@"tsInt = %d currentTS = %d total = %d",tsInt,self.currentTS,total);
  256. self.currentTS = tsInt;
  257. }
  258. // 返回时间戳
  259. -(NSString *)getNowTime{
  260. NSDate *currentDate = [NSDate date];//获取当前时间,日期
  261. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
  262. [dateFormatter setDateFormat:@"YYYY_MM_dd_HH_mm_ss"];//设定时间格式,这里可以设置成自己需要的格式
  263. NSString *dateString = [dateFormatter stringFromDate:currentDate];//将时间转化成字符串
  264. return dateString;
  265. }
  266. @end