2024-12-06 17:51:01 +08:00
|
|
|
|
using DM_Weight.msg;
|
|
|
|
|
using DM_Weight.ViewModels;
|
2024-11-14 10:02:51 +08:00
|
|
|
|
using log4net;
|
|
|
|
|
using log4net.Repository.Hierarchy;
|
|
|
|
|
using Modbus.Device;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
using Polly;
|
2024-12-06 17:51:01 +08:00
|
|
|
|
using Prism.Events;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2024-12-06 17:51:01 +08:00
|
|
|
|
using System.Speech.Synthesis;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
using System.Text;
|
2024-11-26 15:05:37 +08:00
|
|
|
|
using System.Threading;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2024-12-06 17:51:01 +08:00
|
|
|
|
using static DM_Weight.Port.KeepAliveSocket;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
|
|
|
|
|
namespace DM_Weight.Port
|
|
|
|
|
{
|
|
|
|
|
public class ModbusHelper
|
|
|
|
|
{
|
2024-12-06 17:51:01 +08:00
|
|
|
|
private static ModbusHelper instance;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
private ModbusIpMaster master;
|
|
|
|
|
private Socket socket;
|
|
|
|
|
private TcpClient client;
|
2024-11-14 10:02:51 +08:00
|
|
|
|
private readonly ILog logger = LogManager.GetLogger(typeof(CheckOrderNewWindowViewModel));
|
2024-12-06 17:51:01 +08:00
|
|
|
|
string ip = ConfigurationManager.AppSettings["modbusIp"].ToString();
|
|
|
|
|
int port = Convert.ToInt32(ConfigurationManager.AppSettings["modbusPort"]);
|
|
|
|
|
public static bool BoxOperate { get; set; }
|
2024-11-26 15:05:37 +08:00
|
|
|
|
private ModbusHelper()
|
2024-08-12 16:42:53 +08:00
|
|
|
|
{
|
2024-12-06 17:51:01 +08:00
|
|
|
|
logger.Info("ModbusHelper");
|
|
|
|
|
socket = KeepALiveSocket.MakeKeepALiveSocket(ip, port);
|
2024-08-12 16:42:53 +08:00
|
|
|
|
client = new TcpClient();
|
|
|
|
|
client.Client = socket;
|
|
|
|
|
master = ModbusIpMaster.CreateIp(client);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public static ModbusHelper GetInstance()
|
|
|
|
|
{
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
2024-11-28 11:34:42 +08:00
|
|
|
|
instance = new ModbusHelper();
|
2024-08-12 16:42:53 +08:00
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
private void SetModusIpMaster()
|
|
|
|
|
{
|
2024-12-06 17:51:01 +08:00
|
|
|
|
logger.Info("SetModusIpMaster");
|
|
|
|
|
socket = KeepALiveSocket.MakeKeepALiveSocket(ip, port); ;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
client = new TcpClient();
|
|
|
|
|
client.Client = socket;
|
|
|
|
|
master = ModbusIpMaster.CreateIp(client);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-06 17:51:01 +08:00
|
|
|
|
|
2024-08-12 16:42:53 +08:00
|
|
|
|
public bool[] GetAllBoxState()
|
|
|
|
|
{
|
2024-12-06 17:51:01 +08:00
|
|
|
|
//bool[] bools = { true };
|
|
|
|
|
//if (BoxOperate)
|
|
|
|
|
//{
|
|
|
|
|
//if (ConfigurationManager.AppSettings["test"] != null && ConfigurationManager.AppSettings["test"].ToString() == "Y")
|
|
|
|
|
//{
|
|
|
|
|
// return new bool[] { false,false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
|
|
|
|
|
//}
|
|
|
|
|
bool[] bools = Policy.Handle<Exception>()
|
|
|
|
|
.Retry(3, (exception, retryCount) =>
|
|
|
|
|
{
|
|
|
|
|
this.Dispose();
|
|
|
|
|
//Debug.WriteLine($"获取所有箱子门状态出错,第{retryCount}次重试", exception);
|
|
|
|
|
logger.Info($"获取所有箱子门状态出错,第{retryCount}次重试, 异常信息{exception}");
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
this.SetModusIpMaster();
|
|
|
|
|
BoxOperate = false;
|
|
|
|
|
// return TimeSpan.FromSeconds (1);
|
|
|
|
|
}).Execute<bool[]>(() =>
|
2024-08-12 16:42:53 +08:00
|
|
|
|
{
|
2024-12-06 17:51:01 +08:00
|
|
|
|
bool[] flags = new bool[18];
|
|
|
|
|
if(master==null)
|
|
|
|
|
{
|
|
|
|
|
this.SetModusIpMaster();
|
|
|
|
|
}
|
|
|
|
|
var result = master.ReadInputRegisters(1, 0x0033, 2);
|
|
|
|
|
var r1 = Convert.ToString(((int)result[0] >> 14) | ((int)result[1] << 2), 2).PadLeft(18, '0');
|
|
|
|
|
var r2 = r1.ToCharArray();
|
|
|
|
|
logger.Info("r2=>" + string.Join(", ", r2));
|
|
|
|
|
//var r2=new char[18] { '0','0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
|
|
|
|
|
for (int i = 0; i < 18; i++)
|
|
|
|
|
{
|
|
|
|
|
flags[i] = r2[17 - i] == '1' ? true : false;
|
|
|
|
|
}
|
|
|
|
|
logger.Info($"获取所有箱子门状态返回:{string.Join(',', flags)};");
|
|
|
|
|
//bool allFalse = Array.TrueForAll(flags, b => b == false);
|
|
|
|
|
//if (!allFalse)
|
|
|
|
|
//{
|
|
|
|
|
// Task.Delay(500);
|
|
|
|
|
// Task.Factory.StartNew(async () =>
|
|
|
|
|
// {
|
|
|
|
|
// SpeakAsync("药箱已打开,请及时关闭");
|
|
|
|
|
// await Task.Delay(15000);
|
|
|
|
|
// });
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// BoxOperate = false;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
|
});
|
|
|
|
|
//}
|
2024-08-12 16:42:53 +08:00
|
|
|
|
return bools;
|
|
|
|
|
}
|
2024-11-05 08:49:56 +08:00
|
|
|
|
public bool OpenBoxDoor(int boxNum)
|
2024-08-12 16:42:53 +08:00
|
|
|
|
{
|
2024-12-06 17:51:01 +08:00
|
|
|
|
BoxOperate = true;
|
|
|
|
|
//if (ConfigurationManager.AppSettings["test"]!=null&& ConfigurationManager.AppSettings["test"].ToString()=="Y")
|
|
|
|
|
//{
|
|
|
|
|
// return true;
|
|
|
|
|
//}
|
|
|
|
|
bool bFlag = false;
|
|
|
|
|
Thread.Sleep(50);
|
2024-08-12 16:42:53 +08:00
|
|
|
|
Policy.Handle<Exception>().Retry(3, ((exception, retryCount) =>
|
|
|
|
|
{
|
|
|
|
|
this.Dispose();
|
2024-11-14 10:02:51 +08:00
|
|
|
|
//Debug.WriteLine($"打开箱子出错,第{retryCount}次重试", exception);
|
|
|
|
|
logger.Info($"打开箱子出错,第{retryCount}次重试,异常信息{exception}");
|
2024-11-26 15:05:37 +08:00
|
|
|
|
Thread.Sleep(50);
|
2024-08-12 16:42:53 +08:00
|
|
|
|
this.SetModusIpMaster();
|
|
|
|
|
//return TimeSpan.FromSeconds (1);
|
|
|
|
|
})).Execute(() =>
|
|
|
|
|
{
|
2024-11-14 10:02:51 +08:00
|
|
|
|
logger.Info($"正在打开{boxNum}号药箱");
|
2024-08-12 16:42:53 +08:00
|
|
|
|
master.WriteSingleRegister(1, (ushort)boxNum, 14);
|
2024-11-14 10:02:51 +08:00
|
|
|
|
logger.Info($"开门指令已发送{(ushort)boxNum}");
|
2024-12-06 17:51:01 +08:00
|
|
|
|
SpeakAsync("药箱已打开,请及时关闭");
|
2024-11-14 10:02:51 +08:00
|
|
|
|
//Console.WriteLine($"开门指令已发送{(ushort)boxNum}");
|
2024-11-05 08:49:56 +08:00
|
|
|
|
bFlag = true;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
});
|
2024-11-05 08:49:56 +08:00
|
|
|
|
return bFlag;
|
2024-08-12 16:42:53 +08:00
|
|
|
|
}
|
|
|
|
|
private void Dispose()
|
|
|
|
|
{
|
|
|
|
|
socket.Shutdown(SocketShutdown.Both);
|
|
|
|
|
socket.Close();
|
|
|
|
|
client.Close();
|
|
|
|
|
master.Dispose();
|
|
|
|
|
}
|
2024-12-06 17:51:01 +08:00
|
|
|
|
private static SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
|
|
|
|
|
public static void SpeakAsync(string textinfo)
|
2024-08-12 16:42:53 +08:00
|
|
|
|
{
|
2024-12-06 17:51:01 +08:00
|
|
|
|
speechSynthesizer.SpeakAsync(textinfo);
|
2024-08-12 16:42:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|