package com.thingworx.analytics.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.thingworx.analytics.thingwatcher.DurationUnit; import com.thingworx.analytics.thingwatcher.ThingState; import com.thingworx.analytics.thingwatcher.ThingWatcher; import com.thingworx.analytics.thingwatcher.ThingWatcherBuilder; import com.thingworx.analytics.thingwatcher.ThingWatcherState; import com.thingworx.analytics.thingwatcher.exceptions.ThingWatcherConfigurationException; import com.coldlight.cybertron.timeseries.streaming.TimedValue; public class Main { private static Logger LOGGER = LoggerFactory.getLogger(Main.class); public static void main(String[] args) throws ThingWatcherConfigurationException { try { LOGGER.info("hello"); ThingWatcher thingwatcher = new ThingWatcherBuilder() .certainty(90.0) .trainingDataDuration(60) .trainingDataDurationUnit(DurationUnit.SECOND) .trainingBaseURI("http://192.168.99.100:8090/training") .getThingWatcher(); long now=0 ; double value = 0; int duration = 100; for (int i = 0; i < 10000; i++) { now += duration; if (i > 9000 && i < 9900) { value = Math.incrementExact(i); }else { value = Math.sin(i); } TimedValue timedValue = new TimedValue(now, value); Boolean anomalousValue = null; try{ ThingState thingState = thingwatcher.monitor(timedValue); if (i > 1000 && thingState.getThingWatcherState() == ThingWatcherState.CALIBRATING) { while ((thingState = thingwatcher.monitor(timedValue)).getThingWatcherState() == ThingWatcherState.CALIBRATING) { LOGGER.info("Waiting for thingwatcher training to complete..."); try { Thread.sleep(5000); } catch (final InterruptedException e) {} } LOGGER.info("Training is complete, starting monitoring phase"); } switch (thingState.getThingWatcherState()) { case MONITORING: anomalousValue = thingState.isAnomalousValue(); LOGGER.info("Value = " + thingState.getValue()); LOGGER.info("isAnomalousValue = " + anomalousValue); LOGGER.info("ThingWatcherStat = " + thingState.getThingWatcherState()); break; } } catch (final Exception e) {e.printStackTrace();} } } catch (ThingWatcherConfigurationException e) { e.printStackTrace(); } LOGGER.info("Test Complete"); } }