BOXManager_Partial.cs 8.9 KB
using SmartShelf.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartShelf.DeviceLibrary
{
    partial class BOXManager
    {
        #region HTT服务器通信

        private static bool isInProcess = false;
        private static void server_connect_timer(object sender, EventArgs e)
        {

            if (isInProcess)
            {
                return;
            }
            isInProcess = true;
            try
            {
                SendInOutPosId();
            }
            catch (Exception ex)
            {
                LogUtil.error("server_connect_timer_Tick 出错:" + ex.ToString());
            }
            isInProcess = false;
        }
       

        private  static Operation GetLineBoxStatus()
        {
            //构建发送给服务器的对象
            Operation lineOperation = new Operation();
            lineOperation.cid = BOXManager.CID;
            lineOperation.seq = ConfigAppSettings.nextSeq();
            lineOperation.status = 1;
            BoxStatus boxStatus = new BoxStatus();
            boxStatus.boxId = 1;
            boxStatus.status = 1;
            lineOperation.boxStatus.Add(1, boxStatus);
            return lineOperation;
        }
        private static string api_communication = "service/store/communication";	//流水线状态通信接口
      
        public static string GetPostApi(string host)
        {
            if (host == "")
            {
                host = ConfigAppSettings.GetValue(Setting_Init.HttpServerAddr);
            }
            if (!host.StartsWith("http://"))
            {
                host = "http://" + host;
            }
            if (!host.EndsWith("/"))
            {
                host = host + "/";
            }
            return host + api_communication;
        }
        //   public static string StrMsg = "";
        public static void SendInOutPosId()
        {
            StrMsg = "";
            //构建发送给服务器的对象
            Operation lineOperation = GetLineBoxStatus();
            //获取亮灯的库位
            string posId = "";
            foreach (string key in PosIdColorMap.Keys)
            {
                if (!String.IsNullOrEmpty(PosIdColorMap[key]))
                {
                    posId = posId + key + "|";
                }
            }
            if (String.IsNullOrEmpty(posId).Equals(false))
            {
                lineOperation.data.Add(ParamDefine.posOpened, posId);
                
            }

            foreach (string str in lineOperation.data.Keys)
            {
                StrMsg += "[" + str + "=" + lineOperation.data[str] + "]\r\n";
            }
            string server = ConfigAppSettings.GetValue(Setting_Init.HttpServerAddr);
            Operation resultOperation = HttpHelper.Post(GetPostApi(server), lineOperation);

            if (resultOperation == null || resultOperation.data == null)
            {
                //判断服务端是否返回出库操作
                return;
            } 
            //与服务器通信用 | 分割
            if (resultOperation.data.ContainsKey(ParamDefine.open))
            {
                ProcessOpenLed(resultOperation.data[ParamDefine.open]);
            }
            if (resultOperation.data.ContainsKey(ParamDefine.close))
            {
                ProcessCloseLed(resultOperation.data[ParamDefine.close]);
            }

            if (resultOperation.data.ContainsKey(ParamDefine.closeAll))
            {
                ProcessCloseAll();
            }
        }


        #endregion


        #region HTTP接口协议通信

        private static int ServerPort = ConfigAppSettings.GetIntValue(Setting_Init.ServerPort);
        public static string Path_LedOn = "/rest/api/v1/shelf/posOn";
        public static string Path_LedOff = "/rest/api/v1/shelf/posOff";
        public static string Path_ledOnAll = "/rest/api/v1/shelf/allPosOn";
        public static string Path_ledOffAll = "/rest/api/v1/shelf/allPosOff";

        public static string Param_posId = "posId";
        public static string Param_color = "color";
        public static string Param_shelf = "shelf";
        public static char PosId_SpiltChar = ';';
        public static char PosId_Color_SpiltChar = '@';

        // 开灯:http:/localhost:80/rest/api/v1/shelf/posOn?posId=1_3_1@green;1_3_2@green;1_3_7@red
        // 关灯:http:/localhost:80/rest/api/v1/shelf/posOff?posId=1_3_1;1_3_2;1_3_4
        //关闭所有:http:/localhost:80/rest/api/v1/shelf/allPosOn
        //打开所有:http:/localhost:80/rest/api/v1/shelf/allPosOff
        //rest/api/v1/shelf/allPosOff? color = green     指定灭灯的颜色,如果为空,所有灯全部熄灭
        private static string ServerOnReceived(string reqPath, string paramStr)
        {
            LogUtil.info("ServerOnReceived [" + reqPath + "] [" + paramStr + "] ");
            paramStr = paramStr.Replace("%23", "#");
            try
            {
                Dictionary<string, string> paramMap = GetParam(paramStr);
                string msg = ": conot find param " + Param_posId;
                if (reqPath.Equals(Path_LedOn))
                {
                    if (paramMap.ContainsKey(Param_posId))
                    {
                        string pos = paramMap[Param_posId];
                        bool result = ProcessOpenLed(pos);
                        return GetResult(result, "posOn");
                    }
                    else
                    {
                        return GetResult(false, "posOn") + msg;
                    }
                }
                else if (reqPath.Equals(Path_LedOff))
                {
                    if (paramMap.ContainsKey(Param_posId))
                    {
                        string pos = paramMap[Param_posId];
                        bool result = ProcessCloseLed(pos);
                        return GetResult(result, "posOff");
                    }
                    else
                    {
                        return GetResult(false, "posOff") + msg;
                    }
                }
                else if (reqPath.Equals(Path_ledOnAll))
                {
                    bool result = ProcessOpenAll();
                    return GetResult(result, "allPosOn");
                }
                else if (reqPath.Equals(Path_ledOffAll))
                {
                    bool result = true;
                    string color = "";
                    string shelf = "";
                    if (paramMap.ContainsKey(Param_color) && (paramMap[Param_color] != ""))
                    {
                        color = paramMap[Param_color];
                    }
                    if (paramMap.ContainsKey(Param_shelf) && paramMap[Param_shelf] != "")
                    {
                        shelf = paramMap[Param_shelf];
                    }


                    if (String.IsNullOrEmpty(color) && String.IsNullOrEmpty(shelf))
                    {
                        result = ProcessCloseAll();
                    }
                    else
                    {
                        result = ProcessCloseByParam(color, shelf);
                    }
                    return GetResult(result, "allPosOff");
                }
                else
                {
                    LogUtil.info("[" + reqPath + "]没有相关处理");
                    return "[" + reqPath + "]没有相关处理";
                }
                return "OK";
            }
            catch (Exception ex)
            {
                LogUtil.info("[" + reqPath + "][" + paramStr + "]处理错误:" + ex.ToString());
                return "Error";
            }
        }
        private static Dictionary<string, string> GetParam(string paramStr)
        {
            Dictionary<string, string> paramMap = new Dictionary<string, string>();

            try
            {
                string[] paramArray = paramStr.Split('&');
                foreach (string param in paramArray)
                {
                    string[] data = param.Split('=');
                    if (data.Length == 2)
                    {
                        string paramName = data[0];
                        string paramValue = data[1];
                        //LogUtil.info("解析参数【"+paramStr+"】 : ["+paramName+"]["+paramValue+"]");
                        paramMap.Add(paramName, paramValue);
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("解析参数【" + paramStr + "】出错:" + ex.ToString());
            }
            return paramMap;
        }


        private static string GetResult(bool result, string op)
        {
            if (result)
            {
                return op + " OK";
            }
            else
            {
                return op + " FAIL";
            }
        }

        #endregion
 
    }
}