Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Battery Level for WoIOSensor #383

Open
atoxx opened this issue Dec 27, 2024 · 1 comment
Open

Wrong Battery Level for WoIOSensor #383

atoxx opened this issue Dec 27, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@atoxx
Copy link

atoxx commented Dec 27, 2024

Analysis

The API for the WoIOSensor Device returns a 60% since I bought it, looking into Switchbot iOS App I says it is at 94% a value that I never saw in the API responses

Expected Behavior

Should return same vaklue as the App

Steps To Reproduce

use this code - (NSDictionary *)getDeviceStatusForDeviceID:(NSString *)deviceID error:(NSError **)error {
// Generate timestamp and nonce
long long timestamp = (long long)([[NSDate date] timeIntervalSince1970] * 1000);
NSString *nonce = [[NSUUID UUID] UUIDString];

// Generate signature
NSString *timeAdjustedToken = [NSString stringWithFormat:@"%@%lld%@", token, timestamp, nonce];
NSData *keyData = [secret dataUsingEncoding:NSUTF8StringEncoding];
NSData *dataToSign = [timeAdjustedToken dataUsingEncoding:NSUTF8StringEncoding];

uint8_t hmac[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, keyData.bytes, keyData.length, dataToSign.bytes, dataToSign.length, hmac);

NSData *hmacData = [NSData dataWithBytes:hmac length:sizeof(hmac)];
NSString *signature = [hmacData base64EncodedStringWithOptions:0];

// Prepare URL
NSString *urlString = [NSString stringWithFormat:@"https://api.switch-bot.com/v1.1/devices/%@/status", deviceID];
NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"GET";

// Set headers
[request setValue:@"application/json;charset=utf8" forHTTPHeaderField:@"Content-Type"];
[request setValue:token forHTTPHeaderField:@"Authorization"];
[request setValue:[NSString stringWithFormat:@"%lld", timestamp] forHTTPHeaderField:@"t"];
[request setValue:nonce forHTTPHeaderField:@"nonce"];
[request setValue:signature forHTTPHeaderField:@"sign"];

// Make request
__block NSDictionary *result = nil;
__block NSError *blockError = nil; // Local variable to capture errors
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request
            completionHandler:^(NSData *data, NSURLResponse *response, NSError *taskError) {
    if (taskError) {
        blockError = taskError;
        dispatch_semaphore_signal(semaphore);
        return;
    }
    
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSInteger httpStatus = httpResponse.statusCode;
    
    if (httpStatus != 200) {
        blockError = [NSError errorWithDomain:@"DeviceStatusErrorDomain"
                                         code:httpStatus
                                     userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"HTTP error with status code %ld", (long)httpStatus]}];
        dispatch_semaphore_signal(semaphore);
        return;
    }
    
    NSDictionary *apiResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&blockError];
    if (!apiResponse) {
        dispatch_semaphore_signal(semaphore);
        return;
    }
    
    NSInteger apiStatus = [apiResponse[@"statusCode"] integerValue];
    if (apiStatus != 100) {
        blockError = [NSError errorWithDomain:@"DeviceStatusErrorDomain"
                                         code:apiStatus
                                     userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"API error with status code %ld", (long)apiStatus]}];
        dispatch_semaphore_signal(semaphore);
        return;
    }
    
    NSDictionary *body = apiResponse[@"body"];
    NSNumber *temperature = body[@"temperature"];
    NSNumber *humidity = body[@"humidity"];
    NSNumber *battery = body[@"battery"];
    
    result = @{
        @"deviceID": deviceID,
        @"temperature": temperature ?: [NSNull null],
        @"humidity": humidity ?: [NSNull null],
        @"battery": battery ?: [NSNull null],
        @"http_status": @(httpStatus),
        @"api_status": @(apiStatus),
        @"api_response": apiResponse,
    };
    
    dispatch_semaphore_signal(semaphore);
}] resume];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

if (blockError && error) {
    *error = blockError; // Assign the local error to the out parameter
}
return result;

} to get the Status

Logs

no Logs, where to get any???

Configuration

Any iOS or MacOSDevice

Environment

  • OS: any iOS or MacOS
  • Software: Newest
  • Node:
  • npm:

Additional Context

No response

@hsakoh
Copy link

hsakoh commented Dec 27, 2024

same issue #354 #304 #234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants