写入deviceSecrect直到成功
authorliangzhenying <867174124@qq.com>
Fri, 17 May 2024 12:22:13 +0000 (20:22 +0800)
committerliangzhenying <867174124@qq.com>
Fri, 17 May 2024 12:22:13 +0000 (20:22 +0800)
MouseSdk/AiMouseSdk/AiMouseSdk/AiAuthManager.m
MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h
MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m
MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
MouseSdk/MouseSdk/ViewController.m

index db6b87f..e516295 100644 (file)
 
 #pragma mark - pub api
 - (void)loginWith:(AiMouseDeviceInfo *)info{
+
     self.deviceInfo = info;
     if (![info valueForKey:@"productKey"] || ![info valueForKey:@"secretKey"] || !info.sn || !info.sn) {
         [self showLog:@"获取设备信息错误,鉴权失败!"];
         return;
     }
-    
     if([info valueForKey:@"deviceSecret"]){
         //已注册过,直接登录
         [self loginRequest];
index 5e1d5e8..f0e96b6 100644 (file)
@@ -101,4 +101,6 @@ typedef NS_ENUM(NSInteger,AiMouseAuthState) {
 ///   - value: AiMouseDeviceInfo 中可以设置的值CPI_list
 ///   - finished: 设置结果回调
 - (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished;
+
+- (void)setDS;
 @end
index 6f9d9e9..7daa2be 100644 (file)
@@ -39,6 +39,8 @@
 @property (nonatomic, strong) void (^getRSSIBlock)(NSString *value);
 @property (nonatomic, strong) SetDeviceSecretBlock deviceSecretBlock;
 @property (nonatomic, strong) void (^authBlock)(AiMouseAuthState state);
+@property (nonatomic, strong) NSString *updateDS;//更新deviceSecret
+@property (nonatomic, strong) NSTimer *dsTimer;
 @end
 
 @implementation AiMouse
     NSString *str2 = [AiMouseTools2 fixDeviceData:str];
     NSDictionary *dict = [AiMouseTools stringToDictionary:str2];
 //    [self showLog:[NSString stringWithFormat:@"总data = %@",[AiMouseTools dataToHexString:data]]];
-//    [self showLog:[NSString stringWithFormat:@"fix前 = %@",str]];
-//    [self showLog:[NSString stringWithFormat:@"fix后 = %@",str2]];
+    [self showLog:[NSString stringWithFormat:@"fix前 = %@",str]];
+    [self showLog:[NSString stringWithFormat:@"fix后 = %@",str2]];
     if (dict == nil) {
         if(data.length > 7 && [[AiMouseTools dataToHexString:[data subdataWithRange:NSMakeRange(0, 7)]] .lowercaseString isEqualToString:@"7b22534e223a22"] ){
             //SN异常
             if(self.deviceSecretBlock){
                 self.deviceSecretBlock(true);
                 self.deviceSecretBlock = nil;
+                [self removeSendTimer];
             }
         }
         else{
 }
 
 - (void)setDeviceSecret:(NSString *)value completation:(SetDeviceSecretBlock)complete{
+    self.updateDS = value;
     self.deviceSecretBlock = complete;
     NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"set_config",@"set_deviceSecret":value}];
     [self sendDeviceInfoData:data];
+    [self addSendTimer];
+}
+
+- (void)setDS{
+//    NSString *value = @"JXa2HAaXhaw5mnbjqadtMQQoDQOmGGQw";
+    NSString *value = @"JXa2HAaXhaw5mnbjqadtMQQoDQOmGGQw";
+    [self setDeviceSecret:value completation:^(BOOL isSuccess) {
+//        NSLog(@"写入ds成功========");
+    }];
+}
+- (void)addSendTimer{
+    [self removeSendTimer];
+    dispatch_async(dispatch_queue_create("DeviceQueue", DISPATCH_QUEUE_SERIAL), ^{
+        self.dsTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(dsTimerAction) userInfo:nil repeats:true];
+        [[NSRunLoop currentRunLoop] addTimer:self.dsTimer forMode:NSRunLoopCommonModes];
+        [[NSRunLoop currentRunLoop] run];
+    });
+}
+- (void)removeSendTimer{
+    if (self.dsTimer && [self.dsTimer isValid]) {
+        [self.dsTimer invalidate];
+    }
+}
+
+- (void)dsTimerAction{
+    if(self.updateDS && self.deviceSecretBlock){
+        [self setDeviceSecret:self.updateDS completation:self.deviceSecretBlock];
+    }
+    else{
+        [self removeSendTimer];
+    }
 }
 #pragma mark - other
 - (void)sendDeviceInfoData:(NSData *)data{
-    NSString *str = [AiMouseTools dataToUtf8String:data];
-    NSMutableString *str1 = [NSMutableString stringWithString:str];
-    NSString *sendStr = [NSString stringWithFormat:@"-$$%@$$-\n",str1];
-    NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
+//    NSString *str = [AiMouseTools dataToUtf8String:data];
+//    NSMutableString *str1 = [NSMutableString stringWithString:str];
+//    
+//    NSString *sendStr = [NSString stringWithFormat:@"-$$%@$$-\n",str1];
+//    NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
 //    [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
-    if (self.delegate) {
-        [self.delegate aiMouseSendDeviceInfoData:sendData];
+//    if (self.delegate) {
+//        [self.delegate aiMouseSendDeviceInfoData:sendData];
+//    }
+    
+//    NSString *str = [AiMouseTools dataToUtf8String:data];
+    NSString *str = [AiMouseTools dataToUtf8String:data];
+    NSArray *list = [self splitCMD:str];
+    
+    
+    if(list.count > 1){
+        {
+            NSString *sendStr = @"-$$\0";
+            NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
+//            [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
+            if (self.delegate) {
+                [self.delegate aiMouseSendDeviceInfoData:sendData];
+            }
+        }
+        for (NSInteger i = 0; i < list.count; i++) {
+            NSString *sendStr = [NSString stringWithFormat:@"%@\0",list[i]];
+            NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
+//            [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
+            if (self.delegate) {
+                [self.delegate aiMouseSendDeviceInfoData:sendData];
+            }
+        }
+        {
+            NSString *sendStr = @"$$-\0";
+            NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
+//            [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
+            if (self.delegate) {
+                [self.delegate aiMouseSendDeviceInfoData:sendData];
+            }
+        }
+    }
+    else{
+        NSString *sendStr = [NSString stringWithFormat:@"-$$%@$$-\n",str];
+        NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
+//        [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
+        if (self.delegate) {
+            [self.delegate aiMouseSendDeviceInfoData:sendData];
+        }
     }
 }
 
+- (NSArray<NSString *> *)splitCMD:(NSString *)cmd{
+    NSMutableArray *list = [[NSMutableArray alloc] init];
+    NSString *str = cmd;
+    NSInteger len = 30;
+    while (str.length > 0) {
+        if (str.length > len) {
+            NSString *sub = [str substringToIndex:len];
+            [list addObject:sub];
+            str = [str substringFromIndex:len];
+        }
+        else{
+            [list addObject:str];
+            break;
+        }
+    }
+    return list;
+}
+
 - (void)sendRecordData:(NSData *)data{
     if (self.authState != AiMouseAuthState_Success){
         [self showLog:@"设备还未鉴权!!!"];
 //    [self showLog:@"结束录音"];
 }
 
+
 @end
 
index d3bdc84..6c6a72b 100644 (file)
Binary files a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ
index fb74f81..1928a81 100644 (file)
 
 - (void)setupUI{
     self.title = @"AI SDK";
+    {
+        NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height + 80, 100, 45)];
+        btn.bordered = true;
+        btn.title = @"设置CPI";
+        [btn setTarget:self];
+        [btn setAction:@selector(cpiAction)];
+        btn.state = 1;
+        [self.view addSubview:btn];
+    }
+    {
+        NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height, 100, 45)];
+        btn.bordered = true;
+        btn.title = @"写入deviceScrect";
+        [btn setTarget:self];
+        [btn setAction:@selector(deviceScrectAction)];
+        btn.state = 1;
+        [self.view addSubview:btn];
+    }
     {
         NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 80, 100, 45)];
         btn.bordered = true;
     [self presentViewControllerAsModalWindow:vc];
 }
 
+- (void)cpiAction{
+    [[AiMouse shared] setCPI:1000 completation:^(BOOL isSuccess, NSError *error) {
+        NSLog(@"1");
+    }];
+}
+
+- (void)deviceScrectAction{
+    [[AiMouse shared] setDS];
+}
+
 /// 获取设备信息
 - (void)geviceInfoAction{
     [[DeviceManger shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {