import com.veraxsystems.vxipmi.api.async.ConnectionHandle;
import com.veraxsystems.vxipmi.api.sync.IpmiConnector;
import com.veraxsystems.vxipmi.coding.commands.IpmiVersion;
import com.veraxsystems.vxipmi.coding.commands.PrivilegeLevel;
import com.veraxsystems.vxipmi.coding.commands.chassis.GetChassisStatus;
import com.veraxsystems.vxipmi.coding.commands.chassis.GetChassisStatusResponseData;
import com.veraxsystems.vxipmi.coding.commands.sdr.GetSdr;
import com.veraxsystems.vxipmi.coding.commands.sdr.GetSdrResponseData;
import com.veraxsystems.vxipmi.coding.commands.sdr.GetSensorReading;
import com.veraxsystems.vxipmi.coding.commands.sdr.GetSensorReadingResponseData;
import com.veraxsystems.vxipmi.coding.commands.sdr.ReserveSdrRepository;
import com.veraxsystems.vxipmi.coding.commands.sdr.ReserveSdrRepositoryResponseData;
import com.veraxsystems.vxipmi.coding.commands.sdr.record.CompactSensorRecord;
import com.veraxsystems.vxipmi.coding.commands.sdr.record.FullSensorRecord;
import com.veraxsystems.vxipmi.coding.commands.sdr.record.RateUnit;
import com.veraxsystems.vxipmi.coding.commands.sdr.record.ReadingType;
import com.veraxsystems.vxipmi.coding.commands.sdr.record.SensorRecord;
import com.veraxsystems.vxipmi.coding.payload.CompletionCode;
import com.veraxsystems.vxipmi.coding.payload.lan.IPMIException;
import com.veraxsystems.vxipmi.coding.protocol.AuthenticationType;
import com.veraxsystems.vxipmi.coding.security.CipherSuite;
import com.veraxsystems.vxipmi.common.TypeConverter;
import com.veraxsystems.vxipmi.connection.Connection;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;

import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/11/29 15:31
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/11/29 15:31
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class test {
    private static final int MAX_REPO_RECORD_ID = 65535;

    private static final String hostname = "127.0.0.1";

    private static final String username = "Administrator";

    private static final String password = "zwh05966788561";

    /**
     * Size of the initial GetSdr message to get record header and size
     */
    private static final int INITIAL_CHUNK_SIZE = 8;

    /**
     * Chunk size depending on buffer size of the IPMI server. Bigger values will improve performance. If server is
     * returning "Cannot return number of requested data bytes." error during GetSdr command, CHUNK_SIZE should be
     * decreased.
     */
    private static final int CHUNK_SIZE = 16;

    /**
     * Size of SDR record header
     */
    private static final int HEADER_SIZE = 5;

    private int nextRecId;

    public static void main(String[] args) {
        IpmiConnector connector = null;
        ConnectionHandle handle = null;
        try {
            connector = new IpmiConnector(8081);
            System.out.println("Connector created");

            // Create the connection and get the handle, specifiy IP address of the
            // remote host. The connection is being registered in ConnectionManager,
            // the handle will be needed to identify it among other connections
            // (target IP address isn't enough, since we can handle multiple
            // connections to the same host)
            handle = connector.createConnection(InetAddress
                    .getByName("127.0.0.1"));
            System.out.println("Connection created");

            // Get available cipher suites list via getAvailableCipherSuites and
            // pick one of them that will be used further in the session.
            CipherSuite cs = connector.getAvailableCipherSuites(handle).get(3);
            System.out.println("Cipher suite picked");

            // Provide chosen cipher suite and privilege level to the remote host.
            // From now on, your connection handle will contain these information.
            connector.getChannelAuthenticationCapabilities(handle, cs,
                    PrivilegeLevel.User);
            System.out.println("Channel authentication capabilities receivied");

            // Start the session, provide username and password, and optionally the
            // BMC key (only if the remote host has two-key authentication enabled,
            // otherwise this parameter should be null)
            connector.openSession(handle, "admin", "password", null);
            System.out.println("Session open");

            // Send some message and read the response
            GetChassisStatusResponseData response = (GetChassisStatusResponseData) connector
                    .sendMessage(handle, new GetChassisStatus(IpmiVersion.V20, cs, AuthenticationType.RMCPPlus));

            System.out.println("Received answer");
            System.out.println("System power state is "
                    + (response.isPowerOn() ? "up" : "down"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // Close the session
                connector.closeSession(handle);
                System.out.println("Session closed");

                // Close connection manager and release the listener port.
                connector.tearDown();
                System.out.println("Connection manager closed");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
