using DatabaseClient;
using Opc.Ua;
using OpcBasic;
using Robot.Common;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Threading;

namespace MesWork
{
    /// <summary>
    /// 
    /// </summary>
    public class AgvRead
    {
        /// <summary>
        /// 
        /// </summary>
        public string from_to_loc = "";
        /// <summary>
        /// 
        /// </summary>
        string from_to_loc_old = "";
        /// <summary>
        /// 
        /// </summary>
        public string tagID = "";
        /// <summary>
        /// 
        /// </summary>
        string opName = "";
        /// <summary>
        /// 
        /// </summary>
        public string connectStr = "";
        /// <summary>
        /// 
        /// </summary>
        public bool onLine = false;
        /// <summary>
        /// 
        /// </summary>
        public string update_time = "";
        /// <summary>
        /// 
        /// </summary>
        public string from_loc = "";
        /// <summary>
        /// 
        /// </summary>
        public string to_loc = "";
        /// <summary>
        /// 
        /// </summary>
        public void Start(string connectStr,int interval, int intervalUpdate ,string tagID,string opName)
        {
            this.connectStr = connectStr;
            this.tagID = tagID;
            this.opName = opName;

            new Thread(new ThreadStart(delegate () {
                while (true)
                {
                    try
                    {
                        from_to_loc = ReadAgv();
                        if (from_to_loc_old != from_to_loc)
                        {
                            from_to_loc_old = from_to_loc;

                            MesWorkForm.S_C_B_TagIDTrigger(opName,this.tagID, from_to_loc);
                        }
                    }
                    catch (Exception err) { }
                    Thread.Sleep(interval);
                }
            })).Start();

        }
        public string ReadAgv()
        {
            var value = "";
            try
            {
                var sql =
                $" SELECT aid,from_loc,to_loc,start_run,update_time FROM agv_from_to" +
                $" ORDER BY aid LIMIT 1";
                MysqlClient.ExecQueryDataTable(sql, this.connectStr, out DataTable dt, out string err);
                onLine = err.Length < 1;
                if (onLine == false)
                {
                    return value;
                }
                if (dt.Rows.Count > 0)
                {
                    var aid = dt.Rows[0]["aid"].ToString();
                    var from_loc = dt.Rows[0]["from_loc"].ToString();
                    var to_loc = dt.Rows[0]["to_loc"].ToString();
                    var start_run = dt.Rows[0]["start_run"].ToString();
                    update_time = dt.Rows[0]["update_time"].ToString();

                    this.from_loc = from_loc;
                    this.to_loc = to_loc;

                    value = $"{from_loc}__{to_loc}";
                }
            }
            catch (Exception err) 
            { 
            }
            return value;
        }



    }
}
