JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
//  ViewController.m
//  LCOpenSDKDemo
//
//  Created by chenjian on 16/7/11.
//  Copyright (c) 2016年 lechange. All rights reserved.
//
 
#import "LCOpenSDK_Prefix.h"
#import "HintViewController.h"
#import "StartViewController.h"
#import "LCOpenSDK_Prefix.h"
#import "DeviceViewController.h"
#import "openApiService.h"
#import "UserModeViewController.h"
 
#define Y_BTN_APPSECRET 40
#define X_BTN_BOUND 35
#define BTN_LENGTH 50
 
@interface StartViewController ()
{
    UIActivityIndicatorView* _progressInd;
}
 
@end
 
@implementation StartViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.m_imgBG setImage:[UIImage leChangeImageNamed:Start_Png]];
    // Do any additional setup after loading the view, typically from a nib.
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.m_btnMan setImage:[UIImage leChangeImageNamed:Admin_Png] forState:UIControlStateNormal];
    
    UISwitch *switchBtn = [[UISwitch alloc] init];
    switchBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 60,[UIScreen mainScreen].bounds.size.height - 40 , 50, 35);
    switchBtn.on = NO;
    [switchBtn addTarget:self action:@selector(switchBtnDidTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:switchBtn];
    
    NSString *curLanguage = NSLocalizedString(LANGUAGE_TXT, nil);
    if ([curLanguage isEqualToString:@"zh"]) {
        [self.m_btnUser setImage:[UIImage leChangeImageNamed:User_Png] forState:UIControlStateNormal];
    }else if ([curLanguage isEqualToString:@"en"]){
        [self.m_btnUser setImage:[UIImage leChangeImageNamed:User_Png] forState:UIControlStateNormal];
        [self.m_btnUser setTitle:@"" forState:UIControlStateNormal];
        //self.m_btnUser.hidden = YES;
        //self.m_btnUser.enabled = NO;
    }
    [self.m_textAppId setDelegate:self];
    [self.m_textAppSecret setDelegate:self];
 
    self.m_textServerInfo.delegate = self;
    _progressInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _progressInd.transform = CGAffineTransformMakeScale(2.0, 2.0);
    _progressInd.center = CGPointMake(self.view.center.x, self.view.center.y);
    [self.view addSubview:_progressInd];
    [self.view bringSubviewToFront:_progressInd];
    
    //get info
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString* libraryDirectory = [paths objectAtIndex:0];
 
    NSString* myDirectory = [libraryDirectory stringByAppendingPathComponent:@"lechange"];
    NSString* davDirectory = [myDirectory stringByAppendingPathComponent:@"openSDK"];
 
    NSString* infoPath = [davDirectory stringByAppendingPathComponent:@"info"];
    NSString* realPath = [infoPath stringByAppendingString:@".txt"];
    NSFileManager* fileManage = [NSFileManager defaultManager];
    NSError* pErr;
    BOOL isDir;
    if (NO == [fileManage fileExistsAtPath:myDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:myDirectory withIntermediateDirectories:YES attributes:nil error:&pErr];
    }
    if (NO == [fileManage fileExistsAtPath:davDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:davDirectory withIntermediateDirectories:YES attributes:nil error:&pErr];
    }
    if (YES == [fileManage fileExistsAtPath:realPath isDirectory:&isDir]) {
        NSLog(@"%@ exists,isdir[%d]", realPath, isDir);
        NSString* content = [NSString stringWithContentsOfFile:realPath encoding:NSUTF8StringEncoding error:nil];
        char appId[500] = { 0 };
        char appSecret[500] = { 0 };
        char svrInfo[500] = { 0 };
        char appAccount[500] = { 0 };
        NSLog(@"content %s", [content UTF8String]);
        sscanf([content UTF8String], "[%[^]]][%[^]]][%[^]]]%*s", appId, appSecret, svrInfo);
        self.m_textAppId.text = @"lc2ea929282d5f4636";
        self.m_textAppSecret.text = @"f869433de68a4a06bb8f9bb89118fc";
        self.m_textServerInfo.text = [NSString stringWithUTF8String:svrInfo];
 
        NSLog(@"appid[%@],appsecret[%@],account[%s],svrInfo[%s]", self.m_textAppId.text, self.m_textAppSecret.text, appAccount, svrInfo);
    }
    //end
    
    /*
     Ch:ios13获取SSID需要用户授权地理位置信息
     En:Ios13 requires the user to authorize the geographic location information to obtain the SSID.
     */
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    CGFloat version = [phoneVersion floatValue];
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined && version >= 13) {
        self.locationManager = [[CLLocationManager alloc] init];
        if ([ self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [ self.locationManager requestWhenInUseAuthorization];
        }
        [self.locationManager startUpdatingLocation];
    }
}
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *curLanguage = NSLocalizedString(LANGUAGE_TXT, nil);
    if ([curLanguage isEqualToString:@"zh"]) {
        
    }
    else if ([curLanguage isEqualToString:@"en"]) {
 
    }
}
 
