/*
 * Copyright (C) 2014-2015 Gregory S. Meiste  <http://gregmeiste.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.meiste.tempalarm.backend;

import java.io.IOException;
import java.util.logging.Logger;

import static com.meiste.tempalarm.backend.OfyService.ofy;

public class TemperatureCommon {
    private static final Logger log = Logger.getLogger(TemperatureCommon.class.getName());

    public static void report(final float temperature, final float humidity)
            throws IOException {
        // Retrieve last reported temperature from datastore
        final TemperatureRecord prevRecord = ofy().load().type(TemperatureRecord.class)
                .order("-timestamp").limit(1).first().now();
        final float prevTemp = (prevRecord != null) ? prevRecord.getFloatDegF() : Float.NaN;

        log.fine("prevTemp=" + prevTemp + ", newTemp=" + temperature +
                 ", humidity=" + humidity);

        // Check to make sure not being spammed with reports
        if ((prevRecord != null) &&
            ((System.currentTimeMillis() - prevRecord.getTimestamp()) < Constants.MINUTE_IN_MILLIS)) {
            log.warning("Received new report too soon. Ignoring.");
            return;
        }

        final TemperatureRecord record = new TemperatureRecord();
        record.setDegF(temperature);
        record.setHumidity(humidity);
        ofy().save().entity(record);

        final float lowTempThreshold = getLowTempThreshold();
        if ((temperature < lowTempThreshold) && (prevTemp >= lowTempThreshold)) {
            Gcm.sendAlarm(Gcm.AlarmState.TEMP_TOO_LOW);
            AlertEmail.sendLowTemp(record.getDegF());
        }
    }

    private static float getLowTempThreshold() {
        return Float.valueOf(SettingUtils.getSettingValue(Constants.SETTING_THRES_LOW,
                Constants.DEFAULT_THRES_LOW));
    }
}

@@ -1038,356 +1038,29 @@
     
-// Retrieve last reported temperature from datastore%0A        final TemperatureRecord prevRecord = ofy().load().type(TemperatureRecord.class)%0A                .order(%22-timestamp%22).limit(1).first().now();%0A        final float prevTemp = (prevRecord != null) ? prevRecord.getFloatDegF() : Float.NaN;%0A%0A        log.fine(%22prevTemp=%22 + prevTemp + %22, newTemp
+log.fine(%22temperature
 =%22 +
@@ -1073,33 +1073,16 @@
 rature +
-%0A                
  %22, humi
@@ -1106,307 +1106,8 @@
 );%0A%0A
-        // Check to make sure not being spammed with reports%0A        if ((prevRecord != null) &&%0A            ((System.currentTimeMillis() - prevRecord.getTimestamp()) %3C Constants.MINUTE_IN_MILLIS)) %7B%0A            log.warning(%22Received new report too soon. Ignoring.%22);%0A            return;%0A        %7D%0A%0A
     
@@ -1246,45 +1246,8 @@
 ty);
-%0A        ofy().save().entity(record);
 %0A%0A  
@@ -1317,17 +1317,16 @@
      if 
-(
 (tempera
@@ -1354,10 +1354,485 @@
 ld) 
-&&
+%7B%0A            log.warning(%22Reported temperature is below threshold!%22);%0A%0A            // Retrieve previous reported temperature from datastore%0A            final TemperatureRecord prevRecord = ofy().load().type(TemperatureRecord.class)%0A                    .order(%22-timestamp%22).limit(1).first().now();%0A            final float prevTemp = (prevRecord != null) ? prevRecord.getFloatDegF() : Float.NaN;%0A%0A            // Only alarm if this is first reading below threshold%0A            if
  (pr
@@ -1858,20 +1858,23 @@
 reshold)
-)
  %7B%0A
+    
         
@@ -1933,16 +1933,20 @@
         
+  
+  
 AlertEma
@@ -1987,17 +1987,138 @@
         
-%7D
+    %7D%0A        %7D%0A%0A        // New record must be saved after previous temperature check%0A        ofy().save().entity(record);
 %0A    %7D%0A%0A

Remove rate limit on temperature reports