设备定时

更新时间:2024-04-23 07:37:15下载pdf

本文介绍设备定时业务,即设置智能设备定时触发执行既定的设置。例如,有一个自动窗帘设备,设置每天晚上 7 点钟关闭、早上 9 点钟打开,那么到达对应的时间点后,设备就会自动执行相关的操作。

定时模型

ThingTimerModel 类

属性 类型 说明
timerId NSString 定时 ID。
date NSString 定时日期。
time NSString 定时时间,时分:12:30
status BOOL 定时状态。
  • 1:开启。
  • 0:关闭。
loops NSString 循环次数,格式为 0000000,每一位数字的取值可以是 0(关闭)和 1(开启)。从左至右依次表示周日、周一、周二、周三、周四、周五、周六。例如,每周一循环该任务,则取值为 01000000000000 表示只执行一次,1111111 表示每天执行。
dps NSDictionary 定时执行的 DP。格式为 {“dpid”:“dp 值”}。
timezoneId NSString 设备时区。
aliasName NSString 定时别名。
isAppPush BOOL 定时执行时是否发送 App 推送。
  • true:发送。
  • false:不发送。

查询定时列表

接口说明

查询定时列表。

- (void)getTimers:(ThingDeviceTimerGetParams *)params success:(void(^)(NSArray<ThingTimerModel *> *timers))success failure:(void(^)(NSError *error))failure;

参数说明

ThingDeviceTimerGetParams

属性 类型 说明
bizType ThingDeviceTimerBizType 业务类型。
  • ThingDeviceTimerBizTypeDevice:设备。
  • ThingDeviceTimerBizTypeGroup:群组。
bizId NSString 业务类型为设备,则是设备 ID。业务类型为群组,则是群组 ID。
category NSString 分类,一般不传。

返回数据说明

数据 说明
success 成功回调。timers:定时信息,请参考 定时模型
failure 失败回调。

调用示例

func getTimerList(success: @escaping ([ThingTimerModel]) -> Void, failure: @escaping (Error) -> Void) {
    let params = ThingDeviceTimerGetParams(bizType: isGroup ? .group : .device, bizId: bizId, category: "")
    self.manager.getTimers(params, success: success, failure: failure)
}

将定时列表数据同步给设备

接口说明

将定时列表数据同步给设备,仅蓝牙设备支持。

- (void)syncTimers:(NSArray<ThingTimerModel *> *)timers toDevice:(NSString *)deviceId success:(void(^)(void))success failure:(void(^)(NSError *error))failure;


在调接口之前,应该先通过 isDeviceCanSync: 接口判断是否支持同步。

- (BOOL)isDeviceCanSync:(NSString *)deviceId;

参数说明

属性 类型 说明
timers NSArray 定时信息列表。请参考 定时模型
success - 成功回调。
failure - 失败回调。

调用示例

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
    self.setUpNavigationItem()

    SVProgressHUD.show()
    self.getTimerList { [weak self] list in
        self?.items = list
        self?.tableView.reloadData()
        SVProgressHUD.dismiss()

        guard let self = self, self.isGroup == false && self.manager.isDeviceCanSync(bizId) else {
            return
        }
        manager.syncTimers(list, toDevice: bizId) {

        } failure: { e in

        }

    } failure: { e in
        SVProgressHUD.dismiss()
    }
}

添加定时

接口说明

添加定时。

- (void)addTimer:(ThingDeviceTimerAddParams *)params success:(void(^)(NSString *timerId))success failure:(void(^)(NSError *error))failure;


参数说明

属性 类型 说明
params ThingDeviceTimerAddParams 添加定时参数。请参考 ThingDeviceTimerAddParams
success - 成功回调。
failure - 失败回调。

ThingDeviceTimerAddParams

属性 类型 说明
bizType ThingDeviceTimerBizType 业务类型。
  • ThingDeviceTimerBizTypeDevice:设备。
  • ThingDeviceTimerBizTypeGroup:群组。
