RDSDeviceCmd.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.msg = string;
  24. finish(reply);
  25. [kNotificationCenter postNotificationName:RDSDeviceCmdContentParserNotice object:reply];
  26. }
  27. - (void)p_parseCmd:(NSString *)cmd {
  28. }
  29. #pragma mark - Get 方法
  30. - (NSString *)header{
  31. _header = @"aa";
  32. return _header;
  33. }
  34. - (NSString *)leng{
  35. if (_leng == nil) {
  36. NSString *lengString = self.content;
  37. int result = (int)lengString.length/2 + 3;
  38. _leng = [@(result).description rds_getHexFromDecimal];
  39. }
  40. return _leng;
  41. }
  42. - (NSString *)sn{
  43. if (_sn == nil) {
  44. // TheDataManager.msgSN ++;
  45. // int num = TheDataManager.msgSN;
  46. // _sn = [@(num).description rds_getHexFromDecimal];
  47. }
  48. return _sn;
  49. }
  50. - (NSString *)content{
  51. if (_content == nil) {
  52. _content = @"";
  53. }
  54. return _content;;
  55. }
  56. - (NSString *)checksum{
  57. if (_checksum == nil) {
  58. NSString *checkString = [NSString stringWithFormat:@"%@%@%@%@%@", self.header, self.leng, self.cmd, self.sn, self.content];
  59. if (checkString.length % 2 != 0) return @"00";
  60. int result = 0;
  61. for (int i = 0; i + 2 <= checkString.length; i += 2) {
  62. NSString *subString = [checkString rds_getSubStringSafetyWithRange:NSMakeRange(i, 2)];
  63. int intResult = [NSString stringWithFormat:@"%lu",strtoul([subString UTF8String],0,16)].intValue;
  64. result += intResult;
  65. }
  66. NSString *hexSum = [NSString stringWithFormat:@"%x",result];
  67. if (hexSum.length>=2) {
  68. hexSum = [hexSum substringFromIndex:hexSum.length-2];
  69. }else {
  70. hexSum = [NSString stringWithFormat:@"0%@",hexSum];
  71. }
  72. //DDLog(@"校验和:%@\n",hexSum);
  73. _checksum = hexSum;
  74. }
  75. return _checksum;
  76. }
  77. // 时间戳
  78. - (NSString *)timestamp{
  79. int second = [[NSDate date] timeIntervalSince1970];
  80. NSString *time = [NSString stringWithFormat:@"%d", second];
  81. time = [time rds_getHexFromDecimal];
  82. return time;
  83. }
  84. - (NSData *)getCmdData{
  85. NSString *cmd = [NSString stringWithFormat:@"%@%@%@%@%@%@", self.header, self.leng, self.cmd, self.sn, self.content, self.checksum];
  86. NSData *result = [cmd rds_getDataFromHexString];
  87. if (![self.cmd isEqualToString:@"05"]) {
  88. DDLog(@"发送指令~~:%@",cmd);
  89. }
  90. return result;
  91. }
  92. - (NSString *)getHex:(int)num{
  93. return [@(num).description rds_getHexFromDecimal];
  94. }
  95. @end
  96. @implementation RDSDeviceReply
  97. @end