Guest User

Untitled

a guest
May 23rd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.59 KB | None | 0 0
  1. //
  2. // GAGlobal.m
  3. // iControl
  4. //
  5. // Created by Jose Antonio Reyna Fuentes on 07/11/08.
  6. // Copyright 2008 Grupo Anfa. All rights reserved.
  7. //
  8.  
  9. #import "GAGlobal.h"
  10. #import <CommonCrypto/CommonDigest.h>
  11.  
  12. @implementation GAGlobal
  13.  
  14. @synthesize userSettings;
  15.  
  16. -(id) init {
  17. if ((self = [super init])) {
  18. // Do initialization
  19. NSLog(@"INICIANDO GLOBAL");
  20. socket = [Socket socket];
  21. [self fillSettings];
  22. }
  23. return self;
  24. }
  25.  
  26.  
  27. - (void)fillSettings{
  28. NSFileManager *fileManager = [NSFileManager defaultManager];
  29.  
  30. NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
  31. BOOL existe = [fileManager fileExistsAtPath:path];
  32.  
  33. if (!existe){
  34. NSLog(@"No existe Directorio");
  35. mkdir([[NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()] UTF8String], 0755);
  36. }else{
  37. NSLog(@"Existe Directorio");
  38. NSLog(path);
  39. }
  40.  
  41. NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
  42. existe = [fileManager fileExistsAtPath:filePath];
  43. //NSLog(@"FilePath: %@",filePath);
  44.  
  45. NSString *stdFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Config.plist"];
  46. //NSLog(stdFilePath);
  47.  
  48. NSMutableDictionary *stdSettings = [[NSMutableDictionary alloc] initWithContentsOfFile:stdFilePath];
  49. //NSDictionary *settings = nil;
  50.  
  51. if (!existe){
  52. NSLog(@"No existe Archivo Config");
  53. //mkdir([[NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()] UTF8String], 0755);
  54. [stdSettings writeToFile:filePath atomically:YES];
  55. settings = stdSettings;
  56. }else{
  57. NSLog(@"Existe Archivo Config");
  58. //NSLog(filePath);
  59. settings = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
  60. }
  61.  
  62. [self checkUserSettings];
  63. }
  64.  
  65. //verifica que todas las configuraciones esten disponibles
  66. - (void)checkSettings{
  67. NSLog(@"Checando Configuraciones");
  68.  
  69.  
  70. NSString *strServerIP = [settings objectForKey:@"SERVER-IP"];
  71. NSString *strServerPort = [settings objectForKey:@"SERVER-PORT"];
  72. NSString *strRemoteIP = [settings objectForKey:@"REMOTE-IP"];
  73. NSString *strRemotePort = [settings objectForKey:@"REMOTE-PORT"];
  74. NSString *strVideoURL = [settings objectForKey:@"VIDEO-URL"];
  75. NSString *strVideoPort = [settings objectForKey:@"VIDEO-PORT"];
  76. NSString *strVideoUser = [settings objectForKey:@"VIDEO-USER"];
  77. NSString *strVideoPass = [settings objectForKey:@"VIDEO-PASS"];
  78.  
  79. if (strServerIP == nil || strServerPort == nil || strRemoteIP == nil || strRemotePort==nil || strVideoURL == nil || strVideoPort == nil || strVideoUser == nil || strVideoPass==nil){
  80. NSLog(@"Faltan Settings en Archivo");
  81. UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Algunas de las configuraciones hacen falta, para el buen funcionamiento de la aplicacion, rellene los campos faltantes en la parte de Configuracion" delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
  82. //UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
  83. //[myTextField setBackgroundColor:[UIColor whiteColor]];
  84. //[myAlertView addSubview:testTextField];
  85. [myAlertView show];
  86. [myAlertView release];
  87. }
  88. //*/
  89. }
  90.  
  91. - (void) checkUserSettings{
  92. userSettings = [NSUserDefaults standardUserDefaults];
  93.  
  94. if (userSettings){
  95. NSString *strServerIP = [userSettings stringForKey:@"SERVER-IP"];
  96. int strServerPort = [userSettings integerForKey:@"SERVER-PORT"];
  97. NSString *strRemoteIP = [userSettings stringForKey:@"REMOTE-IP"];
  98. int strRemotePort = [userSettings integerForKey:@"REMOTE-PORT"];
  99. NSString *strVideoURL = [userSettings stringForKey:@"VIDEO-URL"];
  100. int strVideoPort = [userSettings integerForKey:@"VIDEO-PORT"];
  101. NSString *strVideoUser = [userSettings stringForKey:@"VIDEO-USER"];
  102. NSString *strVideoPass = [userSettings stringForKey:@"VIDEO-PASS"];
  103. NSString *supportPass = [userSettings stringForKey:@"SUPPORT-PASS"];
  104.  
  105. if (strServerIP == nil || strServerPort == 0 || strRemoteIP == nil || strRemotePort==0 || strVideoURL == nil || strVideoPort == 0 || strVideoUser == nil || strVideoPass==nil || supportPass==nil){
  106. NSLog(@"Faltan Settings en los NSUserDefaults");
  107. /*
  108. [userSettings setObject:[settings objectForKey:@"SERVER-IP"] forKey:@"SERVER-IP"];
  109. [userSettings setValue:[settings valueForKey:@"SERVER-PORT"] forKey:@"SERVER-PORT"];
  110. [userSettings setObject:[settings objectForKey:@"REMOTE-IP"] forKey:@"REMOTE-IP"];
  111. [userSettings setValue:[settings valueForKey:@"REMOTE-PORT"] forKey:@"REMOTE-PORT"];
  112. [userSettings setObject:[settings objectForKey:@"VIDEO-URL"] forKey:@"VIDEO-URL"];
  113. [userSettings setValue:[settings valueForKey:@"VIDEO-PORT"] forKey:@"VIDEO-PORT"];
  114. [userSettings setObject:[settings objectForKey:@"VIDEO-USER"] forKey:@"VIDEO-USER"];
  115. [userSettings setObject:[settings objectForKey:@"VIDEO-PASS"] forKey:@"VIDEO-PASS"];
  116. [userSettings setObject:[settings objectForKey:@"SUPPORT-PASS"] forKey:@"SUPPORT-PASS"];
  117. */
  118. }
  119. }
  120. }
  121.  
  122. - (NSString *)getStringSetting:(NSString *)key{
  123. NSString *setting = [userSettings stringForKey:key];
  124. return setting;
  125. }
  126.  
  127. - (BOOL)getDemoMode{
  128. BOOL setting = [[settings valueForKey:@"DEMO-MODE"] boolValue];
  129. return setting;
  130. }
  131.  
  132. - (BOOL) getBoolSetting:(NSString *)key{
  133. BOOL setting = [userSettings boolForKey:key];
  134. return setting;
  135. }
  136.  
  137. - (int) getIntegerSetting:(NSString *)key{
  138. int setting = [userSettings integerForKey:key];
  139. return setting;
  140. }
  141.  
  142. - (void)setValue:(id)value forKey:(NSString *)key{
  143. NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
  144. NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
  145. NSLog(@"Inicio de guardar valor %@,%@",value,key);
  146. [settings setValue:value forKey:key];
  147. NSLog(@"escribir %@",filePath);
  148. [settings writeToFile:filePath atomically:YES];
  149. NSLog(@"Fin Guardar");
  150. }
  151.  
  152. - (void)setBool:(int)value forKey:(NSString *)key{
  153. NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
  154. NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
  155. NSLog(@"Inicio de guardar valor %d,%@",value,key);
  156. [settings setValue:[NSNumber numberWithInt:value] forKey:key];
  157. //NSLog(@"escribir %@",filePath);
  158. [settings writeToFile:filePath atomically:YES];//*/
  159. NSLog(@"Fin Guardar");
  160. }
  161.  
  162. - (void) sendString:(NSString *)cadena{
  163. BOOL demoMode = [self getDemoMode];
  164. NSLog(@"Demo: %d",demoMode);
  165.  
  166. NSString *host = [self getStringSetting:@"SERVER-IP"] ;
  167. //host = [host cStringUsingEncoding:NSASCIIStringEncoding];
  168. unsigned short port = [self getIntegerSetting:@"SERVER-PORT"];
  169.  
  170. if (!demoMode){
  171. @try
  172. {
  173. [socket connectToHostName:host port:port];
  174. NSLog(@"Connected");
  175. }
  176. @catch (NSException *exception)
  177. {
  178. NSLog ([exception reason]);
  179. NSLog (@"Couldn't connect to %@ port %u.", host, port);
  180. UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Hubo un error al intentar conectarse al servidor" delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
  181. //UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
  182. //[myTextField setBackgroundColor:[UIColor whiteColor]];
  183. //[myAlertView addSubview:testTextField];
  184. [myAlertView show];
  185. [myAlertView release];
  186. }
  187. }
  188. }
  189.  
  190. /*
  191. - (void)sendString:(NSString *)cadena
  192. {
  193. //NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
  194. BOOL demoMode = [self getBoolSetting:@"DEMO-MODE"];
  195. NSLog(@"Demo: %d",demoMode);
  196.  
  197.  
  198.  
  199.  
  200. if (!demoMode){
  201. @try{
  202. //[socket connectToHostName:[self getStringSetting:@"SERVER-IP"] port:[self getIntegerSetting:@"SERVER-PORT"]];
  203. //[socket connectToHostName:@"192.168.1.65" port:20000];
  204. Socket *socket = [Socket socket];
  205. NSString *ipsocket = @"192.168.1.65";//[self getStringSetting:@"SERVER-IP"];
  206. unsigned short portsocket = [self getIntegerSetting:@"SERVER-PORT"];
  207. NSLog(@"ipstring: [%@]",ipsocket);
  208. [socket connectToHostName:ipsocket port:portsocket];
  209. [socket writeString:cadena];
  210. NSLog(@"Envio la cadena %@\n",cadena);
  211. NSMutableData *response = [NSMutableData data];
  212. while ([socket readData:response] <= 0) {
  213. //each read appends to data,
  214. //returns number of bytes read or 0 on EOF
  215. }
  216.  
  217. [[NSFileHandle fileHandleWithStandardOutput] writeData:response];
  218. NSString *respuesta = [[NSString alloc] initWithData:response encoding:4];
  219. NSLog(@"Recibo respuesta: %@\n", respuesta);
  220.  
  221. //[socket release];
  222. //[ipsocket release];
  223. }
  224. @catch (NSException *exeption){
  225. //NSLog(@"Error al intentar la conexion [%@]:%@",ipsocket,portsocket);
  226. UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Hubo un error al intentar conectarse al servidor" delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
  227. //UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
  228. //[myTextField setBackgroundColor:[UIColor whiteColor]];
  229. //[myAlertView addSubview:testTextField];
  230. [myAlertView show];
  231. [myAlertView release];
  232. }
  233. }
  234.  
  235. }
  236. //*/
  237.  
  238. - (NSString *) MD5fromString:(NSString *)string{
  239. NSString *md5 = @"";
  240.  
  241. const char *cStr = [string UTF8String];
  242. unsigned char result[CC_MD5_DIGEST_LENGTH];
  243. CC_MD5( cStr, strlen(cStr), result );
  244. md5 = [NSString stringWithFormat:
  245. @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  246. result[0], result[1], result[2], result[3], result[4],
  247. result[5], result[6], result[7],
  248. result[8], result[9], result[10], result[11], result[12],
  249. result[13], result[14], result[15]
  250. ];
  251.  
  252. return md5;
  253. }
  254.  
  255. - (void)dealloc{
  256. [socket release];
  257. [super dealloc];
  258. }
  259.  
  260. @end
Add Comment
Please, Sign In to add comment