Skip to content

Device Control

Feature Description

The device control feature provides device control, connection, and disconnection of device channels.

It supports the following channel connections:

  1. Cloud channel (WebSocket)
  2. Wi-Fi LAN channel
  3. BLE channel

When a device connects to the platform, the WebSocket channel will be established by default.

When connecting to a device in the default mode, the connection priority is: cloud > Wi-Fi > BLE.

See QuecDeviceModel Definition in Device Management.

Device Control

Initialize Device

Initialize the device control class according to the device ID.

API

Only after you call the interface for obtaining the device list can the device initialization be successful. If the QuecDeviceClient object is required in the business layer, you also need to call the initialization interface.

objc
/// Returns the QuecDevice instance. If the current user does not have this device, a value of nil is returned.
/// @param deviceId deviceModel.deviceId
+ (nullable instancetype)deviceWithId:(NSString *)deviceId;

Parameter

ParameterRequiredDescription
deviceIdYProperty in QuecDeviceModel. Custom format: pk@dk. Example, "p00001@AABBCCDD0001".

Example

objc
QuecDeviceClient *device = [QuecDeviceClient deviceWithId:deviceModel.deviceId];
device.delegate = self;
/// Default auto mode
[device connect];

Listen to Device Status

After QuecDeviceClientDelegate protocol is implemented, you need to handle updates such as refreshing the online/offline status of devices and updating UI elements.

Example

objc
/// Device information updates, such as the name and online status.
/// @param device The device instance.
- (void)deviceInfoUpdate:(QuecDeviceClient *)device;

/// Device online status updates
/// @param device device instance.
/// @param onlineState  device channel online state (0: all offline, 1: websocket online, 2 : wifi online, 3: wifi + websocket online, 4: ble online, 5: websocket + ble online, 6: ble + wifi online , 7: wifi + ble + ws online)
- (void)device:(QuecDeviceClient *)device onlineUpdate:(NSUInteger)onlineState;

/// Device connecting state updates
/// @param device device instance.
/// @param connectingState  device channel online state (0: all offline, 1: websocket online, 2 : wifi online, 3: wifi + websocket online, 4: ble online, 5: websocket + ble online, 6: ble + wifi online , 7: wifi + ble + ws online)
- (void)device:(QuecDeviceClient *)device connectingStateUpdate:(NSUInteger)connectingState;

/// Indicates whether device is removed.
/// @param device device instance.
- (void)deviceRemoved:(QuecDeviceClient *)device;

/// The DP updates.
/// @param device device instance.
/// @param dps  command dictionary.
- (void)device:(QuecDeviceClient *)device dpsUpdate:(QuecIotDataPointsModel *)dps;

QuecIotDataPointsModel Definition

FieldTypeDescription
decodeTypeQuecIotDataDecodeTypeUse QuecIotDataDecodeTypeDecodeDPSCode by default.
typeQuecIotChannelTypeChannel type.
actionQuecIotDataPointActionAction type.
pkNSStringProductKey.
dkNSStringDeviceKey.
dpsNSArray<QuecIotDataPoint*> *DPS data.
originDataidOriginal channel data.
deviceIdNSStringDevice ID.
transparentDataNSDataTransparent transmission data.
resetDataNSDictionaryDevice reset data.

QuecIotDataDecodeType Enumeration Definition

objc
typedef NS_ENUM(NSUInteger, QuecIotDataDecodeType) {
    QuecIotDataDecodeTypeDecodeDPSCode,
    QuecIotDataDecodeTypeDecodeDPSId,
    QuecIotDataDecodeTypeDecodeNONE,
};

Connect and Disconnect Device Channel

API

Connect or disconnect the device channel. You need to obtain the current QuecDeviceClient object first.

objc
- (void)connect;
- (void)connectWithMode:(QuecIotChannelMode)mode;
- (void)disconnect;
- (void)disconnectWithType:(QuecIotChannelType)type;

