com.rcaspar.fitbitbat.FitBitBat.java Source code

Java tutorial

Introduction

Here is the source code for com.rcaspar.fitbitbat.FitBitBat.java

Source

/*
 * Copyright 2013 Rgis Caspar <regis.caspar@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rcaspar.fitbitbat;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.AbstractScheduledService;
import com.rcaspar.fitbitbat.checks.BatChecker;
import com.rcaspar.fitbitbat.services.OAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Scheduled operations.
 *
 * @author regis
 */
@Slf4j
@Component
public class FitBitBat extends AbstractScheduledService {
    /**
     * Failure limit.
     */
    public static final int FAIL_LIMIT = 5;

    /**
     * BatChecker instance.
     */
    @Autowired
    private BatChecker batChecker;

    /**
     * OAuth service instance..
     */
    @Autowired
    private OAuthService oAuthService;

    /**
     * Fail count.
     */
    private int failCount = 0;

    @Override
    protected void runOneIteration() throws Exception {
        log.debug("runOneIteration()");
        try {
            this.batChecker.check();
            this.failCount = 0;
        } catch (final Exception ex) {
            log.warn("Got an exception during check", ex);
            this.failCount++;
            if (this.failCount >= FAIL_LIMIT) {
                // because we throw, the scheduler will stop.
                throw new TooManyFailureException("Check failed 5 times, stopping everything!!!");
            }
        }
    }

    @Override
    protected Scheduler scheduler() {
        log.debug("scheduler()");
        return Scheduler.newFixedDelaySchedule(0, 1, TimeUnit.HOURS);
    }

    /**
     * @see OAuthService#checkCredentials(String)
     */
    public boolean checkCredentials(final String pin) throws OAuthService.OAuthException {
        return this.oAuthService.checkCredentials(pin);
    }

    /**
     * Exception used when check is failing too many times.
     *
     * @author regis
     */
    @VisibleForTesting
    protected static final class TooManyFailureException extends Exception {
        /**
         * @see Exception#Exception(String)
         */
        public TooManyFailureException(final String message) {
            super(message);
        }
    }
}