TempAreaPanel.xaml.cs 5.81 KB
using FLY.DownBlowing.Client;
using FLY.DownBlowing.Common;
using FLY.DownBlowing.IService;
using FLY.OBJComponents.Client;
using MultiLayout.UiModule;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Unity;

namespace FLY.DownBlowing.UI.Client.UiModule
{
    /// <summary>
    /// TempAreaPanel.xaml 的交互逻辑
    /// </summary>
    public partial class TempAreaPanel : UserControl
    {
        IDownBlowingSystemService downBlowingSystemService;
        IUnityContainer container;
        List<SetPLCUpdatePlan> setPlan_items = new List<SetPLCUpdatePlan>();

        static string[] update_propertynames;
        static TempAreaPanel()
        {
            string[] propertynames = new string[] {
                nameof(Common.TempArea.TempPV1),
                nameof(Common.TempArea.TempSV1),
                nameof(Common.TempArea.IsHeating1),
                nameof(Common.TempArea.IsHeatingSet1),
                nameof(Common.TempArea.IsCooling1),
                nameof(Common.TempArea.IsSelfAdjusting1)
            };
            List<string> update_propertynames = new List<string>();
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < propertynames.Count(); j++)
                {
                    update_propertynames.Add(propertynames[j].Replace("1", (i + 1).ToString()));
                }
            }
            update_propertynames.Add(nameof(Common.TempArea.MeltingTemp));

            TempAreaPanel.update_propertynames = update_propertynames.ToArray();
        }
        public TempAreaPanel()
        {
            InitializeComponent();
        }


        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IDownBlowingSystemService downBlowingSystemService)
        {
            this.downBlowingSystemService = downBlowingSystemService;
            this.container = container;
            this.DataContext = this.downBlowingSystemService;

            for (int i = 0; i < this.downBlowingSystemService.TAreas.Count(); i++)
            {
                SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                    this.downBlowingSystemService.PLCos,
                    this.downBlowingSystemService.TAreas[i],
                    update_propertynames);
                setPlan_items.Add(plan);
            }

            if (this.downBlowingSystemService is DownBlowingSystemServiceClient)
            {
                (this.downBlowingSystemService as DownBlowingSystemServiceClient).ResetItemsEvent += MWeighterCsService_ResetItemsEvent;
            }

        }

        private void MWeighterCsService_ResetItemsEvent()
        {
            //把多出来的删除
            int remove_cnt = setPlan_items.Count() - this.downBlowingSystemService.TAreas.Count();
            if (remove_cnt > 0)
            {
                for (int i = 0; i < remove_cnt; i++)
                {
                    SetPLCUpdatePlan plan = setPlan_items[setPlan_items.Count() - 1 - i];
                    plan.Dispose();
                }
                setPlan_items.RemoveRange(setPlan_items.Count() - remove_cnt, remove_cnt);
            }
            else
            {
                int start_idx = setPlan_items.Count();
                int add_cnt = -remove_cnt;
                for (int i = 0; i < add_cnt; i++)
                {
                    SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                    this.downBlowingSystemService.PLCos,
                    this.downBlowingSystemService.TAreas[start_idx + i],
                    update_propertynames);
                    setPlan_items.Add(plan);
                }
            }
        }

        private void btnTAreaClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            var tempArea = button.Tag as TempArea;
            WdTempAreaSet w = new WdTempAreaSet();
            w.Init(this.downBlowingSystemService, tempArea);
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < setPlan_items.Count(); i++)
            {
                setPlan_items[i].Dispose();
            }
            setPlan_items.Clear();
        }

        private void UserControl_Initialized(object sender, System.EventArgs e)
        {

        }
    }

    public class UiModule2_TempAreaPanel : IUiModule2
    {
        /// <summary>
        /// 控件标题
        /// 它的值取决于culture
        /// </summary>
        public string Title => "下吹.温控";
        public ComponentType Type => ComponentType.Graph;
        public bool IsUnique => true;

        /// <summary>
        /// 控件
        /// 创建时,需要给它唯一ID,让加载自己的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FrameworkElement GetComponent(int id, IUnityContainer container)
        {
            TempAreaPanel graph = new TempAreaPanel();
            container.BuildUp(graph);
            return graph;
        }

        /// <summary>
        /// 控件缩略图,用于编辑界面时,大致看看
        /// 创建时,需要给它唯一ID,让加载自己的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FrameworkElement GetThumbnail()
        {
            return new System.Windows.Controls.Grid();
        }


        /// <summary>
        /// 给出全部控件ID, 控件自行删除没有的参数
        /// </summary>
        /// <param name="IDs"></param>
        public void MatchParam(int[] IDs)
        {
        }
    }
}