//
|
// EZSDK.m
|
// EZSDK
|
//
|
// Created by 陈嘉乐 on 2021/1/27.
|
//
|
|
#import "EZSDK.h"
|
//#import "DeviceInfo.h"
|
#import "EZDeviceInfo.h"
|
#import "EZOpenSDK.h"
|
#import "GlobalKit.h"
|
#import "EZAccessToken.h"
|
#import "NSArray+DDKit.h"
|
#import "EZAddByQRCodeViewController.h"
|
#import "EZLivePlayViewController.h"
|
#import "EZSettingViewController.h"
|
#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"
|
|
|
#define APP_KEY @"HDL-HOME-APP-TEST"
|
#define SECRET_KEY @"WeJ8TY88vbakCcnvH8G1tDUqzLWY8yss"
|
|
|
@implementation EZSDK
|
|
/**
|
初始化SDK
|
传入国内版key 和海外版globalAppKey
|
*/
|
+ (BOOL)initLibWithAppKey:(NSString *)appKey globalAppKey:(NSString *)globalAppKey
|
{
|
static BOOL isHavelibInit=NO;
|
if (!isHavelibInit) {
|
// isHavelibInit=YES;
|
// NSLog(@"第一次初始化萤石库");
|
if (!LanguageIsChinese) {
|
//海外版
|
isHavelibInit = [EZGlobalSDK initLibWithAppKey:globalAppKey];
|
NSLog(@"海外版key--%@",globalAppKey);
|
}else{
|
isHavelibInit = [EZOpenSDK initLibWithAppKey:appKey];
|
NSLog(@"国内版key--%@", appKey);
|
}
|
|
[EZHCNetDeviceSDK initSDK];
|
[EZOPENSDK enableP2P:YES];
|
[EZOPENSDK setDebugLogEnable:YES];
|
NSLog(@"EZOpenSDK Version = %@", [EZOPENSDK getVersion]);
|
}
|
return isHavelibInit;
|
}
|
|
/**
|
设置SDK的accessToken
|
*/
|
+(void)setEZAccessToken:(NSString *) accessToken
|
{
|
[[GlobalKit shareKit] setAccessToken:accessToken];
|
[EZOPENSDK setAccessToken:accessToken];
|
}
|
|
|
|
//获取当前屏幕显示的viewcontroller (这里面获取的相当于rootViewController)
|
+(UINavigationController *)getCurrentVC
|
{
|
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
|
if (window.windowLevel != UIWindowLevelNormal)
|
{
|
NSArray *windows = [[UIApplication sharedApplication] windows];
|
for(UIWindow * tmpWin in windows)
|
{
|
if (tmpWin.windowLevel == UIWindowLevelNormal)
|
{
|
window = tmpWin;
|
break;
|
}
|
}
|
}
|
|
return (UINavigationController *)window.rootViewController;
|
}
|
|
|
/**
|
直接跳转到萤石摄像头列表
|
*/
|
+(void)go2EZvizMonitor
|
{
|
// NSLog(@"go2EZvizMonitor");
|
//获取EZMain的stroyboard文件
|
UIStoryboard *ezMainStoryboard = [UIStoryboard storyboardWithName:@"EZMain" bundle:nil];
|
//获取EZMain.storyboard的实例ViewController--获取摄像头列表
|
EZDeviceTableViewController *instanceVC = [ezMainStoryboard instantiateViewControllerWithIdentifier:@"EZCameraList"];
|
//push摄像头列表的viewController
|
[[self getCurrentVC] setNavigationBarHidden:NO];
|
// NSLog(@"willpush********");
|
[[self getCurrentVC] pushViewController:instanceVC animated:YES];
|
// NSLog(@"had pushed********");
|
}
|
|
/**
|
跳转添加添加设备
|
*/
|
+(void)addEzvizMonitor
|
{
|
// NSLog(@"添加摄像头");
|
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
|
UIStoryboard *addDeviceStoryBoard = [UIStoryboard storyboardWithName:@"AddDevice" bundle:nil];
|
EZAddByQRCodeViewController *rootViewController = [addDeviceStoryBoard instantiateViewControllerWithIdentifier:@"AddByQRCode"];
|
[[self getCurrentVC] setNavigationBarHidden:NO];
|
[[self getCurrentVC] pushViewController:rootViewController animated:YES];
|
} else {
|
// [UIView dd_showMessage:@"iOS 7.0以下扫码功能请自行实现"];
|
NSLog(@"iOS 7.0以下扫码功能请自行实现");
|
}
|
|
}
|
|
/**
|
查看视频监控直播
|
*/
|
+(void)Play:(NSObject*)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;
|
[[self getCurrentVC] setNavigationBarHidden:NO];
|
[[self getCurrentVC] pushViewController:rootViewController animated:YES];
|
}
|
|
/**
|
打开摄像头设置页面
|
*/
|
+(void)setting:(NSObject*)deviceInfo
|
{
|
UIStoryboard *settingStoryBoard=[UIStoryboard storyboardWithName:@"EZMain" bundle:nil];
|
EZSettingViewController *settingVC=[settingStoryBoard instantiateViewControllerWithIdentifier:@"EZSettingViewController"];
|
settingVC.deviceInfo=(EZDeviceInfo *)deviceInfo;
|
[[self getCurrentVC] setNavigationBarHidden:NO];
|
[[self getCurrentVC] pushViewController:settingVC animated:YES];
|
}
|
|
/**
|
回放播放历史
|
*/
|
+(void)playBackVideo:(NSObject*)deviceInfo
|
{
|
UIStoryboard *playBackStoryBoard=[UIStoryboard storyboardWithName:@"EZMain" bundle:nil];
|
EZPlaybackViewController *playBackVC=[playBackStoryBoard instantiateViewControllerWithIdentifier:@"EZPlaybackViewController"];
|
playBackVC.deviceInfo=(EZDeviceInfo *)deviceInfo;
|
[[self getCurrentVC] setNavigationBarHidden:NO];
|
[[self getCurrentVC] pushViewController:playBackVC animated:YES];
|
}
|
|
|
//+(void)MonitorPushMessage:(NSString *)MonitorType ID:(NSString *)ID
|
//{
|
// [CommonList login];
|
// [CommonList getlist:0 pageSize:20 listArray:^(NSArray *listArray) {
|
// for ( NSObject *info in listArray) {
|
// DeviceInfo *ezInfo=(DeviceInfo *)info;
|
// if ([ID isEqualToString:ezInfo.DeviceSerial]) {
|
// NSLog(@"相同id");
|
// [CommonList Play:ezInfo.RealObject];
|
// }
|
// }
|
// }];
|
//}
|
|
//+(void)getlist:(int)pageIndex pageSize:(int)pageSize listArray:(void(^)(NSArray *))listArray
|
//{
|
//
|
// static NSMutableArray *deviceListaArray;
|
// deviceListaArray=nil;
|
// if ([GlobalKit shareKit].accessToken)
|
// {
|
// [EZOPENSDK setAccessToken:[GlobalKit shareKit].accessToken];
|
// //获取列表
|
// [EZOpenSDK getDeviceList:0 pageSize:20 completion:^(NSArray *deviceList, NSInteger totalCount, NSError *error) {
|
// deviceListaArray=[NSMutableArray array];
|
// for (int i=0; i<deviceList.count; i++) {
|
// DeviceInfo *deviceInfos=[[DeviceInfo alloc]init];
|
// EZDeviceInfo *ezDeviceInfo=[deviceList dd_objectAtIndex:i];
|
// deviceInfos.DeviceName=ezDeviceInfo.deviceName;
|
// deviceInfos.DeviceSerial=ezDeviceInfo.deviceSerial;
|
// deviceInfos.DeviceNum=(int)ezDeviceInfo.cameraNum;
|
// deviceInfos.DeviceCameraInfoListSize=1;
|
// deviceInfos.cameraInfo=ezDeviceInfo.cameraInfo;
|
// deviceInfos.RealObject=ezDeviceInfo;
|
// [deviceListaArray addObject:deviceInfos];
|
// }
|
// //block回调
|
// listArray(deviceListaArray);
|
// }];
|
// }
|
//
|
//}
|
|
//+(void)CameraList
|
//{
|
// NSLog(@"收到完成通知回调");
|
// [CommonList go2EZvizMonitor];
|
//}
|
|
//+(NSMutableArray<MonitorData *> *)getMonitorDatas
|
//{
|
// static NSMutableArray<MonitorData *> *monitorDataMutableArr;
|
// if (monitorDataMutableArr==nil) {
|
// monitorDataMutableArr=[[NSMutableArray alloc]init];
|
// }
|
// return monitorDataMutableArr;
|
//}
|
//
|
//+(ICommon*)EZICommon{
|
// static ICommon *ICommons;
|
// if(ICommons==nil)
|
// {
|
// ICommons= [[ICommon alloc]init];
|
//
|
// }
|
// return ICommons;
|
//}
|
|
|
//+(NSArray *)getDeviceList:(int)pageIndex :(int)pageSize
|
//{
|
// static NSMutableArray *deviceListaArray;
|
// deviceListaArray=nil;
|
// //判断本地保存的accessToken,然后向SDK设置AccessToken。
|
// if ([GlobalKit shareKit].accessToken)
|
// {
|
// [EZOPENSDK setAccessToken:[GlobalKit shareKit].accessToken];
|
// //获取列表
|
// [EZOpenSDK getDeviceList:0 pageSize:20 completion:^(NSArray *deviceList, NSInteger totalCount, NSError *error) {
|
// deviceListaArray=[NSMutableArray array];
|
// //怎么将 这里block得到的数据return回去
|
// NSLog(@"原生deviceList.count****%ld",(unsigned long)deviceList.count);
|
// for (int i=0; i<deviceList.count; i++) {
|
// DeviceInfo *deviceInfos=[[DeviceInfo alloc]init];
|
// EZDeviceInfo *ezDeviceInfo=[deviceList dd_objectAtIndex:i];
|
// deviceInfos.DeviceName=ezDeviceInfo.deviceName;
|
// deviceInfos.DeviceSerial=ezDeviceInfo.deviceSerial;
|
// deviceInfos.DeviceNum=(int)ezDeviceInfo.cameraNum;
|
// deviceInfos.DeviceCameraInfoListSize=1;
|
// deviceInfos.cameraInfo=ezDeviceInfo.cameraInfo;
|
// deviceInfos.RealObject=ezDeviceInfo;
|
// [deviceListaArray addObject:deviceInfos];
|
// }
|
// }];
|
// }
|
// else
|
// {
|
// return deviceListaArray=[NSMutableArray array];
|
// }
|
// while (deviceListaArray==nil) {
|
// [NSThread sleepForTimeInterval:1];
|
// }
|
// return deviceListaArray ;
|
//}
|
|
//+(void)login
|
//{
|
// if ([GlobalKit shareKit].accessToken)
|
// {
|
// [EZOPENSDK setAccessToken:[GlobalKit shareKit].accessToken];
|
// NSLog(@"login---1");
|
// }
|
// else
|
// {
|
// [EZOPENSDK openLoginPage:^(EZAccessToken *accessToken) {
|
// [[GlobalKit shareKit] setAccessToken:accessToken.accessToken];
|
// [EZOPENSDK setAccessToken:accessToken.accessToken];
|
// NSLog(@"login---2");
|
// }];
|
// }
|
//}
|
|
#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
|