Commit 7e814187 张东亮

使用confighelper

1 个父辈 aae14734
......@@ -23,7 +23,7 @@ namespace LiftController
InitializeComponent();
groupBox4.Enabled = false;
LoadIOList(config);
string[] lifts = Common.AppConfigHelper.GetValue(Common.SettingString.Lift_Ids).Split(',');
string[] lifts = Common.AppConfigHelper.GetValue(Common.SettingString.Lift_Ids, "LIFT_D2,LIFT_C1").Split(',');
comboBox1.Items.AddRange(lifts);
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = 0;
......
......@@ -32,6 +32,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ConfigHelper">
<HintPath>..\..\..\..\..\..\SharedRefDll\Neotel\ConfigHelper\Debug\net462\ConfigHelper.dll</HintPath>
</Reference>
<Reference Include="log4net">
<HintPath>.\log4net.dll</HintPath>
</Reference>
......
......@@ -6,160 +6,15 @@ namespace Common
{
public class AppConfigHelper
{
public static System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
public static string GetValue(string key)
public static T GetValue<T>(string key, T defalutVal = default(T))
{
if (config.AppSettings.Settings[key] == null)
{
LogUtil.info("未找到配置:" + key + ",请检查配置是否完整!");
return "";
}
else
{
return config.AppSettings.Settings[key].Value;
}
return ConfigHelper.Config.Get(key, defalutVal);
}
public static decimal GetNumValue(string key)
{
decimal a = 0;
//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
LogUtil.error("未找到配置:" + key + ",请检查配置是否完整!");
return a;
}
else
{
{
Decimal.TryParse(config.AppSettings.Settings[key].Value, out a);
}
}
return a;
}
public static int GetIntValue(string key)
{
int a = 0;
//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
LogUtil.error("未找到配置:" + key + ",请检查配置是否完整!");
return a;
}
else
{
{
Int32.TryParse(config.AppSettings.Settings[key].Value, out a);
}
}
return a;
}
public static bool GetBoolValue(string key)
{
bool a = false;
//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
LogUtil.error("未找到配置:" + key + ",请检查配置是否完整!");
return a;
}
else
{
{
bool.TryParse(config.AppSettings.Settings[key].Value, out a);
}
}
return a;
}
public static void SaveValue(string key, int value)
{
SaveValue(key, value.ToString());
}
public static void SaveValue(string key, bool value)
public static void SetValue<T>(string key, T val = default(T))
{
SaveValue(key, value.ToString());
}
public static void SaveValue(string key, string value)
{
try
{
if (key.Equals("") || value.Equals(""))
{
return;
}
//增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
SetValue(key, value);
}
else
{
UpdateConfig(key, value);
}
}
catch (Exception ex)
{
LogUtil.error("SaveValue保存配置出错:AppKey=" + key + ",AppValue=" + value + "," + ex.StackTrace);
}
}
/// <summary>
/// 更新配置文件信息
/// </summary>
/// <param name="name">配置文件字段名称</param>
/// <param name="Xvalue">值</param>
private static void UpdateConfig(string name, string Xvalue)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.ExecutablePath + ".config");
XmlNode node = doc.SelectSingleNode(@"//add[@key='" + name + "']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value", Xvalue);
doc.Save(Application.ExecutablePath + ".config");
}
catch (Exception ex)
{
LogUtil.error("UpdateConfig保存配置出错:name=" + name + ",Xvalue=" + Xvalue + "," + ex.StackTrace);
}
ConfigHelper.Config.Set(key, val);
}
///<summary>
///向.config文件的appKey结写入信息AppValue 保存设置
///</summary>
///<param name="AppKey">节点名</param>
///<param name="AppValue">值</param>
private static void SetValue(String AppKey, String AppValue)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
xElem1.SetAttribute("value", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
}
catch (Exception ex)
{
LogUtil.error("SetValue保存配置出错:AppKey=" + AppKey + ",AppValue=" + AppValue + "," + ex.StackTrace);
}
}
}
}
......@@ -16,7 +16,7 @@ namespace DeviceLibrary.Context
}
public void Open()
{
string baseurl = AppConfigHelper.GetValue(SettingString.ServiceBaseUrl);
string baseurl = AppConfigHelper.GetValue(SettingString.ServiceBaseUrl, "http://127.0.0.1:8887");
serviceContext.Open(baseurl);
}
public void Close()
......
......@@ -15,8 +15,8 @@ namespace DeviceLibrary.Context
public static void Init()
{
string[] lifts = Common.AppConfigHelper.GetValue(Common.SettingString.Lift_Ids).Split(',');
string buff = Common.AppConfigHelper.GetValue(Common.SettingString.Lift_Buffs);
string[] lifts = Common.AppConfigHelper.GetValue(Common.SettingString.Lift_Ids, "LIFT_D2,LIFT_C1").Split(',');
string buff = Common.AppConfigHelper.GetValue(Common.SettingString.Lift_Buffs,"");
elevatorInfos = Common.JsonHelper.DeserializeJsonToList<LiftInfo>(buff);
if (elevatorInfos == null)
{
......@@ -92,7 +92,7 @@ namespace DeviceLibrary.Context
{
try
{
Common.AppConfigHelper.SaveValue(Common.SettingString.Lift_Buffs, Common.JsonHelper.SerializeObject(elevatorInfos));
Common.AppConfigHelper.SetValue(Common.SettingString.Lift_Buffs, Common.JsonHelper.SerializeObject(elevatorInfos));
}
catch (Exception e)
{
......
......@@ -60,7 +60,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="bean\Line\LineBean.cs" />
<Compile Include="Context\Context.cs" />
<Compile Include="Context\LiftContext.cs" />
<Compile Include="Context\ServiceContext.cs" />
......@@ -95,5 +94,6 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
......@@ -65,12 +65,12 @@ namespace DeviceLibrary
{
DOValueMap.Remove(ioIp);
}
int DIMS = AppConfigHelper.GetIntValue("DIMS");
int DIMS = AppConfigHelper.GetValue("DIMS",20);
if (DIMS < 20)
{
DIMS = 20;
}
int DOMS = AppConfigHelper.GetIntValue("DOMS");
int DOMS = AppConfigHelper.GetValue("DOMS", 20);
if (DOMS < 200)
{
DOMS = 200;
......
......@@ -117,7 +117,7 @@ namespace DeviceLibrary
#endregion
public static void Init()
{
bool isAIOBox = AppConfigHelper.GetIntValue(SettingString.UseAIOBOX).Equals(1);
bool isAIOBox = AppConfigHelper.GetValue(SettingString.UseAIOBOX,1).Equals(1);
if (isAIOBox)
{
instance = new AIOBOXManager();
......
......@@ -12,7 +12,7 @@ using System.Threading.Tasks;
namespace DeviceLibrary.Models
{
/// <summary>
/// 电梯信息
/// 电梯
/// </summary>
public class LiftInfo
{
......
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 双层流水线当前运动信息类(出入库状态,步骤记录)
/// </summary>
public class LineMoveInfo
{
private string msg = "";
public string Msg
{
get { return msg; }
set
{
if (!string.IsNullOrEmpty(value))
{
//不为空,且与上一个消息不一样才打印
if (!value.Equals(msg))
{
LogUtil.info(string.Format("{0}", value));
}
}
msg = value;
}
}
/// <summary>
/// 超时时间
/// </summary>
public int TimeOutSeconds = 60;
public LineMoveInfo( )
{
moveType = LineMoveType.None;
this.moveStep = MoveStep.Wait;
IsInWait = false;
}
public DateTime LastSetpTime { get; set; }
/// <summary>
/// =true表示满足一个等待条件,就可以完成此步骤的等待
/// </summary>
public bool OneWaitCanEndStep = false;
/// <summary>
/// 操作类型
/// </summary>
private LineMoveType moveType = LineMoveType.None;
public LineMoveType MoveType
{
get { return moveType; }
}
public bool IsTimeOut(int timeOutSeconds = 60)
{
TimeSpan span = DateTime.Now - LastSetpTime;
if (span.TotalSeconds > timeOutSeconds)
{
return true;
}
return false;
}
public string SLog
{
get { return " "+moveType+" " + MoveStep + " "; }
}
public TimeSpan StepSpan()
{
TimeSpan span = DateTime.Now - LastSetpTime;
return span;
}
/// <summary>
/// 是否再当前步骤等待中
/// </summary>
public bool IsInWait { get; set; }
/// <summary>
/// 上一个执行步骤
/// </summary>
public MoveStep PreMoveStep { get; set; }
/// <summary>
/// 当前执行到的步骤
/// </summary>
private MoveStep moveStep;
/// <summary>
/// 可以循环运动的次数(轴卡运动才需要)
/// </summary>
public int CanWhileCount = 0;
/// <summary>
/// 双层流水线运动步骤记录
/// </summary>
public MoveStep MoveStep
{
get { return moveStep; }
}
/// <summary>
/// 是否可以重写IO
/// </summary>
public bool CanRMoveIO = true;
public void NextMoveStep(MoveStep step)
{
PreMoveStep = moveStep;
moveStep = step;
LastSetpTime = DateTime.Now;
IsInWait = true;
WaitList = new List<WaitResultInfo>();
OneWaitCanEndStep = false;
CanWhileCount = 5;
TimeOutSeconds = 60;
CanRMoveIO = true;
}
/// <summary>
/// 当前步骤执行完成
/// </summary>
public void EndStepWait()
{
IsInWait = false;
WaitList = new List<WaitResultInfo>();
}
public void NewMove(LineMoveType type ,MoveStep step=MoveStep.Wait )
{
moveStep = step;
this.moveType = type;
LastSetpTime = DateTime.Now;
IsInWait = true;
WaitList = new List<WaitResultInfo>();
OneWaitCanEndStep = false;
CanWhileCount = 5;
TimeOutSeconds = 60;
CanRMoveIO = true;
}
public void EndMove()
{
this.moveType = LineMoveType.None;
// this.MoveParam = null;
moveStep = MoveStep.Wait;
LastSetpTime = DateTime.Now;
IsInWait = false;
WaitList = new List<WaitResultInfo>();
CanWhileCount = 0;
}
public LineMoveInfo clone()
{
return (LineMoveInfo)this.MemberwiseClone();
}
public List<WaitResultInfo> WaitList = new List<WaitResultInfo>();
/// <summary>
/// 重置之后继续出入库时,退回上一个步骤执行
/// </summary>
public void BackStep()
{
moveStep = PreMoveStep;
IsInWait = false;
}
}
public class WaitResultInfo
{
private WaitResultInfo()
{
}
public static WaitResultInfo WaitIO(string ioType, IO_VALUE ioValue)
{
WaitResultInfo wait = new WaitResultInfo();
wait.CanWhileMoveCount = 0;
wait.WaitType = 2;
wait.IoType = ioType;
wait.IoValue = ioValue;
return wait;
}
public static WaitResultInfo WaitTime(int MScends)
{
WaitResultInfo wait = new WaitResultInfo();
wait.CanWhileMoveCount = 0;
wait.WaitType = 3;
wait.TimeMSeconds = MScends;
wait.IsEnd = false;
return wait;
}
public static WaitResultInfo WaitAbb(string abbip)
{
WaitResultInfo wait = new WaitResultInfo();
wait.CanWhileMoveCount = 0;
wait.WaitType = 7;
wait.IoType = abbip;
return wait;
}
public string ToStr()
{
if (WaitType == 1)
{
}
else if (WaitType == 2)
{
return "IO[" + IoType + "=" + IoValue + "]";
}
else if (WaitType == 3)
{
return "Time[" + TimeMSeconds + "ms]";
}
else if (WaitType == 7)
{
return "ABB["+IoType+"]";
}
else
{
return "[WaitType=" + WaitType + "]";
}
return "";
}
/// <summary>
/// 当未结束时可以重复运动的次数
/// </summary>
public int CanWhileMoveCount { get; set; }
/// <summary>
/// 等待结果,1=轴运动,2=IO运动,3=时间,4=电钢,5=硕科电机,6=等待轴原点信号
/// </summary>
public int WaitType { get; set; }
/// <summary>
/// IO类型
/// </summary>
public String IoType { get; set; }
/// <summary>
/// IO值
/// </summary>
public IO_VALUE IoValue { get; set; }
/// <summary>
/// 等待的毫秒
/// </summary>
public int TimeMSeconds { get; set; }
/// <summary>
/// 是否已经结束
/// </summary>
public bool IsEnd{ get; set; }
}
internal class WaitEnum
{
/// <summary>
/// 伺服运动
/// </summary>
internal static int W001_AxisMove = 1;
/// <summary>
/// 信号到达
/// </summary>
internal static int W002_IOValue = 2;
/// <summary>
/// 时间等待
/// </summary>
internal static int W003_Time = 3;
/// <summary>
/// 电钢运动
/// </summary>
internal static int W004_StellMove = 4;
/// <summary>
/// 硕科电机
/// </summary>
internal static int W005_ShuoKe = 5;
/// <summary>
/// 轴原点信号
/// </summary>
internal static int W006_AxisOrg = 6;
/// <summary>
/// 等待机器人运动
/// </summary>
internal static int W007_WaitAbb = 7;
}
public enum LineMoveType
{
/// <summary>
/// 没有任何操作
/// </summary>
None = 0,
/// <summary>
/// 料架入流水线处理
/// </summary>
LeftShelf = 1,
/// <summary>
/// S1工位料架处理
/// </summary>
S1Shelf = 2,
/// <summary>
/// 机器人放料
/// </summary>
PutTray = 3,
/// <summary>
/// 原点返回
/// </summary>
ReturnHome = 4,
/// <summary>
/// 重置
/// </summary>
StoreReset = 5,
/// <summary>
/// 左侧工作位到缓存
/// </summary>
LTransWork2Buf,
/// <summary>
/// 左侧缓存到工作
/// </summary>
LTransBuf2Work,
/// <summary>
/// 右侧工作位到缓存
/// </summary>
RTransWork2Buf,
/// <summary>
/// 右侧缓存到工作
/// </summary>
RTransBuf2Work,
}
}
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class WaitUtil
{
public delegate bool IsOk();
/// <summary>
/// 使用异步委托检测超时,防止isOk方法不返回结果导致卡死的问题
/// </summary>
/// <param name="waitName"></param>
/// <param name="timeout"></param>
/// <param name="isOk"></param>
public static bool Wait(int timeout, IsOk isOk, string waitName = "")
{
DateTime startTime = System.DateTime.Now;
TimeSpan timoutSpan = TimeSpan.FromMilliseconds(timeout);
TimeSpan waitSpan = TimeSpan.FromMilliseconds(0);
int sleepTime = 10;
while (true)
{
TimeSpan remainTimes = timoutSpan.Subtract(waitSpan);
if (remainTimes.TotalMilliseconds < 0)
{
//已经超时
throw new TimeoutException(waitName + "超时");
}
try
{
IAsyncResult re = isOk.BeginInvoke(null, null);
var waitResult = re.AsyncWaitHandle.WaitOne(remainTimes);
if (waitResult)
{
bool okResult = isOk.EndInvoke(re);
if (okResult)
{
return true;
}
}
}
catch (Exception ex)
{
LogUtil.error("同步等待出现异常:", ex);
}
Thread.Sleep(sleepTime);
waitSpan = System.DateTime.Now.Subtract(startTime);
}
}
}
}
......@@ -52,7 +52,7 @@ namespace DeviceLibrary
{
string appPath = Application.StartupPath;
string linefilePath = appPath +"\\Config\\"+ AppConfigHelper.GetValue(SettingString.Line_Config);
string linefilePath = appPath +"\\Config\\"+ AppConfigHelper.GetValue(SettingString.Line_Config, "LineConfig.csv");
LogUtil.info(" 开始加载配置:" + linefilePath);
RobotConfig storeConfig = CSVConfigReader.LoadConfig(linefilePath);
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!