[
https://issues.apache.org/jira/browse/QUARKS-107?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15228567#comment-15228567
]
ASF GitHub Bot commented on QUARKS-107:
---------------------------------------
Github user ddebrunner commented on a diff in the pull request:
https://github.com/apache/incubator-quarks/pull/75#discussion_r58736939
--- Diff:
samples/utils/src/main/java/quarks/samples/utils/sensor/SimulatedTemperatureSensor.java
---
@@ -0,0 +1,115 @@
+/*
+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.utils.sensor;
+
+import java.text.DecimalFormat;
+import java.util.Objects;
+import java.util.Random;
+
+import quarks.analytics.sensors.Range;
+import quarks.analytics.sensors.Ranges;
+import quarks.function.Supplier;
+
+/**
+ * A Simulated temperature sensor.
+ * <p>
+ * The sensor starts off with an initial value.
+ * Each call to {@link #get()} changes the current value by
+ * a random amount between plus/minus a {@code deltaFactor}.
+ * The new current value is limited to a {@code maxTempRange}.
+ * <p>
+ * No temperature scale is implied (e.g., Fahrenheit, Kelvin, ...).
+ * The {@code double} temperature values are simply generated as described.
+ * The user of the class decides how to interpret them.
+ * <p>
+ * Sample use:
+ * <pre>{@code
+ * Topology t = ...;
+ * SimulatedTemperatureSensor tempSensor = new
SimulatedTemperatureSensor();
+ * TStream<Double> temp = t.poll(tempSensor, 1, TimeUnit.SECONDS);
+ * }</pre>
+ */
+public class SimulatedTemperatureSensor implements Supplier<Double> {
+ private static final long serialVersionUID = 1L;
+ private static DecimalFormat df = new DecimalFormat("#.#");
+ private Random r = new Random();
+ private final Range<Double> maxTempRange;
+ private final double deltaFactor;
+ private double currentTemp;
+
+ /**
+ * Create a temperature sensor.
+ * <p>
+ * Same as {@code SimulatedTemperatureSensor(80.0,
+ * Ranges.closed(28.0, 112.0), 1.0)}
+ * <p>
+ * These default values roughly correspond to normal air temperature
+ * in the Fahrenheit scale.
+ */
+ public SimulatedTemperatureSensor() {
+ this(80.0, Ranges.closed(28.0, 112.0), 1.0);
+ }
+
+ /**
+ * Create a temperature sensor.
+ * <p>
+ * No temperature scale is implied.
+ * @param initialTemp the initial temperature. Must be within
maxTempRange.
+ * @param maxTempRange maximum sensor value range
+ * @param deltaFactor maximum plus/minus change on each {@code get()}.
+ * e.g., 1.0 to limit change to +/- 1.0.
+ * Must be > 0.0
+ */
+ public SimulatedTemperatureSensor(double initialTemp,
+ Range<Double> maxTempRange, double deltaFactor) {
+ this.currentTemp = initialTemp;
+ this.maxTempRange = maxTempRange;
+ this.deltaFactor = deltaFactor;
+ Objects.requireNonNull(maxTempRange, "maxTempRange");
+ if (!maxTempRange.contains(currentTemp))
+ throw new IllegalArgumentException("currentTemp");
--- End diff --
The api argument is initialTemp.
> Add sample use of new Range class in recipe3 - detect value out of range?
> -------------------------------------------------------------------------
>
> Key: QUARKS-107
> URL: https://issues.apache.org/jira/browse/QUARKS-107
> Project: Quarks
> Issue Type: Improvement
> Reporter: Dale LaBossiere
> Assignee: Dale LaBossiere
> Labels: newbie
>
> Add sample use of new Range class in recipe3 - detect value out of range?
> Either or both of the recipe's existing samples could be use Range, or one of
> them could be cloned to use it.
> ```
> TStream<Double> simpleFiltered = temp.filter(tuple ->
> tuple < TEMP_LOW || tuple > TEMP_HIGH);
> // could be
> TStream<Double> simpleFiltered = temp.filter(Ranges.open(TEMP_LOW, TEMP_HIGH);
> TStream<Double> deadbandFiltered = Filters.deadband(temp,
> identity(), tuple -> tuple >= TEMP_LOW && tuple <= TEMP_HIGH);
> // could instead be
> TStream<Double> deadbandFiltered = Filters.deadband(temp,
> identity(), Ranges.closed(TEMP_LOW, TEMP_HIGH));
> ```
> Use of a Range simplifies the code a bit. I can also avoid mistakes if one's
> code "duplicates" the expressions for a particular range in multiple places.
> Using Range can be more compelling for the simplicity with which a range may
> be expressed and created from a config file for an app. e.g. imagine how one
> would express the range in a Properties config file. A range property value
> in a Properties file would simply be:
> ```
> # my sensor filter range
> myFilterRange=[71.0..98.0]
> ```
> and the app code to create the Range would simply be:
> ```
> Range<Double> myFilterRange =
> Ranges.valueOfDouble(props.getProperty("myFilterRange"));
> ```
> Another compelling case is making a filter range dynamically changeable, for
> example as a result of some received IoT "device command". The range could
> be declared like:
> ```
> AtomicReference<Range<Double>> myFilterRange = new AtomicReference<>(
> Ranges.valueOfDouble(props.getProperty("myFilterRange")); // initial
> range value
> // code in the device's set-filter-range cmd handler is ultimately:
> //
> myFilterRange.set(Ranges.valueOfDouble(the-new-range-string-from-the-cmd));
> // sets a new Range object
> // Using the changeable filter range is simply:
> TStream<Double> simpleFiltered = temp.filter(myFilterRange.get());
> ```
> Dynamic filter Predicates is probably a good recipe unto itself :-)
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)