Alejandro Acuña
2024-08-12 1876e65234c20209001178705cfa50d8f9ded67a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * 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;
    }
}