network speed added

This commit is contained in:
RememBerBer 2021-11-18 17:31:39 +08:00
parent 2caf871a13
commit 4a2c320248
4 changed files with 212 additions and 12 deletions

View File

@ -18,8 +18,18 @@ public class Style {
*
* @param component
*/
public static void emphaticFont(JComponent component) {
public static void emphaticTitleFont(JComponent component) {
Font font = MainWindow.getInstance().getMainPanel().getFont();
component.setFont(new Font(font.getName(), Font.BOLD, font.getSize() + 2));
}
/**
* emphatic font for indicator
*
* @param component
*/
public static void emphaticIndicatorFont(JComponent component) {
Font font = MainWindow.getInstance().getMainPanel().getFont();
component.setFont(new Font(font.getName(), Font.BOLD, font.getSize() + 8));
}
}

View File

@ -67,8 +67,8 @@ public class CpuForm {
private static void initUi() {
CpuForm cpuForm = getInstance();
Style.emphaticFont(cpuForm.getScuTitleLabel());
Style.emphaticFont(cpuForm.getPcuTitleLabel());
Style.emphaticTitleFont(cpuForm.getScuTitleLabel());
Style.emphaticTitleFont(cpuForm.getPcuTitleLabel());
Dimension d = new Dimension(-1, 100);
cpuForm.getScuProgressBar().setMinimumSize(d);

View File

@ -8,7 +8,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="a1ab0" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="a1ab0" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -19,7 +19,7 @@
<grid id="51265" binding="paramatersPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@ -36,7 +36,7 @@
</grid>
<scrollpane id="efe72">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@ -47,6 +47,57 @@
</component>
</children>
</scrollpane>
<grid id="f95f0" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="4b9c9">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="56d3b" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="fbe8e" class="javax.swing.JLabel" binding="uploadSpeedLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=" ↑: --"/>
</properties>
</component>
</children>
</grid>
<grid id="70baa" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="10" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5d4b5" class="javax.swing.JLabel" binding="downloadSpeedLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=" ↓: --"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</children>
</grid>
</children>

View File

@ -4,23 +4,28 @@ import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.luoboduner.moo.info.App;
import com.luoboduner.moo.info.ui.Style;
import com.luoboduner.moo.info.ui.UiConsts;
import lombok.Getter;
import oshi.hardware.NetworkIF;
import oshi.software.os.NetworkParams;
import oshi.software.os.OperatingSystem;
import oshi.util.Constants;
import javax.swing.Timer;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* NetworkForm
@ -34,6 +39,8 @@ public class NetworkForm {
private JTable interfacesTable;
private JPanel paramatersPanel;
private JTextPane parametersTextPane;
private JLabel uploadSpeedLabel;
private JLabel downloadSpeedLabel;
private static final int INIT_HASH_SIZE = 100;
private static final String IP_ADDRESS_SEPARATOR = "; ";
@ -44,6 +51,10 @@ public class NetworkForm {
private static NetworkForm networkForm;
private static Long downloadBefore;
private static Long uploadBefore;
private static Long timestampBefore;
public static NetworkForm getInstance() {
if (networkForm == null) {
networkForm = new NetworkForm();
@ -59,11 +70,19 @@ public class NetworkForm {
}
private static void initUi() {
NetworkForm networkForm = getInstance();
Style.emphaticIndicatorFont(networkForm.getUploadSpeedLabel());
Style.emphaticIndicatorFont(networkForm.getDownloadSpeedLabel());
}
private static void initInfo() {
initParameters();
initInterfaces();
Timer timer = new Timer(UiConsts.REFRESH_FAST, e -> {
initNetworkSpeed();
});
timer.start();
}
private static void initInterfaces() {
@ -85,6 +104,62 @@ public class NetworkForm {
parametersTextPane.setText(buildParamsText(App.si.getOperatingSystem()));
}
private static void initNetworkSpeed() {
String genericString;
try {
genericString = getDefaultNetworkInteface();
} catch (Exception e) {
logger.error("NetworkSpeed not supported");
return;
}
long downloadNow = 0;
long uploadNow = 0;
long timestampNow = 0;
List<NetworkIF> networkIFs = App.si.getHardware().getNetworkIFs();
// int i = 0;
// NetworkIF net = networkIFs.get(0);
// try {
// while (!networkIFs.get(i).getName().equals(genericString)) {
// net = networkIFs.get(i);
// i++;
// }
// } catch (ArrayIndexOutOfBoundsException e) {
// logger.error("NetworkSpeed not supported");
// return;
// }
// net.updateAttributes();
for (int i = 0; i < networkIFs.size(); i++) {
NetworkIF net = networkIFs.get(i);
net.updateAttributes();
downloadNow += net.getBytesRecv();
uploadNow += net.getBytesSent();
timestampNow = net.getTimeStamp();
}
if (downloadBefore == null) {
downloadBefore = downloadNow;
}
if (uploadBefore == null) {
uploadBefore = uploadNow;
}
if (timestampBefore == null) {
timestampBefore = timestampNow - 1;
}
NetworkForm networkForm = getInstance();
networkForm.getUploadSpeedLabel().setText("↓: " + formatSize((downloadNow - downloadBefore) / (timestampNow - timestampBefore)) + "/s");
networkForm.getDownloadSpeedLabel().setText("↑: " + formatSize((uploadNow - uploadBefore) / (timestampNow - timestampBefore)) + "/s");
downloadBefore = downloadNow;
uploadBefore = uploadNow;
timestampBefore = timestampNow;
}
private static String buildParamsText(OperatingSystem os) {
NetworkParams params = os.getNetworkParams();
StringBuilder sb = new StringBuilder("Host Name: ").append(params.getHostName());
@ -153,6 +228,53 @@ public class NetworkForm {
}
}
/**
* @return
* @throws Exception
*/
private static String getDefaultNetworkInteface() throws Exception {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress localHost = InetAddress.getLocalHost();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (inetAddress.equals(localHost)) {
return networkInterface.getName();
}
}
}
return "";
}
/**
* this functions converts byte to KB, MB, GB, TB with 3 decimal places
*
* @param size number to convert
* @return coverted number with unit size (ex. 1,234 KB)
*/
private static String formatSize(long size) {
double m = size / 1024.0;
double g = size / 1048576.0;
double t = size / 1073741824.0;
DecimalFormat dec = new DecimalFormat("0.000");
String formattedSize;
if (t > 1) {
formattedSize = dec.format(t).concat(" TB");
} else if (g > 1) {
formattedSize = dec.format(g).concat(" GB");
} else if (m > 1) {
formattedSize = dec.format(m).concat(" MB");
} else {
formattedSize = size + " KB";
}
return formattedSize;
}
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
@ -171,18 +293,35 @@ public class NetworkForm {
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
final JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
panel1.setLayout(new GridLayoutManager(3, 1, new Insets(10, 10, 10, 10), -1, -1));
mainPanel.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
paramatersPanel = new JPanel();
paramatersPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
panel1.add(paramatersPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
panel1.add(paramatersPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
parametersTextPane = new JTextPane();
parametersTextPane.setEditable(false);
paramatersPanel.add(parametersTextPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
final JScrollPane scrollPane1 = new JScrollPane();
panel1.add(scrollPane1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
panel1.add(scrollPane1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
interfacesTable = new JTable();
scrollPane1.setViewportView(interfacesTable);
final JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayoutManager(1, 3, new Insets(10, 10, 10, 10), -1, -1));
panel1.add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
final Spacer spacer1 = new Spacer();
panel2.add(spacer1, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
final JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
panel2.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
uploadSpeedLabel = new JLabel();
uploadSpeedLabel.setText(" ↑: --");
panel3.add(uploadSpeedLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayoutManager(1, 1, new Insets(0, 10, 0, 0), -1, -1));
panel2.add(panel4, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
downloadSpeedLabel = new JLabel();
downloadSpeedLabel.setText(" ↓: --");
panel4.add(downloadSpeedLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
}
/**