QuecIotChannelMode Enumeration Definition

objc
typedef NS_ENUM(NSUInteger, QuecIotChannelMode)
{
    QuecIotChannelModeAuto = 0 ,
    QuecIotChannelModeWS,
    QuecIotChannelModeWifi,
    QuecIotChannelModeBle,
};

QuecIotChannelType Enumeration Definition

objc
typedef NS_ENUM(NSUInteger, QuecIotChannelType)
{
    QuecIotChannelTypeUnknown = 0,
    QuecIotChannelTypeWS = 1,
    QuecIotChannelTypeWifi,
    QuecIotChannelTypeBle,
};

Delete Device and Reset Channel Status

API

Before deleting the device and resetting channel status, you need to first obtain the current QuecDeviceClient object. The following reset interface includes both unbinding from the platform and resetting the device channel status. It is recommended to use the following method when deleting a device. The unbinding interface in QuecDeviceService only removes the binding relationship.

Delete Device

API

objc
- (void)remove:(nullable QuecVoidBlock)success failure:(QuecErrorBlock)failure;

Parameter

ParameterRequiredDescription
successNCallback function of successful request.
failureNCallback function of failed request.

Example

objc
QuecDeviceClient *device = [QuecDeviceClient deviceWithId:@"device id"];
[device remove:^{
    /// Next Action
} failure:^(NSError *error) {
    NSLog(@"check error: %@", error);
}];

Delete Bluetooth-only Device

API

objc
- (void)removeBtWithIsInit:(BOOL)isInit
                     random:(nullable NSString *)random
          resetCredentials:(nullable NSString *)resetCredentials
                    success:(nullable QuecVoidBlock)success
                    failure:(QuecErrorBlock)failure;

Parameter

ParameterRequiredDescription
isInitNWhether to reset the device. Default value: False.
randomNRandom flag returned by the device. Required when isInit = True.
resetCredentialsNReset credentials returned by the device. Required when isInit = True.
successNCallback function of successful request.
failureNCallback function of failed request.

Example

objc
QuecDeviceClient *device = [QuecDeviceClient deviceWithId:@"device id"];
[device removeBtWithIsInit:NO random:@"" resetCredentials:@"" success:^{
    /// Next Action
} failure:^(NSError *error) {
    NSLog(@"check error: %@", error);
}];

Delete Devices in Batches

API

objc
+ (void)batchRemoveWithFid:(nullable NSString *)fid
                     isInit:(BOOL)isInit
                 deviceList:(NSArray<QuecDeviceModel *> *)deviceList
                    success:(nullable QuecVoidBlock)success
                   failure:(QuecErrorBlock)failure;

Parameter

ParameterRequiredDescription
fidNHome ID. Required when home mode is enabled.
isInitNWhether to reset the device. Default value: False.
deviceListYQuecDeviceModel array of the devices to be deleted.
successNCallback function of successful request.
failureNCallback function of failed request.

Example

objc
QuecDeviceModel *deviceModel1 = QuecDeviceModel.new;
QuecDeviceModel *deviceModel2 = QuecDeviceModel.new;
[QuecDeviceClient batchRemoveWithFid:@"" isInit:NO deviceList:@[deviceModel1, deviceModel2] success:^{
    /// Next Action
} failure:^(NSError *error) {
    NSLog(@"check error: %@", error);
}];

Issue Device Data

API

Send device data to the device by assembling DPS data through the following interface.

objc
- (void)writeDps:(NSArray<QuecIotDataPoint*> *)dps
         success:(nullable QuecVoidBlock)success
         failure:(nullable QuecErrorBlock)failure;

- (void)writeDps:(NSArray<QuecIotDataPoint*> *)dps
            mode:(QuecIotChannelMode)mode
         success:(nullable QuecVoidBlock)success
         failure:(nullable QuecErrorBlock)failure;

