10fbfc30aSopenharmony_ci/**
20fbfc30aSopenharmony_ci * @file Describe the file
30fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
40fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
50fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License.
60fbfc30aSopenharmony_ci * You may obtain a copy of the License at
70fbfc30aSopenharmony_ci *
80fbfc30aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
90fbfc30aSopenharmony_ci *
100fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
110fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
120fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and
140fbfc30aSopenharmony_ci * limitations under the License.
150fbfc30aSopenharmony_ci */
160fbfc30aSopenharmony_ci
170fbfc30aSopenharmony_ciimport systemTimer from '@ohos.systemTimer';
180fbfc30aSopenharmony_ciimport { AsyncCallback } from '@ohos.base';
190fbfc30aSopenharmony_ciimport { Log } from './Log';
200fbfc30aSopenharmony_ci
210fbfc30aSopenharmony_ciconst TAG = 'SystemTimerUtils';
220fbfc30aSopenharmony_ci
230fbfc30aSopenharmony_ci/**
240fbfc30aSopenharmony_ci * 创建一个定时器
250fbfc30aSopenharmony_ci *
260fbfc30aSopenharmony_ci * @param options 定时器选项,数据结构参考系统api:SystemTimer
270fbfc30aSopenharmony_ci * @param callback 定时器的回调函数,到达定时器的触发时间时回调
280fbfc30aSopenharmony_ci */
290fbfc30aSopenharmony_ciexport async function createTimer(options: systemTimer.TimerOptions, callback?: AsyncCallback<number>): Promise<number | void> {
300fbfc30aSopenharmony_ci  let timerId: number | void;
310fbfc30aSopenharmony_ci  if (callback !== null && callback !== undefined) {
320fbfc30aSopenharmony_ci    timerId = systemTimer.createTimer(options, callback);
330fbfc30aSopenharmony_ci  } else {
340fbfc30aSopenharmony_ci    timerId = systemTimer.createTimer(options, (err, timerId) => {
350fbfc30aSopenharmony_ci      if (err?.message !== undefined) {
360fbfc30aSopenharmony_ci        Log.error(TAG, 'failed to create a Timer because:' + err?.message);
370fbfc30aSopenharmony_ci      }
380fbfc30aSopenharmony_ci      Log.debug(TAG, "A Timer has been created and Timer ID is:" + timerId);
390fbfc30aSopenharmony_ci    })
400fbfc30aSopenharmony_ci  }
410fbfc30aSopenharmony_ci  return timerId;
420fbfc30aSopenharmony_ci}
430fbfc30aSopenharmony_ci
440fbfc30aSopenharmony_ci/**
450fbfc30aSopenharmony_ci * 开启一个定时器
460fbfc30aSopenharmony_ci * 系统定时器会去比对 currentTime 与 triggerTime的差值
470fbfc30aSopenharmony_ci * 若差值小于5000ms则设置触发时间 triggerTime 为 currentTime+5000
480fbfc30aSopenharmony_ci * 若差值大于5000ms则正常设置触发时间 triggerTime 为传入的时间
490fbfc30aSopenharmony_ci * 若 currentTime(定时器内部时间)大于 triggerTime,则不会开启定时器
500fbfc30aSopenharmony_ci *
510fbfc30aSopenharmony_ci * @Param timer The timer ID.
520fbfc30aSopenharmony_ci * @Param triggerTime 触发时间
530fbfc30aSopenharmony_ci */
540fbfc30aSopenharmony_ciexport function startTimer(timer: number, triggerTime: number, callback?: AsyncCallback<void, void>): void {
550fbfc30aSopenharmony_ci  if (callback !== null && callback !== undefined) {
560fbfc30aSopenharmony_ci    systemTimer.startTimer(timer, triggerTime, callback);
570fbfc30aSopenharmony_ci  } else {
580fbfc30aSopenharmony_ci    systemTimer.startTimer(timer, triggerTime, (err) => {
590fbfc30aSopenharmony_ci      if (err?.message !== undefined) {
600fbfc30aSopenharmony_ci        Log.error(TAG, 'failed to startTimer because:' + err?.message);
610fbfc30aSopenharmony_ci      }
620fbfc30aSopenharmony_ci      Log.debug(TAG, "a Timer start,and this Timer will callback when: " + triggerTime);
630fbfc30aSopenharmony_ci    })
640fbfc30aSopenharmony_ci  }
650fbfc30aSopenharmony_ci}
660fbfc30aSopenharmony_ci
670fbfc30aSopenharmony_ci/**
680fbfc30aSopenharmony_ci * 停止一个定时器
690fbfc30aSopenharmony_ci *
700fbfc30aSopenharmony_ci * @Param timer The timer ID.
710fbfc30aSopenharmony_ci */
720fbfc30aSopenharmony_ciexport function stopTimer(timer: number, callback?: AsyncCallback<void, void>): void {
730fbfc30aSopenharmony_ci  if (callback !== null && callback !== undefined) {
740fbfc30aSopenharmony_ci    systemTimer.stopTimer(timer, callback);
750fbfc30aSopenharmony_ci  } else {
760fbfc30aSopenharmony_ci    systemTimer.stopTimer(timer, (err) => {
770fbfc30aSopenharmony_ci      if (err?.message !== undefined) {
780fbfc30aSopenharmony_ci        Log.error(TAG, 'failed to stop Timer because:' + err?.message);
790fbfc30aSopenharmony_ci      }
800fbfc30aSopenharmony_ci      Log.debug(TAG, "a timer has been stopped,and this Timer id is: " + timer);
810fbfc30aSopenharmony_ci    })
820fbfc30aSopenharmony_ci  }
830fbfc30aSopenharmony_ci}
840fbfc30aSopenharmony_ci
850fbfc30aSopenharmony_ci/**
860fbfc30aSopenharmony_ci * 摧毁一个定时器
870fbfc30aSopenharmony_ci *
880fbfc30aSopenharmony_ci * @Param timer The timer ID.
890fbfc30aSopenharmony_ci */
900fbfc30aSopenharmony_ciexport function destroyTimer(timer: number, callback: AsyncCallback<void, void>): void {
910fbfc30aSopenharmony_ci  if (callback !== null && callback !== undefined) {
920fbfc30aSopenharmony_ci    systemTimer.destroyTimer(timer, callback);
930fbfc30aSopenharmony_ci  } else {
940fbfc30aSopenharmony_ci    systemTimer.destroyTimer(timer, (err) => {
950fbfc30aSopenharmony_ci      if (err?.message !== undefined) {
960fbfc30aSopenharmony_ci        Log.error(TAG, 'failed to destroy Timer' + err?.message);
970fbfc30aSopenharmony_ci      }
980fbfc30aSopenharmony_ci      Log.debug(TAG, "a timer has been destroyed,and this Timer id is: " + timer);
990fbfc30aSopenharmony_ci    })
1000fbfc30aSopenharmony_ci  }
1010fbfc30aSopenharmony_ci}