- (void)viewWillLayoutSubviews {
    
    self.m_btnMan.frame = CGRectMake(35, [UIScreen mainScreen].bounds.size.height - 120 - 80, 120, 120);
     self.m_btnUser.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 35-120, [UIScreen mainScreen].bounds.size.height - 120 - 80, 120, 120);
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
- (void)onManagerMode:(id)sender
{
    //save info
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString* libraryDirectory = [paths objectAtIndex:0];
 
    NSString* myDirectory = [libraryDirectory stringByAppendingPathComponent:@"lechange"];
    NSString* davDirectory = [myDirectory stringByAppendingPathComponent:@"openSDK"];
 
    NSString* infoPath = [davDirectory stringByAppendingPathComponent:@"info"];
    NSString* realPath = [infoPath stringByAppendingString:@".txt"];
 
    NSFileManager* fileManage = [NSFileManager defaultManager];
    NSError* pErr;
    BOOL isDir;
    if (NO == [fileManage fileExistsAtPath:myDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:myDirectory withIntermediateDirectories:YES attributes:nil error:&pErr];
    }
    if (NO == [fileManage fileExistsAtPath:davDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:davDirectory withIntermediateDirectories:YES attributes:nil error:&pErr];
    }
 
    NSString* textTmp = [NSString stringWithFormat:@"[%@][%@][%@]", self.m_textAppId.text, self.m_textAppSecret.text, self.m_textServerInfo.text];
    [textTmp writeToFile:realPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //end
 
    if (nil == self.m_textAppId.text || 0 == self.m_textAppId.text.length
        || nil == self.m_textAppSecret.text || 0 == self.m_textAppSecret.text.length) {
        UIStoryboard* currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        HintViewController* hintView = [currentBoard instantiateViewControllerWithIdentifier:@"HintView"];
        [hintView setInfo:self.m_textAppId.text appSecret:self.m_textAppSecret.text info:NSLocalizedString(CONFIG_TIP_TXT, nil)];
        [self.navigationController pushViewController:hintView animated:NO];
        return;
    }
    [self showLoading];
    self.m_btnMan.enabled = NO;
    dispatch_queue_t enter_device = dispatch_queue_create("enter_device", nil);
    NSString *serverIp = [self parseServerIp:self.m_textServerInfo.text];
    NSInteger serverPort = [self parseServerPort:self.m_textServerInfo.text];
    NSString *appId = self.m_textAppId.text;
    NSString *appSecrect = self.m_textAppSecret.text;
    dispatch_async(enter_device, ^{
        
        NSString* accessTok;
        NSString* errCode;
        NSString* errMsg;
        OpenApiService* openApi = [[OpenApiService alloc] init];
        NSInteger ret = [openApi getAccessToken:serverIp port:serverPort appId:appId appSecret:appSecrect token:&accessTok errcode:&errCode errmsg:&errMsg];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hideLoading];
            self.m_btnMan.enabled = YES;
            if (ret < 0) {
                if ([errCode isEqualToString:@"SN1001"]) {
                    UIStoryboard* currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    HintViewController* hintView = [currentBoard instantiateViewControllerWithIdentifier:@"HintView"];
                    [hintView setInfo:self.m_textAppId.text appSecret:self.m_textAppSecret.text info:NSLocalizedString(CONFIG_TIP_TXT, nil)];
                    [self.navigationController pushViewController:hintView animated:NO];
                }
                return;
            }
            UIStoryboard* currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            DeviceViewController* devView = [currentBoard instantiateViewControllerWithIdentifier:@"DeviceView"];
            ;
            [self.navigationController pushViewController:devView animated:NO];
            [devView setAdminInfo:accessTok protocol:80 == serverPort ? 0 : 1 address:serverIp port:serverPort];
        });
    });
}
- (void)onUserMode:(id)sender
{
    //save info
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString* libraryDirectory = [paths objectAtIndex:0];
 
    NSString* myDirectory = [libraryDirectory stringByAppendingPathComponent:@"lechange"];
    NSString* davDirectory = [myDirectory stringByAppendingPathComponent:@"openSDK"];
 
    NSString* infoPath = [davDirectory stringByAppendingPathComponent:@"info"];
    NSString* realPath = [infoPath stringByAppendingString:@".txt"];
 
    NSFileManager* fileManage = [NSFileManager defaultManager];
    NSError* pErr;
    BOOL isDir;
    if (NO == [fileManage fileExistsAtPath:myDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:myDirectory withIntermediateDirectories:YES attributes:nil error:&pErr];
    }
    if (NO == [fileManage fileExistsAtPath:davDirectory isDirectory:&isDir]) {
        [fileManage createDirectoryAtPath:davDirectory withIntermediateDirectories:YES attributes:nil error:&pErr];
    }
 
    NSString* textTmp = [NSString stringWithFormat:@"[%@][%@][%@]", self.m_textAppId.text, self.m_textAppSecret.text, self.m_textServerInfo.text];
    [textTmp writeToFile:realPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //end
 
    if (nil == self.m_textAppId.text || 0 == self.m_textAppId.text.length
        || nil == self.m_textAppSecret.text || 0 == self.m_textAppSecret.text.length) {
        UIStoryboard* currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        HintViewController* hintView = [currentBoard instantiateViewControllerWithIdentifier:@"HintView"];
 
        [self.navigationController pushViewController:hintView animated:NO];
        [hintView setInfo:self.m_textAppId.text appSecret:self.m_textAppSecret.text info:NSLocalizedString(CONFIG_TIP_TXT, nil)];
        return;
    }
 
    UIStoryboard* currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
 
    UserModeViewController* userModeView = [currentBoard instantiateViewControllerWithIdentifier:@"UserModeView"];
 
    [userModeView setAppIdAndSecret:self.m_textAppId.text appSecret:self.m_textAppSecret.text svr:[self parseServerIp:self.m_textServerInfo.text] port:[self parseServerPort:self.m_textServerInfo.text]];
    [self.navigationController pushViewController:userModeView animated:NO];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField
{
    CGFloat offset = self.view.frame.size.height - (textField.frame.origin.y + textField.frame.size.height + 216 + 50);
    if (offset <= 0) {
        [UIView animateWithDuration:0.1 animations:^{
            CGRect frame = self.view.frame;
            frame.origin.y = offset;
            self.view.frame = frame;
        }];
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField*)textField
{
    [UIView animateWithDuration:0.1 animations:^{
        CGRect rect = self.view.frame;
        rect.origin.y = 0;
        self.view.frame = rect;
    }];
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (NSString*)parseServerIp:(NSString*)svrInfo
{
    NSArray* arr = [svrInfo componentsSeparatedByString:@":"];
    NSLog(@"parseServerIP[%@],ip[%@]", arr, [arr objectAtIndex:0]);
    return [arr objectAtIndex:0];
}
- (NSInteger)parseServerPort:(NSString*)svrInfo
{
    NSArray* arr = [svrInfo componentsSeparatedByString:@":"];
    NSLog(@"parseServerport[%@]", arr);
    if (arr.count <= 1) {
        if ([[arr objectAtIndex:0] rangeOfString:@"https"].location != NSNotFound) {
            return 443;
        }
        else {
            return 80;
        }
    }
    else {
        return [[arr objectAtIndex:1] intValue];
    }
}
 
- (void)switchBtnDidTapped:(UISwitch *)sender {
    LCOpenSDK_LogInfo *logInfo = [[LCOpenSDK_LogInfo alloc] init];
    if (sender.isOn) {
        logInfo.levelType = LogLevelTypeDebug;
    }
    else {
        logInfo.levelType = LogLevelTypeFatal;
    }
    [[LCOpenSDK_Log shareInstance] setLogInfo:logInfo];
}
 
/**
 Ch:显示滚动轮指示器
 En:Show scroll wheel indicator.
 */
- (void)showLoading
{
    [_progressInd startAnimating];
}
/**
 Ch:消除滚动轮指示器
 En:Eliminate scroll wheel indicator.
*/
- (void)hideLoading
{
    if ([_progressInd isAnimating]) {
        [_progressInd stopAnimating];
    }
}
 
@end