using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FlyADBase;
namespace FLY.Simulation.Flyad7.OBJProxy
{
    /// <summary>
    /// 服务代理， Flyad7_OBJProxy 的一部分
    /// </summary>
    class SYS_DATA:FObj
    {
        FLYAD7 Data;
        public SYS_DATA(int objsys_idx, FLYAD7 data):base(objsys_idx)
        {
            ID = FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID;
            Data = data;
        }
        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid) 
            {
                //case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_IP:
                //    {
                //        FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_IP pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_IP();
                //        pack.ep = Data.LocalEP;
                //        infodata = pack.ToBytes();

                //        return 0;
                //    }break;
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSINFO:
                    {
                        FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysInfo pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysInfo();
                        pack.status = Data.AreaStatus;
                        pack.ret = Data.AreaRet;
                        Array.Copy(Data.Code,pack.code, pack.code.Length);
                        Array.Copy(Data.Access, pack.access, pack.access.Length);
                        pack.surplus = (UInt16)Data.Surplus;
                        infodata = pack.ToBytes();
                    }break;
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSPARAM:
                    {
                        FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam();
                        pack.posOfGrid = Data.PosOfGrid;
                        pack.ad1_interval = 1000;
                        pack.ad2_interval = 1000;
                        pack.push_interval = 100;

                        if (Data.MotorType == MOTORTYPE.NULL)
                            pack.func_enable = 0;
                        else if (Data.MotorType == MOTORTYPE.VF0)
                            pack.func_enable = 0x01;
                        else
                            pack.func_enable = 0x02;

                        pack.func_setting = 0;

                        pack.ratio01 = Data.Ratio01;
                        pack.ratio02 = Data.Ratio02;

                        infodata = pack.ToBytes();

                    } break;
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_ZERO_POS:
                    {
                        FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos();
                        pack.zero_pos = Data.PosOffset;
                        pack.jog_velocity = Data.JogVelocity;
                        infodata = pack.ToBytes();

                    } break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                //case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.SET_IP:
                //    {
                //        FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_IP pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_IP();
                //        if (!pack.TryParse(infodata))
                //            break;
                //        Data.LocalEP = pack.ep;
                //        return 0;
                //    } break;
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.SET_SYSPARAM:
                    {
                        FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam();
                        if (!pack.TryParse(infodata))
                            break;
                        MOTORTYPE motortype;
                        if ((pack.func_enable & 0x03) == 0x02)
                            motortype = MOTORTYPE.SERVO;
                        else if ((pack.func_enable & 0x03) == 0x01)
                            motortype = MOTORTYPE.VF0;
                        else
                              motortype = MOTORTYPE.NULL;
                        Data.PosOfGrid = pack.posOfGrid;
                        Data.MotorType = motortype;
                        Data.Ratio01 = pack.ratio01;
                        Data.Ratio02 = pack.ratio02;
                    } break;
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.SET_ZERO_POS:
                    {
                        FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos();
                        if (!pack.TryParse(infodata))
                            break;
                        Data.PosOffset = pack.zero_pos;
                        Data.JogVelocity = pack.jog_velocity;
                    } break;
            }
        }
        public override void  CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid) 
            {
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.CALL_ACCESS:
                    {
                        if (infodata.Length < 8)
                            break;

                        Data.SetAccess(infodata);


                    }break;
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.CALL_SURPLUS_SUB:
                    {
                        if (Data.Surplus!=UInt16.MaxValue && Data.Surplus!=0)
                            Data.Surplus--;

                    } break;
            }
        }
    }
}