bizId NSString 业务类型为设备,则是设备 ID。业务类型为群组,则是群组 ID。
category NSString 分类,一般不传。
time NSString 定时时间,时分:12:30
status BOOL 定时状态。
  • 1:开启。
  • 0:关闭。
loops NSString 循环次数,格式为 0000000,每一位数字的取值可以是 0(关闭)和 1(开启)。从左至右依次表示周日、周一、周二、周三、周四、周五、周六。例如,每周一循环该任务,则取值为 01000000000000 表示只执行一次,1111111 表示每天执行。
dps NSDictionary 定时执行的 DP。格式为 {“dpid”:“dp 值”}。
aliasName NSString 定时别名。
isAppPush BOOL 定时执行时是否发送 App 推送。
  • true:发送。
  • false:不发送。

调用示例

@objc func add() {
    SVProgressHUD.show()

    var dps: [AnyHashable: Any] = ["1": true]
    let params = ThingDeviceTimerAddParams(bizType: isGroup ? .group : .device, bizId: bizId, category: "", loops: "0000000", time: "12:30", dps: dps, aliasName: "new timer", isAppPush: true, status: true)
    self.manager.addTimer(params) { [weak self] timerId in
        self?.getTimerList(success: { list in
            self?.items = list
            self?.tableView.reloadData()
            SVProgressHUD.dismiss()
        }, failure: { e in
            SVProgressHUD.dismiss()
        })
    } failure: { e in
        SVProgressHUD.dismiss()
    }
}

更新定时

接口说明

更新定时。

- (void)updateTimer:(ThingDeviceTimerUpdateParams *)params success:(void(^)(void))success failure:(void(^)(NSError *error))failure;

参数说明

属性 类型 说明
params ThingDeviceTimerUpdateParams 更新定时参数,请参考 ThingDeviceTimerUpdateParams
success - 成功回调。
failure - 失败回调。

ThingDeviceTimerUpdateParams

属性 类型 说明
timerId NSString 定时 ID。
bizType ThingDeviceTimerBizType 业务类型。
  • ThingDeviceTimerBizTypeDevice:设备。
  • ThingDeviceTimerBizTypeGroup:群组。
bizId NSString 业务类型为设备,则是设备 ID。业务类型为群组,则是群组 ID。
time NSString 定时时间,时分:12:30
status BOOL 定时状态。
  • 1:开启。
  • 0:关闭。
loops NSString 循环次数,格式为 0000000,每一位数字的取值可以是 0(关闭)和 1(开启)。从左至右依次表示周日、周一、周二、周三、周四、周五、周六。例如,每周一循环该任务,则取值为 01000000000000 表示只执行一次,1111111 表示每天执行。
dps NSDictionary 定时执行的 DP。格式为 {“dpid”:“dp 值”}。
aliasName NSString 定时别名。
isAppPush BOOL 定时执行时是否发送 App 推送。
  • true:发送。
  • false:不发送。

调用示例

let params = ThingDeviceTimerUpdateParams(timerId: timer.timerId, bizType: self.isGroup ? .group : .device, bizId: self.bizId, loops: timer.loops, time: timer.time, dps: timer.dps, aliasName: timer.aliasName, isAppPush: !timer.isAppPush, status: !timer.status)

SVProgressHUD.show()

self.manager.updateTimer(params) { [weak self] in
    self?.getTimerList { list in
        self?.items = list
        self?.tableView.reloadData()
        SVProgressHUD.dismiss()
    } failure: { e in
        SVProgressHUD.dismiss()
    }
} failure: { e in
    SVProgressHUD.dismiss()
}

更新定时状态

接口说明

更新定时状态。

- (void)updateTimerStatus:(ThingDeviceTimerStatusUpdateParams *)params success:(void(^)(void))success failure:(void(^)(NSError *error))failure;

参数说明

属性 类型 说明
params ThingDeviceTimerStatusUpdateParams 更新定时状态参数,请参考 ThingDeviceTimerStatusUpdateParams
success - 成功回调。
failure - 失败回调。

