Search in sources :

Example 21 with MultistateValue

use of com.serotonin.m2m2.rt.dataImage.types.MultistateValue in project ma-core-public by infiniteautomation.

the class ValueChangeCounterQuantizerTest method testStartValueAtStartManyValuesPerPeriod.

@Test
public void testStartValueAtStartManyValuesPerPeriod() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    NextTimePeriodAdjuster hourlyAdjuster = new NextTimePeriodAdjuster(TimePeriods.HOURS, 1);
    time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
    List<IdPointValueTime> data = new ArrayList<>();
    while (time.toInstant().isBefore(to.toInstant())) {
        // Insert 10 values per day
        int value = 1;
        ZonedDateTime daily = ZonedDateTime.ofInstant(time.toInstant(), zoneId);
        for (int i = 0; i < 10; i++) {
            data.add(new IdPointValueTime(1, new MultistateValue(value), daily.toInstant().toEpochMilli()));
            daily = (ZonedDateTime) hourlyAdjuster.adjustInto(daily);
            value = value + 1;
        }
        time = (ZonedDateTime) adjuster.adjustInto(time);
    }
    // Reset time to track periods
    time = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
    MutableInt counter = new MutableInt(0);
    BucketCalculator bc = new TimePeriodBucketCalculator(from, to, TimePeriods.DAYS, 1);
    ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {

        @Override
        public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws IOException {
            counter.increment();
            ValueChangeCounter stats = (ValueChangeCounter) statisticsGenerator;
            // Test periodStart
            Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
            // Test periiodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            if (counter.getValue() == 1) {
                // Test first
                Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
                Assert.assertEquals((long) time.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            } else {
                // Test first
                Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
                Assert.assertEquals((long) time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
            }
            // Test last
            Assert.assertEquals(10, stats.getLastValue().getIntegerValue());
            Assert.assertEquals((long) time.plusHours(12).plusHours(9).toInstant().toEpochMilli(), (long) stats.getLastTime());
            // Test start (the first start value will be null
            if (counter.getValue() == 1) {
                Assert.assertEquals(1, stats.getStartValue().getIntegerValue());
                // Test count
                Assert.assertEquals(11, stats.getCount());
            } else {
                Assert.assertEquals(10, stats.getStartValue().getIntegerValue());
                // Test count
                Assert.assertEquals(10, stats.getCount());
            }
            // Test Changes
            Assert.assertEquals(10, stats.getChanges());
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new MultistateValue(1), time.toInstant().toEpochMilli()), 0, false);
    for (int count = 0; count < data.size(); count++) quantizer.row(data.get(count), count + 1);
    quantizer.lastValue(data.get(data.size() - 1), data.size() + 1, true);
    quantizer.done();
    Assert.assertEquals(new Integer(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IOException(java.io.IOException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 22 with MultistateValue

use of com.serotonin.m2m2.rt.dataImage.types.MultistateValue in project ma-core-public by infiniteautomation.

the class ValueChangeCounterQuantizerTest method testStartValueManyValuesPerPeriod.

@Test
public void testStartValueManyValuesPerPeriod() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    NextTimePeriodAdjuster hourlyAdjuster = new NextTimePeriodAdjuster(TimePeriods.HOURS, 1);
    time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
    List<IdPointValueTime> data = new ArrayList<>();
    while (time.toInstant().isBefore(to.toInstant())) {
        // Insert 10 values per day
        int value = 1;
        ZonedDateTime daily = ZonedDateTime.ofInstant(time.toInstant(), zoneId);
        for (int i = 0; i < 10; i++) {
            data.add(new IdPointValueTime(1, new MultistateValue(value), daily.toInstant().toEpochMilli()));
            daily = (ZonedDateTime) hourlyAdjuster.adjustInto(daily);
            value = value + 1;
        }
        time = (ZonedDateTime) adjuster.adjustInto(time);
    }
    // Reset time to track periods
    time = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
    MutableInt counter = new MutableInt(0);
    BucketCalculator bc = new TimePeriodBucketCalculator(from, to, TimePeriods.DAYS, 1);
    ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {

        @Override
        public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws IOException {
            counter.increment();
            ValueChangeCounter stats = (ValueChangeCounter) statisticsGenerator;
            // Test periodStart
            Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
            // Test periiodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            // Test first
            Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
            Assert.assertEquals((long) time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(10, stats.getLastValue().getIntegerValue());
            Assert.assertEquals((long) time.plusHours(12).plusHours(9).toInstant().toEpochMilli(), (long) stats.getLastTime());
            // Test start (the first start value will be null
            if (counter.getValue() == 1)
                Assert.assertEquals(1, stats.getStartValue().getIntegerValue());
            else
                Assert.assertEquals(10, stats.getStartValue().getIntegerValue());
            // Test count
            Assert.assertEquals(10, stats.getCount());
            // Ensure data
            if (counter.getValue() == 1) {
                // Test Changes
                Assert.assertEquals(9, stats.getChanges());
            } else {
                // Test Changes
                Assert.assertEquals(10, stats.getChanges());
            }
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new MultistateValue(1), time.minusHours(3).toInstant().toEpochMilli()), 0, true);
    for (int count = 0; count < data.size(); count++) quantizer.row(data.get(count), count + 1);
    quantizer.lastValue(data.get(data.size() - 1), data.size() + 1, true);
    quantizer.done();
    Assert.assertEquals(new Integer(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IOException(java.io.IOException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 23 with MultistateValue

use of com.serotonin.m2m2.rt.dataImage.types.MultistateValue in project ma-core-public by infiniteautomation.

the class ValueChangeCounterQuantizerTest method testStartValueOneValuePerPeriod.

@Test
public void testStartValueOneValuePerPeriod() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
    List<IdPointValueTime> data = new ArrayList<>();
    int value = 1;
    while (time.toInstant().isBefore(to.toInstant())) {
        data.add(new IdPointValueTime(1, new MultistateValue(value), time.toInstant().toEpochMilli()));
        time = (ZonedDateTime) adjuster.adjustInto(time);
    }
    // Reset time to track periods
    time = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
    MutableInt counter = new MutableInt(0);
    BucketCalculator bc = new TimePeriodBucketCalculator(from, to, TimePeriods.DAYS, 1);
    ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {

        @Override
        public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws IOException {
            counter.increment();
            ValueChangeCounter stats = (ValueChangeCounter) statisticsGenerator;
            // Test periodStart
            Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
            // Test periiodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            ZonedDateTime sampleTime = time.plusHours(12);
            // Test first
            Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
            Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(1, stats.getLastValue().getIntegerValue());
            Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getLastTime());
            // Test start (the first start value will be null
            Assert.assertEquals(1, stats.getStartValue().getIntegerValue());
            // Test count
            Assert.assertEquals(1, stats.getCount());
            // Test Changes
            Assert.assertEquals(0, stats.getChanges());
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new MultistateValue(1), time.minusHours(3).toInstant().toEpochMilli()), 0, true);
    for (int count = 0; count < data.size(); count++) quantizer.row(data.get(count), count + 1);
    quantizer.lastValue(data.get(data.size() - 1), data.size() + 1, true);
    quantizer.done();
    Assert.assertEquals(new Integer(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IOException(java.io.IOException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 24 with MultistateValue

use of com.serotonin.m2m2.rt.dataImage.types.MultistateValue in project ma-core-public by infiniteautomation.

the class ScriptUtils method coerce.

/**
 * Coerce an object into a DataValue
 * @param input
 * @param toDataTypeId
 * @return
 * @throws ResultTypeException
 */
public static DataValue coerce(Object input, int toDataTypeId) throws ResultTypeException {
    DataValue value;
    if (input instanceof DataValue)
        return (DataValue) input;
    if (input == null) {
        if (toDataTypeId == DataTypes.BINARY)
            value = new BinaryValue(false);
        else if (toDataTypeId == DataTypes.MULTISTATE)
            value = new MultistateValue(0);
        else if (toDataTypeId == DataTypes.NUMERIC)
            value = new NumericValue(0);
        else if (toDataTypeId == DataTypes.ALPHANUMERIC)
            value = new AlphanumericValue("");
        else
            value = null;
    } else if (input instanceof AbstractPointWrapper) {
        value = ((AbstractPointWrapper) input).getValueImpl();
        if ((value != null) && (value.getDataType() != toDataTypeId))
            return throwResultTypeException(value, toDataTypeId);
    } else // See if the type matches.
    if (toDataTypeId == DataTypes.BINARY && input instanceof Boolean)
        value = new BinaryValue((Boolean) input);
    else if (toDataTypeId == DataTypes.MULTISTATE) {
        if (input instanceof Number)
            value = new MultistateValue(((Number) input).intValue());
        else if (input instanceof String) {
            try {
                value = new MultistateValue(Integer.parseInt((String) input));
            } catch (NumberFormatException e) {
                return throwResultTypeException(input, toDataTypeId);
            }
        } else
            return throwResultTypeException(input, toDataTypeId);
    } else if (toDataTypeId == DataTypes.NUMERIC) {
        if (input instanceof Number)
            value = new NumericValue(((Number) input).doubleValue());
        else if (input instanceof NumericValue)
            value = (NumericValue) input;
        else if (input instanceof String) {
            try {
                value = new NumericValue(Double.parseDouble((String) input));
            } catch (NumberFormatException e) {
                return throwResultTypeException(input, toDataTypeId);
            }
        } else
            return throwResultTypeException(input, toDataTypeId);
    } else if (toDataTypeId == DataTypes.ALPHANUMERIC)
        value = new AlphanumericValue(input.toString());
    else
        // If not, ditch it.
        return throwResultTypeException(input, toDataTypeId);
    return value;
}
Also used : AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue)

Aggregations

MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)23 IOException (java.io.IOException)15 NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)14 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)14 ZonedDateTime (java.time.ZonedDateTime)14 MutableInt (org.apache.commons.lang3.mutable.MutableInt)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)10 StartsAndRuntime (com.infiniteautomation.mango.statistics.StartsAndRuntime)8 StartsAndRuntimeList (com.infiniteautomation.mango.statistics.StartsAndRuntimeList)8 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)8 ValueChangeCounter (com.infiniteautomation.mango.statistics.ValueChangeCounter)7 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)7 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)7 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)7 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2