
package uk.co.wingpath.modbusgui;

import java.io.*;
import java.util.*;
import uk.co.wingpath.event.*;
import uk.co.wingpath.util.*;
import uk.co.wingpath.xml.*;
import uk.co.wingpath.modbus.*;

public class Command43
    implements Command<Command43.Result>
{
    public static final String tag = "cmd_43";
    public static final String typeName = "43/14 Read Device Identification";

    private final String name;
    private final String description;

    private final int slaveId;
    private final int code;
    private final int objectId;

    private Result actualResult;
    private final Result expectedResult;

    public class Result
        implements Command.Result
    {
        private final DeviceId deviceId;
        private final ModbusException exception;
        private final long responseTime;

        Result (DeviceId deviceId, ModbusException exception,
            long responseTime)
        {
            this.deviceId = deviceId;
            this.exception = exception;
            this.responseTime = responseTime;
        }

        @Override
        public ModbusException getException ()
        {
            return exception;
        }

        @Override
        public long getResponseTime ()
        {
            return responseTime;
        }

        public DeviceId getDeviceId ()
        {
            return deviceId;
        }

        @Override
        public boolean matches (Command.Result obj)
        {
            Command43.Result r = (Command43.Result) obj;
            if (r.exception != null)
                return r.exception.matches (exception);
            if (exception != null)
                return false;
            return r.deviceId.matches (deviceId);
        }

        @Override
        public boolean equals (Object obj)
        {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (obj.getClass () != getClass ())
                return false;
            Command43.Result r = (Command43.Result) obj;
            if (r.exception != null)
                return r.exception.equals (exception);
            if (exception != null)
                return false;
            if (!r.deviceId.equals (deviceId))
                return false;
            return true;
        }

        @Override
        public int hashCode ()
        {
            return 0;
        }
    }

    Command43 (String name, String description, int slaveId, int code,
        int objectId, ModbusException exception, DeviceId deviceId)
    {
        this.name = name;
        this.description = description;
        this.slaveId = slaveId;
        this.code = code;
        this.objectId = objectId;
        expectedResult = new Result (deviceId, exception, 0);
        actualResult = null;
    }

    Command43 (String name, String description, int slaveId, int code,
        int objectId)
    {
        this.name = name;
        this.description = description;
        this.slaveId = slaveId;
        this.code = code;
        this.objectId = objectId;
        expectedResult = null;
        actualResult = null;
    }

    public int getSlaveId ()
    {
        return slaveId;
    }

    public int getCode ()
    {
        return code;
    }

    public int getObjectId ()
    {
        return objectId;
    }

    public Result getActualResult ()
    {
        return actualResult;
    }

    public void setActualResult (Result result)
    {
        actualResult = result;
    }

    public Result getExpectedResult ()
    {
        return expectedResult;
    }

    @Override
    public boolean equals (Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (obj.getClass () != getClass ())
            return false;
        Command43 cmd = (Command43) obj;
        if (!cmd.name.equals (name))
            return false;
        if (!cmd.description.equals (description))
            return false;
        if (cmd.slaveId != slaveId)
            return false;
        if (cmd.code != code)
            return false;
        if (cmd.objectId != objectId)
            return false;
        if (expectedResult == null)
            return cmd.expectedResult == null;
        return expectedResult.equals (cmd.expectedResult);
    }

    @Override
    public int hashCode ()
    {
        int hash = 3;
        hash = 29 * hash + name.hashCode ();
        hash = 29 * hash + description.hashCode ();
        hash = 29 * hash + slaveId;
        hash = 29 * hash + code;
        hash = 29 * hash + objectId;
        return hash;
    }

    @Override
    public String toString ()
    {
        return name;
    }

    public String getDescription ()
    {
        return description;
    }

    public String getTypeName ()
    {
        return typeName;
    }

    public String getTag ()
    {
        return tag;
    }

    public void send (ModbusClient client, Settings settings)
        throws InterruptedException, IOException, ValueException
    {
        DeviceId deviceId = new DeviceId ();
        try
        {
            long timeStart = System.nanoTime ();
            boolean strictChecking =
                settings.getGeneral ().getStrictChecking ().getValue ();
            deviceId.readDeviceId (client, slaveId, code, objectId,
                strictChecking);
            actualResult = new Result (deviceId, null,
                System.nanoTime () - timeStart);
        }
        catch (ModbusException e)
        {
            actualResult = new Result (deviceId, e, 0);
        }
    }

    public void save (Xml.Saver saver)
        throws IOException
    {
        saver.saveValue ("name", name);
        saver.saveValue ("description", description);
        saver.saveValue ("slaveid", slaveId);
        saver.saveValue ("code", code);
        saver.saveValue ("objectid", objectId);
        if (expectedResult != null)
        {
            saver.startTag ("result");
            saver.startTag ("objects");
            DeviceIdXml.save (saver, expectedResult.deviceId);
            saver.endTag ("objects");
            saver.saveValue ("conformity",
                expectedResult.deviceId.getConformityLevel ());
            if (expectedResult.exception != null)
                CommandResult.saveException (saver, expectedResult.exception);
            saver.endTag ("result");
        }
    }

    public static Xml.Loader getXmlLoader (Xml.Receiver<Command> receiver,
        boolean isTester)
    {
        return new XmlLoader (receiver, isTester);
    }

    private static class XmlLoader
        extends Xml.AbstractLoader
    {
        private final Xml.Receiver<Command> receiver;
        private final boolean isTester;
        private String name;
        private String description;
        private int slaveId;
        private int code;
        private int objectId;
        private boolean compareData;
        private DeviceId deviceId;
        private ModbusException exception;
        private int conformity;

        XmlLoader (Xml.Receiver<Command> receiver, boolean isTester)
        {
            this.receiver = receiver;
            this.isTester = isTester;
            name = "";
            description = "";
            slaveId = 1;
            code = 1;
            objectId = 0;
            compareData = true;
            deviceId = null;
            exception = null;
            conformity = 0;
        }

        @Override
        public Xml.Loader startChild (String tag)
        {
            if (tag.equalsIgnoreCase ("name"))
            {
                return new Xml.StringLoader (new Xml.Receiver<String> ()
                {
                    public void receive (String value)
                        throws ValueException
                    {
                        if (value.equals (""))
                            throw new ValueException ("Name missing");
                        name = value;
                    }
                });
            }

            if (tag.equalsIgnoreCase ("description"))
            {
                return new Xml.StringLoader (new Xml.Receiver<String> ()
                {
                    public void receive (String value)
                        throws ValueException
                    {
                        description = value;
                    }
                });
            }

            if (tag.equalsIgnoreCase ("slaveid"))
            {
                return new Xml.IntegerLoader (0, 255,
                    new Xml.Receiver<Integer> ()
                    {
                        public void receive (Integer value)
                        {
                            slaveId = value;
                        }
                    });
            }

            if (tag.equalsIgnoreCase ("code"))
            {
                return new Xml.IntegerLoader (0, 255,
                    new Xml.Receiver<Integer> ()
                    {
                        public void receive (Integer value)
                        {
                            code = value;
                        }
                    });
            }

            if (tag.equalsIgnoreCase ("objectid"))
            {
                return new Xml.IntegerLoader (0, 255,
                    new Xml.Receiver<Integer> ()
                    {
                        public void receive (Integer value)
                        {
                            objectId = value;
                        }
                    });
            }

            if (tag.equalsIgnoreCase ("comparevalues"))
            {
                return new Xml.BooleanLoader (new Xml.Receiver<Boolean> ()
                {
                    public void receive (Boolean value)
                        throws ValueException
                    {
                        compareData = value;
                    }
                });
            }

            if (tag.equalsIgnoreCase ("result"))
            {
                return new Xml.AbstractLoader ()
                {
                    @Override
                    public Xml.Loader startChild (String tag)
                    {
                        if (tag.equalsIgnoreCase ("objects"))
                        {
                            return DeviceIdXml.getXmlLoader (
                                new Xml.Receiver<DeviceId> ()
                                {
                                    public void receive (DeviceId value)
                                    {
                                        deviceId = value;
                                    }
                                });
                        }

                        if (tag.equalsIgnoreCase ("exception"))
                        {
                            return CommandResult.getExceptionLoader (
                                new Xml.Receiver<ModbusException> ()
                                {
                                    public void receive (ModbusException e)
                                    {
                                        exception = e;
                                    }
                                });
                        }

                        if (tag.equalsIgnoreCase ("conformity"))
                        {
                            return new Xml.IntegerLoader (0, 255,
                                new Xml.Receiver<Integer> ()
                                {
                                    public void receive (Integer value)
                                    {
                                        conformity = value;
                                    }
                                });
                        }

                        return null;
                    }
                };
            }

            return null;
        }

        public void end (String value)
            throws ValueException
        {
            if (!value.equals (""))
                throw new ValueException ("Value not allowed");
            if (name.equals (""))
                throw new ValueException ("<name> missing");
            if (isTester)
            {
                if (deviceId == null || exception != null || !compareData)
                    deviceId = new DeviceId ();
                deviceId.setConformityLevel (conformity);
                Command43 command = new Command43 (name, description, slaveId,
                    code, objectId, exception, deviceId);
                receiver.receive (command);
            }
            else
            {
                Command43 command = new Command43 (name, description, slaveId,
                    code, objectId);
                receiver.receive (command);
            }
        }
    }
}

