summaryrefslogtreecommitdiff
path: root/Example Apps/Example ObjC/RemoteControlManager.m
blob: d30cb9ad778ce908046a65d8a8c9b67647722973 (plain)
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
//
//  RemoteControlManager.m
//  SmartDeviceLink-Example-ObjC
//
//  Created by Beharry, Justin (J.S.) on 8/1/22.
//  Copyright © 2022 smartdevicelink. All rights reserved.
//

#import <SmartDeviceLink/SDLButtonPress.h>
#import <SmartDeviceLink/SDLClimateControlCapabilities.h>
#import <SmartDeviceLink/SDLClimateControlData.h>
#import <SmartDeviceLink/SDLGetInteriorVehicleData.h>
#import <SmartDeviceLink/SDLGetInteriorVehicleDataConsent.h>
#import <SmartDeviceLink/SDLGetInteriorVehicleDataResponse.h>
#import <SmartDeviceLink/SDLLogMacros.h>
#import <SmartDeviceLink/SDLManager.h>
#import <SmartDeviceLink/SDLModuleData.h>
#import <SmartDeviceLink/SDLOnInteriorVehicleData.h>
#import <SmartDeviceLink/SDLRPCResponse.h>
#import <SmartDeviceLink/SDLRemoteControlCapabilities.h>
#import <SmartDeviceLink/SDLScreenManager.h>
#import <SmartDeviceLink/SDLSetInteriorVehicleData.h>
#import <SmartDeviceLink/SDLSoftButtonObject.h>
#import <SmartDeviceLink/SDLSystemCapability.h>
#import <SmartDeviceLink/SDLSystemCapabilityManager.h>
#import <SmartDeviceLink/SDLTemperature.h>
#import "AlertManager.h"
#import "RemoteControlManager.h"

@interface RemoteControlManager()

@property (strong, nonatomic) SDLManager *sdlManager;
@property (strong, nonatomic) SDLRemoteControlCapabilities *remoteControlCapabilities;
@property (strong, nonatomic) NSString *climateModuleId;
@property (strong, nonatomic) NSNumber<SDLBool> *hasConsent;
@property (strong, nonatomic) SDLClimateControlData *climateData;
@property (copy, nonatomic, readwrite) NSString *climateDataString;

@end

@implementation RemoteControlManager

- (instancetype)initWithManager:(SDLManager *)manager {
    self = [super init];
    if (!self) {
        return nil;
    }
    _sdlManager = manager;
    return self;
}

- (void)start {
    [self.sdlManager.systemCapabilityManager subscribeToCapabilityType:SDLSystemCapabilityTypeRemoteControl withUpdateHandler:^(SDLSystemCapability * _Nullable capability, BOOL subscribed, NSError * _Nullable error) {
        if (!capability.remoteControlCapability) {
            SDLLogE(@"SDL errored getting remote control module information: %@", error);
            return;
        }

        self->_remoteControlCapabilities = capability.remoteControlCapability;
        self->_climateModuleId = self.remoteControlCapabilities.climateControlCapabilities.firstObject.moduleInfo.moduleId;

        /// Get consent to control modules
        SDLGetInteriorVehicleDataConsent *getInteriorVehicleDataConsent = [[SDLGetInteriorVehicleDataConsent alloc] initWithModuleType:SDLModuleTypeClimate moduleIds:@[self.climateModuleId]];
        [self.sdlManager sendRequest:getInteriorVehicleDataConsent withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
            if (!response.success) {
                SDLLogE(@"SDL errored getting interior vehicle data consent: %@", error);
                return;
            }
            self->_hasConsent = response.success;

            /// Initialize climate data and setup subscription
            if (self.hasConsent) {
                [self sdlex_initializeClimateData];
                [self sdlex_subscribeVehicleData];
                [self sdlex_subscribeClimateControlData];
            }
        }];
    }];
}

- (void)showClimateControl {
    if (self.climateModuleId == NULL && self.hasConsent) {
        NSString *errorMessage = @"The climate module id was not set or consent was not given";
        [AlertManager sendAlertWithManager:self.sdlManager image:nil textField1:errorMessage textField2:nil];
    }
    self.sdlManager.screenManager.softButtonObjects = [self sdlex_getButtons];
}

