Device Service

Last Updated on : 2024-05-28 04:15:05download

IMQTTService

The MQTT service protocol defines the basic operations of the MQTT service.

Methods

  • connect(): Connect to the MQTT service.
  • disconnect(): Disconnect from the MQTT service.
  • subscribe(_ topic: String, success: (()->Void)? , failure: ((Error)->Void)?) : Subscribe to the specified topic.
  • unsubscribe(_ topic: String, success: (()->Void)? , failure: ((Error)->Void)?) : Unsubscribe from the specified topic.

Description

connect()

Connect to the MQTT service. This method has no parameters.

disconnect()

Disconnect from the MQTT service. This method has no parameters.

Subscribe to a topic

Parameters

Parameter Type Required Description
topic String Yes The topic to subscribe to.
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

MQTTService.shared.subscribe("topic", success: {
    print("Success")
}, failure: { error in
    print("Failure: \(error.localizedDescription)")
})

Unsubscribe from a topic

Parameters

Parameter Type Required Description
topic String Yes The topic to unsubscribe from.
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

MQTTService.shared.unsubscribe("topic", success: {
    print("Success")
}, failure: { error in
    print("Failure: \(error.localizedDescription)")
})

Device initialization

Parameters

Parameter Type Required Description
deviceId NSString Yes The ID of the target device.

Device object

Parameter Type Description
deviceId String The device ID.
uuid String The UUID of the device.
name String The name of the device.
icon String The device icon.
productId String The product ID.
category String The device category.
timezoneId String The ID of the device’s time zone.
isCloudOnline Bool Indicates whether the device is online over the internet.
isLocalOnline Bool Indicates whether the device is online over the local network.
isOnline Bool Indicates whether the device is online.
latitude String The latitude of the device.
longitude String The longitude of the device.
dps [String : Any] The data point (DP).
schemas [String : IndustryDeviceKit.DpSchema] The DP schema.
deviceType IndustryDeviceKit.DeviceType The type of the device.
localKey String The device’s local key.
pv Double The version of the device protocol.
wifiBackup IndustryDeviceKit.IDeviceBackupNet The alternative Wi-Fi network.
ota IndustryDeviceKit.IDeviceOTA The device OTA update.
delegate IndustryDeviceKit.IDeviceDelegate The delegate of the device.

Example

let device = DeviceService.shared.device(withDeviceId: "123456")

Get device details

Load the device object by device ID.

Parameters

