| 1 | // |
|---|
| 2 | // SFCardDB.m |
|---|
| 3 | // SuicaExample |
|---|
| 4 | // |
|---|
| 5 | // Created by GNUE(鵺) on 08/04/11. |
|---|
| 6 | // Copyright 2008 __MyCompanyName__. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "PasoriKit/suica.h" |
|---|
| 10 | #import "SFCardDB.h" |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | NSString * const kParamAreaCode = @"AreaCode"; |
|---|
| 14 | NSString * const kParamLineCode = @"LineCode"; |
|---|
| 15 | NSString * const kParamStationCode = @"StationCode"; |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | static SFCardDB * sharedSFCardDB = nil; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | @implementation SFCardDB |
|---|
| 22 | |
|---|
| 23 | + (SFCardDB*)sharedSFCardDB |
|---|
| 24 | { |
|---|
| 25 | @synchronized(self) { |
|---|
| 26 | if (sharedSFCardDB == nil) { |
|---|
| 27 | [[self alloc] init]; // ここでは代入していない |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | return sharedSFCardDB; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | + (id)allocWithZone:(NSZone *)zone |
|---|
| 34 | { |
|---|
| 35 | @synchronized(self) { |
|---|
| 36 | if (sharedSFCardDB == nil) { |
|---|
| 37 | sharedSFCardDB = [super allocWithZone:zone]; |
|---|
| 38 | return sharedSFCardDB; // 最初の割り当てで代入し、返す |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | return nil; // 以降の割り当てではnilを返すようにする |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | - (id)copyWithZone:(NSZone *)zone |
|---|
| 45 | { |
|---|
| 46 | return self; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | - (id)retain |
|---|
| 50 | { |
|---|
| 51 | return self; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | - (NSUInteger)retainCount |
|---|
| 55 | { |
|---|
| 56 | return UINT_MAX; // 解放できないオブジェクトであることを示す |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | - (void)release |
|---|
| 60 | { |
|---|
| 61 | // 何もしない |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | - (id)autorelease |
|---|
| 65 | { |
|---|
| 66 | return self; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | #pragma mark - |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /// SOAP を使って駅名を取得する |
|---|
| 74 | - (NSDictionary *)getStationNameWithAreaCode:(int)areaCode lineCode:(int)lineCode stationCode:(int)stationCode |
|---|
| 75 | { |
|---|
| 76 | NSURL * url = [NSURL URLWithString:@"http://www.denno.net/SFCardFan/soapserver.php"]; |
|---|
| 77 | NSString * method = @"getStationName"; |
|---|
| 78 | NSString * namespace = @"http://www.denno.net/SFCardFan/sfcardfandb.wsdl"; |
|---|
| 79 | |
|---|
| 80 | // SOAP request params |
|---|
| 81 | NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: |
|---|
| 82 | [NSNumber numberWithUnsignedShort:areaCode], kParamAreaCode, |
|---|
| 83 | [NSNumber numberWithUnsignedShort:lineCode], kParamLineCode, |
|---|
| 84 | [NSNumber numberWithUnsignedShort:stationCode], kParamStationCode, |
|---|
| 85 | nil]; |
|---|
| 86 | |
|---|
| 87 | NSArray *paramOrder = [NSArray arrayWithObjects:kParamAreaCode, kParamLineCode, kParamStationCode, nil]; |
|---|
| 88 | |
|---|
| 89 | // SOAP request http headers -- some SOAP server impls require even empty SOAPAction headers |
|---|
| 90 | NSDictionary *reqHeaders = [NSDictionary dictionaryWithObject:@"SFCardFanDbGetAction" forKey:@"SOAPAction"]; |
|---|
| 91 | |
|---|
| 92 | // create SOAP request |
|---|
| 93 | WSMethodInvocationRef soapReq = WSMethodInvocationCreate((CFURLRef)url, |
|---|
| 94 | (CFStringRef)method, |
|---|
| 95 | kWSSOAP2001Protocol); |
|---|
| 96 | |
|---|
| 97 | // set SOAP params |
|---|
| 98 | WSMethodInvocationSetParameters(soapReq, (CFDictionaryRef)params, (CFArrayRef)paramOrder); |
|---|
| 99 | |
|---|
| 100 | // set method namespace |
|---|
| 101 | WSMethodInvocationSetProperty(soapReq, kWSSOAPMethodNamespaceURI, (CFStringRef)namespace); |
|---|
| 102 | |
|---|
| 103 | // Add HTTP headers (with SOAPAction header) -- some SOAP impls require even empty SOAPAction headers |
|---|
| 104 | WSMethodInvocationSetProperty(soapReq, kWSHTTPExtraHeaders, (CFDictionaryRef)reqHeaders); |
|---|
| 105 | |
|---|
| 106 | // invoke SOAP request |
|---|
| 107 | NSDictionary *result = (NSDictionary *)WSMethodInvocationInvoke(soapReq); |
|---|
| 108 | CFRelease(soapReq); |
|---|
| 109 | if (! result) return nil; |
|---|
| 110 | |
|---|
| 111 | NSMutableDictionary * station = [NSMutableDictionary dictionary]; |
|---|
| 112 | NSArray * resultSet = [[[result objectForKey:@"/Result"] objectForKey:@"ResultSet"] objectForKey:@"item"]; |
|---|
| 113 | NSEnumerator * enumerator = [resultSet objectEnumerator]; |
|---|
| 114 | NSDictionary * item; |
|---|
| 115 | |
|---|
| 116 | // 結果を転記 |
|---|
| 117 | while (item = [enumerator nextObject]) |
|---|
| 118 | { |
|---|
| 119 | [station setObject:[item objectForKey:@"value"] forKey:[item objectForKey:@"key"]]; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | [result release]; |
|---|
| 123 | |
|---|
| 124 | return station; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | /// SFCard Fan DB Service から駅名を取得する |
|---|
| 129 | - (NSDictionary *)getStationNameWithStation:(uint8_t *)station regionCode:(int)region |
|---|
| 130 | { |
|---|
| 131 | int areaCode; |
|---|
| 132 | |
|---|
| 133 | #if 0 |
|---|
| 134 | // 古い形式(現在は使われていない) |
|---|
| 135 | areaCode = suica_areacode(region, station[0]); |
|---|
| 136 | if (areaCode < 0) return nil; |
|---|
| 137 | #else |
|---|
| 138 | // 新しい形式(リージョンをそのまま地区コードとして使用) |
|---|
| 139 | areaCode = region; |
|---|
| 140 | #endif |
|---|
| 141 | |
|---|
| 142 | return [self getStationNameWithAreaCode:areaCode lineCode:station[0] stationCode:station[1]]; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | @end |
|---|