//
//  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.
//

== IoT Factory example

In this example we will be running the full stack of tools for a rudimentary factory dashboard.

This example however is everything but production ready.
First of all we use create an Elasticsearch single node cluster embedded within the applications VM.
Secondly we communicate with the `cluster` using the NodeClient, which is super-easy to use, but shouldn't be used in production type environments.

We decided to go this path because this way it's super-easy to setup everything and running the example is nothing but starting a Java application.

=== What else do I need?

In General:
- A PLC
- A Factory

Even if you might be able to get your hands on a PLC I doubt you have a spare factory available.

This example is built to visualize a virtual factory simulated with https://factoryio.com/[Factory I/O].

We have decided to stick with one of the on-board examples, to make it as easy as possible for you to reproduce.
The example we used is: "Sorting by Height".

Here's a video demonstrating this:
https://www.youtube.com/watch?v=B0n8gT1vto4

You can run this with a real PLC or use a PLC simulator.
In our IoT lab we are currently running this with the real S7-1200.

=== What happens in detail?

When the application is started it first starts up an Elasticsearch node.

After that is up and running, it checks if the indexes we are going to write to exist.

If they don't and we would just have ES create them on the fly, we wouldn't be able to use the time-data in the time fields as they would be treated as numbers.

Any non-existent indexes are created.

After that an `Apache Edgent` instance is created and one stream is created to fetch the state of all 8 outputs of the PLC every 100ms.

This stream is then split up.

One branch simply dumps the data into the `factory-data` index.

The second branch is a little more complex as it monitors the state of the left and right conveyors to extrapolate if a new `big` or `small box is being transported.

Only when a new box is detected an entry is written into the `product-data` index.

