init: 导入项目到 meswork Gitea
This commit is contained in:
237
DataMirrorTools_Simulator/ModelBasic/Base/MM.SimDeviceSignal.cs
Normal file
237
DataMirrorTools_Simulator/ModelBasic/Base/MM.SimDeviceSignal.cs
Normal file
@@ -0,0 +1,237 @@
|
||||
using Global;
|
||||
using MesServerFunctions;
|
||||
using ModelBasic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace DTTest
|
||||
{
|
||||
public partial class E { }
|
||||
public class DeviceSignalSimGanttBase : ModelManagement
|
||||
{
|
||||
static string MqttTargetTopicPLC = System.Configuration.ConfigurationManager.AppSettings["MqttTargetTopicPLC"];
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static int playIndex;
|
||||
/// <summary>
|
||||
/// 循环播放
|
||||
/// 2021.3.31
|
||||
/// </summary>
|
||||
public static bool LoopPlay = false;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
private Thread t_PlayCurrDeviceSignalSimTask = null;
|
||||
/// <summary>
|
||||
/// 播放当前的信号仿真的任务
|
||||
/// 2020.12.10
|
||||
/// </summary>
|
||||
public void PlayCurrDeviceSignalSimTask(string deviceSignalSimCode)
|
||||
{
|
||||
if (t_PlayCurrDeviceSignalSimTask == null)
|
||||
{
|
||||
t_PlayCurrDeviceSignalSimTask = new Thread(new ParameterizedThreadStart(PlayDeviceSignalSim));
|
||||
t_PlayCurrDeviceSignalSimTask.IsBackground = true;
|
||||
t_PlayCurrDeviceSignalSimTask.Name = deviceSignalSimCode.ToString();
|
||||
t_PlayCurrDeviceSignalSimTask.Start(deviceSignalSimCode);
|
||||
}
|
||||
else
|
||||
{
|
||||
// // ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("当前存在播放的信号仿真!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static public bool IsCanPlay(string deviceSignalSimCode)
|
||||
{
|
||||
var dt_DeviceSignalSim = SqlOperation.SqlDeviceSignalSim.Select_DeviceSignalSim(deviceSignalSimCode);
|
||||
if (dt_DeviceSignalSim.Rows.Count < 1)
|
||||
{
|
||||
// // ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("当前不存在播放的信号仿真!");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
private void PlayDeviceSignalSim(object obj)
|
||||
{
|
||||
var deviceSignalSimCode = (string)obj;
|
||||
var dt_DeviceSignalSim = SqlOperation.SqlDeviceSignalSim.Select_DeviceSignalSim(deviceSignalSimCode);
|
||||
if (dt_DeviceSignalSim.Rows.Count < 1)
|
||||
{
|
||||
// // ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("当前不存在播放的信号仿真!");
|
||||
return;
|
||||
}
|
||||
var dt_DeviceSignalSim_Count = dt_DeviceSignalSim.Rows.Count;
|
||||
var list_TagStartTime_TagID_TagValue = new List<OperationResultMesDtSim<Int32, String, String,Int32,string,string>>();
|
||||
var timeList = new List<int>();
|
||||
|
||||
for (int i = 0; i < dt_DeviceSignalSim_Count; i++)
|
||||
{
|
||||
var startTime = Convert.ToInt32(dt_DeviceSignalSim.Rows[i]["StartTime"]);
|
||||
var tagStartTime = startTime / 100;
|
||||
var keepTime = Convert.ToInt32(dt_DeviceSignalSim.Rows[i]["KeepTime"]);
|
||||
var tagID = dt_DeviceSignalSim.Rows[i]["TagID"].ToString();
|
||||
var tagValue = dt_DeviceSignalSim.Rows[i]["TagValue"].ToString();
|
||||
var opName = dt_DeviceSignalSim.Rows[i]["OpName"].ToString();
|
||||
var tagIDCopyFrom = dt_DeviceSignalSim.Rows[i]["TagIDCopyFrom"].ToString();
|
||||
|
||||
int tagFrom = 1;
|
||||
if(dt_DeviceSignalSim.Rows[i]["TagFrom"]!=DBNull.Value)
|
||||
{
|
||||
tagFrom = Convert.ToInt32(dt_DeviceSignalSim.Rows[i]["TagFrom"]);
|
||||
|
||||
}
|
||||
list_TagStartTime_TagID_TagValue.Add(new OperationResultMesDtSim<Int32, String, String, Int32, String, String>()
|
||||
{ PlayIDList = tagStartTime, PlayName = tagID, ModelName = tagValue, TagFrom = tagFrom, OpName = opName, TagIDCopyFrom = tagIDCopyFrom });
|
||||
|
||||
timeList.Add(startTime + keepTime);
|
||||
}
|
||||
var maxTime = timeList.Max();
|
||||
var maxFrame = maxTime / 100;
|
||||
|
||||
var dc_TagStartTime_TagIDAndTagValue = list_TagStartTime_TagID_TagValue.GroupBy(m => m.PlayIDList)
|
||||
.ToDictionary(k => k.First().PlayIDList, v => v.ToList());
|
||||
|
||||
do{
|
||||
do{
|
||||
if (dc_TagStartTime_TagIDAndTagValue.ContainsKey(playIndex))
|
||||
foreach (var item in dc_TagStartTime_TagIDAndTagValue[playIndex])
|
||||
{
|
||||
var tagID = item.PlayName;
|
||||
var tagValue = item.ModelName;
|
||||
var opName = item.OpName;
|
||||
var tagIDCopyFrom = item.TagIDCopyFrom;
|
||||
switch (item.TagFrom)
|
||||
{
|
||||
case 1:
|
||||
DtWebApiDT.PLC.WritePLC(tagID, tagValue);
|
||||
break;
|
||||
case 2:
|
||||
DtWebApi.ReadWritePLC.WritePLC(tagID, tagValue);
|
||||
break;
|
||||
case 3://扫描条码
|
||||
string Message;
|
||||
|
||||
|
||||
Message = "BarCode|Bar|" + opName + "|" + tagValue;
|
||||
|
||||
SendMessage.SendMessageBARToMES(opName,Message);
|
||||
//asyncSendMessage(Message);
|
||||
//DtWebApi.ReadWritePLC.WritePLC(tagID, tagValue);
|
||||
break;
|
||||
case 4://拷贝变量值
|
||||
tagValue = DtWebApi.ReadWritePLC.ReadPLC(tagIDCopyFrom).ToString();
|
||||
DtWebApi.ReadWritePLC.WritePLC(tagID, tagValue);
|
||||
break;
|
||||
case 5://MQTT
|
||||
|
||||
Message = "MesToWeb|Web|" + opName + "|" + tagValue;
|
||||
SendMessage.SendMessageMESToWEB(opName, Message);
|
||||
//asyncSendMessage(Message);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
//if (GlobalVar.signalWriteTask != null)
|
||||
//{
|
||||
// GlobalVar.signalWriteTask.BeginInvoke(Cmd_Frame.SignalWrite, tagID, tagValue, "-1", new AsyncCallback(SignalWriteTask_0), GlobalVar.signalWriteTask);
|
||||
//}
|
||||
}
|
||||
|
||||
ModelControl_CurrRunInfoEventPs(playIndex);
|
||||
playIndex++;
|
||||
Thread.Sleep(100);
|
||||
} while (playIndex <= maxFrame) ;
|
||||
|
||||
playIndex = 0;
|
||||
|
||||
} while(LoopPlay);
|
||||
|
||||
t_PlayCurrDeviceSignalSimTask = null;
|
||||
PlayDeviceSignalSimTaskEnd(deviceSignalSimCode);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void End_DeviceSignalSim_ExecTask(string deviceSignalSimCode)
|
||||
{
|
||||
if (t_PlayCurrDeviceSignalSimTask != null)
|
||||
{
|
||||
var ts = Get_DeviceSignalSim_ExecTask_State();
|
||||
if (ts == (ThreadState.Background | ThreadState.Suspended))
|
||||
{
|
||||
t_PlayCurrDeviceSignalSimTask.Resume();
|
||||
}
|
||||
t_PlayCurrDeviceSignalSimTask.Abort();
|
||||
}
|
||||
|
||||
playIndex = 0;
|
||||
t_PlayCurrDeviceSignalSimTask = null ;
|
||||
PlayDeviceSignalSimTaskEnd(deviceSignalSimCode);
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="craftCode"></param>
|
||||
public void Suspend_DeviceSignalSim_ExecTask()
|
||||
{
|
||||
t_PlayCurrDeviceSignalSimTask.Suspend();
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="craftCode"></param>
|
||||
public void Resume_DeviceSignalSim_ExecTask()
|
||||
{
|
||||
t_PlayCurrDeviceSignalSimTask.Resume();
|
||||
}
|
||||
|
||||
public ThreadState Get_DeviceSignalSim_ExecTask_State()
|
||||
{
|
||||
ThreadState threadState = ThreadState.Stopped;
|
||||
if (t_PlayCurrDeviceSignalSimTask != null)
|
||||
{
|
||||
threadState = t_PlayCurrDeviceSignalSimTask.ThreadState;
|
||||
}
|
||||
return threadState;
|
||||
}
|
||||
|
||||
public MesWorkMqtt.Mqtt2 mqtt2;
|
||||
public void InitMqtt2(string url, string topic)
|
||||
{
|
||||
mqtt2 = new MesWorkMqtt.Mqtt2();
|
||||
mqtt2.MqttUrl = url;
|
||||
mqtt2.topic = topic;
|
||||
StartAsync();
|
||||
|
||||
}
|
||||
private async void StartAsync()
|
||||
{
|
||||
await mqtt2.StartAsync();
|
||||
}
|
||||
|
||||
|
||||
public async void asyncSendMessage(string Message)
|
||||
{
|
||||
|
||||
|
||||
await mqtt2.Publish(MqttTargetTopicPLC, Message);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user