using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
    public class ScanWarningService : FObj, IScanWarningService
    {
        IFConn mConn;
        UInt32 mServerID;

        public ScanWarningService()
        {
            mServerID = SCANWARNING_OBJ_INTERFACE.ID;

        }


        #region IScanWarningService
        private bool enable;
        public bool Enable
        {
            get
            {
                return enable;
            }
            set
            {
                if (enable != value)
                {
                    enable = value;
                    NotifyPropertyChanged("Enable");
                }
            }
        }

        double alarmline;
        /// <summary>
        /// 报警线 = 产品公差 * AlarmLine
        /// </summary>
        public double AlarmLine
        {
            get
            {
                return alarmline;
            }
            set
            {
                if (alarmline != value)
                {
                    alarmline = value;
                    NotifyPropertyChanged("AlarmLine");
                }
            }
        }

        int alarmcnt;
        /// <summary>
        /// 连续多少个点，才算报警
        /// </summary>
        public int AlarmCnt
        {
            get
            {
                return alarmcnt;
            }
            set
            {
                if (alarmcnt != value)
                {
                    alarmcnt = value;
                    NotifyPropertyChanged("AlarmCnt");
                }
            }
        }



        int mix;
        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix
        {
            get
            {
                return mix;
            }
            set
            {
                if (mix != value)
                {
                    mix = value;
                    NotifyPropertyChanged("Mix");
                }
            }
        }
        #endregion

        public void Apply() 
        {
            SCANWARNING_OBJ_INTERFACE.Pack_Params p = new SCANWARNING_OBJ_INTERFACE.Pack_Params();
            p.alarmline = AlarmLine;
            p.alarmcnt = AlarmCnt;
            p.enable = Enable;
            p.mix = Mix;

            //获取所有数据，设置推送
            CurrObjSys.SetValueEx(
                mConn, mServerID, ID, 
                SCANWARNING_OBJ_INTERFACE.SET_PARAMS,
                p.ToBytes()
                );
        }

        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    SCANWARNING_OBJ_INTERFACE.GET_PARAMS);
                CurrObjSys.SenseConfigEx(mConn, mServerID, ID, 
                    0xffffffff, SENSE_CONFIG.ADD);
                    
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case SCANWARNING_OBJ_INTERFACE.GET_PARAMS:
                    {
                        SCANWARNING_OBJ_INTERFACE.Pack_Params p = new SCANWARNING_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;
                        AlarmLine = p.alarmline;
                        AlarmCnt = p.alarmcnt;
                        Enable = p.enable;
                        Mix = p.mix;
                    } break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {

                    PushGetValue(from, srcid, infoid, infodata);
        }

        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
