package org.jeecg.common.util;

import com.alibaba.fastjson.JSONObject;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Map;

/**
 * @author fenghaifu
 * @version V1.0
 * @Copyright: 上海萃颠信息科技有限公司. All rights reserved.
 * @Title:HardwareUtil
 * @projectName jeecg-boot
 * @Description:TODO
 * @date: 2020/7/5 23:00
 * @updatehistory: 2020/7/5 23:00 新增
 */
public class HardwareUtil {
	public static JSONObject get(){
		JSONObject ret = new JSONObject();
		try {
			InetAddress addr;
			addr = InetAddress.getLocalHost();
			String ip = addr.getHostAddress();
			ret.put("ip", ip);

			Map<String, String> map = System.getenv();
			String computerName = map.get("COMPUTERNAME");// 获取计算机名
			ret.put("computer_name", computerName);

			byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < mac.length; i++) {
				if (i != 0) {
					sb.append("-");
				}
				String s = Integer.toHexString(mac[i] & 0xFF);
				sb.append(s.length() == 1 ? 0 + s : s);
			}
			ret.put("mac", sb.toString());
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return ret;
	}
}