- (NSString *)climateDataString {
    return [NSString stringWithFormat:@"AC Enable %@\n"
            "AC Max Enabled %@\n"
            "Auto Mode Enable %@\n"
            "Circulate Air Enable %@\n"
            "Climate Enable %@\n"
            "Current Temperature %@\n"
            "Defrost Zone %@\n"
            "Desired Temperature %@\n"
            "Dual Mode Enable %@\n"
            "Heated Mirrors Enable %@\n"
            "Heated Rears Window Enable %@\n"
            "Heated Steering Enable %@\n"
            "Heated Windshield Enable %@\n"
            "Ventilation %@\n",
            self.climateData.acEnable.boolValue ? @"On" : @"Off",
            self.climateData.acMaxEnable.boolValue ? @"On" : @"Off",
            self.climateData.autoModeEnable.boolValue ? @"On" : @"Off",
            self.climateData.circulateAirEnable.boolValue ? @"On" : @"Off",
            self.climateData.climateEnable.boolValue ? @"On" : @"Off",
            self.climateData.currentTemperature.description,
            self.climateData.defrostZone.description,
            self.climateData.desiredTemperature.description,
            self.climateData.dualModeEnable.boolValue ? @"On" : @"Off",
            self.climateData.heatedMirrorsEnable.boolValue ? @"On" : @"Off",
            self.climateData.heatedRearWindowEnable.boolValue ? @"On" : @"Off",
            self.climateData.heatedSteeringWheelEnable.boolValue ? @"On" : @"Off",
            self.climateData.heatedWindshieldEnable.boolValue ? @"On" : @"Off",
            self.climateData.ventilationMode.description
    ];
}

- (void)sdlex_initializeClimateData {
    if (self.climateModuleId == NULL && self.hasConsent == 0) {
        NSString *errorMessage = @"The climate module id was not set or consent was not given";
        [AlertManager sendAlertWithManager:self.sdlManager image:nil textField1:errorMessage textField2:nil];
    }
    
    SDLGetInteriorVehicleData *getInteriorVehicleData = [[SDLGetInteriorVehicleData alloc] initWithModuleType:SDLModuleTypeClimate moduleId:self.climateModuleId];
    [self.sdlManager sendRequest:getInteriorVehicleData withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        SDLGetInteriorVehicleDataResponse *dataResponse = (SDLGetInteriorVehicleDataResponse *)response;
        self->_climateData = dataResponse.moduleData.climateControlData;
    }];
}

- (void)sdlex_subscribeVehicleData {
    [self.sdlManager subscribeToRPC:SDLDidReceiveInteriorVehicleDataNotification withBlock:^(__kindof SDLRPCMessage * _Nonnull message) {
        SDLOnInteriorVehicleData *onInteriorVehicleData = (SDLOnInteriorVehicleData *)message;
        if (onInteriorVehicleData == nil) {
            SDLLogE(@"SDL onInteriorVehicleData was set to NULL when trying to subscribe to vehicle data");
            return;
        }
        self.climateData = onInteriorVehicleData.moduleData.climateControlData;
    }];
}

- (void)sdlex_subscribeClimateControlData {
    SDLGetInteriorVehicleData *getInteriorVehicleData = [[SDLGetInteriorVehicleData alloc] initAndSubscribeToModuleType:SDLModuleTypeClimate moduleId:self.climateModuleId];
    [self.sdlManager sendRequest:getInteriorVehicleData withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        if (error != nil) {
            SDLLogE(@"SDL errored trying to subscribe to climate data: %@", error);
        }
        SDLLogD(@"SDL Subscribing to Climate Control Data. Data should appear in SDLDidReceiveInteriorVehicleDataNotification");
    }];
}

- (void)sdlex_turnOnAC {
    SDLClimateControlData *climateControlData = [[SDLClimateControlData alloc] initWithDictionary:@{ @"acEnable": @YES }];
    SDLModuleData *moduleData = [[SDLModuleData alloc] initWithClimateControlData:climateControlData];
    SDLSetInteriorVehicleData *setInteriorVehicleData = [[SDLSetInteriorVehicleData alloc] initWithModuleData:moduleData];

    [self.sdlManager sendRequest:setInteriorVehicleData withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        if(!response.success) {
            SDLLogE(@"SDL errored trying to set interior vehicle data: %@", error);
            return;
        }
    }];
}

- (void)sdlex_turnOffAC {
    SDLClimateControlData *climateControlData = [[SDLClimateControlData alloc] initWithDictionary:@{ @"acEnable": @NO }];
    SDLModuleData *moduleData = [[SDLModuleData alloc] initWithClimateControlData:climateControlData];
    SDLSetInteriorVehicleData *setInteriorVehicleData = [[SDLSetInteriorVehicleData alloc] initWithModuleData:moduleData];

    [self.sdlManager sendRequest:setInteriorVehicleData withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        if(!response.success) {
            SDLLogE(@"SDL errored trying to set interior vehicle data: %@", error);
            return;
        }
    }];
}

- (void)sdlex_setClimate {
    SDLTemperature *temperature = [[SDLTemperature alloc] initWithFahrenheitValue:73];
    NSDictionary<NSString *, id> *climateDictionary = @{@"acEnable": @YES, @"fanSpeed": @100, @"desiredTemperature": temperature, @"ventilationMode": SDLVentilationModeBoth };

    SDLClimateControlData *climateControlData = [[SDLClimateControlData alloc] initWithDictionary:climateDictionary];
    SDLModuleData *moduleData = [[SDLModuleData alloc] initWithClimateControlData:climateControlData];
    SDLSetInteriorVehicleData *setInteriorVehicleData = [[SDLSetInteriorVehicleData alloc] initWithModuleData:moduleData];

    [self.sdlManager sendRequest:setInteriorVehicleData withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        if(!response.success) {
            SDLLogE(@"SDL errored trying to set interior vehicle data: %@", error);
            return;
        }
    }];
}

- (NSArray*)sdlex_getButtons {
    SDLSoftButtonObject *acOnButton = [[SDLSoftButtonObject alloc] initWithName:@"AC On" text:@"AC On" artwork:nil handler:^(SDLOnButtonPress * _Nullable buttonPress, SDLOnButtonEvent * _Nullable buttonEvent) {
        if (buttonPress == nil) { return; }
        [self sdlex_turnOnAC];
    }];

    SDLSoftButtonObject *acOffButton = [[SDLSoftButtonObject alloc] initWithName:@"AC Off" text:@"AC Off" artwork:nil handler:^(SDLOnButtonPress * _Nullable buttonPress, SDLOnButtonEvent * _Nullable buttonEvent) {
        if (buttonPress == nil) { return; }
        [self sdlex_turnOffAC];
    }];

    SDLSoftButtonObject *acMaxToggle = [[SDLSoftButtonObject alloc] initWithName:@"AC Max" text:@"AC Max" artwork:nil handler:^(SDLOnButtonPress * _Nullable buttonPress, SDLOnButtonEvent * _Nullable buttonEvent) {
        if (buttonPress == nil) { return; }

        SDLButtonPress *buttonTouch = [[SDLButtonPress alloc] initWithButtonName:SDLButtonNameACMax moduleType:SDLModuleTypeClimate moduleId:self.climateModuleId buttonPressMode:SDLButtonPressModeShort];

        [self.sdlManager sendRequest:buttonTouch withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
            if(!response.success) {
                SDLLogE(@"SDL errored handling remote button: %@", error);
                return;
            }
        }];
    }];

    SDLSoftButtonObject *temperatureDecreaseButton = [[SDLSoftButtonObject alloc] initWithName:@"Temperature Decrease" text:@"Temperature -" artwork:nil handler:^(SDLOnButtonPress * _Nullable buttonPress, SDLOnButtonEvent * _Nullable buttonEvent) {
        if (buttonPress == nil) { return; }

        SDLButtonPress *buttonTouch = [[SDLButtonPress alloc] initWithButtonName:SDLButtonNameTempDown moduleType:SDLModuleTypeClimate moduleId:self.climateModuleId buttonPressMode:SDLButtonPressModeShort];

        [self.sdlManager sendRequest:buttonTouch withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
            if(!response.success) {
                SDLLogE(@"SDL errored handling remote button: %@", error);
                return;
            }
        }];
    }];
    
    SDLSoftButtonObject *temperatureIncreaseButton = [[SDLSoftButtonObject alloc] initWithName:@"Temperature Increase" text:@"Temperature +" artwork:nil handler:^(SDLOnButtonPress * _Nullable buttonPress, SDLOnButtonEvent * _Nullable buttonEvent) {
        if (buttonPress == nil) { return; }

        SDLButtonPress *buttonTouch = [[SDLButtonPress alloc] initWithButtonName:SDLButtonNameTempUp moduleType:SDLModuleTypeClimate moduleId:self.climateModuleId buttonPressMode:SDLButtonPressModeShort];

        [self.sdlManager sendRequest:buttonTouch withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
            if(!response.success) {
                SDLLogE(@"SDL errored handling remote button: %@", error);
                return;
            }
        }];
    }];
    
    SDLSoftButtonObject *setClimateButton = [[SDLSoftButtonObject alloc] initWithName:@"Set Climate" text:@"Set Climate" artwork:nil handler:^(SDLOnButtonPress * _Nullable buttonPress, SDLOnButtonEvent * _Nullable buttonEvent) {
        if (buttonPress == nil) { return; }
        [self sdlex_setClimate];
    }];

    return @[acOnButton, acOffButton, acMaxToggle, temperatureDecreaseButton, temperatureIncreaseButton, setClimateButton];
}

@end