LEDControl/src/main/java/de/zuim/ledcontrol/effects/NetworkSpeedEffect.java

122 lines
3.8 KiB
Java

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 = 60;
private static final int MAX_DOWNLOAD_MBIT_S = 1000;
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<Measurement> 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) {
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);
}
g.setFont(font);
g.setColor(new Color(2, 0, 2));
g.drawString(downloadText, WIDTH - g.getFontMetrics().stringWidth(downloadText), HEIGHT * getScale() - 3);
g.setColor(new Color(0, 2, 2));
g.drawString(uploadText, WIDTH - g.getFontMetrics().stringWidth(uploadText), HEIGHT / 2 * getScale() - 2);
}
}
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;
}
}
}