/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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 quarks.samples.apps.obd2;

import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static quarks.analytics.math3.stat.Regression.SLOPE;
import static quarks.analytics.math3.stat.Statistic.MAX;
import static quarks.samples.connectors.elm327.Cmd.PID;
import static quarks.samples.connectors.elm327.Cmd.VALUE;
import static quarks.samples.connectors.elm327.Pids01.AIR_INTAKE_TEMP;
import static quarks.samples.connectors.elm327.Pids01.ENGINE_COOLANT_TEMP;
import static quarks.samples.connectors.elm327.Pids01.RPM;
import static quarks.samples.connectors.elm327.Pids01.SPEED;

import java.util.concurrent.TimeUnit;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import quarks.analytics.math3.json.JsonAnalytics;
import quarks.connectors.serial.SerialDevice;
import quarks.samples.connectors.elm327.ELM327Streams;
import quarks.topology.TStream;
import quarks.topology.TWindow;

/**
 * Sample OBD-II streams.
 *
 */
public class Obd2Streams {

    /**
     * Get a stream of temperature readings which
     * are increasing over the last minute.
     * 
     * Poll temperatures every five seconds and
     * calculate the maximum reading and rate of change
     * (slope) over the last minute, partitioned by parameter
     * {@link Cmd#PID pid}. Filter so that only
     * those with a rate of increase greater than
     * or equal to 1°C/minute is present on the returned stream.
     * 
     * Temperatures included are {@link AIR_INTAKE_TEMP} and
     * {@link ENGINE_COOLANT_TEMP}.
     * 
     * @param device Serial device the ELM327 is connected to.
     * @return Stream that will contain parameters with increasing temperatures.
     */
    public static TStream<JsonObject> increasingTemps(SerialDevice device) {

        TStream<JsonArray> tempsA = ELM327Streams.poll(device, 5, SECONDS,
                AIR_INTAKE_TEMP,
                ENGINE_COOLANT_TEMP);

        TStream<JsonObject> temps = tempsA.flatMap(je -> je).map(je -> je.getAsJsonObject());

        TWindow<JsonObject, JsonElement> window = temps.last(1, MINUTES, j -> j.get(PID));

        TStream<JsonObject> temperatureRate = JsonAnalytics.aggregate(window, PID, VALUE, MAX, SLOPE);

        // Have the stream contain only tuples where
        // the rise in temperatures >= 1°C/minute
        temperatureRate = temperatureRate.filter(j -> {
            JsonObject v = getObject(j, "value");
            return v.has("SLOPE") && getDouble(v, "SLOPE") >= 1.0;
        });

        return temperatureRate;
    }
    
    /**
     * Get a stream containing vehicle speed (km/h)
     * and engine revs (rpm).
     * 
     * {@link SPEED Speed} and {@link RPM engine revs}
     * are polled every 200ms and returned as a stream
     * containing JSON objects with keys {@code speed}
     * and {@code rpm}.
     * 
     * The two readings may not be exactly consistent with
     * each other as there are fetched sequentially from
     * the ELM327. 
     * 
     * @param device Serial device the ELM327 is connected to.
     * @return Stream that will contain speed and engine revolutions.
     */
    public static TStream<JsonObject> tach(SerialDevice serial) {

        TStream<JsonArray> rpmSpeed = ELM327Streams.poll(serial, 200, TimeUnit.MILLISECONDS,
                SPEED, RPM);

        TStream<JsonObject> tach = rpmSpeed.map(ja -> {
            JsonObject j = new JsonObject();
            
            double speed = getDouble(ja.get(0), VALUE);
            double rpm = getDouble(ja.get(1), VALUE);
            j.addProperty("speed", speed);
            j.addProperty("rpm", rpm);
                            
            return j;
        });

        return tach;
    }
    
    /**
     * Utility method to simplify accessing a JSON object.
     * @param json JSON object containing the object to be got.
     * @param key Key of the object to be got.
     * @return JSON object with key {@code key} from {@code json}.
     */
    public static JsonObject getObject(JsonObject json, String key) {
        return json.getAsJsonObject(key);
    }

    /**
     * Utility method to simplify accessing a number as a double.
     * @param json JSON object containing the number to be got.
     * @param key Key of the number to be got.
     * @return Number with key {@code key} from {@code json}.
     */
    public static double getDouble(JsonElement json, String key) {
        return json.getAsJsonObject().get(key).getAsDouble();
    }
}

Add initial Obd2Streamss