ScanRequestLabel.cs 11.6 KB
using ExcelDataReader;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using TSA_V.Common;
using TSA_V.LoadCSVLibrary;

namespace TSA_V.DeviceLibrary.manager
{
    public  class ScanRequestLabel
    {
       //static Dictionary<string,string> pairs = new Dictionary<string,string>();  

       /// <summary>
       /// 开灯、记录地址信息
       /// </summary>
       /// <param name="position"></param>
       /// <param name="count"></param>
       /// <param name="name"></param>
       /// <param name="ComponentDes"></param>
       /// <param name="dic"></param>
        public static void RequestPost(TSAVPosition position,string count,string name,string ComponentDes,out Dictionary<string, string> dic)
        {
            Dictionary<string, string> pairs = new Dictionary<string, string>();
            try
            {
                #region
                ////不为空时,关闭上一个指示灯
                //if (pairs.Count>=3)
                //{
                //    DataModel data = new DataModel()
                //    {
                //        ip = pairs["Ip"],
                //        mac = pairs["Mac"],
                //        PNum = pairs["PNum"],
                //        count=count,
                //        name= pairs["Name"],
                //        ComponentDes = pairs["ComponentDes"]   
                //    };
                //    //关灯操作
                //    LogUtil.info($"电子屏关灯:{data.ip},{data.mac},{false},上一个电子屏信息:{data.mac}");
                //    lightTagsLed(data.ip, data.mac, false);

                //    UpdateQty(data);
                //    pairs.Clear();
                //}
                #endregion
                if (!string.IsNullOrWhiteSpace(position.DeviceIP))
                {
                    //开灯
                    LogUtil.info($"电子屏开灯:{position.DeviceIP},{position.Leds},{true}");
                    lightTagsLed(position.DeviceIP, position.Leds, true);                   
                    pairs.Add("Ip", position.DeviceIP);
                    pairs.Add("Mac", position.Leds);
                    pairs.Add("PNum", position.PositionNum);//库位编号
                    pairs.Add("Count",count);
                    pairs.Add("ComponentDes", ComponentDes);
                    pairs.Add("Name", name);                    
                    LogUtil.info($"{ComponentDes};{name};");
                }
                else
                {
                    pairs.Clear();
                }

            }
            catch (Exception)
            {
            }
            dic = pairs;
        }

        /// <summary>
        /// 关灯
        /// </summary>
        /// <param name="pairs"></param>
        public static void Turnoffthelights(Dictionary<string,string> pairs) 
        {           
            DataModel data = new DataModel()
            {
                ip = pairs["Ip"],
                mac = pairs["Mac"],
                PNum = pairs["PNum"],
                count = pairs["Count"],
                name = pairs["Name"],
                ComponentDes = pairs["ComponentDes"]
            };
            //关灯操作
            LogUtil.info($"电子屏关灯:{data.ip},{data.mac},{false},上一个电子屏信息:{data.mac}");
            lightTagsLed(data.ip, data.mac, false);

            UpdateQty(data);
            pairs.Clear();
        }

        /// <summary>
        /// 请求标签,关灯、开灯
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="mac">标签地址</param>
        /// <param name="isled">true=亮灯</param>
        public  static void lightTagsLed(string ip,string mac,bool isled) 
        {
            string ledrgb=isled?"ff00":"0";
            var client = new HttpClient();
            var requests = new HttpRequestMessage(HttpMethod.Post, $"http://{ip}/wms/associate/lightTagsLed");
            Dictionary<string, object> keyValuePairss = new Dictionary<string, object>{
                    { "mac", mac },//标签地址
                    { "lednum", 255 },
                    { "timeout", 0 },
                    { "ledrgb", ledrgb },
                    { "ledmode", 0 },
                    { "reserve", "reserve" },
                    { "cmdtoken", "Inve123ntec" }};
            string json= JsonHelper.SerializeObject(keyValuePairss);    
            var contents = new StringContent($"[{json}]", null, "application/json");
            requests.Content = contents;
            client.SendAsync(requests);
        }

        public static void Switchlanguage(int idenx) 
        {
            if (idenx>1)
            {
                return;
            }
            //查询出所有的mac数据
            var list=CSVPositionReader<TSAVPosition>.getPositionList();
            var  entity= list.Where(a => a.PositionType == 2).ToList();
            if (entity == null)
                return;



            List<string> strings=new List<string>();
            foreach (var item in entity)
            {
                var dic = dictionry(idenx);
                dic["mac"] = item.Leds.ToString();              
                dic["lot"] = item.PositionNum.ToString();
                dic["desc"] = null;
                dic["PN"] = null;
                dic["QTY"] = null;
                string str=JsonHelper.SerializeObject(dic);
                dic.Clear();
                strings.Add(str);
            }
            string jsons = "";
            foreach (var item in strings)
            {
                jsons += item + ",";
            }

            var client = new HttpClient();
            var requests = new HttpRequestMessage(HttpMethod.Post, $"http://{entity[0].DeviceIP}/wms/associate/updateScreen");          
            string json = JsonHelper.SerializeObject(strings);
            string aaa = $"[{jsons.Substring(0,jsons.Length - 1)}]";
            var contents = new StringContent($"[{jsons.Substring(0,jsons.Length-1)}]", null, "application/json");
            requests.Content = contents;
            client.SendAsync(requests);
        }

