package de.zuim.ledcontrol.effects; import de.zuim.ledcontrol.LEDControl; import de.zuim.ledcontrol.LEDEffect; import oshi.SystemInfo; import oshi.hardware.*; import java.awt.*; import java.util.ArrayList; import java.util.List; import static de.zuim.ledcontrol.LEDControl.HEIGHT; import static de.zuim.ledcontrol.LEDControl.WIDTH; public class NetworkSpeedEffect implements LEDEffect { private static final int MAX_UPLOAD_MBIT_S = 290; private static final int MAX_DOWNLOAD_MBIT_S = 500; private static final long NANOS_PER_PIXEL = 1000000000; private static final String NETWORK_NAME = "Realtek"; @Override public String getDescription() { return "Network Speed"; } NetworkIF network = null; @Override public void load() { if (network == null) { SystemInfo si = new SystemInfo(); HardwareAbstractionLayer hal = si.getHardware(); for(NetworkIF net : hal.getNetworkIFs()){ //System.out.println(net.getDisplayName()); if(net.getDisplayName().contains(NETWORK_NAME)) network = net; } if(network == null) System.err.println("Network name not configured or \""+NETWORK_NAME+"\" not found."); else System.out.println("Using network adapter: "+network.getDisplayName()); } } private final Font font = new Font("Calibri", Font.BOLD, 8 * getScale()); long lastDownBytes = 0, lastUpBytes = 0; private final List history = new ArrayList<>(); private long nanosElapsed = 0; private String uploadText = "", downloadText = ""; @Override public void render(long timeDelta, Graphics g) { nanosElapsed += timeDelta; if (network != null && nanosElapsed > NANOS_PER_PIXEL) { network.updateAttributes(); double factor_MBit = (8 * 1000000000.0) / (1000 * 1000 * nanosElapsed); double up_speed = ((network.getBytesSent() - lastUpBytes) * (factor_MBit)); double down_speed = ((network.getBytesRecv() - lastDownBytes) * (factor_MBit)); lastUpBytes = network.getBytesSent(); lastDownBytes = network.getBytesRecv(); history.add(new Measurement((int) (Math.round((HEIGHT * up_speed) / MAX_UPLOAD_MBIT_S)), (int) (Math.round((HEIGHT * down_speed) / MAX_DOWNLOAD_MBIT_S)))); if (history.size() > WIDTH) { history.remove(0); } uploadText = Math.round(up_speed) + "↑"; downloadText = Math.round(down_speed) + "↓"; //System.out.println(up_speed+" "+down_speed); nanosElapsed = 0; } if (history.size() > 1) { g.setFont(font); g.setColor(new Color(1, 0, 0)); g.drawString(downloadText, WIDTH - g.getFontMetrics().stringWidth(downloadText), HEIGHT * getScale() - 3); g.setColor(new Color(0, 1, 0)); g.drawString(uploadText, WIDTH - g.getFontMetrics().stringWidth(uploadText), HEIGHT / 2 * getScale() - 2); for (int x = 0; x < WIDTH && x < history.size(); x++) { boolean firstUp = history.get(x).upY > history.get(x).downY; drawMeasurement(g, x, firstUp); drawMeasurement(g, x, !firstUp); } } } private void drawMeasurement(Graphics g, int x, boolean up) { int offset = WIDTH - history.size(); if (up) { g.setColor(new Color(0, 1, 0)); g.drawLine(x + offset, HEIGHT - history.get(x).upY, x + offset, HEIGHT - 1); g.setColor(new Color(0, 7, 0)); g.drawRect(x + offset, HEIGHT - 1 - history.get(x).upY, 0, 0); //g.drawLine(x + offset, HEIGHT - 1 - history.get(x).upY, x + offset + 1, HEIGHT - 1 - history.get(x+1).upY); } else { g.setColor(new Color(1, 0, 0)); g.drawLine(x + offset, HEIGHT - history.get(x).downY, x + offset, HEIGHT - 1); g.setColor(new Color(7, 0, 0)); g.drawRect(x + offset, HEIGHT - 1 - history.get(x).downY, 0, 0); //g.drawLine(x + offset, HEIGHT - 1 - history.get(x).downY, x + offset + 1, HEIGHT - 1 - history.get(x+1).downY); } } static class Measurement { public int upY; public int downY; public Measurement(int upY, int downY) { this.upY = upY; this.downY = downY; } } }