using com.hnshituo.core.webapp.vo;
using Common;
using iCore.Rtdb.RtdbTable;
using Infragistics.Win.UltraWinEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BeltScaleSystem
{
    public partial class ucBeltInstrumentInfo_Real : UserControl
    {
        public ucBeltInstrumentInfo_Real()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 计量点信息
        /// </summary>
        public string sPointNo = "", sPointName = "", collectionCodeA="", collectionCodeB="";
        MemoryTableDataSocket_Belt MemoClass = new MemoryTableDataSocket_Belt();
        private ComBaseInfoService comBaseInfoService = new ComBaseInfoService(); //基础数服务

        private void ucBeltInstrumentInfo_Load(object sender, EventArgs e)
        {
            RESTfulResult<List<ComBaseInfo>> rrComGradeName = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001051", baseName = IpAddress.getIp });
            if (rrComGradeName.Succeed && rrComGradeName.Data != null && rrComGradeName.Data.Count > 0)
            {
                this.PntNameLabel.Text = sPointName;
                this.timer1.Start();
            }
            else
            {
                this.Visible = false;
                MessageBox.Show("该Ip:" + IpAddress.getIp + "无此页面权限！");
                return;
            }
          
        }

        private delegate void UpdateUIEventHander(object sender, UpdateUIArgs args); //自定义事件用来从线程中更新控件的值
        /// <summary>
        /// 更新界面的UI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void UpdateUI_Method(object sender, UpdateUIArgs args)
        {
            if (sender is UltraTextEditor)
            {
                if (args.textValue == "" || args.textValue == null)
                {
                    ((UltraTextEditor)sender).Text = "0 " + args.unitValue;
                }
                else
                {
                    ((UltraTextEditor)sender).Text = args.textValue + " " + args.unitValue;
                }
            }
            else if (sender is Button)
            {
                if (args.textValue == "red")
                {
                    ((Button)sender).BackColor = Color.Red;
                    ((Button)sender).ForeColor = Color.White;
                }
                else if (args.textValue == "green")
                {
                    ((Button)sender).BackColor = Color.White;
                    ((Button)sender).ForeColor = Color.Black;
                }
                else
                {
                    ((Label)sender).Text = args.textValue;
                }
            }
        }
        public class UpdateUIArgs : EventArgs
        {
            public string textValue { get; private set; }
            public string unitValue { get; private set; }
            public UpdateUIArgs(string textValue, string unit)
            {
                this.textValue = textValue;
                this.unitValue = unit;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            List<LiveData> ll = MemoClass.getWarnInfo_Belt(collectionCodeA);
         //   List<LiveData> l2 = MemoClass.getWarnInfo_Belt(collectionCodeB);
            if (ll != null)
            {
                foreach (LiveData lv in ll)
                {
                    string value = lv.Value == null ? "" : lv.Value.ToString().Trim();
                    switch (lv.Tagname.Replace(collectionCodeA, ""))
                    {
                        case "Flow"://瞬时流量
                             setTedValue("realtime_amount_Ted", Math.Round(Convert.ToDouble(lv.Value), 2).ToString());//实时量
                            break;

                        //case "Total"://总累计量
                        //    double total = Math.Round(Convert.ToDouble(lv.Value), 2);
                        //    setTedValue("cumulative_weight_Ted", (Math.Floor(total / 100) * 100).ToString());
                        //    break;

                        //case "BeltSpeed"://皮带速度
                        //    setTedValue("belt_speed_Ted", Math.Round(Convert.ToDouble(lv.Value), 2).ToString());
                        //    break;

                        //case "FieldTemp": //现场温度
                        //    setTedValue("temperature_Ted", Math.Round(Convert.ToDouble(lv.Value), 2).ToString());
                        //    break;
                    }
                }
            }
            //if (l2 != null)
            //{
            //    foreach (LiveData lv in l2)
            //    {
            //        string value = lv.Value == null ? "" : lv.Value.ToString().Trim();
            //        switch (lv.Tagname.Replace(collectionCodeB, ""))
            //        {
            //            case "Flow"://瞬时流量
            //                setTedValue("realtime_amount_Ted_1", Math.Round(Convert.ToDouble(lv.Value), 2).ToString());//实时量
            //                break;

            //            case "Total"://总累计量
            //                double total = Math.Round(Convert.ToDouble(lv.Value), 2);
            //                setTedValue("cumulative_weight_Ted_1", (Math.Floor(total / 100) * 100).ToString());
            //                break;

            //            case "BeltSpeed"://皮带速度
            //                setTedValue("belt_speed_Ted_1", Math.Round(Convert.ToDouble(lv.Value), 2).ToString());
            //                break;

            //            case "FieldTemp": //现场温度
            //                setTedValue("temperature_Ted_1", Math.Round(Convert.ToDouble(lv.Value), 2).ToString());
            //                break;
            //        }
            //    }
            //}
        }

        public void Close() {

            timer1.Stop();
        
        }

        private void setTedValue(string Ted, string values)
        {
            try
            {
                switch (Ted)
                {
                    //A磅
                    case "instrument_weight_Ted"://仪表重量
                     //   this.instrument_weight_Ted.Invoke(new UpdateUIEventHander(UpdateUI_Method), instrument_weight_Ted, new UpdateUIArgs(values, "t"));
                        break;
                    case "cumulative_weight_Ted"://当前累计量
                      //  this.cumulative_weight_Ted.Invoke(new UpdateUIEventHander(UpdateUI_Method), cumulative_weight_Ted, new UpdateUIArgs(values, "t"));
                        break;
                    case "realtime_amount_Ted"://实时量
                        this.realtime_amount_Ted.Invoke(new UpdateUIEventHander(UpdateUI_Method), realtime_amount_Ted, new UpdateUIArgs(values, "t/h"));
                        break;
                    case "belt_speed_Ted"://皮带速度
                       // this.belt_speed_Ted.Invoke(new UpdateUIEventHander(UpdateUI_Method), belt_speed_Ted, new UpdateUIArgs(values, "m/s"));
                        break;
                    case "temperature_Ted"://温度
                       // this.temperature_Ted.Invoke(new UpdateUIEventHander(UpdateUI_Method), temperature_Ted, new UpdateUIArgs(values, "℃"));
                        break;
                    case "parameter_Ted"://物料参数
                      //  this.parameter_Ted.Invoke(new UpdateUIEventHander(UpdateUI_Method), parameter_Ted, new UpdateUIArgs(values, ""));
                        break;
                    //B磅
                    case "instrument_weight_Ted_1"://仪表重量
                       // this.instrument_weight_Ted_1.Invoke(new UpdateUIEventHander(UpdateUI_Method), instrument_weight_Ted_1, new UpdateUIArgs(values, "t"));
                        break;
                    case "cumulative_weight_Ted_1"://当前累计量
                      //  this.cumulative_weight_Ted_1.Invoke(new UpdateUIEventHander(UpdateUI_Method), cumulative_weight_Ted_1, new UpdateUIArgs(values, "t"));
                        break;
                    case "realtime_amount_Ted_1"://实时量
                     //   this.realtime_amount_Ted_1.Invoke(new UpdateUIEventHander(UpdateUI_Method), realtime_amount_Ted_1, new UpdateUIArgs(values, "t/h"));
                        break;
                    case "belt_speed_Ted_1"://皮带速度
                     //   this.belt_speed_Ted_1.Invoke(new UpdateUIEventHander(UpdateUI_Method), belt_speed_Ted_1, new UpdateUIArgs(values, "m/s"));
                        break;
                    case "temperature_Ted_1"://温度
                    //    this.temperature_Ted_1.Invoke(new UpdateUIEventHander(UpdateUI_Method), temperature_Ted_1, new UpdateUIArgs(values, "℃"));
                        break;
                    case "parameter_Ted_1"://物料参数
                      //  this.parameter_Ted_1.Invoke(new UpdateUIEventHander(UpdateUI_Method), parameter_Ted_1, new UpdateUIArgs(values, ""));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        //更新UltraTextEditor、Button控件值
        private double getTedValue(string Ted, string values)
        {
            try
            {
                switch (Ted)
                {
                    //A磅
                    case "instrument_weight_Ted"://仪表重量
                   //     return Convert.ToDouble(instrument_weight_Ted.Text.Replace("t", "").Trim());
                    case "cumulative_weight_Ted"://当前累计量
                    //    return Convert.ToDouble(cumulative_weight_Ted.Text.Replace("t", "").Trim());
                    case "realtime_amount_Ted"://实时量
                        return Convert.ToDouble(realtime_amount_Ted.Text.Replace("t/h", "").Trim());
                    case "belt_speed_Ted"://皮带速度
                      //  return Convert.ToDouble(belt_speed_Ted.Text.Replace("m/s", "").Trim());
                    case "temperature_Ted"://温度
                    //    return Convert.ToDouble(temperature_Ted.Text.Replace("℃", "").Trim());
                    case "parameter_Ted"://物料参数
                    //    return Convert.ToDouble(parameter_Ted.Text);
                    //B磅
                    case "instrument_weight_Ted_1"://仪表重量
                      //  return Convert.ToDouble(instrument_weight_Ted_1.Text.Replace("t", "").Trim());
                    case "cumulative_weight_Ted_1"://当前累计量
                   //     return Convert.ToDouble(cumulative_weight_Ted_1.Text.Replace("t", "").Trim());
                    case "realtime_amount_Ted_1"://实时量
                       // return Convert.ToDouble(realtime_amount_Ted_1.Text.Replace("t/h", "").Trim());
                    case "belt_speed_Ted_1"://皮带速度
                      //  return Convert.ToDouble(belt_speed_Ted_1.Text.Replace("m/s", "").Trim());
                    case "temperature_Ted_1"://温度
                    //    return Convert.ToDouble(temperature_Ted_1.Text.Replace("℃", "").Trim());
                    case "parameter_Ted_1"://物料参数
                      //  return Convert.ToDouble(parameter_Ted_1.Text);

                    default:
                        return 0;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        /// <summary>
        /// 登录人及登陆IP信息
        /// </summary>
        public string  sIpAddress = "";




    }
}
