/*
|
* To change this license header, choose License Headers in Project Properties.
|
* To change this template file, choose Tools | Templates
|
* and open the template in the editor.
|
*/
|
package devices.mockers;
|
|
import art.library.interop.serialization.Serialization;
|
import art.library.model.devices.Device;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import java.net.URL;
|
import java.net.URLConnection;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.Optional;
|
import java.util.concurrent.CompletableFuture;
|
import java.util.stream.Stream;
|
|
/**
|
*
|
* @author Konstantin
|
*/
|
public class LocalFileDeviceMocker <T extends Device>
|
{
|
private final Optional<T> deviceOpt;
|
|
public LocalFileDeviceMocker(Class deviceClass)
|
{
|
deviceOpt = getDevices().stream()
|
.filter(deviceClass::isInstance)
|
.map(dev -> (T)dev).findFirst();
|
}
|
|
public T getDevice()
|
{
|
return deviceOpt.orElseThrow(() -> new Error("Device not found"));
|
}
|
|
private List<Device> getDevices()
|
{
|
|
List<Device> lDeviceOutput = new ArrayList();
|
|
try
|
{
|
URL url = new URL("http://172.16.11.207:10305/Device/GetDevices");
|
URLConnection connection = url.openConnection();
|
|
String[] map = new ObjectMapper().readValue(connection.getInputStream(), String[].class);
|
|
Device[] ldevice = Stream.of(map).map(d ->
|
{
|
try
|
{
|
return (Device) Serialization.deserialize(Device.class, d);
|
} catch (Exception ex)
|
{
|
return null;
|
}
|
}).filter(Objects::nonNull)
|
.toArray(Device[]::new);
|
|
Stream.of(ldevice).forEach(lDeviceOutput::add);
|
|
} catch (Exception ex)
|
{
|
}
|
|
CompletableFuture.runAsync(() ->
|
{
|
while (true)
|
{
|
try
|
{
|
URL url = new URL("http://172.16.11.207:10305/Device/GetDevices");
|
URLConnection connection = url.openConnection();
|
|
String[] map = new ObjectMapper().readValue(connection.getInputStream(), String[].class);
|
|
Device[] ldevice = Stream.of(map).map(d ->
|
{
|
try
|
{
|
return (Device) Serialization.deserialize(Device.class, d);
|
} catch (Exception ex)
|
{
|
return null;
|
}
|
}).filter(Objects::nonNull)
|
.toArray(Device[]::new);
|
|
for(Device dev : ldevice)
|
{
|
if(dev.getIdentifier().equals(deviceOpt.get().getIdentifier()))
|
{
|
deviceOpt.get().status = dev.getDeviceStatus();
|
}
|
}
|
|
Thread.sleep(2500);
|
|
} catch (Exception ex)
|
{
|
}
|
}
|
});
|
|
return lDeviceOutput;
|
}
|
}
|