app/src/main/java/com/fjun/hassalarm/NextAlarmUpdaterJob.java
public static Request createRequest(Context context,
                                        String host,
                                        String apiKeyOrToken,
                                        String entityId,
                                        boolean isToken,
                                        boolean entityIdIsLegacy) throws IllegalArgumentException {
        final AlarmManager alarmManager = context.getSystemService(AlarmManager.class);
        final AlarmManager.AlarmClockInfo alarmClockInfo = alarmManager.getNextAlarmClock();

        // Verify host and API key.
        if (TextUtils.isEmpty(host)) {
            throw new IllegalArgumentException("Host is missing. You need to specify the host to your hass.io instance.");
        }

        // No port number? Add default one.
        if (!host.contains(":")) {
            host = String.format(Locale.US, "%s:%d", host, DEFAULT_PORT);
        }
        // Default to http:// if there is no protocol defined.
        if (!host.startsWith("http://") && !host.startsWith("https://")) {
            host = String.format(Locale.US, "http://%s", host);
        }

        // Support empty API key, if there is no one required.
        final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        final HassApi hassApi = retrofit.create(HassApi.class);

        // Get next scheduled alarm, if any.
        final State state;
        final Datetime datetime;
        final long triggerTimestamp;
        if (alarmClockInfo != null) {
            triggerTimestamp = alarmClockInfo.getTriggerTime();
            final Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(triggerTimestamp);
            state = new State(DATE_FORMAT_LEGACY.format(calendar.getTime()));
            datetime = new Datetime(entityId, DATE_FORMAT.format(calendar.getTime()));
        } else {
            triggerTimestamp = 0;
            state = new State("");
            datetime = new Datetime(entityId, "1970-01-01 00:00:00");
        }
        Log.d(Constants.LOG_TAG, "Setting time to " + datetime.datetime);

        // Enqueue call and run on background thread.
        // Check if it is using long lived access tokens
        final Call<ResponseBody> call;
        if (isToken) {
            // Create Authorization Header value
            String bearer = String.format(BEARER_PATTERN, apiKeyOrToken);
            if (entityIdIsLegacy) {
                call = hassApi.updateStateUsingToken(state, entityId, bearer);
            } else {
                call = hassApi.setInputDatetimeUsingToken(datetime, bearer);
            }
        } else {
            if (entityIdIsLegacy) {
                call = hassApi.updateStateUsingApiKey(state, entityId, apiKeyOrToken);
            } else {
                call = hassApi.setInputDatetimeUsingApiKey(datetime, apiKeyOrToken);
            }
        }
        return Request.create(call, triggerTimestamp);
    }
/**
     * Create a call that can be executed. Will throw an exception in case of any failure,
     * like missing parameters etc.
     */
Create a call that can be executed. Will throw an exception in case of any failure,
like missing parameters etc.
Create a call that can be executed . Will throw an exception in case of any failure like missing parameters etc .
public static Request createRequest(Context context,
                                        String host,
                                        String apiKeyOrToken,
                                        String entityId,
                                        boolean isToken,
                                        boolean entityIdIsLegacy) throws IllegalArgumentException {
        final AlarmManager alarmManager = context.getSystemService(AlarmManager.class);
        final AlarmManager.AlarmClockInfo alarmClockInfo = alarmManager.getNextAlarmClock();
        if (TextUtils.isEmpty(host)) {
            throw new IllegalArgumentException("Host is missing. You need to specify the host to your hass.io instance.");
        }
        if (!host.contains(":")) {
            host = String.format(Locale.US, "%s:%d", host, DEFAULT_PORT);
        }
        if (!host.startsWith("http://") && !host.startsWith("https://")) {
            host = String.format(Locale.US, "http://%s", host);
        }
        final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        final HassApi hassApi = retrofit.create(HassApi.class);
        final State state;
        final Datetime datetime;
        final long triggerTimestamp;
        if (alarmClockInfo != null) {
            triggerTimestamp = alarmClockInfo.getTriggerTime();
            final Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(triggerTimestamp);
            state = new State(DATE_FORMAT_LEGACY.format(calendar.getTime()));
            datetime = new Datetime(entityId, DATE_FORMAT.format(calendar.getTime()));
        } else {
            triggerTimestamp = 0;
            state = new State("");
            datetime = new Datetime(entityId, "1970-01-01 00:00:00");
        }
        Log.d(Constants.LOG_TAG, "Setting time to " + datetime.datetime);
        final Call<ResponseBody> call;
        if (isToken) {
            String bearer = String.format(BEARER_PATTERN, apiKeyOrToken);
            if (entityIdIsLegacy) {
                call = hassApi.updateStateUsingToken(state, entityId, bearer);
            } else {
                call = hassApi.setInputDatetimeUsingToken(datetime, bearer);
            }
        } else {
            if (entityIdIsLegacy) {
                call = hassApi.updateStateUsingApiKey(state, entityId, apiKeyOrToken);
            } else {
                call = hassApi.setInputDatetimeUsingApiKey(datetime, apiKeyOrToken);
            }
        }
        return Request.create(call, triggerTimestamp);
    }
