using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.IService;

namespace FLY.Thick.Base.Client
{
    public class DynAreaService:FObj
    {
        public DynArea mDynArea;
        private UInt32 mServerID;
        private IFConn mConn;

        public DynAreaService()
        {
            mServerID = DYNAREA_OBJ_INTERFACE.ID;
            mDynArea = new DynArea();
        }

        #region IFObj 成员
        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mDynArea.ServerIsConnected = from.IsConnected;
            mConn = from;

            if (from.IsConnected) 
            {
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_ADPOS);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_SCANINFO);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_CONTROLLER_STATE);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_PROFILE);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_BOLTMAP);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_SYSTEM);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_IO);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_POSITION2);

                CurrObjSys.SenseConfigEx(mConn, mServerID, ID,0xffffffff, SENSE_CONFIG.ADD);
            }
        }

        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            PushInfo(from,srcid,memid,infodata);
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case DYNAREA_OBJ_INTERFACE.PUSH_ADPOS:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetADPos pack = new DYNAREA_OBJ_INTERFACE.Pack_GetADPos();
                        if (!pack.TryParse(infodata))
                            return;
                        mDynArea.AD = pack.ad;
                        mDynArea.Thick = pack.thick;
                        mDynArea.Position = pack.position;
                        mDynArea.BoltIndex = pack.boltIndex;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_SCANINFO:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetScanInfo pack = new DYNAREA_OBJ_INTERFACE.Pack_GetScanInfo();
                        if (!pack.TryParse(infodata))
                            return;
                        //TODO
                        for (int i = 0; i < 3; i++)
                        {
                            mDynArea.SampleAD[i] = pack.sampleAD[i];
                        }

                        mDynArea.TempState = pack.tempState;

                        mDynArea.DataValid = pack.datavalid;

                        mDynArea.RollDown = pack.rolldown;

                        mDynArea.Direction = pack.direction;

                        mDynArea.Width = pack.width;

                        mDynArea.FilmVelocity = pack.filmVelocity;

                        mDynArea.Velocity = pack.velocity;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_PROFILE:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetProfile pack = new DYNAREA_OBJ_INTERFACE.Pack_GetProfile();
                        if (!pack.TryParse(infodata))
                            return;
                        mDynArea.ProductName = pack.ProductName;
                        mDynArea.Target = pack.Target;
                        mDynArea.Alarm = pack.Alarm;
                        mDynArea.DataBoltNoBegin = pack.DataBoltNoBegin;
                        mDynArea.DataBoltNoEnd = pack.DataBoltNoEnd;
                        mDynArea.ScanBoltNoBegin = pack.ScanBoltNoBegin;
                        mDynArea.ScanBoltNoEnd = pack.ScanBoltNoEnd;
                        mDynArea.Comp = pack.Comp;
                        mDynArea.Shift = pack.Shift;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_SYSTEM:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetSystem pack = new DYNAREA_OBJ_INTERFACE.Pack_GetSystem();
                        if (!pack.TryParse(infodata))
                            return;
                        mDynArea.FLYADIsConnect = pack.isConnect;
                        mDynArea.SecuteLock = pack.secuteLock;
                        mDynArea.Hrs = pack.hrs;
                        mDynArea.Version = pack.version;
                        mDynArea.GageType = pack.gagetype;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_BOLTMAP:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetBoltmap pack = new DYNAREA_OBJ_INTERFACE.Pack_GetBoltmap();
                        if (!pack.TryParse(infodata))
                            return;
                        mDynArea.NBolts = pack.NBolts;
                        mDynArea.FirstBoltNo = pack.FirstBoltNo;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_IO:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetIO pack = new DYNAREA_OBJ_INTERFACE.Pack_GetIO();
                        if (!pack.TryParse(infodata))
                            return;
                        mDynArea.IStatus = pack.iStatus;
                        mDynArea.OStatus = pack.oStatus;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_CONTROLLER_STATE:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_GetControllerState pack = new DYNAREA_OBJ_INTERFACE.Pack_GetControllerState();
                        if (!pack.TryParse(infodata))
                            return;
                        mDynArea.ControllerState = pack.controller_state;
                        mDynArea.AutoScanCounter = pack.autoscan_counter;
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_POSITION2:
                    {
                        DYNAREA_OBJ_INTERFACE.Pack_Position2 pack = new DYNAREA_OBJ_INTERFACE.Pack_Position2();
                        if (!pack.TryParse(infodata))
                            return;
                        mDynArea.Position2 = pack.position;
                    }break;
            }
        }
        #endregion


    }
}
