OshiPlatformCache { @SuppressWarnings("unchecked") public Map<String, PowerSource> getPowerSources() { Map<String, PowerSource> ret; wLock.lock(); try { if (!sysInfoCache.containsKey(PlatformResourceType.POWER_SOURCE)) { HashMap<String, PowerSource> cache = new HashMap<>(); PowerSource[] arr = sysInfo.getHardware().getPowerSources(); if (arr != null) { for (PowerSource item : arr) { cache.put(item.getName(), item); } } sysInfoCache.put(PlatformResourceType.POWER_SOURCE, cache); } } finally { rLock.lock(); try { wLock.unlock(); ret = (Map<String, PowerSource>) sysInfoCache.get(PlatformResourceType.POWER_SOURCE); } finally { rLock.unlock(); } } return ret; } OshiPlatformCache(String feedId, String machineId, String containerId); void refresh(); OperatingSystem getOperatingSystem(); @SuppressWarnings("unchecked") Map<String, OSFileStore> getFileStores(); GlobalMemory getMemory(); CentralProcessor getProcessor(); @SuppressWarnings("unchecked") Map<String, PowerSource> getPowerSources(); Double getPowerSourceMetric(String powerSourceName, ID metricToCollect); Double getProcessorMetric(String processorNumber, ID metricToCollect); Double getFileStoreMetric(String fileStoreNameName, ID metricToCollect); Double getMemoryMetric(ID metricToCollect); Double getOperatingSystemMetric(ID metricId); Double getMetric(PlatformResourceType type, String name, ID metricToCollect); String getMachineId(); String getContainerId();  }
@Test public void getPowerSources() { OshiPlatformCache oshi = newOshiPlatformCache(); Map<String, PowerSource> powersources = oshi.getPowerSources(); Assert.assertNotNull(powersources); if (powersources.size() == 0) { print("===NO POWER SOURCES ON THIS MACHINE==="); } else { int i = 0; for (PowerSource powersource : powersources.values()) { String name = powersource.getName(); double remainingCapacity = powersource.getRemainingCapacity(); double timeRemaining = powersource.getTimeRemaining(); Assert.assertNotNull(name); print("===POWER SOURCE #%d ===", ++i); print(" Name=[%s]", name); print(" RemainingCapacity=[%.0f%%] (%.2f)", remainingCapacity * 100, remainingCapacity); long roundedTimeRemaining = Math.round(timeRemaining); if (roundedTimeRemaining == -1) { print(" TimeRemaining=[calculating] (%.1f)", timeRemaining); } else if (roundedTimeRemaining == -2) { print(" TimeRemaining=[unlimited] (%.1f)", timeRemaining); } else { print(" TimeRemaining=[%s] (%.1f)", FormatUtil.formatElapsedSecs(roundedTimeRemaining), timeRemaining); } print(" toString=[%s]", powersource.toString()); } } }