Last Updated on : 2024-12-25 08:30:00download
The MQTT operation tool can implement functions such as subscribing to and publishing MQTT topics.
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| topic | String | Yes | The topic to subscribe to |
| success | (() -> Void)? | No | Callback function for successful subscription |
| failure | ((Error) -> Void)? | No | Callback function for subscription failure |
Example
MQTTService.shared.subscribe("topic", success: {
print("Subscription successful")
}, failure: { error in
print("Subscription failed: \(error.localizedDescription)")
})
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| topic | String | Yes | The topic to unsubscribe from |
| success | (() -> Void)? | No | Callback function for successful unsubscription |
| failure | ((Error) -> Void)? | No | Callback function for unsubscription failure |
Example
MQTTService.shared.unsubscribe("topic", success: {
print("Unsubscription successful")
}, failure: { error in
print("Unsubscription failed: \(error.localizedDescription)")
})
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| topic | String | Yes | The topic to publish to |
| data | String | Yes | The content to publish |
| success | (() -> Void)? | No | Success callback |
| failure | ((Error) -> Void)? | No | Failure callback |
Example
MQTTService.shared.publish(topic, data: data, success: {
print("Data published successfully!")
}, failure: { error in
print("Failed to publish data: \(error.localizedDescription)")
})
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| listener | IMQTTListener | Yes | Message listener |
Example
// Receive message callback through IMQTTListener
MQTTService.shared.register(listener)
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| listener | IMQTTListener | Yes | Message listener |
Example
MQTTService.shared.unregister(listener)
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| success | (() -> Void)? | No | Success callback |
| failure | ((Error) -> Void)? | No | Failure callback |
Example
let iDevice = IndustryDevice(deviceId: device?.deviceId ?? "")
iDevice.connectBLE {
// Connection successful
} failure: {
// Connection failed
}
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| success | (() -> Void)? | No | Success callback |
| failure | ((Error) -> Void)? | No | Failure callback |
Example
let iDevice = IndustryDevice(deviceId: device?.deviceId ?? "")
iDevice.disconnectBLE {
// Disconnection successful
} failure: {
// Disconnection failed
}
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 : DpSchema] | The DP schema. |
| deviceType | DeviceType | The type of the device. |
| localKey | String | The device’s local key. |
| pv | Double | The version of the device protocol. |
| wifiBackup | IDeviceBackupNet | The alternative Wi-Fi network. |
| ota | IDeviceOTA | The device OTA update. |
| delegate | IDeviceDelegate | The delegate of the device. |
Example
let device = DeviceService.shared.device(withDeviceId: "123456")
Load the device object by device ID.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| deviceId | String | Yes | The ID of the device to be loaded. |
| success | ((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.
})
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.
})
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.
})
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)")
}
Listen for device status.
Device information update callback
@objc optional func deviceInfoUpdated(device: IDevice)
| Parameter | Type | Description |
|---|---|---|
| device | IDevice | The device object. |
Device data update callback
@objc optional func deviceDpsUpdated(device: IDevice)
| Parameter | Type | Description |
|---|---|---|
| device | IDevice | The device object. |
Device removal callback
@objc optional func deviceRemoved(device: IDevice)
| Parameter | Type | Description |
|---|---|---|
| device | IDevice | The device object. |
Device signal strength callback
@objc optional func device(_ device: IDevice, signalStrength: String)
| Parameter | Type | Description |
|---|---|---|
| device | IDevice | The device object. |
| signalStrength | String | The signal strength of the device. |
OTA status update callback
@objc optional func device(_ device: IDevice, otaStatusChanged: String)
| Parameter | Type | Description |
|---|---|---|
| device | IDevice | The device object. |
| otaStatusChanged | String | The OTA status change. |
// 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 | (([IFirmwareUpgradeModel]) -> Void)? | The success callback, returning the update information. |
| failure | ((Error) -> Void)? | The failure callback. |
industryOTAService.startFirmwareUpgrade(firmwares)
| Parameter | Type | Description |
|---|---|---|
| firmwares | [IFirmwareUpgradeModel] | The update information. |
industryOTAService.confirmWarningUpgradeTask(true)
| Parameter | Type | Description |
|---|---|---|
| isContinue | Bool | Indicates whether to continue the update task. |
// 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. |
// Define the success and failure callbacks.
let onSuccess = { (statusModel: 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 | ((IFirmwareUpgradeStatusModel) -> Void)? | The success callback, returning the update status. |
| failure | ((Error) -> Void)? | The failure callback. |
// Define the success and failure callbacks.
let onSuccess = { (firmwareModels: [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 | (([IFirmwareUpgradeModel]) -> Void)? | The success callback, returning the local firmware version. |
| failure | ((Error) -> Void)? | The failure callback. |
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
dps |
DpCommand |
Yes | The DP command. |
success |
(() -> Void)? |
No | The success callback. |
failure |
((Error) -> Void)? |
No | The failure callback. |
Example
// Define DP commands.
let dps = 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)
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback