123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // ELMeatProbeCustomDataModel.m
- // AILinkBleSDK
- //
-
- #import "ELMeatProbeCustomDataModel.h"
- #import <AILinkBleSDK/NSData+AILinkBle.h>
- #import <objc/runtime.h>
-
-
- @implementation ELMeatProbeCustomDataModel
-
- + (Byte)version {
- return 0x02;
- }
-
- + (Byte)modelSize {
- return 128;//sizeof(ELMeatProbeCustomDataStruct);
- }
-
- + (instancetype)modelWithData:(NSData *)data {
- if (data.length != self.modelSize) {
- NSLog(@"数据长度错误 data:%@", data);
- return nil;
- }
-
- ELMeatProbeCustomDataModel *model = [[ELMeatProbeCustomDataModel alloc] initWithData:data];
- if (model.customStruct.ver != self.version) {
- NSLog(@"数据版本错误 data:%@", data);
- return nil;
- }
-
- return model;
- }
-
- - (instancetype)init {
- if (self = [super init]) {
- memset(&_customStruct, 0, sizeof(_customStruct));
- _customStruct.ver = self.class.version;
- }
- return self;
- }
-
- - (instancetype)initWithData:(NSData *)data {
- if (self = [self init]) {
- memcpy(&_customStruct, data.bytes, sizeof(_customStruct));
- }
- return self;
- }
-
- - (NSData *)dataValue {
- Byte bytes[128] = {0x00};
- memcpy(bytes, &_customStruct, sizeof(_customStruct));
- return [NSData dataWithBytes:bytes length:sizeof(bytes)];
- }
-
- - (NSString *)remark {
- char *remarkBytes[64] = {0};
- memcpy(remarkBytes, _customStruct.remarkBytes, sizeof(_customStruct.remarkBytes));
- NSString *str = [NSString stringWithUTF8String:(const char *)remarkBytes];
- return str;
- }
-
- - (NSString *)setRemark:(NSString *)remark {
- NSData *data = [remark dataUsingEncoding:NSUTF8StringEncoding];
- if (data.length > 32) {
- while (data.length > 29) {
- remark = [remark substringToIndex:(remark.length - 1)];
- data = [remark dataUsingEncoding:NSUTF8StringEncoding];
- }
- remark = [remark stringByAppendingString:@"..."];
- data = [remark dataUsingEncoding:NSUTF8StringEncoding];
- }
- Byte *bytes = (Byte *)data.bytes;
- NSUInteger total = data.length > 32 ? 32 : data.length;
- memset(_customStruct.remarkBytes, 0, 32);
- memcpy(_customStruct.remarkBytes, bytes, total);
- return [self remark];
- }
-
- #pragma mark setter getter
-
- - (NSInteger)ver {
- return self.customStruct.ver;
- }
-
- - (NSInteger)cookingId {
- return self.customStruct.cookingId;
- }
-
- - (NSInteger)foodType {
- return self.customStruct.foodType;
- }
-
- - (NSInteger)foodRawness {
- return self.customStruct.foodRawness;
- }
-
- - (double)alarmTemperaturePercent {
- return self.customStruct.alarmTemperaturePercent;
- }
-
- - (NSInteger)alarmTemperature_C {
- return self.customStruct.alarmTemperature_C;
- }
-
- - (NSInteger)alarmTemperature_F {
- return self.customStruct.alarmTemperature_F;
- }
-
- - (void)setAlarmTemperaturePercent:(double)alarmTemperaturePercent {
- _customStruct.alarmTemperaturePercent = alarmTemperaturePercent;
- _customStruct.alarmTemperature_C = _customStruct.targetTemperature_C * alarmTemperaturePercent;
- _customStruct.alarmTemperature_F = _customStruct.targetTemperature_F * alarmTemperaturePercent;
- }
-
- - (ELDeviceTemperatureUnit)currentUnit {
- return self.customStruct.currentUnit;
- }
-
-
- #pragma mark ---- description
-
- - (NSString *)description {
- NSMutableString *desc = [NSStringFromClass([self class]) stringByAppendingString:@": {\n"].mutableCopy;
-
- [desc appendString:[NSString stringWithFormat:@"ver: %@,\n", @(self.customStruct.ver)]];
- [desc appendString:[NSString stringWithFormat:@"cookingId: %@,\n", @(self.customStruct.cookingId)]];
- [desc appendString:[NSString stringWithFormat:@"foodType: %@,\n", @(self.customStruct.foodType)]];
- [desc appendString:[NSString stringWithFormat:@"foodRawness: %@,\n", @(self.customStruct.foodRawness)]];
- [desc appendString:[NSString stringWithFormat:@"targetTemperature_C: %@,\n", @(self.customStruct.targetTemperature_C)]];
- [desc appendString:[NSString stringWithFormat:@"targetTemperature_F: %@,\n", @(self.customStruct.targetTemperature_F)]];
- [desc appendString:[NSString stringWithFormat:@"ambientMinTemperature_C: %@,\n", @(self.customStruct.ambientMinTemperature_C)]];
- [desc appendString:[NSString stringWithFormat:@"ambientMinTemperature_F: %@,\n", @(self.customStruct.ambientMinTemperature_F)]];
- [desc appendString:[NSString stringWithFormat:@"ambientMaxTemperature_C: %@,\n", @(self.customStruct.ambientMaxTemperature_C)]];
- [desc appendString:[NSString stringWithFormat:@"ambientMaxTemperature_F: %@,\n", @(self.customStruct.ambientMaxTemperature_F)]];
- [desc appendString:[NSString stringWithFormat:@"alarmTemperaturePercent: %@,\n", @(self.alarmTemperaturePercent)]];
- [desc appendString:[NSString stringWithFormat:@"timerStart: %@,\n", @(self.customStruct.timerStart)]];
- [desc appendString:[NSString stringWithFormat:@"timerEnd: %@,\n", @(self.customStruct.timerEnd)]];
- [desc appendString:[NSString stringWithFormat:@"alarmTemperature_C: %@,\n", @(self.customStruct.alarmTemperature_C)]];
- [desc appendString:[NSString stringWithFormat:@"alarmTemperature_F: %@,\n", @(self.customStruct.alarmTemperature_F)]];
- [desc appendString:[NSString stringWithFormat:@"currentUnit: %@,\n", @(self.customStruct.currentUnit)]];
- [desc appendString:[NSString stringWithFormat:@"remark: %@,\n", self.remark]];
-
- [desc appendString:@"}"];
- return desc;
- }
-
- - (NSString *)debugDescription {
- return [self description];
- }
-
-
- @end
|