RDSDeviceCmd.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // RDSDeviceCmd.m
  3. // RuiZhi
  4. //
  5. // Created by Rayson on 2020/6/12.
  6. // Copyright © 2020 RDIOT. All rights reserved.
  7. //
  8. #import "RDSDeviceCmd.h"
  9. #import "NSDate+currentDate.h"
  10. #import "NSString+convert.h"
  11. #import "NSString+safe.h"
  12. #define kModel [[RDSDeviceCmd alloc] init]
  13. @implementation RDSDeviceCmd
  14. #pragma mark - 解析指令
  15. + (void)rds_receiveData:(NSData *)data finish:(RDSCmdReceiveCmdFinish)finish{
  16. NSString *string = [NSString rds_getStringFromData:data];
  17. DDLog(@"收到信息 %@",string);
  18. NSDictionary *dic = [NSString rds_jsonToDic:string];
  19. if(dic == nil) return;
  20. RDSDeviceReply *reply = [[RDSDeviceReply alloc] init];
  21. reply.bindStep = [dic[@"s"] integerValue];
  22. reply.code = [dic[@"c"] intValue];
  23. reply.deviceCode = [dic objectForKey:@"id"];
  24. reply.msg = string;
  25. finish(reply);
  26. [kNotificationCenter postNotificationName:RDSDeviceCmdContentParserNotice object:reply];
  27. }
  28. - (void)p_parseCmd:(NSString *)cmd {
  29. }
  30. #pragma mark - Get 方法
  31. - (NSString *)header{
  32. _header = @"aa";
  33. return _header;
  34. }
  35. - (NSString *)leng{
  36. if (_leng == nil) {
  37. NSString *lengString = self.content;
  38. int result = (int)lengString.length/2 + 3;
  39. _leng = [@(result).description rds_getHexFromDecimal];
  40. }
  41. return _leng;
  42. }
  43. - (NSString *)sn{
  44. if (_sn == nil) {
  45. // TheDataManager.msgSN ++;
  46. // int num = TheDataManager.msgSN;
  47. // _sn = [@(num).description rds_getHexFromDecimal];
  48. }
  49. return _sn;
  50. }
  51. - (NSString *)content{
  52. if (_content == nil) {
  53. _content = @"";
  54. }
  55. return _content;;
  56. }
  57. - (NSString *)checksum{
  58. if (_checksum == nil) {
  59. NSString *checkString = [NSString stringWithFormat:@"%@%@%@%@%@", self.header, self.leng, self.cmd, self.sn, self.content];
  60. if (checkString.length % 2 != 0) return @"00";
  61. int result = 0;
  62. for (int i = 0; i + 2 <= checkString.length; i += 2) {
  63. NSString *subString = [checkString rds_getSubStringSafetyWithRange:NSMakeRange(i, 2)];
  64. int intResult = [NSString stringWithFormat:@"%lu",strtoul([subString UTF8String],0,16)].intValue;
  65. result += intResult;
  66. }
  67. NSString *hexSum = [NSString stringWithFormat:@"%x",result];
  68. if (hexSum.length>=2) {
  69. hexSum = [hexSum substringFromIndex:hexSum.length-2];
  70. }else {
  71. hexSum = [NSString stringWithFormat:@"0%@",hexSum];
  72. }
  73. //DDLog(@"校验和:%@\n",hexSum);
  74. _checksum = hexSum;
  75. }
  76. return _checksum;
  77. }
  78. // 时间戳
  79. - (NSString *)timestamp{
  80. int second = [[NSDate date] timeIntervalSince1970];
  81. NSString *time = [NSString stringWithFormat:@"%d", second];
  82. time = [time rds_getHexFromDecimal];
  83. return time;
  84. }
  85. - (NSData *)getCmdData{
  86. NSString *cmd = [NSString stringWithFormat:@"%@%@%@%@%@%@", self.header, self.leng, self.cmd, self.sn, self.content, self.checksum];
  87. NSData *result = [cmd rds_getDataFromHexString];
  88. if (![self.cmd isEqualToString:@"05"]) {
  89. DDLog(@"发送指令~~:%@",cmd);
  90. }
  91. return result;
  92. }
  93. - (NSString *)getHex:(int)num{
  94. return [@(num).description rds_getHexFromDecimal];
  95. }
  96. @end
  97. @implementation RDSDeviceReply
  98. @end