AppDelegate.m 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. //
  2. // AppDelegate.m
  3. // RedAnt ERP Suite
  4. //
  5. // Created by Ray on 14-5-19.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "UILabel+FontAppearance.h"
  10. #import "MainViewController.h"
  11. @implementation AppDelegate
  12. {
  13. #ifdef USE_SOFTSCAN
  14. DeviceInfo* _softScanDeviceInfo;
  15. #endif
  16. DeviceInfo* _deviceInfoToTrigger;
  17. }
  18. @synthesize price_hidden = _price_hidden;
  19. @synthesize ScanApi;
  20. @synthesize ScanApiConsumer;
  21. @synthesize scanApiVersion;
  22. //@synthesize devices;
  23. -(void) set_priceHidden:(bool)price_hidden
  24. {
  25. _price_hidden = price_hidden;
  26. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  27. // NSObject* obj;
  28. // [obj isEqual:@""];
  29. [avc showHidePrice];
  30. }
  31. -(void) update_count_mark
  32. {
  33. dispatch_async(dispatch_get_main_queue(), ^{
  34. if(self.main_vc==nil)
  35. return;
  36. MainViewController * main_vc= (MainViewController*)self.main_vc;
  37. if(self.user==nil||self.cart_count==0)
  38. {
  39. main_vc.label_ccount.hidden = true;
  40. // NSString* title = [NSString stringWithFormat:@"Sign in"];
  41. // [main_vc.buttonUser setTitle:title forState:UIControlStateNormal];
  42. //
  43. // main_vc.labelMode.text=nil;
  44. // [main_vc.labelMode setTextColor:UIColorFromRGB(0x336699)];
  45. // // [main_vc.buttonUser setTitleColor:UIColorFromRGB(0x996633) forState:UIControlStateNormal];
  46. // // main_vc.buttonUser.hidden=true;
  47. // // main_vc.buttonUser.hidden=false;
  48. // // [main_vc.headerView setNeedsDisplay];
  49. // // [main_vc.headerView setNeedsLayout];
  50. // return;
  51. }
  52. else
  53. {
  54. NSString* strcount;
  55. if(self.cart_count>999)
  56. strcount=@"999+";
  57. else
  58. strcount=[NSString stringWithFormat:@"%ld",self.cart_count];
  59. main_vc.label_ccount.text = strcount;
  60. if(strcount.length==1)
  61. main_vc.label_ccount.frame = CGRectMake(370, 15, 21, 21);
  62. if(strcount.length==2)
  63. main_vc.label_ccount.frame = CGRectMake(364, 15, 25, 21);
  64. if(strcount.length==3)
  65. main_vc.label_ccount.frame = CGRectMake(360, 15, 31, 21);
  66. if(strcount.length==4)
  67. main_vc.label_ccount.frame = CGRectMake(349, 15, 42, 21);
  68. // [main_vc.label_ccount sizeToFit];
  69. // main_vc.label_ccount.center = CGPointMake(360, 15);
  70. main_vc.label_ccount.hidden = false;
  71. // if(self.user_type==USER_ROLE_CUSTOMER)
  72. // {
  73. // NSString* title = [NSString stringWithFormat:@"%@",self.user];
  74. // [main_vc.buttonUser setTitle:title forState:UIControlStateNormal];
  75. // main_vc.labelMode.text=nil;
  76. // [main_vc.labelMode setTextColor:UIColorFromRGB(0x336699)];
  77. //
  78. // // [main_vc.buttonUser setTitleColor:UIColorFromRGB(0x996633) forState:UIControlStateNormal];
  79. // // main_vc.buttonUser.hidden=true;
  80. // // main_vc.buttonUser.hidden=false;
  81. // // [main_vc.headerView setNeedsDisplay];
  82. // // [main_vc.headerView setNeedsLayout];
  83. // }
  84. // else
  85. // {
  86. // if([mode isEqualToString:@"Regualar Mode"])
  87. // {
  88. // NSString* title = self.user;
  89. // [main_vc.buttonUser setTitle:title forState:UIControlStateNormal];
  90. //
  91. // main_vc.labelMode.text=@"RM";
  92. // [main_vc.labelMode setTextColor:UIColorFromRGB(0x336699)];
  93. // // [main_vc.buttonUser setTitleColor:UIColorFromRGB(0x996633) forState:UIControlStateNormal];
  94. // // main_vc.buttonUser.hidden=true;
  95. // // main_vc.buttonUser.hidden=false;
  96. // // [main_vc.headerView setNeedsDisplay];
  97. // // [main_vc.headerView setNeedsLayout];
  98. // }
  99. // else
  100. // {
  101. // NSString* title = self.user;
  102. // [main_vc.buttonUser setTitle:title forState:UIControlStateNormal];
  103. //
  104. // main_vc.labelMode.text=@"TM";
  105. // [main_vc.labelMode setTextColor:UIColorFromRGB(0x009966)];
  106. // // [main_vc.buttonUser setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateNormal];
  107. // // main_vc.buttonUser.hidden=true;
  108. // // main_vc.buttonUser.hidden=false;
  109. // // [main_vc.headerView setNeedsDisplay];
  110. // // [main_vc.headerView setNeedsLayout];
  111. // }
  112. //
  113. // NSString * contact =[self.customerInfo valueForKey:@"customer_contact"];
  114. // NSString * customer=[self.customerInfo valueForKey:@"customer_name"];
  115. // if(contact==nil||contact.length==0)
  116. // {
  117. // contact=@"No Name";
  118. // [main_vc.btnContact setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  119. // }
  120. // else
  121. // {
  122. //
  123. // if(contact==nil||contact.length==0)
  124. // {
  125. // contact=@"No Name";
  126. // [main_vc.btnContact setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  127. // }
  128. // else
  129. // {
  130. // [main_vc.btnContact setTitleColor:UIColorFromRGB(0x009900) forState:UIControlStateNormal];
  131. // }
  132. // }
  133. //
  134. // NSMutableArray* arr_contact = [[NSMutableArray alloc] init];
  135. // if(contact.length>0)
  136. // [arr_contact addObject:contact];
  137. // if(customer.length>0)
  138. // [arr_contact addObject:customer];
  139. //
  140. // NSString *contact_string = [arr_contact componentsJoinedByString:@" @"];
  141. //
  142. //
  143. // [main_vc.btnContact setTitle:contact_string forState:UIControlStateNormal];
  144. //
  145. // }
  146. }
  147. });
  148. }
  149. -(void) closeOrder
  150. {
  151. self.order_code = nil;
  152. self.order_customer_id = nil;
  153. self.customerInfo = nil;
  154. self.contact_id = nil;
  155. [self SetMode:nil];
  156. // [self SetSo:nil];
  157. [((MainViewController*)self.main_vc) reloadCart:true immediately:false];
  158. [((MainViewController*)self.main_vc) reloadOrder:true immediately:false];
  159. }
  160. -(void) switchToPreviousVC
  161. {
  162. MainViewController * main_vc= (MainViewController*)self.main_vc;
  163. [main_vc switchToPreviousVC];
  164. }
  165. -(void) SetMode:(NSString*)mode
  166. {
  167. dispatch_async(dispatch_get_main_queue(), ^{
  168. if(self.main_vc==nil)
  169. return;
  170. MainViewController * main_vc= (MainViewController*)self.main_vc;
  171. if(self.user==nil)
  172. {
  173. NSString* title = [NSString stringWithFormat:@"Sign in"];
  174. [main_vc.buttonUser setTitle:title forState:UIControlStateNormal];
  175. main_vc.labelMode.text=nil;
  176. [main_vc.labelMode setTextColor:UIColorFromRGB(0x336699)];
  177. // [main_vc.buttonUser setTitleColor:UIColorFromRGB(0x996633) forState:UIControlStateNormal];
  178. // main_vc.buttonUser.hidden=true;
  179. // main_vc.buttonUser.hidden=false;
  180. // [main_vc.headerView setNeedsDisplay];
  181. // [main_vc.headerView setNeedsLayout];
  182. return;
  183. }
  184. else
  185. {
  186. if(self.user_type==USER_ROLE_CUSTOMER)
  187. {
  188. NSString* title = [NSString stringWithFormat:@"%@",self.user];
  189. [main_vc.buttonUser setTitle:title forState:UIControlStateNormal];
  190. main_vc.labelMode.text=nil;
  191. [main_vc.labelMode setTextColor:UIColorFromRGB(0x336699)];
  192. // [main_vc.buttonUser setTitleColor:UIColorFromRGB(0x996633) forState:UIControlStateNormal];
  193. // main_vc.buttonUser.hidden=true;
  194. // main_vc.buttonUser.hidden=false;
  195. // [main_vc.headerView setNeedsDisplay];
  196. // [main_vc.headerView setNeedsLayout];
  197. }
  198. else
  199. {
  200. if([mode isEqualToString:@"Regular Mode"])
  201. {
  202. NSString* title = self.user;
  203. [main_vc.buttonUser setTitle:title forState:UIControlStateNormal];
  204. main_vc.labelMode.text=@"Regular Mode";
  205. [main_vc.labelMode setTextColor:UIColorFromRGB(0x336699)];
  206. // [main_vc.buttonUser setTitleColor:UIColorFromRGB(0x996633) forState:UIControlStateNormal];
  207. // main_vc.buttonUser.hidden=true;
  208. // main_vc.buttonUser.hidden=false;
  209. // [main_vc.headerView setNeedsDisplay];
  210. // [main_vc.headerView setNeedsLayout];
  211. }
  212. else
  213. {
  214. NSString* title = self.user;
  215. [main_vc.buttonUser setTitle:title forState:UIControlStateNormal];
  216. main_vc.labelMode.text=@"Trade Show Mode";
  217. [main_vc.labelMode setTextColor:UIColorFromRGB(0x009966)];
  218. // [main_vc.buttonUser setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateNormal];
  219. // main_vc.buttonUser.hidden=true;
  220. // main_vc.buttonUser.hidden=false;
  221. // [main_vc.headerView setNeedsDisplay];
  222. // [main_vc.headerView setNeedsLayout];
  223. }
  224. NSString * contact =[self.customerInfo valueForKey:@"customer_contact"];
  225. NSString * customer=[self.customerInfo valueForKey:@"customer_name"];
  226. if(self.customerInfo==nil)
  227. {
  228. // contact=@"No Name"; if(customerinfo==nil)
  229. // {
  230. contact=@"Select Contact";
  231. [main_vc.btnContact setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  232. }
  233. else
  234. {
  235. if(contact==nil||contact.length==0)
  236. {
  237. contact=@"No Name";
  238. [main_vc.btnContact setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  239. }
  240. else
  241. {
  242. [main_vc.btnContact setTitleColor:UIColorFromRGB(0x009900) forState:UIControlStateNormal];
  243. }
  244. }
  245. NSMutableArray* arr_contact = [[NSMutableArray alloc] init];
  246. if(contact.length>0)
  247. [arr_contact addObject:contact];
  248. if(customer.length>0)
  249. [arr_contact addObject:customer];
  250. NSString *contact_string = [arr_contact componentsJoinedByString:@" @"];
  251. [main_vc.btnContact setTitle:contact_string forState:UIControlStateNormal];
  252. }
  253. }
  254. });
  255. }
  256. -(void) SetSo:(NSString *)So
  257. {
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. if(self.main_vc==nil)
  260. return;
  261. MainViewController * main_vc= (MainViewController*)self.main_vc;
  262. if(self.user==nil)
  263. {
  264. // NSString* title = [NSString stringWithFormat:@"Sign in"];
  265. main_vc.labelSo.text=nil;
  266. return;
  267. }
  268. else
  269. {
  270. if(self.user_type==USER_ROLE_CUSTOMER)
  271. {
  272. main_vc.labelSo.text=nil;
  273. // main_vc.buttonUser.hidden=true;
  274. // main_vc.buttonUser.hidden=false;
  275. // [main_vc.headerView setNeedsDisplay];
  276. // [main_vc.headerView setNeedsLayout];
  277. }
  278. else
  279. {
  280. if(So.length==0)
  281. main_vc.labelSo.text=nil;
  282. else
  283. {
  284. NSString* text =[NSString stringWithFormat:@"SO#: %@",So];
  285. main_vc.labelSo.text=text;
  286. }
  287. }
  288. }
  289. });
  290. }
  291. //- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
  292. // if ([rootViewController isKindOfClass:[UITabBarController class]]) {
  293. // UITabBarController* tabBarController = (UITabBarController*)rootViewController;
  294. // return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
  295. // } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
  296. // UINavigationController* navigationController = (UINavigationController*)rootViewController;
  297. // return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
  298. // } else if (rootViewController.presentedViewController) {
  299. // UIViewController* presentedViewController = rootViewController.presentedViewController;
  300. // return [self topViewControllerWithRootViewController:presentedViewController];
  301. // } else {
  302. // return rootViewController;
  303. // }
  304. //}
  305. //- (UIViewController*)topViewController {
  306. // return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
  307. //}
  308. //-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  309. ////   // Get topmost/visible view controller
  310. // UIViewController *currentViewController = [self topViewController];
  311. //
  312. // if ([currentViewController respondsToSelector:@selector(lockPortrait)]) {
  313. //
  314. // return UIInterfaceOrientationMaskPortrait;
  315. // }
  316. ////
  317. ////   // Only allow portrait (standard behaviour)
  318. // return UIInterfaceOrientationMaskAll;
  319. //}
  320. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  321. {
  322. // Override point for customization after application launch.
  323. NSString* order_filter_source_path =[[NSBundle mainBundle] pathForResource:@"status_filter_cadedate" ofType:@"json" ];
  324. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  325. NSString *documents = [paths objectAtIndex:0];
  326. NSString *dist_path = [documents stringByAppendingPathComponent:@"status_filter_cadedate.json"];
  327. if ([[NSFileManager defaultManager] fileExistsAtPath:dist_path]) {
  328. NSLog(@"文件已经存在了");
  329. }
  330. else
  331. {
  332. // NSLog(@"resourceSampleImagesFolderPath=%@",resourceFolderPath);
  333. NSData *mainBundleFile = [NSData dataWithContentsOfFile:order_filter_source_path];
  334. // NSLog(@"mainBundleFile==%@",mainBundleFile);
  335. [[NSFileManager defaultManager] createFileAtPath:dist_path
  336. contents:mainBundleFile
  337. attributes:nil];
  338. }
  339. if (! [iSalesDB initializeDb])
  340. // TODO: alert the user!
  341. DebugLog(@"couldn't init db");
  342. [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
  343. self.contact_id=nil;
  344. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  345. NSString * enable_cache = [defaults stringForKey:@"enable_cache"];
  346. if([enable_cache isEqualToString:@"false"])
  347. self.bEnable_Cache = false;
  348. else
  349. self.bEnable_Cache = true;
  350. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  351. self.build =[infoDict objectForKey:@"CFBundleVersion"];
  352. //scanner
  353. self.devices=[[NSMutableArray alloc]init];
  354. // change this to YES if you want SingleEntry to
  355. // confirm the decoded data
  356. _doAppDataConfirmation=NO;
  357. if(ScanApi==nil){
  358. #ifdef USE_SOFTSCAN
  359. // this is useful for SoftScan to keep a handle
  360. // that is used in the trigger button
  361. _softScanDeviceInfo=nil;
  362. #endif
  363. ScanApi=[[ScanApiHelper alloc]init];
  364. [ScanApi setDelegate:self];
  365. self.enable_ScannerLog = true;
  366. self.api_Status= API_STOP;
  367. }
  368. //UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:uialertcontroller.class, nil];
  369. // [appearanceLabel setAppearanceFont:[UIFont boldSystemFontOfSize:10]]; //for example
  370. return YES;
  371. }
  372. - (void)applicationWillResignActive:(UIApplication *)application
  373. {
  374. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  375. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  376. }
  377. - (void)applicationDidEnterBackground:(UIApplication *)application
  378. {
  379. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  380. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  381. }
  382. - (void)applicationWillEnterForeground:(UIApplication *)application
  383. {
  384. // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  385. }
  386. - (void)applicationDidBecomeActive:(UIApplication *)application
  387. {
  388. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  389. }
  390. - (void)applicationWillTerminate:(UIApplication *)application
  391. {
  392. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  393. }
  394. -(void) Logout
  395. {
  396. self.user = nil;
  397. self.password=nil;
  398. self.user_icon=nil;
  399. self.user_type = 0;
  400. self.bLogin = false;
  401. self.contact_id=nil;
  402. self.customerInfo = nil;
  403. self.order_code = nil;
  404. [self SetMode:nil];
  405. }
  406. -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  407. {
  408. self.duid = [[[[NSString stringWithFormat:@"%@",deviceToken] substringWithRange:NSMakeRange(0, 72)] substringWithRange:NSMakeRange(1, 71)] stringByReplacingOccurrencesOfString:@" " withString:@""];
  409. NSLog(@"regisger success:%@",deviceToken);
  410. //注册成功,将deviceToken保存到应用服务器数据库中
  411. }
  412. #pragma mark - Scaner
  413. -(void)onTimer: (NSTimer*)theTimer{
  414. if(theTimer==ScanApiConsumer){
  415. // [self addlog:@"ScanApi::doScanApiReceive:"];
  416. [ScanApi doScanApiReceive];
  417. }
  418. }
  419. -(void) addlog:(NSString*) newtext
  420. {
  421. if(!self.enable_ScannerLog)
  422. return;
  423. dispatch_async(dispatch_get_main_queue(), ^{
  424. NSString* date;
  425. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  426. [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
  427. date = [formatter stringFromDate:[NSDate date]];
  428. if(newtext.length<=0)
  429. return;
  430. NSString* text = self.log;
  431. if(text.length==0)
  432. text=@"";
  433. text = [NSString stringWithFormat:@"%@%@----------%@\n",text,date,newtext];
  434. self.log = text;
  435. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  436. [avc onLogUpdate];
  437. });
  438. }
  439. - (void)enableScanner:(bool)enable {
  440. [self addlog:@"Enable scanner:"];
  441. if(enable)
  442. {
  443. [self addlog:@"Enable"];
  444. self.api_Status = API_INIT;
  445. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  446. self.Status_Text = @"Initialize...";
  447. [avc onStatusChanged:@"Initialize..."];
  448. [ScanApi open];
  449. [self addlog:@"ScanApi open"];
  450. ScanApiConsumer=[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
  451. [self addlog:@"ScanApiConsumer init"];
  452. // [ScanApi close];
  453. // [self addlog:@"ScanApi opent"];
  454. }
  455. else
  456. {
  457. [self addlog:@"Disable"];
  458. // [ScanApiConsumer invalidate];
  459. // ScanApiConsumer= nil;
  460. // [self addlog:@"ScanApiConsumer invalidate"];
  461. self.api_Status = API_CLOSING;
  462. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  463. self.Status_Text = @"Stoping...";
  464. [avc onStatusChanged:@"Stoping..."];
  465. [ScanApi close];
  466. [self addlog:@"ScanApi close"];
  467. }
  468. }
  469. #pragma mark - Device Info List management
  470. -(void) updateDevicesList:(DeviceInfo*) deviceInfo Add:(BOOL)add{
  471. [self addlog:@"updateDevicesList:"];
  472. if(add==YES){
  473. [self.devices addObject:deviceInfo];
  474. }
  475. else{
  476. [self.devices removeObject:deviceInfo];
  477. }
  478. NSMutableString* temp=[[NSMutableString alloc]init];
  479. for (DeviceInfo* info in self.devices) {
  480. [temp appendString:[info getName]];
  481. [temp appendString:@"\n"];
  482. }
  483. if(self.devices.count>0)
  484. [temp appendString:@"ready to scan"];
  485. else
  486. [temp appendString:@"Waiting for Scanner..."];
  487. // _Status.text=temp ;
  488. [self addlog:temp];
  489. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  490. self.Status_Text = temp;
  491. [avc onStatusChanged:temp];
  492. [avc OnUpdateDevicesList];
  493. // if ([self.active_controller respondsToSelector:@selector(lockPortrait)])
  494. // [self.dev_table reloadData];
  495. }
  496. #pragma mark - ScanApiHelper complete delegates
  497. // THE 2 FOLLOWING CALLBACKS ARE FOR DEMO ONLY FOR SHOWING HOW
  498. // TO CHECK IF A SYMBOLOGY IS ALREADY ENABLED AND IF NOT IT ENABLES
  499. // IT RIGHT THERE. THE GOAL IS TO CONFIGURE ONCE THE SCANNER WHEN IT
  500. // CONNECTS TO THE DEVICE. IF THERE IS NO SPECIFIC NEED TO CONFIGURE
  501. // THE SCANNER, THESE CALLBACKS CAN BE REMOVED
  502. // callback received when the Get Symbology Status is completed
  503. -(void)onGetSymbologyDpm:(ISktScanObject*)scanObj{
  504. [self addlog:@"onGetSymbologyDpm:"];
  505. SKTRESULT result=[[scanObj Msg]Result];
  506. if(SKTSUCCESS(result)){
  507. DeviceInfo* deviceInfo=[ScanApi getDeviceInfoFromScanObject:scanObj];
  508. if(deviceInfo!=nil){
  509. ISktScanSymbology* symbology=[[scanObj Property]Symbology];
  510. if([symbology getStatus]==kSktScanSymbologyStatusDisable){
  511. [ScanApi postSetSymbologyInfo:deviceInfo SymbologyId:kSktScanSymbologyDirectPartMarking Status:TRUE Target:self Response:@selector(onSetSymbology:)];
  512. }
  513. }
  514. }
  515. else{
  516. // an error message should be displayed here
  517. // indicating that the DPM symbology status cannot be retrieved
  518. }
  519. }
  520. // callback received when the Set Symbology Status is completed
  521. -(void)onSetSymbology:(ISktScanObject*)scanObj{
  522. [self addlog:@"onSetSymbology:"];
  523. SKTRESULT result=[[scanObj Msg]Result];
  524. if(!SKTSUCCESS(result)){
  525. // display an error message saying a symbology cannot be set
  526. }
  527. }
  528. /**
  529. *
  530. */
  531. -(void) onSetDataConfirmationMode:(ISktScanObject*)scanObj{
  532. [self addlog:@"onSetDataConfirmationMode:"];
  533. SKTRESULT result=[[scanObj Msg]Result];
  534. if(SKTSUCCESS(result)){
  535. [self addlog:@"DataConfirmation Mode OK"];
  536. }
  537. else{
  538. [self addlog:[NSString stringWithFormat:@"DataConfirmation Mode Error %ld",result]];
  539. }
  540. }
  541. /**
  542. *
  543. */
  544. -(void) onDataConfirmation:(ISktScanObject*)scanObj{
  545. [self addlog:@"onDataConfirmation:"];
  546. SKTRESULT result=[[scanObj Msg]Result];
  547. if(SKTSUCCESS(result)){
  548. [self addlog:@"Data Confirmed OK"];
  549. }
  550. else{
  551. [self addlog:[NSString stringWithFormat:@"Data Confirmed Error %ld",result]];
  552. }
  553. }
  554. /**
  555. *
  556. */
  557. -(void) onSetLocalDecodeAction:(ISktScanObject*)scanObj{
  558. [self addlog:@"onSetLocalDecodeAction:"];
  559. SKTRESULT result=[[scanObj Msg]Result];
  560. if(SKTSUCCESS(result)){
  561. [self addlog:@"Local Decode Action OK"];
  562. }
  563. else{
  564. [self addlog:[NSString stringWithFormat:@"Local Decode Action Error %ld",result]];
  565. }
  566. }
  567. /**
  568. *
  569. */
  570. -(void) onGetSoftScanStatus:(ISktScanObject*)scanObj{
  571. SKTRESULT result=[[scanObj Msg]Result];
  572. if(SKTSUCCESS(result)){
  573. ISktScanProperty* property=[scanObj Property];
  574. [self addlog:@"SoftScan status:"];
  575. if([property getByte]==kSktScanEnableSoftScan){
  576. [self addlog:@"SoftScan is ENABLED"];
  577. _softScannerEnabled=TRUE;
  578. }
  579. else{
  580. _softScannerEnabled=FALSE;
  581. [self addlog:@"SoftScan is DISABLED"];
  582. }
  583. }
  584. else{
  585. [self addlog:[ NSString stringWithFormat:@"getting SoftScanStatus returned the error %ld",result]];
  586. }
  587. }
  588. /**
  589. *
  590. */
  591. -(void) onSetSoftScanStatus:(ISktScanObject*)scanObj{
  592. [self addlog:@"onSetSoftScanStatus:"];
  593. SKTRESULT result=[[scanObj Msg]Result];
  594. if(SKTSUCCESS(result)){
  595. [self addlog:@"SoftScan set status success"];
  596. }
  597. else{
  598. [self addlog:[NSString stringWithFormat:@"SoftScan set status returned the error %ld",result]];
  599. }
  600. }
  601. /**
  602. *
  603. */
  604. -(void) onSetTrigger:(ISktScanObject*)scanObj{
  605. [self addlog:@"onSetTrigger:"];
  606. SKTRESULT result=[[scanObj Msg]Result];
  607. if(SKTSUCCESS(result)){
  608. [self addlog:@"Trigger set success"];
  609. }
  610. else{
  611. [self addlog:[NSString stringWithFormat:@"Trigger set returned the error %ld",result]];
  612. }
  613. }
  614. /**
  615. *
  616. */
  617. -(void) onGetScanApiVersion:(ISktScanObject*)scanObj{
  618. [self addlog:@"onGetScanApiVersion:"];
  619. SKTRESULT result=[[scanObj Msg]Result];
  620. if(SKTSUCCESS(result))
  621. {
  622. ISktScanProperty*property=[scanObj Property];
  623. if([property getType]==kSktScanPropTypeVersion)
  624. {
  625. scanApiVersion=[NSString stringWithFormat:@"%lx.%lx.%lx.%ld",
  626. [[property Version]getMajor],
  627. [[property Version]getMiddle],
  628. [[property Version]getMinor],
  629. [[property Version]getBuild]];
  630. }
  631. }
  632. else{
  633. scanApiVersion=[NSString stringWithFormat:@"Get ScanAPI version Error: %ld",result];
  634. }
  635. [self addlog:[NSString stringWithFormat:@"ver: %@",scanApiVersion]];
  636. }
  637. /**
  638. *
  639. */
  640. #ifdef USE_SOFTSCAN
  641. -(void) onSetOverlayView:(ISktScanObject*)scanObj{
  642. SKTRESULT result=[[scanObj Msg]Result];
  643. if(SKTSUCCESS(result)){
  644. [self addlog:@"Overlay view set success"];
  645. }
  646. else{
  647. [self addlog:[NSString stringWithFormat:@"Overlay view set returned the error %ld",result]];
  648. }
  649. }
  650. #endif
  651. /**
  652. * called each time a device connects to the host
  653. * @param result contains the result of the connection
  654. * @param newDevice contains the device information
  655. */
  656. -(void)onDeviceArrival:(SKTRESULT)result device:(DeviceInfo*)deviceInfo{
  657. [self addlog:@"onDeviceArrival:"];
  658. [self updateDevicesList:deviceInfo Add:YES];
  659. #ifdef USE_SOFTSCAN
  660. // if the scanner is a SoftScan scanner
  661. if([deviceInfo.getTypeString compare:@"SoftScan"]==NSOrderedSame){
  662. // _softScannerTriggerBtn.hidden=NO;
  663. _softScanDeviceInfo=deviceInfo;
  664. if(_deviceInfoToTrigger==nil)
  665. _deviceInfoToTrigger=deviceInfo;
  666. NSMutableDictionary* overlayParameter=[[NSMutableDictionary alloc]init];
  667. [overlayParameter setValue:self forKey:[NSString stringWithCString:kSktScanSoftScanContext encoding:NSASCIIStringEncoding]];
  668. [ScanApi postSetOverlayView:deviceInfo OverlayView:overlayParameter Target:self Response:@selector(onSetOverlayView:)];
  669. }
  670. else
  671. #endif
  672. {
  673. if([deviceInfo.getTypeString compare:@"CHS 8Ci Scanner"]==NSOrderedSame){
  674. // _softScannerTriggerBtn.hidden=NO;
  675. _deviceInfoToTrigger=deviceInfo;
  676. }
  677. if(_doAppDataConfirmation==YES){
  678. // switch the comment between the 2 following lines for handling the
  679. // data confirmation beep from the scanner (local)
  680. // if none is set, the scanner will beep only once when SingleEntry actually
  681. // confirm the decoded data, otherwise the scanner will beep twice, one locally,
  682. // and one when SingleEntry will confirm the decoded data
  683. [ScanApi postSetDecodeAction:deviceInfo DecodeAction:kSktScanLocalDecodeActionNone Target:self Response:@selector(onSetLocalDecodeAction:)];
  684. // [ScanApi postSetDecodeAction:deviceInfo DecodeAction:kSktScanLocalDecodeActionBeep|kSktScanLocalDecodeActionFlash|kSktScanLocalDecodeActionRumble Target:self Response:@selector(onSetLocalDecodeAction:)];
  685. }
  686. // for demonstration only, let's make sure the DPM is enabled
  687. // first interrogate the scanner to see if it's already enabled
  688. // and in the onGetSymbologyDpm callback, if the DPM is not already set
  689. // then we send a Symbology property to enable it.
  690. [ScanApi postGetSymbologyInfo:deviceInfo SymbologyId:kSktScanSymbologyDirectPartMarking Target:self Response:@selector(onGetSymbologyDpm:)];
  691. }
  692. }
  693. /**
  694. * called each time a device disconnect from the host
  695. * @param deviceRemoved contains the device information
  696. */
  697. -(void) onDeviceRemoval:(DeviceInfo*) deviceRemoved{
  698. [self addlog:@"onDeviceRemoval:"];
  699. [self updateDevicesList:deviceRemoved Add:NO];
  700. if(_deviceInfoToTrigger==deviceRemoved){
  701. _deviceInfoToTrigger=nil;
  702. }
  703. #ifdef USE_SOFTSCAN
  704. if(_softScanDeviceInfo==deviceRemoved){
  705. _softScanDeviceInfo=nil;
  706. }
  707. if(_deviceInfoToTrigger==nil)
  708. _deviceInfoToTrigger=_softScanDeviceInfo;
  709. #endif
  710. if(_deviceInfoToTrigger==nil){
  711. // _softScannerTriggerBtn.hidden=YES;
  712. }
  713. }
  714. /**
  715. * called each time ScanAPI is reporting an error
  716. * @param result contains the error code
  717. */
  718. -(void) onError:(SKTRESULT) result{
  719. [self addlog:@"onError:"];
  720. [self addlog:[NSString stringWithFormat:@"ScanAPI is reporting an error: %ld",result]];
  721. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  722. [avc onErrorInfo:[NSString stringWithFormat:@"ScanAPI is reporting an error: %ld",result]];
  723. // _Status.text=[NSString stringWithFormat:@"ScanAPI is reporting an error: %ld",result];
  724. }
  725. /**
  726. * called each time ScanAPI receives decoded data from scanner
  727. * @param deviceInfo contains the device information from which
  728. * the data has been decoded
  729. * @param decodedData contains the decoded data information
  730. */
  731. -(void) onDecodedDataResult:(long)result device:(DeviceInfo *)device decodedData:(ISktScanDecodedData*)decodedData{
  732. [self addlog:@"onDecodedDataResult:"];
  733. //-(void) onDecodedData:(DeviceInfo *)device decodedData:(ISktScanDecodedData*)decodedData{
  734. if(SKTSUCCESS(result)){
  735. NSString *cleanString = [[NSString stringWithUTF8String:(const char *)[decodedData getData]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  736. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  737. [avc onDecodedData:cleanString];
  738. //_Entry.text=[NSString stringWithUTF8String:(const char *)[decodedData getData]];
  739. [self addlog:[NSString stringWithUTF8String:(const char *)[decodedData getData]]];
  740. if(_doAppDataConfirmation==YES){
  741. [ScanApi postSetDataConfirmation:device Target:self Response:@selector(onDataConfirmation:)];
  742. }
  743. }
  744. }
  745. -(void) test_onDecodedDataResult:(NSString*) value
  746. {
  747. [self addlog:@"onDecodedDataResult:"];
  748. //-(void) onDecodedData:(DeviceInfo *)device decodedData:(ISktScanDecodedData*)decodedData{
  749. if(true){
  750. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  751. [avc onDecodedData:value];
  752. //_Entry.text=[NSString stringWithUTF8String:(const char *)[decodedData getData]];
  753. [self addlog:value];
  754. // if(_doAppDataConfirmation==YES){
  755. // [ScanApi postSetDataConfirmation:device Target:self Response:@selector(onDataConfirmation:)];
  756. // }
  757. }
  758. }
  759. -(void) didRotated
  760. {
  761. }
  762. /**
  763. * called when ScanAPI initialization has been completed
  764. * @param result contains the initialization result
  765. */
  766. -(void) onScanApiInitializeComplete:(SKTRESULT) result{
  767. [self addlog:@"onScanApiInitializeComplete:"];
  768. if(SKTSUCCESS(result))
  769. {
  770. self.api_Status = API_START;
  771. NSString* strlog =[NSString stringWithFormat:@"onScanApiInitialize SUCCESS result:%ld",result];
  772. [self addlog:strlog];
  773. #ifdef USE_SOFTSCAN
  774. // make sure we support SoftScan
  775. [ScanApi postSetSoftScanStatus:kSktScanSoftScanSupported Target:self Response:@selector(onSetSoftScanStatus:)];
  776. // check if SoftScan is enabled
  777. [ScanApi postGetSoftScanStatus:self Response:@selector(onGetSoftScanStatus:)];
  778. #else
  779. // disable support SoftScan (Default, not really needed if it was never activated)
  780. [ScanApi postSetSoftScanStatus:kSktScanSoftScanNotSupported Target:self Response:@selector(onSetSoftScanStatus:)];
  781. #endif
  782. // ask for ScanAPI version (not a requirement but always nice to know)
  783. [ScanApi postGetScanApiVersion:self Response:@selector(onGetScanApiVersion:)];
  784. // configure ScanAPI for doing App Data confirmation,
  785. // if TRUE then SingleEntry will confirm the decoded data
  786. if(_doAppDataConfirmation==YES){
  787. [ScanApi postSetConfirmationMode:kSktScanDataConfirmationModeApp Target:self Response:@selector(onSetDataConfirmationMode:)];
  788. }
  789. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  790. [avc onStatusChanged:@"Waiting for scanner..."];
  791. self.Status_Text = @"Waiting for scanner...";
  792. // _Status.text=@"Waiting for scanner...";
  793. }
  794. else{
  795. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  796. [avc onStatusChanged:[NSString stringWithFormat:@"Error initializing ScanAPI:%ld",result]];
  797. self.Status_Text =[NSString stringWithFormat:@"Error initializing ScanAPI:%ld",result];
  798. // _Status.text=[NSString stringWithFormat:@"Error initializing ScanAPI:%ld",result];
  799. NSString* strlog =[NSString stringWithFormat:@"Error initializing ScanAPI:%ld",result];
  800. [self addlog:strlog];
  801. }
  802. }
  803. /**
  804. * called when ScanAPI has been terminated. This will be
  805. * the last message received from ScanAPI
  806. */
  807. -(void) onScanApiTerminated{
  808. [self addlog:@"onScanApiTerminated:"];
  809. [ScanApiConsumer invalidate];
  810. ScanApiConsumer= nil;
  811. [self addlog:@"ScanApiConsumer invalidate"];
  812. self.api_Status = API_STOP;
  813. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  814. [avc onStatusChanged:@"Off"];
  815. self.Status_Text =@"Off";
  816. // _Status.text = @"Off";
  817. }
  818. /**
  819. * called when an error occurs during the retrieval
  820. * of a ScanObject from ScanAPI.
  821. * @param result contains the retrieval error code
  822. */
  823. -(void) onErrorRetrievingScanObject:(SKTRESULT) result{
  824. [self addlog:@"onErrorRetrievingScanObject:"];
  825. [self addlog:[NSString stringWithFormat:@"Error retrieving ScanObject:%ld",result]];
  826. //_Status.text=[NSString stringWithFormat:@"Error retrieving ScanObject:%ld",result];
  827. ActiveViewController* avc=(ActiveViewController*)self.active_controller;
  828. [avc onErrorInfo:[NSString stringWithFormat:@"Error retrieving ScanObject:%ld",result]];
  829. }
  830. @end