        /// <summary>
        /// 修改样式模板
        /// </summary>
        /// <param name="ip">ip地址</param>
        /// <param name="mac">mac地址</param>
        /// <param name="count">物料数量</param>
        /// <param name="pnname">物料名称</param>
        /// <param name="PN">库位编号位置名称</param>
        public static void UpdateQty(DataModel data) 
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, $"http://{data.ip}/wms/associate/updateScreen");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Count", "Qty:");
            dic.Add("Describe", "Desc:");
            dic.Add("PN", data.name);
            dic.Add("PartNumber", "Name:");
            dic.Add("QRcode", "123456");
            dic.Add("QTY", $"{int.Parse(data.count)-1}");
            dic.Add("Striptype", "----------------------------------------");
            dic.Add("desc", data.ComponentDes);
            dic.Add("ledrgb", "0");
            dic.Add("ledstate", "0");
            dic.Add("location", "Lot.:");
            dic.Add("logo", "neotel12");
            dic.Add("logoname", "NEO LABEL");
            dic.Add("lot", data.PNum);
            dic.Add("mac", data.mac);
            dic.Add("mappingtype",538);
            dic.Add("outtime", "0");
            dic.Add("styleid", 53);
            string json = JsonHelper.SerializeObject(dic);
            var contents = new StringContent($"[{json}]", null, "application/json");
            request.Content = contents;
            client.SendAsync(request);
        }

        public static Dictionary<string, object> dictionry(int idenx) 
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            //中文请求
            if (idenx == 0)
            {
                dic.Add("Count", "数量:");
                dic.Add("Describe", "物料描述:");               
                dic.Add("PartNumber", "名称:");
                dic.Add("QRcode", "123456");
                dic.Add("Striptype", "----------------------------------------"); 
                dic.Add("ledrgb", "0");
                dic.Add("ledstate", "0");
                dic.Add("location", "库位编号:");
                dic.Add("logo", "neotel12");
                dic.Add("logoname", "NEO LABEL");
                dic.Add("mappingtype", 538);
                dic.Add("outtime", "0");
                dic.Add("styleid", 53);
                dic.Add("mac", null);
                dic.Add("PN", null);
                dic.Add("QTY", $"{null}");
                dic.Add("lot", null);               
                dic.Add("desc", null);
            }
            else if (idenx == 1)//英文请求
            {
                dic.Add("Count", "Qty:");
                dic.Add("Describe", "Desc:");                
                dic.Add("PartNumber", "Name:");
                dic.Add("QRcode", "123456");               
                dic.Add("Striptype", "----------------------------------------");
                dic.Add("ledrgb", "0");
                dic.Add("ledstate", "0");
                dic.Add("location", "Lot.:");
                dic.Add("logo", "neotel12");
                dic.Add("logoname", "NEO LABEL");              
                dic.Add("mappingtype", 538);
                dic.Add("outtime", "0");
                dic.Add("styleid", 53);
                dic.Add("mac", null);
                dic.Add("PN", null);
                dic.Add("QTY", $"{null}");
                dic.Add("lot", null);
                dic.Add("desc", null);
            }
            return dic;
        }

        //public static DataTable GetData(string filePath) 
        //{

        //    // 设置文件路径和工作表名称  
        //    //string filePath = "path/to/your/excel/file.xlsx";
        //    string worksheetName = "Sheet1";

        //    // 创建ExcelDataReader对象并打开工作簿  
        //    using (var reader = ExcelReaderFactory.CreateReader(filePath))
        //    {
        //        reader.IsFirstRowAsColumnNames = true; // 将第一行作为列名  
        //        var result = reader.AsDataSet(new ExcelDataSetConfiguration()
        //        {
        //            ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
        //            {
        //                UseHeaderRow = true // 使用标题行作为列名  
        //            }
        //        });

        //        // 获取指定工作表的数据表  
        //        DataTable dt = result.Tables[worksheetName];

        //        // 在此处使用数据表进行处理  
        //    }
        //}

        public class DataModel
        {
           /// <summary>
           /// ip地址
           /// </summary>
            public string ip { get; set; }
            /// <summary>
            /// mac地址
            /// </summary>
            public string mac { get; set; }
            /// <summary>
            /// 记录上一个库位编号
            /// </summary>
            public string PNum { get; set; }
            /// <summary>
            /// 元器件数量
            /// </summary>
            public string count { get; set; }

            /// <summary>
            /// 元器件名称
            /// </summary>
            public string name { get; set; }

            /// <summary>
            /// 元器件描述
            /// </summary>
            public string ComponentDes { get; set; }
        }
    }
}