2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nest.internal.wwn.dto;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.io.Reader;
20 import java.nio.charset.StandardCharsets;
21 import java.util.stream.Collectors;
23 import javax.measure.Unit;
24 import javax.measure.quantity.Temperature;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.openhab.binding.nest.internal.wwn.WWNUtils;
28 import org.openhab.core.library.unit.ImperialUnits;
29 import org.openhab.core.library.unit.SIUnits;
32 * Utility class for working with Nest test data in unit tests.
34 * @author Wouter Born - Initial contribution
37 public final class WWNDataUtil {
39 public static final String COMPLETE_DATA_FILE_NAME = "top-level-streaming-data.json";
40 public static final String INCOMPLETE_DATA_FILE_NAME = "top-level-streaming-data-incomplete.json";
41 public static final String EMPTY_DATA_FILE_NAME = "top-level-streaming-data-empty.json";
43 public static final String CAMERA1_DEVICE_ID = "_LK8j9rRXwCKEBOtDo7JskNxzWfHBOIm3CLouCT3FQZzrvokK_DzFQ";
44 public static final String CAMERA1_WHERE_ID = "z8fK075vJJPPWnXxLx1m3GskRSZQ64iQydB59k-UPsKCxvyZfxNpKA";
46 public static final String CAMERA2_DEVICE_ID = "VG7C7BU6Zf8OjEfizmBCVnwnuKHSnOBIHgbQKa57xKJzrvokK_DzFQ";
47 public static final String CAMERA2_WHERE_ID = "qpWvTu89Knhn6GRFM-VtGoE4KYwbzbJg9INR6WyPfhW1EJ04GRyYbQ";
49 public static final String SMOKE1_DEVICE_ID = "p1b1oySOcs_sbi4iczruW3Ou-iQr8PMV";
50 public static final String SMOKE1_WHERE_ID = "z8fK075vJJPPWnXxLx1m3GskRSZQ64iQydB59k-UPsIm5E0NfJPeeg";
52 public static final String SMOKE2_DEVICE_ID = "p1b1oySOcs8W9WwaNu80oXOu-iQr8PMV";
53 public static final String SMOKE2_WHERE_ID = "z8fK075vJJPPWnXxLx1m3GskRSZQ64iQydB59k-UPsKCxvyZfxNpKA";
55 public static final String SMOKE3_DEVICE_ID = "p1b1oySOcs-OJHIgmgeMkHOu-iQr8PMV";
56 public static final String SMOKE3_WHERE_ID = "6UAWzz8czKpFrH6EK3AcjDiTjbRgts8x5MJxEnn1yKKQpYTBO7n2UQ";
58 public static final String SMOKE4_DEVICE_ID = "p1b1oySOcs8Qu7IAJVrQ7XOu-iQr8PMV";
59 public static final String SMOKE4_WHERE_ID = "z8fK075vJJPPWnXxLx1m3GskRSZQ64iQydB59k-UPsKQrCrjN0yXiw";
61 public static final String STRUCTURE1_STRUCTURE_ID = "ysCnsCaq1pQwKUPP9H4AqE943C1XtLin3x6uCVN5Qh09IDyTg7Ey5A";
63 public static final String THERMOSTAT1_DEVICE_ID = "G1jouHN5yl6mXFaQw5iGwXOu-iQr8PMV";
64 public static final String THERMOSTAT1_WHERE_ID = "z8fK075vJJPPWnXxLx1m3GskRSZQ64iQydB59k-UPsKQrCrjN0yXiw";
66 private WWNDataUtil() {
67 // Hidden utility class constructor
70 public static Reader openDataReader(String fileName) {
71 String packagePath = (WWNDataUtil.class.getPackage().getName()).replaceAll("\\.", "/");
72 String filePath = "/" + packagePath + "/" + fileName;
73 InputStream inputStream = WWNDataUtil.class.getClassLoader().getResourceAsStream(filePath);
74 return new InputStreamReader(inputStream, StandardCharsets.UTF_8);
77 public static <T> T fromJson(String fileName, Class<T> dataClass) throws IOException {
78 try (Reader reader = openDataReader(fileName)) {
79 return WWNUtils.fromJson(reader, dataClass);
83 public static String fromFile(String fileName, Unit<Temperature> temperatureUnit) throws IOException {
84 String json = fromFile(fileName);
85 if (SIUnits.CELSIUS.equals(temperatureUnit)) {
86 json = json.replace("\"temperature_scale\": \"F\"", "\"temperature_scale\": \"C\"");
87 } else if (ImperialUnits.FAHRENHEIT.equals(temperatureUnit)) {
88 json = json.replace("\"temperature_scale\": \"C\"", "\"temperature_scale\": \"F\"");
93 public static String fromFile(String fileName) throws IOException {
94 try (Reader reader = openDataReader(fileName)) {
95 return new BufferedReader(reader).lines().parallel().collect(Collectors.joining("\n"));