Parameter Type Required Description
deviceId String Yes The ID of the device to be loaded.
success ((IndustryDeviceKit.IDevice) -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

DeviceService.shared.load("123456", success: { device in
    // The callback invoked when the device is loaded.
}, failure: { error in
    // The callback invoked when the device failed to load.
})

Rename device

Parameters

Parameter Type Required Description
deviceId String Yes The ID of the target device.
newName String Yes The new name of the device.
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

DeviceService.shared.rename("123456", newName: "New Device Name", success: {
    // The callback invoked when the device is renamed.
}, failure: { error in
    // The callback invoked when the device failed to rename.
})

Remove device

Parameters

Parameter Type Required Description
deviceId String Yes The ID of the target device.
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

DeviceService.shared.remove("123456", success: {
    // The callback invoked when the device is deleted.
}, failure: { error in
    // The callback invoked when the device failed to delete.
})

Factory reset

Parameters

Parameter Type Required Description
deviceId String Yes The device ID.
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

// Call resetFactory.
DeviceService.shared.resetFactory("your_device_id", success: {
    print("Factory reset succeeded")
}) { (error) in
    print("Factory reset failed: \(error.localizedDescription)")
}

IDeviceDelegate

Listen for device status.

Optional methods

deviceInfoUpdated

Device information update callback

@objc optional func deviceInfoUpdated(device: IndustryDeviceKit.IDevice)
Parameter Type Description
device IndustryDeviceKit.IDevice The device object.

deviceDpsUpdated

Device data update callback

@objc optional func deviceDpsUpdated(device: IndustryDeviceKit.IDevice)
Parameter Type Description
device IndustryDeviceKit.IDevice The device object.

deviceRemoved

Device removal callback

@objc optional func deviceRemoved(device: IndustryDeviceKit.IDevice)
Parameter Type Description
device IndustryDeviceKit.IDevice The device object.

device(_:signalStrength:)

Device signal strength callback

@objc optional func device(_ device: IndustryDeviceKit.IDevice, signalStrength: String)
Parameter Type Description
device IndustryDeviceKit.IDevice The device object.
signalStrength String The signal strength of the device.

device(_:otaStatusChanged:)

OTA status update callback

@objc optional func device(_ device: IndustryDeviceKit.IDevice, otaStatusChanged: String)
Parameter Type Description
device IndustryDeviceKit.IDevice The device object.
otaStatusChanged String The OTA status change.

OTA update

Method

Get the update information

// Call checkFirmwareUpgrade
industryOTAService.checkFirmwareUpgrade(success: { (firmwareUpgradeModels) in
    if firmwareUpgradeModels.count > 0 {
        print("Firmware update available:")
        for firmwareUpgradeModel in firmwareUpgradeModels {
            print("- \(firmwareUpgradeModel.version)")
        }
    } else {
        print("No firmware update.")
    }
}) { (error) in
    print("Check for firmware updates failed: \(error.localizedDescription)")
}
Parameter Type Description
success (([IndustryDeviceKit.IFirmwareUpgradeModel]) -> Void)? The success callback, returning the update information.
failure ((Error) -> Void)? The failure callback.

Start update

industryOTAService.startFirmwareUpgrade(firmwares)
Parameter Type Description
firmwares [IndustryDeviceKit.IFirmwareUpgradeModel] The update information.

Continue update

industryOTAService.confirmWarningUpgradeTask(true)
Parameter Type Description
isContinue Bool Indicates whether to continue the update task.

Cancel update

// Define the success and failure callbacks.
let onSuccess = {
    print("The firmware update was canceled.")
}

let onFailure = { (error: Error) in
    print("The firmware update failed to cancel: \(error.localizedDescription)")
}

// Call cancelFirmwareUpgrade
industryOTAService.cancelFirmwareUpgrade(success: onSuccess, failure: onFailure)
Parameter Type Description
success (() -> Void)? The success callback.
failure ((Error) -> Void)? The failure callback.

Get update status

// Define the success and failure callbacks.
let onSuccess = { (statusModel: IndustryDeviceKit.IFirmwareUpgradeStatusModel) in
    print("Firmware update status: \(statusModel.status)")
}

let onFailure = { (error: Error) in
    print("Failed to get firmware update status: \(error.localizedDescription)")
}

// Call getFirmwareUpgradingStatus
industryOTAService.getFirmwareUpgradingStatus(success: onSuccess, failure: onFailure)
Parameter Type Description
success ((IndustryDeviceKit.IFirmwareUpgradeStatusModel) -> Void)? The success callback, returning the update status.
failure ((Error) -> Void)? The failure callback.

Get the device’s local firmware information

// Define the success and failure callbacks.
let onSuccess = { (firmwareModels: [IndustryDeviceKit.IFirmwareUpgradeModel]) in
    for firmwareModel in firmwareModels {
        print("Firmware version: \(firmwareModel.version), firmware path: \(firmwareModel.path)")
    }
}

let onFailure = { (error: Error) in
    print("Failed to get the local firmware information: \(error.localizedDescription)")
}

// Call getDeviceLocalFirmwareInfo
industryOTAService.getDeviceLocalFirmwareInfo(success: onSuccess, failure: onFailure)
Parameter Type Description
success (([IndustryDeviceKit.IFirmwareUpgradeModel]) -> Void)? The success callback, returning the local firmware version.
failure ((Error) -> Void)? The failure callback.

Device control

Parameters

Parameter Type Required Description
dps IndustryDeviceKit.DpCommand Yes The DP command.
success (() -> Void)? No The success callback.
failure ((Error) -> Void)? No The failure callback.

Example

// Define DP commands.
let dps = IndustryDeviceKit.DpCommand()

// Define the success and failure callbacks.
let onSuccess = {
    print("The DP command was sent successfully")
}

let onFailure = { (error: Error) in
    print("Failed to send the DP command: \(error.localizedDescription)")
}

// Call publish
device.publish(dps: dps, success: onSuccess, failure: onFailure)