ThingDeviceTimerStatusUpdateParams

属性 类型 说明
timerId NSString 定时 ID。
bizType ThingDeviceTimerBizType 业务类型。
  • ThingDeviceTimerBizTypeDevice:设备。
  • ThingDeviceTimerBizTypeGroup:群组。
bizId NSString 业务类型为设备,则是设备 ID。业务类型为群组,则是群组 ID。
time NSString 定时时间,时分:12:30
status BOOL 定时状态。
  • 1:开启。
  • 0:关闭。
loops NSString 循环次数,格式为 0000000,每一位数字的取值可以是 0(关闭)和 1(开启)。从左至右依次表示周日、周一、周二、周三、周四、周五、周六。例如,每周一循环该任务,则取值为 01000000000000 表示只执行一次,1111111 表示每天执行。
dps NSDictionary 定时执行的 DP。格式为 {“dpid”:“dp 值”}。
aliasName NSString 定时别名。
isAppPush BOOL 定时执行时是否发送 App 推送。
  • true:发送。
  • false:不发送。

调用示例

let params = ThingDeviceTimerStatusUpdateParams(timerId: timer.timerId, bizType: self.isGroup ? .group : .device, bizId: self.bizId, loops: timer.loops, time: timer.time, dps: timer.dps, aliasName: timer.aliasName, isAppPush: timer.isAppPush, status: !timer.status)

SVProgressHUD.show()

self.manager.updateTimerStatus(params) { [weak self] in
    self?.getTimerList { list in
        self?.items = list
        self?.tableView.reloadData()
        SVProgressHUD.dismiss()
    } failure: { e in
        SVProgressHUD.dismiss()
    }
} failure: { e in
    SVProgressHUD.dismiss()
}

删除定时

接口说明

删除定时。

- (void)removeTimer:(ThingDeviceTimerRemoveParams *)params success:(void(^)(void))success failure:(void(^)(NSError *error))failure;

参数说明

属性 类型 说明
params ThingDeviceTimerRemoveParams 删除定时参数,请参考 ThingDeviceTimerRemoveParams
success - 成功回调。
failure - 失败回调。

ThingDeviceTimerRemoveParams

属性 类型 说明
timerId NSString 定时 ID。
bizType ThingDeviceTimerBizType 业务类型。
  • ThingDeviceTimerBizTypeDevice:设备。
  • ThingDeviceTimerBizTypeGroup:群组。
bizId NSString 业务类型为设备,则是设备 ID。业务类型为群组,则是群组 ID。

调用示例

let params = ThingDeviceTimerRemoveParams(timerId: timer.timerId, bizType: self.isGroup ? .group : .device, bizId: self.bizId)

SVProgressHUD.show()

self.manager.removeTimer(params) { [weak self] in
    self?.getTimerList { list in
        self?.items = list
        self?.tableView.reloadData()
        SVProgressHUD.dismiss()
    } failure: { e in
        SVProgressHUD.dismiss()
    }
} failure: { e in
    SVProgressHUD.dismiss()
}

完整示例

import UIKit
import ThingDeviceDetailKit
import SVProgressHUD
import ThingSmartDeviceCoreKit

class DeviceDetailKitTimerVC: UITableViewController {

    var bizId: String
    var isGroup: Bool
    var manager: ThingDeviceTimerManager

    var items: [ThingTimerModel] = []

