iOS AILinkBleSDK - 蓝牙SDK
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ELMeatProbeBoxBleParser.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // ELMeatProbeBoxBleParser.m
  3. // AILinkBleSDK
  4. //
  5. // Created by LarryZhang on 2023/2/16.
  6. //
  7. #import "ELMeatProbeBoxBleParser.h"
  8. #import "ELMeatProbeBoxBleHeader.h"
  9. #import <objc/runtime.h>
  10. @interface ELMeatProbeBoxBleParser ()
  11. @property (nonatomic, strong) ELMeatProbeBoxBleBoxStatusModel *boxStatusModel;
  12. @property (nonatomic, strong) ELMeatProbeBoxBleProbeStatusModel *probeStatusModel;
  13. @end
  14. @implementation ELMeatProbeBoxBleParser
  15. - (instancetype)init {
  16. if (self = [super init]) {
  17. _boxStatusModel = [[ELMeatProbeBoxBleBoxStatusModel alloc] init];
  18. _probeStatusModel = [[ELMeatProbeBoxBleProbeStatusModel alloc] init];
  19. }
  20. return self;
  21. }
  22. #pragma mark - 解析包
  23. - (void)parseA7Payload:(NSData *)payloadData {
  24. UInt8 *bytes = (Byte *)payloadData.bytes;
  25. // UInt8 len = payloadData.length;
  26. UInt8 cmd = bytes[0];
  27. switch (cmd) {
  28. case ELMeatProbeBoxCMDMcuUploadStatus_Type02:
  29. [self handelMcuUploadStatus:payloadData];
  30. break;
  31. case ELMeatProbeBoxCMDCustomData_Type03:
  32. [self handelCustomData:payloadData];
  33. break;
  34. case ELMeatProbeBoxCMDAppSwitchUnit_Type04:
  35. [self handelSwitchUnit:payloadData];
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. - (void)handelCustomData:(NSData *)payloadData {
  42. UInt8 *bytes = (Byte *)payloadData.bytes;
  43. UInt8 len = payloadData.length;
  44. BOOL state = bytes[1];
  45. NSData *mac = [payloadData subdataWithRange:NSMakeRange(2, 6)];
  46. Byte *bytesMac = (Byte *)mac.bytes;
  47. NSString *macString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", bytesMac[5], bytesMac[4], bytesMac[3], bytesMac[2], bytesMac[1], bytesMac[0]];
  48. NSData *customData = [payloadData subdataWithRange:NSMakeRange(8, len - 8)];
  49. if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxBleParser:state:mac:customData:)]) {
  50. [self.delegate meatProbeBoxBleParser:self state:state mac:macString customData:customData];
  51. }
  52. }
  53. - (void)handelSwitchUnit:(NSData *)payloadData {
  54. UInt8 *bytes = (Byte *)payloadData.bytes;
  55. //UInt8 len = payloadData.length;
  56. ELDeviceTemperatureUnit unit = bytes[1];
  57. if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxBleParser:unit:)]) {
  58. [self.delegate meatProbeBoxBleParser:self unit:unit];
  59. }
  60. }
  61. - (void)handelMcuUploadStatus:(NSData *)payloadData {
  62. UInt8 *bytes = (Byte *)payloadData.bytes;
  63. UInt8 len = payloadData.length;
  64. _boxStatusModel.version = bytes[1];
  65. UInt8 probeMaxCount = bytes[2];
  66. if (probeMaxCount < 1) {
  67. return;//探针数排错
  68. }
  69. _boxStatusModel.probeMaxCount = probeMaxCount;
  70. UInt8 probeCount = bytes[3];
  71. if (probeCount > probeMaxCount) {
  72. return;//探针数排错
  73. }
  74. if (len < 1 + 8 + 16 * probeCount) {
  75. return;//数据长度排错
  76. }
  77. _boxStatusModel.probeCount = probeCount;
  78. _boxStatusModel.batteryStatus = bytes[4] >> 7;
  79. _boxStatusModel.batteryPercent = bytes[4] & 0x7F;
  80. _boxStatusModel.unit = bytes[5];
  81. UInt8 reserved = bytes[6];
  82. reserved = bytes[7];
  83. reserved = bytes[8];
  84. double timestamp = [[NSDate date] timeIntervalSince1970];
  85. _boxStatusModel.timestamp = timestamp;
  86. // for (int i=0, j=9+16*i; i<probeCount; i++) {
  87. for (int i=0, j=9+16*i; i<probeMaxCount; i++) {
  88. _probeStatusModel.index = bytes[j+0];
  89. _probeStatusModel.dataIndex = i;
  90. _probeStatusModel.macData = [payloadData subdataWithRange:NSMakeRange(j+1, 6)];
  91. _probeStatusModel.macString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", bytes[j+6], bytes[j+5], bytes[j+4], bytes[j+3], bytes[j+2], bytes[j+1]];
  92. if ([_probeStatusModel.macString isEqualToString:@"00:00:00:00:00:00"] || [_probeStatusModel.macString isEqualToString:@"FF:FF:FF:FF:FF:FF"]) {
  93. continue;
  94. }
  95. if (i >= probeCount) {
  96. [_probeStatusModel makeInvalid];
  97. _probeStatusModel.timestamp = timestamp;
  98. if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxBleParser:boxStatus:probeStatus:offline:)]) {
  99. [self.delegate meatProbeBoxBleParser:self boxStatus:_boxStatusModel probeStatus:_probeStatusModel offline:YES];
  100. }
  101. continue;
  102. }
  103. UInt16 temp = (bytes[j+7] << 8) | bytes[j+8];
  104. _probeStatusModel.internalTemperatureRaw = temp;
  105. _probeStatusModel.internalTemperatureUnit = (temp & 0x8000) ? ELDeviceTemperatureUnit_F : ELDeviceTemperatureUnit_C;
  106. _probeStatusModel.internalTemperature = (temp & 0x4000) ? -(temp & 0x3FFF) : (temp & 0x3FFF);
  107. temp = (bytes[j+9] << 8) | bytes[j+10];
  108. _probeStatusModel.ambientTemperatureRaw = temp;
  109. _probeStatusModel.ambientTemperatureUnit = (temp & 0x8000) ? ELDeviceTemperatureUnit_F : ELDeviceTemperatureUnit_C;
  110. _probeStatusModel.ambientTemperature = (temp & 0x4000) ? -(temp & 0x3FFF) : (temp & 0x3FFF);
  111. #if DEBUG
  112. // _probeStatusModel.ambientTemperature += 40;
  113. #endif
  114. UInt8 battery = bytes[j+11];
  115. _probeStatusModel.batteryRaw = battery;
  116. _probeStatusModel.batteryStatus = battery >> 7;
  117. _probeStatusModel.batteryPercent = battery & 0x7F;
  118. _probeStatusModel.insertStatusRaw = bytes[j+12];
  119. _probeStatusModel.timestamp = timestamp;
  120. if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxBleParser:boxStatus:probeStatus:offline:)]) {
  121. [self.delegate meatProbeBoxBleParser:self boxStatus:_boxStatusModel probeStatus:_probeStatusModel offline:NO];
  122. }
  123. }
  124. }
  125. @end
  126. @implementation ELMeatProbeBoxBleBoxStatusModel
  127. #pragma mark ---- description
  128. - (NSString *)description {
  129. NSMutableString *desc = [NSStringFromClass([self class]) stringByAppendingString:@": {\n"].mutableCopy;
  130. unsigned int count;
  131. Ivar *ivar = class_copyIvarList([self class], &count);
  132. for (int i = 0 ; i < count ; i++) {
  133. Ivar iv = ivar[i];
  134. const char *name = ivar_getName(iv);
  135. NSString *strName = [NSString stringWithUTF8String:name];
  136. //利用KVC取值
  137. id value = [self valueForKey:strName];
  138. [desc appendString:[NSString stringWithFormat:@" %@: %@,\n", strName, value]];
  139. }
  140. free(ivar);
  141. [desc appendString:@"}"];
  142. return desc;
  143. }
  144. - (NSString *)debugDescription {
  145. return [self description];
  146. }
  147. @end
  148. @implementation ELMeatProbeBoxBleProbeStatusModel
  149. //数据无效
  150. - (void)makeInvalid {
  151. self.internalTemperatureRaw = 0xFFFF;
  152. self.internalTemperatureUnit = 0;
  153. self.internalTemperature = 0;
  154. self.ambientTemperatureRaw = 0xFFFF;
  155. self.ambientTemperatureUnit = 0;
  156. self.ambientTemperature = 0;
  157. self.batteryRaw = 0xFF;
  158. self.batteryStatus = 0;
  159. self.batteryPercent = 0;
  160. self.insertStatusRaw = 0;
  161. }
  162. #pragma mark ---- description
  163. - (NSString *)description {
  164. NSMutableString *desc = [NSStringFromClass([self class]) stringByAppendingString:@": {\n"].mutableCopy;
  165. unsigned int count;
  166. Ivar *ivar = class_copyIvarList([self class], &count);
  167. for (int i = 0 ; i < count ; i++) {
  168. Ivar iv = ivar[i];
  169. const char *name = ivar_getName(iv);
  170. NSString *strName = [NSString stringWithUTF8String:name];
  171. //利用KVC取值
  172. id value = [self valueForKey:strName];
  173. [desc appendString:[NSString stringWithFormat:@" %@: %@,\n", strName, value]];
  174. }
  175. free(ivar);
  176. [desc appendString:@"}"];
  177. return desc;
  178. }
  179. - (NSString *)debugDescription {
  180. return [self description];
  181. }
  182. @end