public static Request createRequest ( Context context , String host , String apiKeyOrToken , String entityId , boolean isToken , boolean entityIdIsLegacy ) throws IllegalArgumentException { final AlarmManager alarmManager = context . getSystemService ( AlarmManager . class ) ; final AlarmManager . AlarmClockInfo alarmClockInfo = alarmManager . getNextAlarmClock ( ) ; if ( TextUtils . isEmpty ( host ) ) { throw new IllegalArgumentException ( "Host is missing. You need to specify the host to your hass.io instance." ) ; } if ( ! host . contains ( ":" ) ) { host = String . format ( Locale . US , "%s:%d" , host , DEFAULT_PORT ) ; } if ( ! host . startsWith ( "http://" ) && ! host . startsWith ( "https://" ) ) { host = String . format ( Locale . US , "http://%s" , host ) ; } final Retrofit retrofit = new Retrofit . Builder ( ) . baseUrl ( host ) . addConverterFactory ( GsonConverterFactory . create ( ) ) . build ( ) ; final HassApi hassApi = retrofit . create ( HassApi . class ) ; final State state ; final Datetime datetime ; final long triggerTimestamp ; if ( alarmClockInfo != null ) { triggerTimestamp = alarmClockInfo . getTriggerTime ( ) ; final Calendar calendar = Calendar . getInstance ( ) ; calendar . setTimeInMillis ( triggerTimestamp ) ; state = new State ( DATE_FORMAT_LEGACY . format ( calendar . getTime ( ) ) ) ; datetime = new Datetime ( entityId , DATE_FORMAT . format ( calendar . getTime ( ) ) ) ; } else { triggerTimestamp = 0 ; state = new State ( "" ) ; datetime = new Datetime ( entityId , "1970-01-01 00:00:00" ) ; } Log . d ( Constants . LOG_TAG , "Setting time to " + datetime . datetime ) ; final Call < ResponseBody > call ; if ( isToken ) { String bearer = String . format ( BEARER_PATTERN , apiKeyOrToken ) ; if ( entityIdIsLegacy ) { call = hassApi . updateStateUsingToken ( state , entityId , bearer ) ; } else { call = hassApi . setInputDatetimeUsingToken ( datetime , bearer ) ; } } else { if ( entityIdIsLegacy ) { call = hassApi . updateStateUsingApiKey ( state , entityId , apiKeyOrToken ) ; } else { call = hassApi . setInputDatetimeUsingApiKey ( datetime , apiKeyOrToken ) ; } } return Request . create ( call , triggerTimestamp ) ; }
Create a call that can be executed.
Create a call that can be executed .
// Verify host and API key. // No port number? Add default one. // Default to http:// if there is no protocol defined. // Support empty API key, if there is no one required. // Get next scheduled alarm, if any. // Enqueue call and run on background thread. // Check if it is using long lived access tokens // Create Authorization Header value