Device Service

Last Updated on : 2024-12-25 08:30:00download

IMQTTService

The MQTT operation tool can implement functions such as subscribing to and publishing MQTT topics.

Subscribe to Topic

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)")
})

Unsubscribe from Topic

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)")
})

Publish a Message to a Specified Topic

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)")
})

Register Message Listener

Parameter Description

Parameter Type Required Description
listener IMQTTListener Yes Message listener

Example

// Receive message callback through IMQTTListener
MQTTService.shared.register(listener)

Unregister Message Listener

Parameter Description

Parameter Type Required Description
listener IMQTTListener Yes Message listener

Example

MQTTService.shared.unregister(listener)

Bluetooth Operations

Connect to Offline Device

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
}

Disconnect Device

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
}

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 : 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")

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 ((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: IDevice)
Parameter Type Description
device IDevice The device object.

deviceDpsUpdated

Device data update callback

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

deviceRemoved

Device removal callback

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

device(_:signalStrength:)

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.

device(_:otaStatusChanged:)

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.

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 (([IFirmwareUpgradeModel]) -> Void)? The success callback, returning the update information.
failure ((Error) -> Void)? The failure callback.

Start update

industryOTAService.startFirmwareUpgrade(firmwares)
Parameter Type Description
firmwares [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: 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.

Get the device’s local firmware information

// 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.

Device control

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)