- (void)writeDps:(NSArray<QuecIotDataPoint*> *)dps
       extraData:(nullable QuecIotChannelExtraData *)extraData
         success:(nullable QuecVoidBlock)success
         failure:(nullable QuecErrorBlock)failure;

- (void)writeDps:(NSArray<QuecIotDataPoint*> *)dps
       extraData:(nullable QuecIotChannelExtraData *)extraData
            mode:(QuecIotChannelMode)mode
         success:(nullable QuecVoidBlock)success
         failure:(nullable QuecErrorBlock)failure;

Parameter

QuecIotDataPoint Definition

FieldTypeDescription
IdintFeature ID.
dataTypeQuecIotDataPointDataTypeData type.
codeNSStringCode.
valueidValue corresponding to dataType.
stringValueNSStringHigh-precision values for float & double types will be written to this field.
minNSStringMinimum value (no need to handle on reception).
maxNSStringMaximum value (no need to handle on reception).

QuecIotChannelExtraData Definition

FieldTypeDescription
cacheTimelongCommand cache time, valid for WebSocket channel.
msgIdlong longMessage ID, valid for WebSocket channel.
isCoverBOOLWhether to overwrite the data sent.
isCacheBOOLWhether to cache data. Default: NO. This parameter is determined by cacheTime.
sendTTLVBOOLSend data in TTLV format. Default: true. (Only reading/writing properties is supported, and cacheTime can be used.)
productKeyNSStringDevice ProductKey (used by the panel to send messages to sub-devices)
deviceKeyNSStringDeviceKey (used by the panel to send messages to sub-devices)
transparentDataNSDataTransparent transmission data.

Example

objc
QuecDeviceClient *device = [QuecDeviceClient deviceWithId:@"device id"];
QuecIotDataPoint *dp = QuecIotDataPoint.new;
dp.value = @(3);
dp.dataType = QuecIotDataPointDataTypeINT;
dp.Id = 2; /// tsl id
[device writeDps:@[dp] mode:QuecIotChannelModeAuto success:^{
    /// Next Action
} failure:^(NSError * _Nonnull error) {
    NSLog(@"check error: %@", error);
}];

Read Device Data

API

Read device data by assembling DPS data.

objc
- (void)readDps:(NSArray<QuecIotDataPoint*> *)dps
        success:(nullable QuecVoidBlock)success
        failure:(nullable QuecErrorBlock)failure;

- (void)readDps:(NSArray<QuecIotDataPoint*> *)dps
           mode:(QuecIotChannelMode)mode
        success:(nullable QuecVoidBlock)success
        failure:(nullable QuecErrorBlock)failure;

- (void)readDps:(NSArray<QuecIotDataPoint*> *)dps
      extraData:(nullable QuecIotChannelExtraData *)extraData
        success:(nullable QuecVoidBlock)success
        failure:(nullable QuecErrorBlock)failure;

- (void)readDps:(NSArray<QuecIotDataPoint*> *)dps
      extraData:(nullable QuecIotChannelExtraData *)extraData
           mode:(QuecIotChannelMode)mode
        success:(nullable QuecVoidBlock)success
        failure:(nullable QuecErrorBlock)failure;

Parameter

See QuecIotDataPoint, QuecIotChannelExtraData, and QuecIotChannelMode Definitions above.

Update the UI through the DPS method of the QuecDeviceClient protocol after reading the device data.

objc
- (void)device:(QuecDeviceClient *)device dpsUpdate:(QuecIotDataPointsModel *)dps;

Example

objc
QuecDeviceClient *device = [QuecDeviceClient deviceWithId:@"device id"];
QuecIotDataPoint *dp = QuecIotDataPoint.new;
dp.dataType = QuecIotDataPointDataTypeINT;
dp.Id = 2; /// tsl id
[device readDps:@[dp] mode:QuecIotChannelModeAuto success:^{
    /// Next Action
} failure:^(NSError * _Nonnull error) {
    NSLog(@"check error: %@", error);
}];

