package com.tofly.ghrq_route.module.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.Nullable;
import android.util.Log;

import com.tofly.ghrq_route.module.deviceInfo.DeviceInfoTool;
import com.tofly.ghrq_route.common.util.AlarmUtils.AlarmUtils;

/**
 * Created by Amu on 2017/4/14.
 */

public class DeviceMonitorService extends Service {

    private static final String TAG = DeviceMonitorService.class.getName();

    private DeviceInfoTool deviceInfoTool;

    private static final int WAKE_TIME = 60 * 1000; //唤醒时间间隔(ms)

    public static final int REQCODE_DEVICEINFO= 200;

    public static final String ACTION_DEVICEINFO = "com.route.DeviceMonitorService.wakeup";

    @Nullable @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        deviceInfoTool = DeviceInfoTool.getInstance();
        deviceInfoTool.startChecking(this);
        AlarmUtils.getInstance().startBroadcast(WAKE_TIME, AlarmReceiver.class, ACTION_DEVICEINFO, REQCODE_DEVICEINFO);

        Log.d(TAG, "onStartCommand");
        flags = START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        deviceInfoTool.stopChecking();
        AlarmUtils.getInstance().stopBroadcast(AlarmReceiver.class, ACTION_DEVICEINFO, REQCODE_DEVICEINFO);
        super.onDestroy();
    }
}
