From 5507c7249150784f3c96a561af43b8469898ebf2 Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 8 Jan 2024 17:30:29 +0800 Subject: [PATCH] feat:send interval --- HDFwear.xcodeproj/project.pbxproj | 4 ++++ HDFwear/Tools/BluetoothManager+Function.swift | 7 +++++++ HDFwear/Tools/BluetoothManager.swift | 3 +++ HDFwear/Tools/TaskManager.swift | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 0 deletions(-) create mode 100644 HDFwear/Tools/TaskManager.swift diff --git a/HDFwear.xcodeproj/project.pbxproj b/HDFwear.xcodeproj/project.pbxproj index 4485a5a..1540b41 100644 --- a/HDFwear.xcodeproj/project.pbxproj +++ b/HDFwear.xcodeproj/project.pbxproj @@ -281,6 +281,7 @@ 6CFEEBC22773354500621863 /* HRReferenceVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFEEBC12773354500621863 /* HRReferenceVC.swift */; }; 6CFEEBC42773355700621863 /* SleepReferenceVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFEEBC32773355700621863 /* SleepReferenceVC.swift */; }; 844959782B48EF900029E2E0 /* BluetoothManager+Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844959772B48EF900029E2E0 /* BluetoothManager+Deprecated.swift */; }; + 8473FB622B4BF1A200409148 /* TaskManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8473FB612B4BF1A200409148 /* TaskManager.swift */; }; 847482762B03793C0004F0C2 /* NewWeatherModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847482752B03793C0004F0C2 /* NewWeatherModel.swift */; }; 847672B82B074E43007DC2DE /* NewBeiDouContactModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847672B72B074E43007DC2DE /* NewBeiDouContactModel.swift */; }; B212F1FD2A14CE0400781D59 /* LaunchSetting.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B212F1FF2A14CE0400781D59 /* LaunchSetting.storyboard */; }; @@ -656,6 +657,7 @@ 6CFEEBC12773354500621863 /* HRReferenceVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HRReferenceVC.swift; sourceTree = ""; }; 6CFEEBC32773355700621863 /* SleepReferenceVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SleepReferenceVC.swift; sourceTree = ""; }; 844959772B48EF900029E2E0 /* BluetoothManager+Deprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BluetoothManager+Deprecated.swift"; sourceTree = ""; }; + 8473FB612B4BF1A200409148 /* TaskManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskManager.swift; sourceTree = ""; }; 847482752B03793C0004F0C2 /* NewWeatherModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewWeatherModel.swift; sourceTree = ""; }; 847672B72B074E43007DC2DE /* NewBeiDouContactModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewBeiDouContactModel.swift; sourceTree = ""; }; 847D1C4A2B009FAC0097A96E /* 20231111ReadMe.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = 20231111ReadMe.md; sourceTree = ""; }; @@ -1113,6 +1115,7 @@ 844959772B48EF900029E2E0 /* BluetoothManager+Deprecated.swift */, 6C1F987E279BF0AA00C0C3BA /* BluetoothManager+Set.swift */, B279A3792A4AC2F800A7C7B6 /* BluetoothManager+Function.swift */, + 8473FB612B4BF1A200409148 /* TaskManager.swift */, 6C411EE4275DFF37009B2E02 /* Bluetooth+Types.swift */, 6C6051B92760843F00286B37 /* BleMessage.swift */, B279A37B2A4AC5B000A7C7B6 /* BleMessage+Function.swift */, @@ -1712,6 +1715,7 @@ 6C5A979127CCC00D00221361 /* PressReferenceVC.swift in Sources */, 6CFE53FA277B0D5D00520B00 /* StartMotionVC.swift in Sources */, 6C5B545427755E9D007F7901 /* WristViewController.swift in Sources */, + 8473FB622B4BF1A200409148 /* TaskManager.swift in Sources */, 6C6F40892743835F00F9473C /* WomenHealthCell.swift in Sources */, 6C187CD62786E9A500150D1B /* StepCircleView.swift in Sources */, 6C411EDF275DE737009B2E02 /* BluetoothManager.swift in Sources */, diff --git a/HDFwear/Tools/BluetoothManager+Function.swift b/HDFwear/Tools/BluetoothManager+Function.swift index 4d1cd82..7ad3640 100644 --- a/HDFwear/Tools/BluetoothManager+Function.swift +++ b/HDFwear/Tools/BluetoothManager+Function.swift @@ -334,6 +334,13 @@ extension BluetoothManager { } //MARK: - 发送数据 func sendData(_ data: Data) { + taskManager.executeTaskIfNeeded { [weak self] in + print("Task executed at \(Date())") + self?.coreSendData(data) + } + } + + func coreSendData(_ data: Data) { if peripheral == nil { print("peripheral == nil") return diff --git a/HDFwear/Tools/BluetoothManager.swift b/HDFwear/Tools/BluetoothManager.swift index 283d873..216dbfa 100644 --- a/HDFwear/Tools/BluetoothManager.swift +++ b/HDFwear/Tools/BluetoothManager.swift @@ -142,6 +142,9 @@ class BluetoothManager: NSObject { var isNextSync: Bool = false //是否有下一个同步包 // private var + // 任务执行管理 + let taskManager = TaskManager() + override init() { super.init() // manger.registerClientProfile(BLEConfig.MTKServerUUID, clientProfileDelegate: self) diff --git a/HDFwear/Tools/TaskManager.swift b/HDFwear/Tools/TaskManager.swift new file mode 100644 index 0000000..c164642 --- /dev/null +++ b/HDFwear/Tools/TaskManager.swift @@ -0,0 +1,33 @@ +// +// TaskManager.swift +// HDFwear +// +// Created by admin on 2024/1/8. +// + +import Foundation + +class TaskManager { + private var lastExecutionTime: DispatchTime = .now() + private let queue = DispatchQueue(label: "com.watch.taskQueue", attributes: .concurrent) + + func executeTaskIfNeeded(taskBlock: @escaping () -> Void) { + let now = DispatchTime.now() + let deadline = lastExecutionTime + .milliseconds(300) + + let delay: DispatchTimeInterval + if now >= deadline { + // If enough time has passed, execute the task immediately + delay = .seconds(0) + } else { + // If not, calculate the delay until the next execution + delay = .nanoseconds(Int(deadline.uptimeNanoseconds - now.uptimeNanoseconds)) + } + + // Schedule the task to execute after the required delay + lastExecutionTime = .now() + delay + queue.asyncAfter(deadline: lastExecutionTime) { + taskBlock() + } + } +} -- libgit2 0.21.4