    init(bizId: String, isGroup: Bool) {
        self.bizId = bizId
        self.isGroup = isGroup
        self.manager = ThingDeviceTimerManager()
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setUpNavigationItem() {
        let title = "add"
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: #selector(add))
    }

    @objc func add() {
        SVProgressHUD.show()

        var dps: [AnyHashable: Any] = ["1": true]
        let params = ThingDeviceTimerAddParams(bizType: isGroup ? .group : .device, bizId: bizId, category: "", loops: "0000000", time: "12:30", dps: dps, aliasName: "new timer", isAppPush: true, status: true)
        self.manager.addTimer(params) { [weak self] timerId in
            self?.getTimerList(success: { list in
                self?.items = list
                self?.tableView.reloadData()
                SVProgressHUD.dismiss()
            }, failure: { e in
                SVProgressHUD.dismiss()
            })
        } failure: { e in
            SVProgressHUD.dismiss()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        self.setUpNavigationItem()

        SVProgressHUD.show()
        self.getTimerList { [weak self] list in
            self?.items = list
            self?.tableView.reloadData()
            SVProgressHUD.dismiss()

            guard let self = self, self.isGroup == false && self.manager.isDeviceCanSync(bizId) else {
                return
            }
            manager.syncTimers(list, toDevice: bizId) {

            } failure: { e in

            }

        } failure: { e in
            SVProgressHUD.dismiss()
        }

    }

    func getTimerList(success: @escaping ([ThingTimerModel]) -> Void, failure: @escaping (Error) -> Void) {
        let params = ThingDeviceTimerGetParams(bizType: isGroup ? .group : .device, bizId: bizId, category: "")
        self.manager.getTimers(params, success: success, failure: failure)
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        cell.textLabel?.text = self.items[indexPath.row].time
        cell.detailTextLabel?.text = self.items[indexPath.row].status ? "on" : "off"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        self.handle(index: indexPath.row)
    }

    func handle(index: Int) {
        if (index >= self.items.count) {return}

        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        let timer = self.items[index]

        alert.addAction(UIAlertAction(title: "delete", style: .default, handler: { action in
            let params = ThingDeviceTimerRemoveParams(timerId: timer.timerId, bizType: self.isGroup ? .group : .device, bizId: self.bizId)

            SVProgressHUD.show()

            self.manager.removeTimer(params) { [weak self] in
                self?.getTimerList { list in
                    self?.items = list
                    self?.tableView.reloadData()
                    SVProgressHUD.dismiss()
                } failure: { e in
                    SVProgressHUD.dismiss()
                }
            } failure: { e in
                SVProgressHUD.dismiss()
            }
        }))

        alert.addAction(UIAlertAction(title: "update", style: .default, handler: { action in
            let params = ThingDeviceTimerUpdateParams(timerId: timer.timerId, bizType: self.isGroup ? .group : .device, bizId: self.bizId, loops: timer.loops, time: timer.time, dps: timer.dps, aliasName: timer.aliasName, isAppPush: !timer.isAppPush, status: !timer.status)

            SVProgressHUD.show()

            self.manager.updateTimer(params) { [weak self] in
                self?.getTimerList { list in
                    self?.items = list
                    self?.tableView.reloadData()
                    SVProgressHUD.dismiss()
                } failure: { e in
                    SVProgressHUD.dismiss()
                }
            } failure: { e in
                SVProgressHUD.dismiss()
            }
        }))

        alert.addAction(UIAlertAction(title: "update status", style: .default, handler: { action in
            let params = ThingDeviceTimerStatusUpdateParams(timerId: timer.timerId, bizType: self.isGroup ? .group : .device, bizId: self.bizId, loops: timer.loops, time: timer.time, dps: timer.dps, aliasName: timer.aliasName, isAppPush: timer.isAppPush, status: !timer.status)

            SVProgressHUD.show()

            self.manager.updateTimerStatus(params) { [weak self] in
                self?.getTimerList { list in
                    self?.items = list
                    self?.tableView.reloadData()
                    SVProgressHUD.dismiss()
                } failure: { e in
                    SVProgressHUD.dismiss()
                }
            } failure: { e in
                SVProgressHUD.dismiss()
            }
        }))

        alert.addAction(UIAlertAction(title: "cancel", style: .cancel))
        self.present(alert, animated: true)
    }
}

错误码

错误码 说明
-10001 获取设备信息失败。
-10002 设备离线。
-10003 获取定时信息失败。
-10004 蓝牙数据传输超时。
-10005 蓝牙数据解析失败。
-10006 设备不支持同步。