Commit bdb4f1c3 LN

给服务器发送消息增加锁验证

1 个父辈 e34d6510
...@@ -12,64 +12,49 @@ using System.Security.Cryptography; ...@@ -12,64 +12,49 @@ using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Reflection; using System.Reflection;
using log4net; using log4net;
using System.Threading;
namespace OnlineStore.Common namespace OnlineStore.Common
{ {
public class HttpHelper public class HttpHelper
{ {
public static int isLog = ConfigAppSettings.GetIntValue(Setting_Init.Server_Log_Open); public static int isLog = ConfigAppSettings.GetIntValue(Setting_Init.Server_Log_Open);
private static object lockObj = "";
public static Operation PostOP(string url, Operation operation, bool simulate) public static Operation PostOP(string url, Operation operation, int lockTime = 0)
{
try
{ {
if (simulate) if (Monitor.TryEnter(lockObj, lockTime))
{//模拟服务器返回
operation.status = 200;
operation.data.Clear();
operation.data.Add("posId", "A001");
string pos = "D100-111#D102-222#D104-333";
operation.data.Add("pos", pos);
if (operation.op == 1 || operation.op == 2)
{//入库或出库
string json = JsonHelper.SerializeObject(operation);
LogUtil.error( "模拟HTTP服务器返回出库入库信息:" + json);
return operation;
}
}
else
{ {
string json = "";
try try
{ {
json = JsonHelper.SerializeObject(operation); string json = JsonHelper.SerializeObject(operation);
}
catch (Exception ex)
{
LogUtil.error("PostOP JsonHelper.SerializeObject(operation) 出错【operation.op=" + operation.op + "】" + ex);
}
string result = PostJson(url, json, Encoding.UTF8); string result = PostJson(url, json, Encoding.UTF8);
if (operation.op > 0) if (operation.op > 0)
{ {
LogUtil.info("send 【"+url+"】【"+json+"】 " + ",receive 【 " + result+"】"); LogUtil.info("send 【" + url + "】【" + json + "】 " + ",receive 【 " + result + "】");
} }
if (!string.IsNullOrEmpty(result)) if (!string.IsNullOrEmpty(result))
{ {
try Operation resultOP = JsonHelper.DeserializeJsonToObject<Operation>(result);
if (resultOP != null && (!operation.seq.Equals(resultOP.seq)))
{ {
return JsonHelper.DeserializeJsonToObject<Operation>(result); LogUtil.error(" seq error : send 【" + json + "】 " + ",receive 【 " + result + "】");
}
return resultOP;
}
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error("PostOP JsonHelper.DeserializeJsonToObject 出错【result=" + result + "】" + ex); LogUtil.error("PostOP error【operation.op=" + operation.op + "】:" + ex);
}
} }
finally
{
Monitor.Exit(lockObj);
} }
} }
catch (Exception ex) else if (lockTime > 10)
{ {
LogUtil.error("PostOP 出错【operation.op=" + operation.op + "】:" + ex); string json = JsonHelper.SerializeObject(operation);
LogUtil.error(" PostOP " + lockTime + " send " + json + " TryEnter fail ");
} }
return null; return null;
} }
......
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using Newtonsoft.Json; using Newtonsoft.Json;
...@@ -16,9 +17,17 @@ namespace OnlineStore.Common ...@@ -16,9 +17,17 @@ namespace OnlineStore.Common
/// <returns>json字符串</returns> /// <returns>json字符串</returns>
public static string SerializeObject(object o) public static string SerializeObject(object o)
{ {
try
{
string json = JsonConvert.SerializeObject(o); string json = JsonConvert.SerializeObject(o);
return json; return json;
} }
catch (Exception ex)
{
LogUtil.error("JsonHelper.SerializeObject error【" + o.ToString() + "】" + ex);
}
return "";
}
/// <summary> /// <summary>
/// 解析JSON字符串生成对象实体 /// 解析JSON字符串生成对象实体
...@@ -28,12 +37,20 @@ namespace OnlineStore.Common ...@@ -28,12 +37,20 @@ namespace OnlineStore.Common
/// <returns>对象实体</returns> /// <returns>对象实体</returns>
public static T DeserializeJsonToObject<T>(string json) where T : class public static T DeserializeJsonToObject<T>(string json) where T : class
{ {
try
{
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json); StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T)); object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
T t = o as T; T t = o as T;
return t; return t;
} }
catch (Exception ex)
{
LogUtil.error(" JsonHelper.DeserializeJsonToObject error【" + json + "】" + ex);
}
return null;
}
/// <summary> /// <summary>
/// 解析JSON数组生成对象实体集合 /// 解析JSON数组生成对象实体集合
......
...@@ -963,7 +963,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -963,7 +963,7 @@ namespace OnlineStore.DeviceLibrary
{ {
if (IsChongfu) if (IsChongfu)
{ {
LogUtil.info("【" + info + "】 处理完成,耗时:" + stopwatch.Elapsed.ToString()); LogUtil.info("ShowTimeLog【" + info + "】 :" + stopwatch.Elapsed.ToString()+" ms");
} }
} }
private void LedProcess() private void LedProcess()
...@@ -1867,7 +1867,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -1867,7 +1867,7 @@ namespace OnlineStore.DeviceLibrary
Operation lineOperation = getLineBoxStatus(); Operation lineOperation = getLineBoxStatus();
string server = ConfigAppSettings.GetValue(Setting_Init.http_server); string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
Operation resultOperation = HttpHelper.PostOP(StoreManager.GetPostApi(server), lineOperation, false); Operation resultOperation = HttpHelper.PostOP(StoreManager.GetPostApi(server), lineOperation);
//发送状态信息到服务器 //发送状态信息到服务器
if (resultOperation == null ) if (resultOperation == null )
{ {
...@@ -1879,7 +1879,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -1879,7 +1879,7 @@ namespace OnlineStore.DeviceLibrary
TimeSpan span = DateTime.Now - time; TimeSpan span = DateTime.Now - time;
if (span.TotalMilliseconds > 500) if (span.TotalMilliseconds > 500)
{ {
LogUtil.info(StoreName + "SendLineStatus 共处理了【" + span.TotalMilliseconds + "】毫秒"); LogUtil.info(StoreName + "SendLineStatus 【" + span.TotalMilliseconds + "】ms");
} }
} }
...@@ -1902,12 +1902,11 @@ namespace OnlineStore.DeviceLibrary ...@@ -1902,12 +1902,11 @@ namespace OnlineStore.DeviceLibrary
} }
else if (resultOperation.op.Equals(1)) else if (resultOperation.op.Equals(1))
{ {
string json = JsonHelper.SerializeObject(resultOperation);
//ReviceInStoreProcess("", resultOperation); //ReviceInStoreProcess("", resultOperation);
LogUtil.error("发送定时消息时,服务器返回Op=1 【" + json + "】");
} }
//else if (resultOperation.op.Equals(5))
//{
// ProcessHumidityCMD(resultOperation);
//}
else if (resultOperation.data != null) else if (resultOperation.data != null)
{ {
string result = ""; string result = "";
......
...@@ -495,7 +495,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -495,7 +495,7 @@ namespace OnlineStore.DeviceLibrary
//message = CodeManager.ReplaceCode(message); //message = CodeManager.ReplaceCode(message);
if (message.Equals("") || string.IsNullOrEmpty(message)) if (message.Equals("") || string.IsNullOrEmpty(message))
{ {
CodeMsg =ResourceControl.GetString(ResourceControl.NoCodeMsg, "没有收到二维码信息,请重新放入料盘"); CodeMsg = ResourceControl.GetString(ResourceControl.NoCodeMsg, "没有收到二维码信息,请重新放入料盘");
LogUtil.info(LogName + "没有收到二维码信息,请重新放入料盘"); LogUtil.info(LogName + "没有收到二维码信息,请重新放入料盘");
return null; return null;
} }
...@@ -528,11 +528,11 @@ namespace OnlineStore.DeviceLibrary ...@@ -528,11 +528,11 @@ namespace OnlineStore.DeviceLibrary
LogUtil.debug("添加自动入库库位号:" + autoposId); LogUtil.debug("添加自动入库库位号:" + autoposId);
} }
string server = ConfigAppSettings.GetValue(Setting_Init.http_server); string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
Operation resultOperation = HttpHelper.PostOP(StoreManager.GetPostApi(server), operation, false); Operation resultOperation = HttpHelper.PostOP(StoreManager.GetPostApi(server), operation, 2000);
if (resultOperation == null) if (resultOperation == null)
{ {
LogUtil.info(LogName + "二维码【" + message + "】没有收到服务器反馈"); LogUtil.info(LogName + "二维码【" + message + "】没有收到服务器反馈");
return null; return null;
} }
else if (!string.IsNullOrEmpty(resultOperation.msg)) else if (!string.IsNullOrEmpty(resultOperation.msg))
...@@ -577,7 +577,12 @@ namespace OnlineStore.DeviceLibrary ...@@ -577,7 +577,12 @@ namespace OnlineStore.DeviceLibrary
{ {
LogUtil.error(LogName + "解析服务发送的入库【" + posId + "】出错" + ex.StackTrace); LogUtil.error(LogName + "解析服务发送的入库【" + posId + "】出错" + ex.StackTrace);
} }
}return null; }
else
{
LogUtil.error(LogName + "二维码【" + message + "】获取入库库位失败 ");
}
return null;
} }
public static Dictionary<string, string> GetBtnStatus() public static Dictionary<string, string> GetBtnStatus()
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!