LibS7Adv/PLCViewModel.cs

970 lines
35 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Siemens.Simatic.Simulation.Runtime;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.ComponentModel;
using Newtonsoft.Json.Linq;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
namespace LibS7Adv
{
[DisplayName("PLC Advanced Setup:")]
public partial class PlcData : ObservableObject
{
[ObservableProperty]
[property: Description("IP of the PLC")]
[property: Category("Connection:")]
string iP = "10.1.30.11";
[ObservableProperty]
[property: Description("Name of the instance at the PLC Advanced")]
[property: Category("Connection:")]
string name = "PLC Name";
[ObservableProperty]
[property: Description("CPU Info")]
[property: Category("Status:")]
[property: ReadOnly(true)]
string cpuTime;
[ObservableProperty]
[property: Display(Name = "Status")]
[property: Description("CPU Status")]
[property: Category("Status:")]
[property: ReadOnly(true)]
string connectionStatus;
[ObservableProperty]
[property: Description("API Error")]
[property: Category("Status:")]
[property: ReadOnly(true)]
string lastError;
}
public partial class PLCViewModel : ObservableObject
{
[JsonIgnore]
private IInstance Instance { get; set; }
private bool IsConfigured { get; set; }
// Añade una bandera para controlar el estado de actualización
private bool _isUpdating = false;
private DateTime _lastUpdateStartTime = DateTime.MinValue;
private TimeSpan _updateTimeout = TimeSpan.FromSeconds(10); // Timeout para reintentar después de un tiempo
[ObservableProperty]
PlcData plcData = new PlcData();
public PLCViewModel()
{
}
public void ucLoaded()
{
IsConnected = false;
PlcData.LastError = "";
PlcData.ConnectionStatus = "offline";
}
[ObservableProperty]
[property: JsonIgnore]
bool isConnected;
[RelayCommand]
[property: JsonIgnore]
public void ResetAlarmButton()
{
PlcData.LastError = "";
}
[RelayCommand]
[property: JsonIgnore]
public void ConnectButton()
{
if (isConnected)
Disconnect();
else
Connect();
}
public void Connect()
{
if (IsConnected)
return;
try
{
// Implementa la conexión utilizando PLCModel
Instance = SimulationRuntimeManager.CreateInterface(PlcData.Name);
Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
//_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
if (Instance != null)
{
UpdateTagList();
PlcData.ConnectionStatus = Instance.OperatingState.ToString();
IsConnected = true;
}
}
catch (Exception ex)
{
PlcData.LastError = ex.Message;
PlcData.ConnectionStatus = "offline";
}
}
public void Disconnect()
{
IsConnected = false;
PlcData.ConnectionStatus = "offline";
Instance = null;
}
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Actualizar cuando recibimos el evento
UpdateTagList();
// Cuando la actualización se completa exitosamente, desactivar la bandera
if (IsConfigured)
{
_isUpdating = false;
}
else
{
// Si no se pudo configurar, programar un reintento después de un tiempo
Task.Delay(1000).ContinueWith(_ => UpdateTagList());
}
}
public void UpdateTagList()
{
IsConfigured = false;
_isUpdating = true;
try
{
Instance?.UpdateTagList(ETagListDetails.IO | ETagListDetails.DB | ETagListDetails.M, true);
IsConfigured = true;
_isUpdating = false; // Éxito en la actualización
}
catch (Exception ex)
{
PlcData.LastError = ex.Message;
// Mantener _isUpdating = true ya que hubo un error
}
}
public bool? LeerBool(string sTag)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return false;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada,
// posiblemente se quedó trabado, intentamos de nuevo
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar leer durante la actualización
return false;
}
if (sTag == null)
{ return false; }
var tag = ParseTagAddress(sTag);
if (tag.tagType == EDataType.Unknown) // Normal Tag?
{
return Instance?.ReadBool(sTag);
}
else if (tag.tagType == EDataType.Bool)
{
// Read not Work for ImputArea
if (tag.areaType == EArea.Output)
return Instance?.OutputArea.ReadBit(tag.word_offset, tag.bit);
if (tag.areaType == EArea.Marker)
return Instance?.MarkerArea.ReadBit(tag.word_offset, tag.bit);
}
return false;
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = sTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = sTag + ":" + ex.Message;
}
return false;
}
}
public bool? EscribirBool(string sTag, bool Value)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return false;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada,
// posiblemente se quedó trabado, intentamos de nuevo
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar escribir durante la actualización
return false;
}
if (sTag == null)
{ return false; }
var tag = ParseTagAddress(sTag);
if (tag.tagType == EDataType.Unknown) // Normal Tag?
{
Instance?.WriteBool(sTag, Value);
}
else if (tag.tagType == EDataType.Bool)
{
if (tag.areaType == EArea.Input)
Instance?.InputArea.WriteBit(tag.word_offset, tag.bit, Value);
if (tag.areaType == EArea.Output)
Instance?.OutputArea.WriteBit(tag.word_offset, tag.bit, Value);
if (tag.areaType == EArea.Marker)
Instance?.MarkerArea.WriteBit(tag.word_offset, tag.bit, Value);
}
return false;
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = sTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = sTag + ":" + ex.Message;
}
return false;
}
}
public string? LeerNumber(string sTag, bool Signed = false)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return "";
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar leer durante la actualización
return "";
}
if (sTag == null)
{ return ""; }
var tag = ParseTagAddress(sTag.Trim());
if (tag.tagType == EDataType.Unknown) // Normal Tag?
{
var data = Instance?.Read(sTag);
return data.Value.ToString();
}
else if (tag.tagType == EDataType.Byte)
{
// Read not Work for ImputArea
if (tag.areaType == EArea.Output)
{
var val = Instance?.OutputArea.ReadByte(tag.word_offset);
if (Signed)
return val > 127 ? (val - 256).ToString() : val.ToString();
return val.ToString();
}
if (tag.areaType == EArea.Marker)
{
var val = Instance?.MarkerArea.ReadByte(tag.word_offset);
if (Signed)
return val > 127 ? (val - 256).ToString() : val.ToString();
return val.ToString();
}
}
else if (tag.tagType == EDataType.Word)
{
// Read not Work for ImputArea
if (tag.areaType == EArea.Output)
{
int value;
byte[] bytes = Instance?.OutputArea.ReadBytes(tag.word_offset, 2);
Array.Reverse(bytes);
if (Signed)
value = BitConverter.ToInt16(bytes, 0);
else
value = BitConverter.ToUInt16(bytes, 0);
return value.ToString();
}
if (tag.areaType == EArea.Marker)
{
int value;
byte[] bytes = Instance?.MarkerArea.ReadBytes(tag.word_offset, 2);
Array.Reverse(bytes);
if (Signed)
value = BitConverter.ToInt16(bytes, 0);
else
value = BitConverter.ToUInt16(bytes, 0);
return value.ToString();
}
}
return "";
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = sTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = sTag + ":" + ex.Message;
}
return "";
}
}
public bool LeerSalidaBool(byte pByte, int pBit)
{
try
{
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar leer durante la actualización
return false;
}
return Instance?.OutputArea.ReadBit(pByte, (byte)pBit) ?? false;
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = "Byte " + pByte + ", Bit " + pBit + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = ex.Message;
}
return false;
}
}
public void EscribirInputBool(byte pByte, int pBit, bool pValue)
{
try
{
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar escribir durante la actualización
return;
}
Instance?.InputArea.WriteBit(pByte, (byte)pBit, pValue);
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = "Byte " + pByte + ", Bit " + pBit + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = ex.Message;
}
}
}
public void EscribirTag(string pTag, SDataValue Value)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar escribir durante la actualización
return;
}
Instance?.Write(pTag, Value);
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = pTag + ":" + ex.Message;
}
}
}
public SDataValue LeerTag(string pTag)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return new SDataValue();
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar leer durante la actualización
return new SDataValue();
}
return Instance.Read(pTag);
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = pTag + ":" + ex.Message;
}
return new SDataValue();
}
}
public void EscribirTagBool(string pTag, bool pValue)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar escribir durante la actualización
return;
}
Instance?.WriteBool(pTag, pValue);
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = pTag + ":" + ex.Message;
}
}
}
public void EscribirTagInt16(string pTag, int pValue)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar escribir durante la actualización
return;
}
Instance?.WriteInt16(pTag, (short)pValue);
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = pTag + ":" + ex.Message;
}
}
}
public bool LeerTagBool(string pTag)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return false;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar leer durante la actualización
return false;
}
bool result = Instance?.ReadBool(pTag) ?? false;
return result;
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = pTag + ":" + ex.Message;
}
return false;
}
}
public int? LeerTagInt16(string pTag)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return 0;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar leer durante la actualización
return 0;
}
int? result = Instance?.ReadInt16(pTag);
return result;
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = pTag + ":" + ex.Message;
}
return 0;
}
}
public int? LeerTagDInt(string pTag)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return 0;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar leer durante la actualización
return 0;
}
int? result = Instance?.ReadInt32(pTag);
return result;
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = pTag + ":" + ex.Message;
}
return 0;
}
}
public void EscribirTagDInt(string pTag, int pValue)
{
try
{
if (!isConnected)
{
PlcData.LastError = "Not Connected";
return;
}
// Verificar si estamos en proceso de actualización
if (_isUpdating)
{
// Si ha pasado mucho tiempo desde la última actualización intentada
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
{
_lastUpdateStartTime = DateTime.Now;
UpdateTagList();
}
// No intentar escribir durante la actualización
return;
}
Instance?.WriteInt32(pTag, pValue);
}
catch (Exception ex)
{
// Verificar si el error es NotUpToDate y activar la bandera si es así
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
{
_isUpdating = true;
_lastUpdateStartTime = DateTime.Now;
// Iniciar actualización de tags
Task.Run(() => UpdateTagList());
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
}
else
{
PlcData.LastError = pTag + ":" + ex.Message;
}
}
}
public static TagAddress ParseTagAddress(string tag)
{
TagAddress tagAddress = new TagAddress();
if (tag == null)
return null;
if (tag.StartsWith("%"))
{
if (tag.StartsWith("%DB"))
{
tagAddress.areaType = EArea.DataBlock;
var match = Regex.Match(tag, @"^%DB(\d+)\.DB([X|B|W|D])(\d+)\.(\d+)$");
if (match.Success)
{
tagAddress.DB = int.Parse(match.Groups[1].Value);
char dataTypeChar = match.Groups[2].Value[0];
tagAddress.word_offset = uint.Parse(match.Groups[3].Value);
tagAddress.bit = byte.Parse(match.Groups[4].Value);
switch (dataTypeChar)
{
case 'X':
tagAddress.tagType = EDataType.Bool;
break;
case 'B':
tagAddress.tagType = EDataType.Byte;
break;
case 'W':
tagAddress.tagType = EDataType.Word;
break;
case 'D':
tagAddress.tagType = EDataType.DWord;
break;
default:
tagAddress.tagType = EDataType.Unknown;
break;
}
}
}
else if (tag.StartsWith("%M"))
{
tagAddress.areaType = EArea.Marker;
ParseNonDBAddress(tag, tagAddress);
}
else if (tag.StartsWith("%E"))
{
tagAddress.areaType = EArea.Input;
ParseNonDBAddress(tag, tagAddress);
}
else if (tag.StartsWith("%A"))
{
tagAddress.areaType = EArea.Output;
ParseNonDBAddress(tag, tagAddress);
}
else
{
tagAddress.areaType = EArea.InvalidArea;
}
}
else
{
// If it's not a direct address, treat it as a tag name
tagAddress.areaType = EArea.InvalidArea;
tagAddress.tagType = EDataType.Unknown;
}
return tagAddress;
}
private static void ParseNonDBAddress(string tag, TagAddress tagAddress)
{
var match = Regex.Match(tag, @"^%[EMA](\d+)\.(\d+)$");
if (match.Success)
{
tagAddress.word_offset = uint.Parse(match.Groups[1].Value);
tagAddress.bit = byte.Parse(match.Groups[2].Value);
tagAddress.tagType = EDataType.Bool;
}
else
{
match = Regex.Match(tag, @"^%[EMA]([XBWDF])(\d+)$");
if (match.Success)
{
char dataTypeChar = match.Groups[1].Value[0];
tagAddress.word_offset = uint.Parse(match.Groups[2].Value);
switch (dataTypeChar)
{
case 'X':
tagAddress.tagType = EDataType.Bool;
break;
case 'B':
tagAddress.tagType = EDataType.Byte;
break;
case 'W':
tagAddress.tagType = EDataType.Word;
break;
case 'D':
tagAddress.tagType = EDataType.DWord;
break;
default:
tagAddress.tagType = EDataType.Unknown;
break;
}
}
}
}
}
public class TagAddress
{
public EArea areaType;
public EDataType tagType;
public int DB;
public uint word_offset;
public byte bit;
}
}