@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
- (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) {