LedLabelController.cs 11.9 KB
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
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);
             
            if (int.Parse(com.ComCount) <= 0)
            {
                label.notes = ResourceControl.GetString("元器件数量不足", "元器件数量不足");
            }
            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 Task<string> 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);
                                return jsonResponse;
                            }
                            else
                            {
                                string jsonResponse = response.Content.ReadAsStringAsync().Result;
                                LogUtil.info($"电子屏 {opName} 发送{json} 结果 {response.IsSuccessStatusCode}: " + jsonResponse);
                                return jsonResponse;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.info($"电子屏 {opName} 发送{json} 出错 {ex.ToString()}");
            }
            return "";
        }
        //    {
        //    "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 List<DataInfo> queryState(string ip, List<string> ids)
        {
            try
            { 
                if (ids == null)
                {
                    ids = new List<string>();
                } 
                string url = $"http://{ip}/wms/associate/queryTagsInRouterId"; 
                string value = JsonHelper.SerializeObject(ids);

                Task<string> task = Post(url, value, "查询状态信息");
                task.Wait();
                string resutljson = task.Result;
                ReturnData data = JsonHelper.DeserializeJsonToObject<ReturnData>(resutljson);
                if (data != null && data.DataList != null)
                {
                    return data.DataList;
                }
                return new List<DataInfo>();

            }
            catch (Exception ex)
            {
                LogUtil.error($"查询标签{string.Join(",", ids)}状态出错:" + ex.ToString());
            }
            return new List<DataInfo>();
        }
    }
     
          public class ReturnData
        {
            public List<DataInfo> DataList { get; set; }
            /// <summary>
            /// 返回码
            /// </summary>
            public int ResultCode { get; set; }
            /// <summary>
            /// 接口响应信息
            /// </summary>
            public string ResultMsg { get; set; }
        }
 

        //        { "datalist": [
        //{ "hardwareVersion": "4.5", "height": 128, "mac": "99.26.17.85", "manufacture": "CoreWind31", "power": 100, "productionBatch": "20-08-29", "routerId": 1, "rssi": -21, "screenType": 1, "serialNumber": "CNSHZH1000", "shopNumber": "A0015", "showStyle": "拣货模板单列", "softwareVersion": "7.0", "state": true, "status": 4, "tagRegisterEN": 1, "width": 296
        //},{ "hardwareVersion": "4.5", "height": 128, "mac": "99.26.18.21", "manufacture": "CoreWind31", "power": 87, "productionBatch": "20-08-29", "routerId": 1, "rssi": -19, "screenType": 1, "serialNumber": "CNSHZH1000", "shopNumber": "A0015", "showStyle": "拣货模板单列", "softwareVersion": "7.0", "state": true, "status": 4, "tagRegisterEN": 1, "width": 296
        //},{
        //    "hardwareVersion": "4.5", "height": 128, "mac": "99.26.18.36", "manufacture": "CoreWind31", "power": 100, "productionBatch": "20-08-29",
        //"routerId": 1, "rssi": -25, "screenType": 1, "serialNumber": "CNSHZH1000", "shopNumber": "A0015", "showStyle": "拣货模板单列", "softwareVersion": "7.0", "state": true, "status": 4, "tagRegisterEN": 0, "width": 296
        //}
        //],"resultCode": 10, "resultMsg": "success"
        //}
      

        public class DataInfo
        {
            /// <summary>
            /// 硬件版本号
            /// </summary>
            public string HardwareVersion { get; set; }
            /// <summary>
            /// 分辨率(高)
            /// </summary>
            public int Height { get; set; }
            /// <summary>
            /// 标签id号
            /// </summary>
            public string Mac { get; set; }
            /// <summary>
            /// 型号标识
            /// </summary>
            public string Manufacture { get; set; }
            /// <summary>
            /// 标签电量
            /// </summary>
            public int Power { get; set; }
            /// <summary>
            /// 生产批次
            /// </summary>
            public string ProductionBatch { get; set; }
            /// <summary>
            /// 绑定基站ID
            /// </summary>
            public int RouterId { get; set; }
            /// <summary>
            /// 信号强度
            /// </summary>
            public int Rssi { get; set; }
            /// <summary>
            /// 屏幕类型
            /// </summary>
            public int ScreenType { get; set; }
            /// <summary>
            /// 序列号
            /// </summary>
            public string SerialNumber { get; set; }
            /// <summary>
            /// 所属店铺
            /// </summary>
            public string ShopNumber { get; set; }
            /// <summary>
            /// 使用模板名称
            /// </summary>
            public string ShowStyle { get; set; }
            /// <summary>
            /// 软件版本号
            /// </summary>
            public string SoftwareVersion { get; set; }
            /// <summary>
            /// 在线状态
            /// </summary>
            public bool State { get; set; }
            /// <summary>
            /// 更新状态
            /// </summary>
            public int Status { get; set; }
            /// <summary>
            /// 允许标签注册开关
            /// </summary>
            public int TagRegisterEN { get; set; }
            /// <summary>
            /// 分辨率(宽)
            /// </summary>
            public int Width { get; set; }
        } 
}