1212#import " DPSpheroManager.h"
1313#import " DPSpheroDeviceRepeatExecutor.h"
1414
15- // LEDは色を変えられる
16- static NSString *const SpheroLED = @" 1" ;
17- static NSString *const SpheroLEDName = @" Sphero LED" ;
18- // Calibrationは色を変えられない
19- static NSString *const SpheroCalibration = @" 2" ;
20- static NSString *const SpheroCalibrationName = @" Sphero CalibrationLED" ;
2115
2216@implementation DPSpheroLightProfile {
2317 DPSpheroDeviceRepeatExecutor *_flashingExecutor;
@@ -41,7 +35,12 @@ - (id)init
4135 [response setErrorToEmptyServiceId ];
4236 return YES ;
4337 }
44-
38+ NSArray *arr = [serviceId componentsSeparatedByString: @" _" ];
39+ NSString *lightId = nil ;
40+ if (arr.count == 2 ) {
41+ lightId = arr[1 ];
42+ serviceId = arr[0 ];
43+ }
4544 // 接続確認
4645 CONNECT_CHECK ();
4746
@@ -50,20 +49,22 @@ - (id)init
5049 DConnectMessage *calibration = [DConnectMessage new ];
5150
5251 [response setResult: DConnectMessageResultTypeOk];
53-
54- // 全体の色を変えるためのID
55- [DConnectLightProfile setLightId: SpheroLED target: led];
56- [DConnectLightProfile setLightName: SpheroLEDName target: led];
57- [DConnectLightProfile setLightOn: [DPSpheroManager sharedManager ].isLEDOn target: led];
58- [DConnectLightProfile setLightConfig: @" " target: led];
59- [lights addMessage: led];
60-
61- // CalibrationのライトをつけるためのID(ON/OFFのみ)
62- [DConnectLightProfile setLightId: SpheroCalibration target: calibration];
63- [DConnectLightProfile setLightName: SpheroCalibrationName target: calibration];
64- [DConnectLightProfile setLightOn: [DPSpheroManager sharedManager ].calibrationLightBright>0 target: calibration];
65- [DConnectLightProfile setLightConfig: @" " target: calibration];
66- [lights addMessage: calibration];
52+ if (!lightId || (lightId && [lightId isEqualToString: kDPSpheroLED ])) {
53+ // 全体の色を変えるためのID
54+ [DConnectLightProfile setLightId: kDPSpheroLED target: led];
55+ [DConnectLightProfile setLightName: kDPSpheroLEDName target: led];
56+ [DConnectLightProfile setLightOn: [DPSpheroManager sharedManager ].isLEDOn target: led];
57+ [DConnectLightProfile setLightConfig: @" " target: led];
58+ [lights addMessage: led];
59+ }
60+ if (!lightId || (lightId && [lightId isEqualToString: kDPSpheroCalibration ])) {
61+ // CalibrationのライトをつけるためのID(ON/OFFのみ)
62+ [DConnectLightProfile setLightId: kDPSpheroCalibration target: calibration];
63+ [DConnectLightProfile setLightName: kDPSpheroCalibrationName target: calibration];
64+ [DConnectLightProfile setLightOn: [DPSpheroManager sharedManager ].calibrationLightBright>0 target: calibration];
65+ [DConnectLightProfile setLightConfig: @" " target: calibration];
66+ [lights addMessage: calibration];
67+ }
6768
6869 [DConnectLightProfile setLights: lights target: response];
6970
@@ -81,7 +82,11 @@ - (id)init
8182 NSNumber *brightness = [DConnectLightProfile brightnessFromRequest: request];
8283 NSString *color = [DConnectLightProfile colorFromRequest: request];
8384 NSArray *flashing = [DConnectLightProfile parsePattern: [DConnectLightProfile flashingFromRequest: request] isId: NO ];
84-
85+ NSArray *arr = [serviceId componentsSeparatedByString: @" _" ];
86+ if (arr.count == 2 ) {
87+ lightId = arr[1 ];
88+ serviceId = arr[0 ];
89+ }
8590 return [weakSelf postLightRequest: request response: response serviceId: serviceId lightId: lightId brightness: brightness color: color flashing: flashing];
8691 }];
8792
@@ -106,17 +111,16 @@ - (id)init
106111 [response setErrorToInvalidRequestParameter ];
107112 return YES ;
108113 }
109-
110- if (!serviceId) {
111- [response setErrorToEmptyServiceId ];
112- return YES ;
113- }
114-
114+
115115 if (name == nil || [name isEqualToString: @" " ]) {
116116 [response setErrorToInvalidRequestParameterWithMessage: @" name is invalid." ];
117117 return YES ;
118118 }
119-
119+ NSArray *arr = [serviceId componentsSeparatedByString: @" _" ];
120+ if (arr.count == 2 ) {
121+ serviceId = arr[0 ];
122+ lightId = arr[1 ];
123+ }
120124 return [weakSelf postLightRequest: request response: response serviceId: serviceId lightId: lightId brightness: brightness color: color flashing: flashing];
121125 }];
122126
@@ -133,19 +137,24 @@ - (id)init
133137 [response setErrorToEmptyServiceId ];
134138 return YES ;
135139 }
136-
140+ NSArray *arr = [serviceId componentsSeparatedByString: @" _" ];
141+ if (arr.count == 2 ) {
142+ serviceId = arr[0 ];
143+ lightId = arr[1 ];
144+ }
145+
137146 // lightIdが省略された場合には、SpherorLEBを使用する
138147 if (!lightId) {
139- lightId = SpheroLED ;
148+ lightId = kDPSpheroLED ;
140149 }
141150
142151 // 接続確認
143152 CONNECT_CHECK ();
144153
145- if ([lightId isEqualToString: SpheroCalibration ]) {
154+ if ([lightId isEqualToString: kDPSpheroCalibration ]) {
146155 // キャリブレーションライト消灯。
147156 [[DPSpheroManager sharedManager ] setCalibrationLightBright: 0 serviceId: serviceId];
148- } else if ([lightId isEqualToString: SpheroLED ]) {
157+ } else if ([lightId isEqualToString: kDPSpheroLED ]) {
149158 // LED消灯
150159 [[DPSpheroManager sharedManager ] setLEDLightColor: [UIColor blackColor ] serviceId: serviceId];
151160 } else {
@@ -188,15 +197,15 @@ -(BOOL) postLightRequest:(DConnectRequestMessage *)request
188197
189198 // lightIdが省略された場合には、SpherorLEDを使用する
190199 if (!lightId) {
191- lightId = SpheroLED ;
200+ lightId = kDPSpheroLED ;
192201 }
193202
194203 // 接続確認
195204 CONNECT_CHECK ();
196205
197206 // lightId確認
198- if (![lightId isEqualToString: SpheroCalibration ] &&
199- ![lightId isEqualToString: SpheroLED ]) {
207+ if (![lightId isEqualToString: kDPSpheroCalibration ] &&
208+ ![lightId isEqualToString: kDPSpheroLED ]) {
200209 [response setErrorToInvalidRequestParameterWithMessage: @" lightId is Invalid." ];
201210 return YES ;
202211 }
@@ -209,7 +218,7 @@ -(BOOL) postLightRequest:(DConnectRequestMessage *)request
209218 return YES ;
210219 }
211220
212- if ([lightId isEqualToString: SpheroCalibration ]) {
221+ if ([lightId isEqualToString: kDPSpheroCalibration ]) {
213222 // キャリブレーションライト点灯。 colorは変えられない。点灯、消灯のみ
214223 if (flashing.count > 0 ) {
215224 // 点滅
@@ -222,7 +231,7 @@ -(BOOL) postLightRequest:(DConnectRequestMessage *)request
222231 // 点灯
223232 [[DPSpheroManager sharedManager ] setCalibrationLightBright: [brightness doubleValue ] serviceId: serviceId];
224233 }
225- } else if ([lightId isEqualToString: SpheroLED ]) {
234+ } else if ([lightId isEqualToString: kDPSpheroLED ]) {
226235 // LED点灯
227236 UIColor *ledColor;
228237 if (color) {
0 commit comments