萤石云 iOSSDK,移植跨平台相关工程
Davin
2024-12-18 b4e1288a9b63eb820e9c9489c56aac4bf6b31067
Demo/EZOpensdk_iOS_4.15.1_build20201104/Demo/EZOpenSDKDemo/Global/EZSDK.m
@@ -18,27 +18,34 @@
#import "EZPlaybackViewController.h"
#import "EZDeviceTableViewController.h"
#import "EZHCNetDeviceSDK.h"
#import "AFNetworking.h"
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#define API_POST_EZ_AddDevice @"/home-wisdom/platform/child/device/add"
#define API_POST_EZ_GetChildToken @"/home-wisdom/platform/childToken"
#import "EZMessagePhotoViewController.h"
#define APP_KEY @"HDL-HOME-APP-TEST"
#define SECRET_KEY @"WeJ8TY88vbakCcnvH8G1tDUqzLWY8yss"
@implementation EZSDK
///
+ (instancetype)sharedInstance{
    static EZSDK *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!instance) {
            instance = [[EZSDK alloc] init];
        }
    });
    return instance;
}
static BOOL isHavelibInit=NO;
/**
 初始化SDK
 传入国内版key 和海外版globalAppKey
 */
+ (BOOL)initLibWithAppKey:(NSString *)appKey globalAppKey:(NSString *)globalAppKey
- (BOOL)initLibWithAppKey:(NSString *)appKey globalAppKey:(NSString *)globalAppKey
{
    static BOOL isHavelibInit=NO;
    if (!isHavelibInit) {
        //        isHavelibInit=YES;
        //        NSLog(@"第一次初始化萤石库");
@@ -62,16 +69,31 @@
/**
 设置SDK的accessToken
 */
+(void)setEZAccessToken:(NSString *) accessToken
-(void)setEZAccessToken:(NSString *) accessToken
{
    [[GlobalKit shareKit] setAccessToken:accessToken];
    [EZOPENSDK setAccessToken:accessToken];
}
/**
 设置HDLSDK的accessToken
 */
-(void)setHDlAccessToken:(NSString *) accessToken refreshToken:(NSString *) refreshToken
{
    [[GlobalKit shareKit] setHdlAccessToken:accessToken];
    [[GlobalKit shareKit] setHdlRefreshToken:refreshToken];
}
/**
 设置SDK的河东接口的requestHttpsHost和平台 标识,1.on+(默认) 2.evoyo
 */
-(void)setRequestHttpsHostAndPlatform:(NSString *) requestHttpsHost platform:(int)platform homeId:(NSString *)homeId{
    [[GlobalKit shareKit] setGlobalRequestHttpsHost:requestHttpsHost];
    [[GlobalKit shareKit] setHdlPlatform:platform];
    [[GlobalKit shareKit] setHdlHomeId:homeId];
}
//获取当前屏幕显示的viewcontroller   (这里面获取的相当于rootViewController)
+(UINavigationController *)getCurrentVC
-(UINavigationController *)getCurrentVC
{
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
@@ -94,7 +116,7 @@
/**
 直接跳转到萤石摄像头列表
 */
+(void)go2EZvizMonitor
-(void)go2EZvizMonitor
{
    //    NSLog(@"go2EZvizMonitor");
    //获取EZMain的stroyboard文件
@@ -111,7 +133,7 @@
/**
 跳转添加添加设备
 */
+(void)addEzvizMonitor
-(void)addEzvizMonitor
{
    //    NSLog(@"添加摄像头");
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
@@ -129,22 +151,40 @@
/**
 查看视频监控直播
 */
+(void)Play:(NSObject*)deviceInfo
-(void)Play:(EZDeviceInfo*)deviceInfo
{
    UIStoryboard *addDeviceStoryBoard = [UIStoryboard storyboardWithName:@"EZMain" bundle:nil];
    EZLivePlayViewController *rootViewController = [addDeviceStoryBoard instantiateViewControllerWithIdentifier:@"EZLivePlayViewController"];
    //        NSLog(@"play--iphone--rootViewController");
    rootViewController.deviceInfo=(EZDeviceInfo *)deviceInfo;
    NSLog(@"play--iphone--deviceName -%@",rootViewController.deviceInfo.deviceName);
    //        rootViewController.cameraIndex=0;
//            rootViewController.cameraIndex=0;
    [[self getCurrentVC] setNavigationBarHidden:NO];
    [[self getCurrentVC] pushViewController:rootViewController animated:YES];
}
/// 指定序列号播放
/// @param deviceSerial 序列号
-(void)PlayWithDeviceSerial:(NSString *)deviceSerial{
    //获取设备列表接口
    [EZOpenSDK getDeviceInfo:deviceSerial completion:^(EZDeviceInfo *deviceInfo, NSError *error) {
        if(error)
        {
            NSLog(@"EZ 查询设备信息失败");
            return;
        }
        if (deviceInfo) {
            [self Play:deviceInfo];
        }
    }];
}
/**
 打开摄像头设置页面
 */
+(void)setting:(NSObject*)deviceInfo
-(void)setting:(EZDeviceInfo*)deviceInfo
{
    UIStoryboard *settingStoryBoard=[UIStoryboard storyboardWithName:@"EZMain" bundle:nil];
    EZSettingViewController *settingVC=[settingStoryBoard instantiateViewControllerWithIdentifier:@"EZSettingViewController"];
@@ -156,7 +196,7 @@
/**
 回放播放历史
 */
+(void)playBackVideo:(NSObject*)deviceInfo
-(void)playBackVideo:(EZDeviceInfo*)deviceInfo
{
    UIStoryboard *playBackStoryBoard=[UIStoryboard storyboardWithName:@"EZMain" bundle:nil];
    EZPlaybackViewController *playBackVC=[playBackStoryBoard instantiateViewControllerWithIdentifier:@"EZPlaybackViewController"];
@@ -165,6 +205,17 @@
    [[self getCurrentVC] pushViewController:playBackVC animated:YES];
}
////
//-(void)goToEZMessagePhotoVC:(EZAlarmInfo*)alarmInfo{
//    UIStoryboard *addDeviceStoryBoard = [UIStoryboard storyboardWithName:@"EZMain" bundle:nil];
//    EZMessagePhotoViewController *rootViewController = [addDeviceStoryBoard instantiateViewControllerWithIdentifier:@"EZMessagePhotoViewController"];
//    //        NSLog(@"play--iphone--rootViewController");
//    rootViewController.info = alarmInfo;
////    NSLog(@"play--iphone--deviceName -%@",rootViewController.deviceInfo.deviceName);
////            rootViewController.cameraIndex=0;
//    [[self getCurrentVC] setNavigationBarHidden:NO];
//    [[self getCurrentVC] pushViewController:rootViewController animated:YES];
//}
//+(void)MonitorPushMessage:(NSString *)MonitorType ID:(NSString *)ID
//{
@@ -288,175 +339,6 @@
//    }
//}
#pragma mark -接口请求部分
/**
 *  @since 河东获取子账号token的接口
 *
 *  @param block 回调block
 */
+ (NSURLSessionDataTask *)getChildToken:(void (^)(NSString *accessToken))block{
    //1,创建你得请求url
    NSString *URL = @"";
    if([GlobalKit shareKit].GlobalRequestHttpsHost == NULL){
        URL = @"https://test-gz.hdlcontrol.com";
    }
    URL = [NSString stringWithFormat:@"%@%@", URL, API_POST_EZ_GetChildToken];
    //2.设置请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters = [self GetSignRequestDictionary:parameters];
    NSURLSessionDataTask*task=nil;
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer = responseSerializer;
    NSMutableURLRequest * request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"post" URLString:URL parameters:parameters error:nil];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:GlobalKit.shareKit.hdlAccessToken forHTTPHeaderField:@"Authorization"];
    [[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        //4.解析拿到的响应数据
        NSLog(@"%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);
        if (block) {
            NSString * token = @"";
            block (token);
        }
    }]resume];
    return task;
}
/**
 *  @since 河东添加设备的接口
 *  根据设备序列号和设备验证码添加设备接口
 *
 *  @param deviceSerial 设备序列号
 *  @param verifyCode   设备验证码
 *  @param completion   回调block,error为空时表示添加成功
 *
 *  @return operation
 */
+ (NSURLSessionDataTask *)addDeviceByHDL:(NSString *)deviceSerial
                              verifyCode:(NSString *)verifyCode
                              completion:(void (^)(NSError *error))completion{
    //1,创建你得请求url
    NSString *URL = @"";
    if([GlobalKit shareKit].GlobalRequestHttpsHost == NULL){
        URL = @"https://test-gz.hdlcontrol.com";
    }
    URL = [NSString stringWithFormat:@"%@%@",URL, @"/smart-footstone/region/regionByAccount"];
    //2.设置请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    //    [parameters setValue:deviceSerial forKey:@"deviceSerial"];
    //    [parameters setValue:verifyCode forKey:@"verifyCode"];
    //    parameters[@"deviceSerial"] = deviceSerial;
    //    parameters[@"verifyCode"] = verifyCode;
    parameters[@"account"] = @"18824864143";
//    parameters = [self GetSignRequestDictionary:parameters];
    NSURLSessionDataTask*task=nil;
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer = responseSerializer;
    NSMutableURLRequest * request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"post" URLString:URL parameters:parameters error:nil];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        //            //拿到响应头信息
        //            NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
        //4.解析拿到的响应数据
        NSLog(@"%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);
        if (completion) {
//            error.code = 105002;
            completion (error);
        }
    }]resume];
    return task;
}
#pragma mark HttpUtil
/**
 *  基础服务的接口都要校验sign
 */
+(NSMutableDictionary *)GetSignRequestDictionary:(NSMutableDictionary *)params{
    if(params == NULL){
        params =[NSMutableDictionary dictionary];
    }
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
    [params setValue:APP_KEY forKey:@"appKey"];
    [params setValue:[NSString stringWithFormat:@"%llu",recordTime] forKey:@"timestamp"];
    //1.对KEY升序
    NSArray *keyArray = [params allKeys];
    NSArray *sortKeyArray = [keyArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    //2.2 拼接按URL键值对
    NSString *newString = @"";
    for(NSString *key in sortKeyArray){
        if(params[key] != NULL){
            NSString *valueStr = params[key];
            //检测当前参数是否需要参与校验
            if([self IfValueNeedSign:valueStr]){
                newString = [newString stringByAppendingString:[NSString stringWithFormat:@"%@=%@&", key,valueStr]];
            }
        }
    }
    //2.3 拼接SECRET_KEY
    newString = [newString substringToIndex:[newString length] - 1];
    newString = [newString stringByAppendingString: SECRET_KEY];
    //2.4 MD5转换+转小写
    if(newString == nil || newString == NULL){
        newString = @"";
    }
    NSString* signstr = [self signMD5Encrypt:newString];
    [params setValue:signstr forKey:@"sign"];
    return params;
}
/**
 MD5转换+转小写
 */
+ (NSString*)signMD5Encrypt:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr),digest );
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    [result appendFormat:@"%02x", digest[i]];
    return result;
}
/// <summary>
/// 判断当前值是否需要参与签名,保持跟云端一致
/// 空字符串不参与
/// 数组,集合,对象不参与
/// </summary>
/// <param name="valueStr"></param>
/// <returns></returns>
+(bool)IfValueNeedSign:(NSString *)valueStr{
    if (( (valueStr == nil || [valueStr isKindOfClass:[NSNull class]] || valueStr.length == 0))//判空字符
        || ([[valueStr substringToIndex:1] isEqual:@"{"])//判断是否为对象
        || ([[valueStr substringToIndex:1] isEqual:@"["])//判断是否为数组
        ) {
        return false;
    }
    return true;
}
@end