|
|
@@ -0,0 +1,247 @@
|
|
|
+using System;
|
|
|
+using System.ComponentModel;
|
|
|
+using System.Data;
|
|
|
+using System.IO;
|
|
|
+using System.Net;
|
|
|
+using System.Text;
|
|
|
+using System.Threading;
|
|
|
+
|
|
|
+using Curtain.DataAccess;
|
|
|
+using Curtain.Extension.ExSystemData;
|
|
|
+using Curtain.Framework.Json;
|
|
|
+using Curtain.Log;
|
|
|
+using Curtain.Net.Sockets.PLC;
|
|
|
+using Curtain.Net.Sockets.PLC.Model.Melsec;
|
|
|
+using Curtain.Net.Sockets.PLC.Model.Siemens;
|
|
|
+
|
|
|
+using PLC_S.Proxy;
|
|
|
+
|
|
|
+/// <summary>
|
|
|
+/// 获取设备参数的定时服务
|
|
|
+/// </summary>
|
|
|
+namespace PLC_S.EPTS
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// 打磨
|
|
|
+ /// </summary>
|
|
|
+ public class EPTS_ApiAuto : IWindowsServer, IShowFormLog
|
|
|
+ {
|
|
|
+ public const string M_NAME = "EAA";
|
|
|
+ private Logger logger = null;
|
|
|
+ private Logger logger_t = null;
|
|
|
+ private Logger logger_e = null;
|
|
|
+ private Logger logger_e_out = null;
|
|
|
+
|
|
|
+ private bool _Started = false;
|
|
|
+ //private System.Timers.Timer _timing = null;
|
|
|
+ private BackgroundWorker backgroundWorker = null;
|
|
|
+ private MES_S _mes_s = null;
|
|
|
+
|
|
|
+ public bool Started
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ //return _timing?.Enabled ?? false;
|
|
|
+ return _Started;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public IFormLogShow FormLogShow
|
|
|
+ {
|
|
|
+ get;
|
|
|
+ set;
|
|
|
+ }
|
|
|
+ public int Interval
|
|
|
+ {
|
|
|
+ get;
|
|
|
+ set;
|
|
|
+ }
|
|
|
+ public string EPTS_CODE
|
|
|
+ {
|
|
|
+ get;
|
|
|
+ set;
|
|
|
+ }
|
|
|
+
|
|
|
+ public EPTS_ApiAuto()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Start(int value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string S_NAME = "EPTS_" + EPTS_CODE;
|
|
|
+
|
|
|
+ logger = Logger.CreateLogger(S_NAME + "-S");
|
|
|
+
|
|
|
+ logger_t = Logger.CreateLogger(S_NAME + "-T");
|
|
|
+ logger_e = Logger.CreateLogger(S_NAME + "-E");
|
|
|
+ logger_e_out = Logger.CreateLogger(S_NAME + "-EO");
|
|
|
+
|
|
|
+ logger.FileNameWithoutDate = true;
|
|
|
+ logger.FileExistDays = 30;
|
|
|
+ logger.LevelOneFile = true;
|
|
|
+ logger.FilePrefix = null;
|
|
|
+ logger.FileSuffix = logger.Name;
|
|
|
+
|
|
|
+ logger_t.FileExistDays = 30;
|
|
|
+ logger_t.LevelOneFile = true;
|
|
|
+ logger_t.SubFolderFormat = logger_t.Name;
|
|
|
+
|
|
|
+ logger_e.FileExistDays = 0;
|
|
|
+
|
|
|
+ logger_e_out.FileExistDays = 0;
|
|
|
+ logger_e_out.FilePrefix = null;
|
|
|
+ logger_e_out.FileSuffix = S_NAME;
|
|
|
+ logger_e_out.Directory = "..\\_PLC_Logs";
|
|
|
+
|
|
|
+ logger.OutputTrace($"{M_NAME}_Starting:[{EPTS_CODE}]");
|
|
|
+ FormLogShow?.ShowLog($"{M_NAME}_Starting:[{EPTS_CODE}]");
|
|
|
+
|
|
|
+ if (Interval < 1000)
|
|
|
+ {
|
|
|
+ Interval = 1000;
|
|
|
+ }
|
|
|
+ //if (Interval > 60000)
|
|
|
+ //{
|
|
|
+ // Interval = 60000;
|
|
|
+ //}
|
|
|
+
|
|
|
+ backgroundWorker = new BackgroundWorker();
|
|
|
+ backgroundWorker.WorkerSupportsCancellation = true;
|
|
|
+ backgroundWorker.DoWork += BackgroundWorker_DoWork;
|
|
|
+ backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
|
|
|
+
|
|
|
+ _Started = true;
|
|
|
+ backgroundWorker.RunWorkerAsync();
|
|
|
+
|
|
|
+ logger.OutputTrace($"{M_NAME}_Started:[{Interval}]");
|
|
|
+ FormLogShow?.ShowLog($"{M_NAME}_Started:[{Interval}]");
|
|
|
+ }
|
|
|
+ catch(Exception ex)
|
|
|
+ {
|
|
|
+ logger.OutputError(ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
|
+ {
|
|
|
+ logger.OutputTrace(JsonHelper.ToJson(e));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ _mes_s = MES_S.Get();
|
|
|
+ INI_EAA plc_ini = INI_EAA.Get(EPTS_CODE);
|
|
|
+
|
|
|
+ while (_Started && !e.Cancel)
|
|
|
+ {
|
|
|
+ this.Do(plc_ini);
|
|
|
+ Thread.Sleep(Interval);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger_e_out.OutputError(ex);
|
|
|
+ logger_e.OutputError(ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Do(INI_EAA plc_ini)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ logger_t.BeginTracking();
|
|
|
+ #region 自动调用API接口
|
|
|
+ string outputMessage = "";
|
|
|
+ if (!string.IsNullOrEmpty(plc_ini.API_URL))
|
|
|
+ {
|
|
|
+ outputMessage = $"接口地址={plc_ini.API_URL}";
|
|
|
+ logger_t.OutputTrace(outputMessage);
|
|
|
+ outputMessage = $"执行结果={GetApi(plc_ini.API_URL)}";
|
|
|
+ logger_t.OutputTrace(outputMessage);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ outputMessage = $"API_URL未设置,请检查确认!";
|
|
|
+ logger_t.OutputTrace(outputMessage);
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ logger_t.EndTracking();
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger_e_out.OutputError(ex);
|
|
|
+ logger_e.OutputError(ex);
|
|
|
+ logger_t.OutputError(ex);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string GetApi(string url)
|
|
|
+ {
|
|
|
+ //拼合URL参数
|
|
|
+ string webUrl = url;
|
|
|
+ //创建Web访问对象
|
|
|
+ HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(webUrl);
|
|
|
+ //通过Web访问对象获取响应内容
|
|
|
+ HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
|
|
|
+ //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
|
|
|
+ StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
|
|
|
+ //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
|
|
|
+ string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
|
|
|
+ reader.Close();
|
|
|
+ myResponse.Close();
|
|
|
+ return returnXml;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Stop()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ logger.OutputTrace($"{M_NAME}_Stoping:[{EPTS_CODE}]");
|
|
|
+ FormLogShow?.ShowLog($"{M_NAME}_Stoping:[{EPTS_CODE}]");
|
|
|
+
|
|
|
+ _Started = false;
|
|
|
+ backgroundWorker?.CancelAsync();
|
|
|
+ backgroundWorker?.Dispose();
|
|
|
+ backgroundWorker = null;
|
|
|
+
|
|
|
+ logger.OutputTrace($"{M_NAME}_Stoped:[{EPTS_CODE}]");
|
|
|
+ FormLogShow?.ShowLog($"{M_NAME}_Stoped:[{EPTS_CODE}]");
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.OutputError(ex);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ _Started = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Dispose()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ _Started = false;
|
|
|
+ backgroundWorker?.CancelAsync();
|
|
|
+ backgroundWorker?.Dispose();
|
|
|
+ backgroundWorker = null;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.OutputError(ex);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ _Started = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|