LedLabelController.cs 10.6 KB
using System;
using System.Collections.Generic; 
using System.Net.Http; 
using TSA_V.Common;
using TSA_V.LoadCSVLibrary;

namespace TSA_V.DeviceLibrary
{
    public class LedLabelController
    { 
         
        public static void OpenAll()
        {
            OpenOrCloseAll(true);

        }
        public static void CloseAll()
        {
            OpenOrCloseAll(false);
        }
        private static void OpenOrCloseAll(bool open)
        {
            List<TSAVPosition> positions = CSVPositionReader<TSAVPosition>.getPositionList();
            List<string> macs = new List<string>();
            List<bool> ledvs = new List<bool>();
            string ip = "";
            foreach (var pos in positions)
            {
                if (pos.PositionType.Equals(2))
                {
                    ip = pos.DeviceIP;
                    macs.Add(pos.Leds);
                    ledvs.Add(open);
                }
            }
            if (macs.Count > 0)
            {
                string url = $"http://{ip}/wms/associate/lightTagsLed";
                string json = JsonHelper.SerializeObject(getLedMaps(macs, ledvs));
                Post(url, json,(open ? "打开所有":"关闭所有"));
            }
        }
        public static LabelInfo GetLabel(TSAVPosition position, ComponetInfo com, bool opendLed   )
        {
            if (com == null)
            {
                com = new ComponetInfo();
            }
            if (position == null)
            {
                position = new TSAVPosition();
            }
            if (!position.PositionType.Equals(2))
            {
                return new LabelInfo();
            }
            LabelInfo label = new LabelInfo(position.PositionNum, com.ComCount, com.PN, com.ComponentDes, com.Notes, position.DeviceIP, position.Leds, opendLed);

            return label;

        }
        public static void OpenLed(string ip, string mac)
        {
            controlLed(ip, mac, true);
        }
        public static void CloseLed(string ip, string mac)
        {
            controlLed(ip, mac, false);
        }

        private static List<Dictionary<string, object>> getLedMaps(List<string> mac, List<bool> ledrgb)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            for (int i = 0; i < mac.Count; i++)
            {
                if (ledrgb.Count > i)
                {
                    string rgb = ledrgb[i] ? "ff00" : "0";

                    list.Add(getLedMap(mac[i], rgb));
                }
            }
            return list;
        }

        private static Dictionary<string, object> getLedMap(string mac, string ledrgb)
        {
            Dictionary<string, object> keyValuePairss = new Dictionary<string, object>{
                    { "mac", mac .Trim()},//标签地址
                    { "lednum", 255 },
                    { "timeout", 0 },
                    { "ledrgb", ledrgb },
                    { "ledmode", 0 },
                    { "reserve", "reserve" },
                    { "cmdtoken", "Inve123ntec" }};
            return keyValuePairss;
        }
        private static void controlLed(string ip, string mac, bool openLed)
        {
            if (ip == "")
            {
                return;
            }
            string ledrgb = openLed ? "ff00" : "0";

            string url = $"http://{ip}/wms/associate/lightTagsLed";

            Dictionary<string, object> keyValuePairss = getLedMap(mac, ledrgb);
            string json = JsonHelper.SerializeObject(keyValuePairss);
            string value = $"[{json}]";
            Post(url, value, mac + (openLed ?"开灯":"关灯"));
        }
        public static void UpdateScreen(LabelInfo label)
        {
            if (label.ip == "")
            {
                return;
            }

            string url = $"http://{label.ip}/wms/associate/updateScreen";

            Dictionary<string, object> dic = new Dictionary<string, object>();

            dic.Add("mac", label.mac.Trim());
            dic.Add("mappingtype", "868");
            dic.Add("styleid", 47);
            //dic.Add("ledrgb", "ff00");
            dic.Add("ledstate", "0");
            dic.Add("outtime", "0");

            Dictionary<string, object> lmap = label.toMap();
            foreach (string key in lmap.Keys)
            {
                if (dic.ContainsKey(key))
                {
                    dic[key] = lmap[key];
                }
                else
                {
                    dic.Add(key, lmap[key]);
                }
            }
            string json = JsonHelper.SerializeObject(dic);
            string value = $"[{json}]";
            Post(url, value,"更新屏幕"); 
        }

