//
//  RDSDeviceCmd.m
//  RuiZhi
//
//  Created by Rayson on 2020/6/12.
//  Copyright © 2020 RDIOT. All rights reserved.
//

#import "RDSDeviceCmd.h"

#import "NSDate+currentDate.h"
#import "NSString+convert.h"
#import "NSString+safe.h"

#define kModel  [[RDSDeviceCmd alloc] init]

@implementation RDSDeviceCmd





#pragma mark - 解析指令
+ (void)rds_receiveData:(NSData *)data finish:(RDSCmdReceiveCmdFinish)finish{
    
    
    NSString *string = [NSString rds_getStringFromData:data];
    DDLog(@"收到信息 %@",string);
    NSDictionary *dic = [NSString rds_jsonToDic:string];
    if(dic == nil) return;
    
    RDSDeviceReply *reply = [[RDSDeviceReply alloc] init];
    reply.bindStep = [dic[@"s"] integerValue];
    reply.code = [dic[@"c"] intValue];
    reply.deviceCode = [dic objectForKey:@"id"];
    reply.msg = string;
    
    finish(reply);
    
    [kNotificationCenter postNotificationName:RDSDeviceCmdContentParserNotice object:reply];
}


- (void)p_parseCmd:(NSString *)cmd {
    
}





#pragma mark - Get 方法
- (NSString *)header{
    _header = @"aa";
    return _header;
}

- (NSString *)leng{
    if (_leng == nil) {
        NSString *lengString = self.content;
        int result = (int)lengString.length/2 + 3;
        _leng = [@(result).description rds_getHexFromDecimal];
    }
    
    return _leng;
}

- (NSString *)sn{
    if (_sn == nil) {
//        TheDataManager.msgSN ++;
//        int num = TheDataManager.msgSN;
//        _sn = [@(num).description rds_getHexFromDecimal];
        
    }
    
    return _sn;
}

- (NSString *)content{
    if (_content == nil) {
        _content = @"";
    }
    return _content;;
}

- (NSString *)checksum{
    
    if (_checksum == nil) {
        NSString *checkString = [NSString stringWithFormat:@"%@%@%@%@%@", self.header, self.leng, self.cmd, self.sn, self.content];
        
        if (checkString.length % 2 != 0) return @"00";
        
        int result = 0;
        for (int i = 0; i + 2 <= checkString.length; i += 2) {
            NSString *subString = [checkString rds_getSubStringSafetyWithRange:NSMakeRange(i, 2)];
            
            int intResult = [NSString stringWithFormat:@"%lu",strtoul([subString UTF8String],0,16)].intValue;
            
            result += intResult;
        }
        
        NSString *hexSum = [NSString stringWithFormat:@"%x",result];
        if (hexSum.length>=2) {
            hexSum = [hexSum substringFromIndex:hexSum.length-2];
        }else {
            hexSum = [NSString stringWithFormat:@"0%@",hexSum];
        }
        //DDLog(@"校验和：%@\n",hexSum);
        _checksum = hexSum;
    }
    
    return _checksum;
}

// 时间戳
- (NSString *)timestamp{
    int second = [[NSDate date] timeIntervalSince1970];
    NSString *time = [NSString stringWithFormat:@"%d", second];
    time = [time rds_getHexFromDecimal];
    return time;
}

- (NSData *)getCmdData{
    
    NSString *cmd = [NSString stringWithFormat:@"%@%@%@%@%@%@", self.header, self.leng, self.cmd, self.sn, self.content, self.checksum];
    
    NSData *result = [cmd rds_getDataFromHexString];
    
    if (![self.cmd isEqualToString:@"05"]) {
        DDLog(@"发送指令～～:%@",cmd);
    }
    
    
    return result;
}

- (NSString *)getHex:(int)num{
    return [@(num).description rds_getHexFromDecimal];
}
@end




@implementation RDSDeviceReply


@end