Send Transparent Transmission Data

API

Send transparent transmission data. Note that transparent transmission data can only be sent via the Bluetooth channel.

objc
- (void)writeTransparent:(NSData *)transparentData
                 success:(nullable QuecVoidBlock)success
                 failure:(nullable QuecErrorBlock)failure;

Parameter

ParameterRequiredDescription
transparentDataYBinary data.
successNCallback function of successful request.
failureNCallback function of failed request.
objc
QuecDeviceClient *device = [QuecDeviceClient deviceWithId:@"device id"];
NSData *sendData = [[NSData alloc] initWithBase64EncodedString:@"1234567890" options:0];
[device writeTransparent:sendData success:^{
    /// Next Action
} failure:^(NSError * _Nonnull error) {
    NSLog(@"check error: %@", error);
}];

Control Devices in Batches

Control Devices in Batches on Platform

API

Control devices in batches on the platform.

objc
- (void)sendDataToDevicesByHttpWithData:(NSString *)data
                              deviceList:(NSArray<QuecBatchControlDeviceModel *> *)deviceList
                                    type:(NSInteger)type
                              extraData:(nullable QuecBatchControlExtraModel *)extraData
                                 success:(void(^)(QuecBatchControlModel *model))success
                                 failure:(QuecErrorBlock)failure;

Parameter

ParameterRequiredDescription
dataYJSON string in TSL format.
deviceListYDevice list.
typeYType.
1: Transparent transmission
2: Property
3: Service
extraDataNdataFormat type. This parameter must be specified when type = 1. dataFormat is passed within extraData.
1: Hex
2: Text
successNCallback function of successful request.
failureNCallback function of failed request.

QuecBatchControlDeviceModel Definition

FieldTypeDescription
productKeyNSStringProductKey.
deviceKeyNSStringDeviceKey.
gatewayDeviceKeyNSStringGateway DeviceKey.
gatewayProductKeyNSStringGateway ProductKey.

QuecBatchControlExtraModel Definition

FieldTypeDescription
cacheTimeNSIntegerCache time in seconds, range: 1-7776000. This parameter must be set when isCache is enabled. Optional parameter.
dataFormatNSIntegerData type. This parameter must be specified when type = 1. Optional parameter.
1: Hex
2: Text
isCacheNSIntegerWhether to enable caching. Optional parameter.
1 - Enable
2 - Disable (default)
isCoverNSIntegerWhether to overwrite previously sent identical commands. This parameter must be set when isCache is enabled. Optional parameter.
1 - Overwrite
2 - Do not overwrite (default)
qosNSIntegerQoS level. Range: 0 or 1. Default value: 1. Optional parameter.

QuecBatchControlModel Definition

FieldTypeDescription
successListNSArray<QuecBatchControlResultModel *> *List of successful executions.
failureListNSArray<QuecBatchControlResultModel *> *List of failed executions.

QuecBatchControlResultModel Definition

FieldTypeDescription
dataQuecBatchControlDeviceModelControl device information.
ticketNSStringticket (Only valid for successList).
msgNSStringError message (Only valid for failureList).

QuecBatchControlDeviceModel Definition

FieldTypeDescription
productKeyNSStringProductKey.
deviceKeyNSStringDeviceKey.
gatewayDeviceKeyNSStringGateway DeviceKey.
gatewayProductKeyNSStringGateway ProductKey.

Example

objc
QuecBatchControlExtraModel *extraModel = QuecBatchControlExtraModel.new;
extraModel.dataFormat = 1;
[QuecDeviceControlService.sharedInstance sendDataToDevicesByHttpWithData:@"[{\"read_write_float\":\"4.4\"}]"
                                                           deviceList:deviceList
                                                                 type:2
                                                            extraData:extraModel
                                                              success:^(QuecBatchControlModel * _Nonnull model) {
        /// Next Action
    } failure:^(NSError *error) {
        NSLog(@"check error: %@", error);
    }];