        public static async void Post(string url, string json, string opName = "")
        {
            try
            { 
                using (var client = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(HttpMethod.Post, url))
                    {
                        var contents = new StringContent($"{json}", null, "application/json");
                        request.Content = contents;
                        using (HttpResponseMessage response = await client.SendAsync(request))
                        {
                            if (response.IsSuccessStatusCode)
                            {
                                string jsonResponse = response.Content.ReadAsStringAsync().Result;
                                LogUtil.info($"电子屏 {opName} 发送{json} 结果 {response.IsSuccessStatusCode}: " + jsonResponse);
                            }
                            else
                            {
                                string jsonResponse = response.Content.ReadAsStringAsync().Result;
                                LogUtil.info($"电子屏 {opName} 发送{json} 结果 {response.IsSuccessStatusCode}: " + jsonResponse); 
                            } 
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.info($"电子屏 {opName} 发送{json} 出错 {ex.ToString()}");
            }
        }
        //    {
        //    "mac":"99.96.19.64",
        //    "mappingtype":868,
        //    "styleid":47,
        //    "lblPos":"位置:",
        //    "pos":"1_8",
        //    "lblNum":"库位数量:",
        //    "lblPn":"物料编码:",
        //    "lblDes":"描述:",
        //    "lblNotes":"注意事项:",
        //    "num":"234",
        //    "pn":"R218",
        //    "des":"描述内容",
        //    "notes":"注意事项内容", 
        //    "ledrgb":"ff00",
        //    "ledstate":"0",
        //    "outtime":"0"
        //}

        //private static void controlLed(string ip, string mac, bool openLed)
        //{
        //    string ledrgb = openLed ? "ff00" : "0";
        //    using (var client = new HttpClient())
        //    {
        //        string url = $"http://{ip}/wms/associate/lightTagsLed";
        //        using (var requests = new HttpRequestMessage(HttpMethod.Post, url))
        //        {
        //            Dictionary<string, object> keyValuePairss = getLedMap(mac, ledrgb);
        //            string json = JsonHelper.SerializeObject(keyValuePairss);
        //            var contents = new StringContent($"[{json}]", null, "application/json");
        //            LogUtil.info($"电子屏 {ip}-{mac} : {(openLed ? "开灯" : "关灯")}  url:{url}, 发送内容:{json}");
        //            requests.Content = contents;
        //            using (HttpResponseMessage response = client.SendAsync(requests).Result)
        //            {
        //                if (response.IsSuccessStatusCode)
        //                {
        //                    string jsonResponse = response.Content.ReadAsStringAsync().Result;
        //                    LogUtil.info($"电子屏 {ip}-{mac} : {(openLed ? "开灯" : "关灯")}  结果: " + jsonResponse);
        //                }
        //                else
        //                {
        //                    LogUtil.info($"电子屏 {ip}-{mac} : {(openLed ? "开灯" : "关灯")}  结果: " + response.RequestMessage.ToString());

        //                }

        //            }

        //            requests.Dispose();
        //        }
        //    }
        //}
        //public static void UpdateScreen(LabelInfo label)
        //{
        //    string ledrgb = label.openLed ? "ff00" : "0";
        //    using (var client = new HttpClient())
        //    {
        //        string url = $"http://{label.ip}/wms/associate/updateScreen";
        //        using (var request = new HttpRequestMessage(HttpMethod.Post, url))
        //        {
        //            Dictionary<string, object> dic = new Dictionary<string, object>();

        //            dic.Add("mac", label.mac);
        //            dic.Add("mappingtype", "868");
        //            dic.Add("styleid", 47);
        //            dic.Add("ledrgb", "0");
        //            dic.Add("ledstate", "0");
        //            dic.Add("outtime", "0");

        //            Dictionary<string, object> lmap = label.toMap();
        //            foreach (string key in lmap.Keys)
        //            {
        //                if (dic.ContainsKey(key))
        //                {
        //                    dic[key] = lmap[key];
        //                }
        //                else
        //                {
        //                    dic.Add(key, lmap[key]);
        //                }
        //            }
        //            string json = JsonHelper.SerializeObject(dic);
        //            var contents = new StringContent($"[{json}]", null, "application/json");
        //            LogUtil.info($"电子屏 {label.ip}-{label.mac} : 更改屏幕内容  url:{url}, 发送内容:{json}");
        //            request.Content = contents;
        //            using (HttpResponseMessage response = client.SendAsync(request).Result)
        //            {
        //                if (response.IsSuccessStatusCode)
        //                {
        //                    string jsonResponse = response.Content.ReadAsStringAsync().Result;
        //                    LogUtil.info($"电子屏 {label.ip}-{label.mac} : 更改屏幕结果: " + jsonResponse);
        //                }
        //                else
        //                {
        //                    LogUtil.info($"电子屏 {label.ip}-{label.mac} : 更改屏幕失败: " + response.RequestMessage.ToString());

        //                }

        //            }
        //        }
        //    }
        //}

    }
}