From 390dff442f29abc77118ecaeabefe2e9128b231e Mon Sep 17 00:00:00 2001 From: zuim Date: Sun, 28 Feb 2021 03:37:17 +0100 Subject: [PATCH] add networkEffect --- .../de/zuim/ledcontrol/EffectManager.java | 48 ++++++-- .../{AudioFFT.java => AudioFFTEffect.java} | 3 +- ...udioVolume.java => AudioVolumeEffect.java} | 2 +- .../zuim/ledcontrol/effects/ClockEffect.java | 7 +- ...{ColorSweep.java => ColorSweepEffect.java} | 2 +- .../effects/NetworkSpeedEffect.java | 112 ++++++++++++++++++ .../ledcontrol/effects/TemperatureEffect.java | 55 --------- 7 files changed, 155 insertions(+), 74 deletions(-) rename src/main/java/de/zuim/ledcontrol/effects/{AudioFFT.java => AudioFFTEffect.java} (98%) rename src/main/java/de/zuim/ledcontrol/effects/{AudioVolume.java => AudioVolumeEffect.java} (94%) rename src/main/java/de/zuim/ledcontrol/effects/{ColorSweep.java => ColorSweepEffect.java} (91%) create mode 100644 src/main/java/de/zuim/ledcontrol/effects/NetworkSpeedEffect.java delete mode 100644 src/main/java/de/zuim/ledcontrol/effects/TemperatureEffect.java diff --git a/src/main/java/de/zuim/ledcontrol/EffectManager.java b/src/main/java/de/zuim/ledcontrol/EffectManager.java index aabe28a..51dc09e 100644 --- a/src/main/java/de/zuim/ledcontrol/EffectManager.java +++ b/src/main/java/de/zuim/ledcontrol/EffectManager.java @@ -4,30 +4,41 @@ import de.zuim.ledcontrol.effects.*; import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.util.TimerTask; +import java.util.concurrent.TimeUnit; public class EffectManager { - int activeId = 4; - private final LEDEffect[] effects = new LEDEffect[]{new ClockEffect(),new AudioVolume(), new TemperatureEffect(), new SineEffect(), new AudioFFT(), new ColorSweep()}; + private boolean sweep = false; + + + private final LEDEffect[] effects = new LEDEffect[]{new ClockEffect(),new AudioVolumeEffect(), new NetworkSpeedEffect(), new SineEffect(), new AudioFFTEffect(), new ColorSweepEffect()}; + private int activeId = 0; + private final TrayIcon trayIcon; public EffectManager() { final SystemTray tray = SystemTray.getSystemTray(); - final TrayIcon trayIcon = new TrayIcon(new ImageIcon(getClass().getResource("/icon.png")).getImage(), "LEDEffects"); + trayIcon = new TrayIcon(new ImageIcon(getClass().getResource("/icon.png")).getImage(), "LEDEffects"); trayIcon.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { - if (e.getButton() != MouseEvent.BUTTON1) - System.exit(0); - getActiveEffect().unload(); - activeId++; - if (activeId == effects.length) - activeId = 0; - getActiveEffect().load(); - trayIcon.setToolTip("LEDEffects - " + getActiveEffect().getDescription()); + switch (e.getButton()){ + case MouseEvent.BUTTON1: + loadNext(); + break; + case MouseEvent.BUTTON2: + sweep = !sweep; + loadNext(); + break; + case MouseEvent.BUTTON3: + System.exit(0); + } } @Override @@ -46,13 +57,28 @@ public class EffectManager { public void mouseExited(MouseEvent e) { } }); + try { tray.add(trayIcon); } catch (AWTException e) { e.printStackTrace(); } + new Timer(30 * 1000, e -> { + if(sweep) + loadNext(); + }).start(); + + loadNext(); + } + + private void loadNext() { + getActiveEffect().unload(); + activeId++; + if (activeId == effects.length) + activeId = 0; getActiveEffect().load(); + trayIcon.setToolTip("LEDEffects - " + getActiveEffect().getDescription()+ (sweep?" (autoswitch)":"")); } public LEDEffect getActiveEffect() { diff --git a/src/main/java/de/zuim/ledcontrol/effects/AudioFFT.java b/src/main/java/de/zuim/ledcontrol/effects/AudioFFTEffect.java similarity index 98% rename from src/main/java/de/zuim/ledcontrol/effects/AudioFFT.java rename to src/main/java/de/zuim/ledcontrol/effects/AudioFFTEffect.java index a348948..22f7837 100644 --- a/src/main/java/de/zuim/ledcontrol/effects/AudioFFT.java +++ b/src/main/java/de/zuim/ledcontrol/effects/AudioFFTEffect.java @@ -2,7 +2,6 @@ package de.zuim.ledcontrol.effects; import com.tagtraum.jipes.math.FFTFactory; import com.tagtraum.jipes.math.Transform; -import de.zuim.ledcontrol.LEDControl; import de.zuim.ledcontrol.LEDEffect; import javax.sound.sampled.*; @@ -13,7 +12,7 @@ import java.util.List; import static de.zuim.ledcontrol.LEDControl.HEIGHT; import static de.zuim.ledcontrol.LEDControl.WIDTH; -public class AudioFFT implements LEDEffect { +public class AudioFFTEffect implements LEDEffect { @Override public String getDescription() { return "Audio FFT"; diff --git a/src/main/java/de/zuim/ledcontrol/effects/AudioVolume.java b/src/main/java/de/zuim/ledcontrol/effects/AudioVolumeEffect.java similarity index 94% rename from src/main/java/de/zuim/ledcontrol/effects/AudioVolume.java rename to src/main/java/de/zuim/ledcontrol/effects/AudioVolumeEffect.java index c34d6a1..762c5e1 100644 --- a/src/main/java/de/zuim/ledcontrol/effects/AudioVolume.java +++ b/src/main/java/de/zuim/ledcontrol/effects/AudioVolumeEffect.java @@ -7,7 +7,7 @@ import java.util.List; import static de.zuim.ledcontrol.LEDControl.HEIGHT; import static de.zuim.ledcontrol.LEDControl.WIDTH; -public class AudioVolume extends AudioFFT { +public class AudioVolumeEffect extends AudioFFTEffect { @Override public String getDescription() { diff --git a/src/main/java/de/zuim/ledcontrol/effects/ClockEffect.java b/src/main/java/de/zuim/ledcontrol/effects/ClockEffect.java index 3fa49c6..4bfae57 100644 --- a/src/main/java/de/zuim/ledcontrol/effects/ClockEffect.java +++ b/src/main/java/de/zuim/ledcontrol/effects/ClockEffect.java @@ -6,8 +6,7 @@ import java.awt.*; import java.text.SimpleDateFormat; import java.util.Date; -import static de.zuim.ledcontrol.LEDControl.WIDTH; -import static java.awt.image.ImageObserver.HEIGHT; +import static de.zuim.ledcontrol.LEDControl.HEIGHT; public class ClockEffect implements LEDEffect { private Font font = new Font("Calibri", Font.BOLD, 12*getScale()); @@ -39,9 +38,9 @@ public class ClockEffect implements LEDEffect { g.setFont(font); g.setColor(new Color(0,10-date.getMinutes()/6,date.getMinutes()/6)); posOffset-=(4*getScale()*timeDelta)/1000000000.0; - g.drawString(text, (int) posOffset, 8*getScale()); + g.drawString(text, (int) posOffset, HEIGHT/2*getScale()); g.setColor(new Color(10-date.getSeconds()/6,date.getSeconds()/6,0)); - g.drawString(text2, 2, 16*getScale()); + g.drawString(text2, 2, HEIGHT*getScale()); if(posOffset<-g.getFontMetrics().stringWidth(text)) posOffset=0; } diff --git a/src/main/java/de/zuim/ledcontrol/effects/ColorSweep.java b/src/main/java/de/zuim/ledcontrol/effects/ColorSweepEffect.java similarity index 91% rename from src/main/java/de/zuim/ledcontrol/effects/ColorSweep.java rename to src/main/java/de/zuim/ledcontrol/effects/ColorSweepEffect.java index bf46d59..de48bdc 100644 --- a/src/main/java/de/zuim/ledcontrol/effects/ColorSweep.java +++ b/src/main/java/de/zuim/ledcontrol/effects/ColorSweepEffect.java @@ -7,7 +7,7 @@ import java.awt.*; import static de.zuim.ledcontrol.LEDControl.HEIGHT; import static de.zuim.ledcontrol.LEDControl.WIDTH; -public class ColorSweep implements LEDEffect { +public class ColorSweepEffect implements LEDEffect { @Override public String getDescription() { return "Solid Color"; diff --git a/src/main/java/de/zuim/ledcontrol/effects/NetworkSpeedEffect.java b/src/main/java/de/zuim/ledcontrol/effects/NetworkSpeedEffect.java new file mode 100644 index 0000000..7e84926 --- /dev/null +++ b/src/main/java/de/zuim/ledcontrol/effects/NetworkSpeedEffect.java @@ -0,0 +1,112 @@ +package de.zuim.ledcontrol.effects; + +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 = 33; + private static final int MAX_DOWNLOAD_MBIT_S = 330; + private static final long NANOS_PER_PIXEL = 1000000000; + + @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(); + network = hal.getNetworkIFs().get(0); + } + } + + private final Font font = new Font("Calibri", Font.BOLD, 9*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; + } + } +} diff --git a/src/main/java/de/zuim/ledcontrol/effects/TemperatureEffect.java b/src/main/java/de/zuim/ledcontrol/effects/TemperatureEffect.java deleted file mode 100644 index 1f50972..0000000 --- a/src/main/java/de/zuim/ledcontrol/effects/TemperatureEffect.java +++ /dev/null @@ -1,55 +0,0 @@ -package de.zuim.ledcontrol.effects; - -import com.tagtraum.jipes.math.FFTFactory; -import com.tagtraum.jipes.math.Transform; -import de.zuim.ledcontrol.LEDEffect; -import oshi.SystemInfo; -import oshi.hardware.*; - -import javax.sound.sampled.*; -import java.awt.*; -import java.text.SimpleDateFormat; -import java.util.*; -import java.util.List; - -import static de.zuim.ledcontrol.LEDControl.WIDTH; -import static java.awt.image.ImageObserver.HEIGHT; - - -public class TemperatureEffect implements LEDEffect { - @Override - public String getDescription() { - return "Temperatur Sensoren"; - } - - NetworkIF network = null; - CentralProcessor cpu = null; - Sensors sensors = null; - - @Override - public void load() { - if(sensors == null){ - SystemInfo si = new SystemInfo(); - HardwareAbstractionLayer hal = si.getHardware(); - network = hal.getNetworkIFs().get(0); - cpu = hal.getProcessor(); - sensors = hal.getSensors(); - } - } - - private Font font = new Font("Calibri", Font.BOLD, 12*getScale()); - - @Override - public void render(long timeDelta, Graphics g) { - - String text = "---"; - if(sensors!=null){ - text = Math.round(cpu.getSystemLoadAverage(1)[0]*100)+"%\n"; - text+= sensors.getCpuTemperature(); - } - - g.setFont(font); - g.setColor(new Color(26, 9, 9)); - g.drawString(text, 0, 8*getScale()); - } -}