|
|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
|
|
using HybirdFrameworkCore.Const;
|
|
|
|
|
using HybirdFrameworkDriver.ModbusTcpMaster;
|
|
|
|
|
using Minio.DataModel;
|
|
|
|
|
using Service.PlcAfrica.Msg;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Entity.DbModel.Station;
|
|
|
|
|
using log4net;
|
|
|
|
|
using Repository.Station;
|
|
|
|
|
using static HybirdFrameworkCore.Const.EndingConst;
|
|
|
|
|
|
|
|
|
|
namespace Service.PlcAfrica.Client
|
|
|
|
|
{
|
|
|
|
|
[Scope]
|
|
|
|
|
public class PlcAfricaClient : ModbusTcpMaster
|
|
|
|
|
{
|
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(PlcAfricaClient));
|
|
|
|
|
public BinInfoRepository BinInfoRepository { get; set; }
|
|
|
|
|
private long TaskNo = 0;
|
|
|
|
|
public PlcAfricaClient()
|
|
|
|
|
{
|
|
|
|
|
ReadAction = BatchRead;
|
|
|
|
|
Ip = "192.168.3.200";
|
|
|
|
|
Port = 502;
|
|
|
|
|
Duration = 1000;
|
|
|
|
|
AutoReConnect = true;
|
|
|
|
|
ByteSeq = EndingConst.ByteSeq.BA;
|
|
|
|
|
WordSeq = EndingConst.WordSeq.DC;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool BatchRead(ModbusTcpMaster master)
|
|
|
|
|
{
|
|
|
|
|
bool resul = Connected;
|
|
|
|
|
|
|
|
|
|
//0x03
|
|
|
|
|
var bytes01 = master.Read("x=3;7000", 2500).Content;
|
|
|
|
|
if (bytes01 != null)
|
|
|
|
|
{
|
|
|
|
|
PlcAfricaMgr.DataValidityTime = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
ModbusDecoder.Decode<PlcAfricaMsg>(bytes01, PlcAfricaMgr.PlcAfricaMsg,
|
|
|
|
|
EndingConst.ByteSeq.AB,
|
|
|
|
|
EndingConst.WordSeq.DC);
|
|
|
|
|
|
|
|
|
|
Task task01 = Task.Run(async () => await UpdateBinInfo(PlcAfricaMgr.PlcAfricaMsg.Loader1SeatSensor1.Value?1:0, "1"));
|
|
|
|
|
Task task02 = Task.Run(async () => await UpdateBinInfo(PlcAfricaMgr.PlcAfricaMsg.Loader2SeatSensor1.Value?1:0, "2"));
|
|
|
|
|
Task task03 = Task.Run(async () => await UpdateBinInfo(PlcAfricaMgr.PlcAfricaMsg.Loader3SeatSensor1.Value?1:0, "3"));
|
|
|
|
|
Task task04 = Task.Run(async () => await UpdateBinInfo(PlcAfricaMgr.PlcAfricaMsg.Loader4SeatSensor1.Value?1:0, "4"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resul;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UpdateBinInfo(int exists, string binNo)
|
|
|
|
|
{
|
|
|
|
|
if (exists == 1)
|
|
|
|
|
BinInfoRepository.Update(it => it.Exists == 1,
|
|
|
|
|
it => it.No == binNo);
|
|
|
|
|
else
|
|
|
|
|
BinInfoRepository.Update(
|
|
|
|
|
it =>
|
|
|
|
|
new BinInfo()
|
|
|
|
|
{
|
|
|
|
|
Exists = 0,
|
|
|
|
|
BatteryNo = "-1",
|
|
|
|
|
Soc = (decimal)-1,
|
|
|
|
|
Soe = (decimal)-1,
|
|
|
|
|
Soh = (decimal)-1,
|
|
|
|
|
},
|
|
|
|
|
it => it.No == binNo);
|
|
|
|
|
Log.Info($"Update exists:{exists},binNo{binNo}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UpdateBinInfoCache(int exists, string binNo)
|
|
|
|
|
{
|
|
|
|
|
if (exists == 1)
|
|
|
|
|
BinInfoRepository.Update(it => it.Exists == 1,
|
|
|
|
|
it => it.No == binNo);
|
|
|
|
|
else
|
|
|
|
|
BinInfoRepository.Update(
|
|
|
|
|
it =>
|
|
|
|
|
new BinInfo()
|
|
|
|
|
{
|
|
|
|
|
Exists = 0,
|
|
|
|
|
},
|
|
|
|
|
it => it.No == binNo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//修改当前任务
|
|
|
|
|
public void ExChangeTaskNo(long taskNo)
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Exchange(ref TaskNo, taskNo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//重置当前任务
|
|
|
|
|
public void ResetTaskNo()
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Exchange(ref TaskNo, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsTaskFree()
|
|
|
|
|
{
|
|
|
|
|
return ReadTaskNo() == 0l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//读取当前任务
|
|
|
|
|
public long ReadTaskNo()
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Read(ref TaskNo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|