Compare commits

...

2 Commits

Author SHA1 Message Date
c34aa59ed1 Delete and Clear button in tables 2024-11-22 13:38:21 +07:00
092429cec8 Modbus working 22/11/2024 2024-11-22 11:54:01 +07:00
4 changed files with 622 additions and 264 deletions

View File

@@ -33,7 +33,7 @@ namespace FAtoPA
{
connection.Open();
var createtablecmd = connection.CreateCommand();
createtablecmd.CommandText = "CREATE TABLE IF NOT EXISTS FsmData (SIID TEXT PRIMARY KEY, Enable INTEGER, Description TEXT)";
createtablecmd.CommandText = "CREATE TABLE IF NOT EXISTS FsmData (SIID TEXT PRIMARY KEY, Enable INTEGER, Label TEXT, Type TEXT)";
createtablecmd.ExecuteNonQuery();
Debug.WriteLine("CreateFSMTable success");
return true;
@@ -79,7 +79,8 @@ namespace FAtoPA
{
while (reader.Read())
{
FSMData fsmData = new FSMData(reader.GetString(0), reader.GetBoolean(1), reader.GetString(2));
FSMData fsmData = new FSMData(reader.GetString(0), reader.GetBoolean(1), reader.GetString(2), reader.GetString(3));
fsmDatas.Add(fsmData);
}
}
@@ -97,10 +98,11 @@ namespace FAtoPA
foreach (var item in data)
{
var insertCmd = connection.CreateCommand();
insertCmd.CommandText = "INSERT INTO FsmData (SIID, Enable, Description) VALUES (@SIID, @Enable, @Description)";
insertCmd.CommandText = "INSERT INTO FsmData (SIID, Enable, Label, Type) VALUES (@SIID, @Enable, @Label, @Type)";
insertCmd.Parameters.AddWithValue("@SIID", item.SIID);
insertCmd.Parameters.AddWithValue("@Enable", item.Enable);
insertCmd.Parameters.AddWithValue("@Description", item.Description);
insertCmd.Parameters.AddWithValue("@Label", item.Label);
insertCmd.Parameters.AddWithValue("@Type", item.Type);
try
{
int result = insertCmd.ExecuteNonQuery();
@@ -123,6 +125,29 @@ namespace FAtoPA
}
}
public FSMData FSMDataHaveSIID(String SIID)
{
if (SIID!=null && SIID.Length > 0)
{
using (var conn = new SQLiteConnection(connectionString))
{
conn.Open();
var findCmd = conn.CreateCommand();
findCmd.CommandText = "SELECT FROM FsmData WHERE SIID = @SIID";
findCmd.Parameters.AddWithValue("@SIID", SIID);
using (var reader = findCmd.ExecuteReader())
{
if (reader.Read())
{
FSMData result = new FSMData(reader.GetString(0), reader.GetBoolean(1), reader.GetString(2), reader.GetString(3));
return result;
}
}
}
}
return null;
}
public bool RemoveFSMDatabySIID(String SIID)
{
using (var conn = new SQLiteConnection(connectionString))
@@ -222,6 +247,29 @@ namespace FAtoPA
}
}
public ModbusData ModbusDataHaveSIID(String SIID)
{
if (SIID != null && SIID.Length > 0)
{
using (var conn = new SQLiteConnection(connectionString))
{
conn.Open();
var findCmd = conn.CreateCommand();
findCmd.CommandText = "SELECT FROM ModbusData WHERE SIID = @SIID";
findCmd.Parameters.AddWithValue("@SIID", SIID);
using (var reader = findCmd.ExecuteReader())
{
if (reader.Read())
{
ModbusData result = new ModbusData(reader.GetString(0),(ushort) reader.GetInt16(1), reader.GetString(2));
return result;
}
}
}
}
return null;
}
public List<ModbusData> GetModbusDatas()
{
List<ModbusData> modbusDatas = new List<ModbusData>();
@@ -235,7 +283,7 @@ namespace FAtoPA
{
while (reader.Read())
{
ModbusData modbusdata = new ModbusData(reader.GetString(0), reader.GetFieldValue<UInt16>(1), reader.GetString(2));
ModbusData modbusdata = new ModbusData(reader.GetString(0), (ushort) reader.GetInt16(1), reader.GetString(2));
modbusDatas.Add(modbusdata);
}
}
@@ -306,6 +354,29 @@ namespace FAtoPA
return false;
}
public VXData VXDataHaveSIID(String SIID)
{
if (SIID != null && SIID.Length > 0)
{
using (var conn = new SQLiteConnection(connectionString))
{
conn.Open();
var findCmd = conn.CreateCommand();
findCmd.CommandText = "SELECT FROM VxTable WHERE SIID = @SIID";
findCmd.Parameters.AddWithValue("@SIID", SIID);
using (var reader = findCmd.ExecuteReader())
{
if (reader.Read())
{
VXData result = new VXData(reader.GetString(0), reader.GetByte(1), reader.GetByte(2), reader.GetString(3));
return result;
}
}
}
}
return null;
}
public List<VXData> GetVXDatas()
{
List<VXData> vxDatas = new List<VXData>();
@@ -319,7 +390,7 @@ namespace FAtoPA
{
while (reader.Read())
{
VXData vxdata = new VXData(reader.GetString(0), reader.GetByte(1), reader.GetByte(2));
VXData vxdata = new VXData(reader.GetString(0), reader.GetByte(1), reader.GetByte(2), reader.GetString(3));
vxDatas.Add(vxdata);
}
}
@@ -388,36 +459,89 @@ namespace FAtoPA
class FSMData : INotifyPropertyChanged
{
public String SIID { get; set; }
public Boolean Enable { get; set; }
public String Description { get; set; }
public String Value { get; set; }
public String LastUpdate { get; set; }
public FSMData(String siid, Boolean enable, String description)
private String _siid;
public String SIID
{
get => _siid;
set
{
if (_siid != value)
{
_siid = value;
OnPropertyChanged(nameof(SIID));
}
}
}
private Boolean _enable;
public Boolean Enable
{
get => _enable;
set
{
if (_enable != value)
{
_enable = value;
OnPropertyChanged(nameof(Enable));
}
}
}
private String _label;
public String Label {
get => _label;
set {
if (_label != value)
{
_label = value;
OnPropertyChanged(nameof(Label));
}
}
}
private String _type;
public String Type
{
get => _type;
set
{
if (_type != value)
{
_type = value;
OnPropertyChanged(nameof(Type));
}
}
}
private String _value;
public String Value {
get => _value;
set
{
if (_value != value)
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
}
private String _lastupdate;
public String LastUpdate {
get => _lastupdate;
set {
if (_lastupdate != value)
{
_lastupdate = value;
OnPropertyChanged(nameof(LastUpdate));
}
}
}
public FSMData(String siid, Boolean enable, String label, String type)
{
this.SIID = siid;
this.Enable = enable;
this.Description = description;
this.Label = label;
this.Type = type;
this.Value = "";
this.LastUpdate = "";
}
public FSMData(String siid)
{
this.SIID = siid;
this.Enable = true;
this.Description = "";
this.Value = "";
this.LastUpdate = "";
}
public FSMData(String siid, Boolean enable)
{
this.SIID = siid;
this.Enable = enable;
this.Description = "";
this.LastUpdate = "";
this.Value = "";
}
protected void OnPropertyChanged(string propertyName)
{
@@ -425,16 +549,77 @@ namespace FAtoPA
}
public event PropertyChangedEventHandler PropertyChanged;
}
class ModbusData
class ModbusData : INotifyPropertyChanged
{
public String SIID { get; set; }
public UInt16 Register { get; set; }
public String Description { get; set; }
public String Value { get; set; }
public String LastUpdate { get; set; }
private String _siid;
public String SIID
{
get => _siid;
set
{
if (_siid != value)
{
_siid = value;
OnPropertyChanged(nameof(SIID));
}
}
}
private UInt16 _register;
public UInt16 Register
{
get => _register;
set
{
if (_register != value)
{
_register = value;
OnPropertyChanged(nameof(Register));
}
}
}
private String _description;
public String Description
{
get => _description;
set
{
if (_description != value)
{
_description = value;
OnPropertyChanged(nameof(Description));
}
}
}
private String _value;
public String Value
{
get => _value;
set
{
if (_value != value)
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
}
private String _lastupdate;
public String LastUpdate
{
get => _lastupdate;
set
{
if (_lastupdate != value)
{
_lastupdate = value;
OnPropertyChanged(nameof(LastUpdate));
}
}
}
public ModbusData(String siid, UInt16 register, String description)
{
this.SIID = siid;
@@ -451,16 +636,94 @@ namespace FAtoPA
this.Value = "";
this.LastUpdate = "";
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
class VXData
class VXData : INotifyPropertyChanged
{
public String SIID { get; set; }
public Byte FrameID { get; set; }
public Byte CIN { get; set; }
public String Description { get; set; }
public String Value { get; set; }
public String LastUpdate { get; set; }
private String _siid;
public String SIID
{
get => _siid;
set
{
if (_siid != value)
{
_siid = value;
OnPropertyChanged(nameof(SIID));
}
}
}
private Byte _frameid;
public Byte FrameID
{
get => _frameid;
set
{
if (_frameid != value)
{
_frameid = value;
OnPropertyChanged(nameof(FrameID));
}
}
}
private Byte _cin;
public Byte CIN
{
get => _cin;
set
{
if (_cin != value)
{
_cin = value;
OnPropertyChanged(nameof(CIN));
}
}
}
private String _description;
public String Description
{
get => _description;
set
{
if (_description != value)
{
_description = value;
OnPropertyChanged(nameof(Description));
}
}
}
private String _value;
public String Value
{
get => _value;
set
{
if (_value != value)
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
}
private String _lastupdate;
public String LastUpdate
{
get => _lastupdate;
set
{
if (_lastupdate != value)
{
_lastupdate = value;
OnPropertyChanged(nameof(LastUpdate));
}
}
}
public VXData(String siid, Byte frameid, Byte cin, String Description)
{
this.SIID = siid;
@@ -479,5 +742,11 @@ namespace FAtoPA
this.Value = "";
this.LastUpdate = "";
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

View File

@@ -58,7 +58,7 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid Visibility="Hidden" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
@@ -66,7 +66,7 @@
<Label Content="NetGroup" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/>
<ComboBox x:Name="netGroupNumber" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding NetGroupList, IsAsync=True}" SelectedIndex="0"/>
</Grid>
<Grid Grid.Column="1">
<Grid Visibility="Hidden" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
@@ -74,7 +74,7 @@
<Label Content="NetNode" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/>
<ComboBox x:Name="netNodeNumber" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding NetNodeList, IsAsync=True}" SelectedIndex="0"/>
</Grid>
<Grid Grid.Column="2">
<Grid Visibility="Hidden" Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
@@ -82,7 +82,7 @@
<Label Content="SI Type" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/>
<ComboBox x:Name="siType" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding SIType, IsAsync=True}" SelectedIndex="0"/>
</Grid>
<Grid Grid.Column="3">
<Grid Visibility="Hidden" Grid.Column="3">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
@@ -90,7 +90,7 @@
<Label Content="SI Number" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/>
<ComboBox x:Name="siNumber" Text="1" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding SINumberList, IsAsync=True}" SelectedIndex="0"/>
</Grid>
<Grid Grid.Column="4">
<Grid Visibility="Hidden" Grid.Column="4">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
@@ -105,12 +105,12 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="5" x:Name="btnAddSIID" Content="Manual Add To Table" Click="btnAddSIID_Click"/>
<Button Visibility="Hidden" Grid.Column="0" Margin="5" x:Name="btnAddSIID" Content="Manual Add To Table" Click="btnAddSIID_Click"/>
<Button Grid.Column="1" Margin="5" x:Name="btnDelSIID" Content="Remove From Table" Click="btnDelSIID_Click"/>
<Button Grid.Column="2" Margin="5" x:Name="btnClearSIID" Content="Clear Table" Click="btnClearSIID_Click"/>
</Grid>
</Grid>
<DockPanel DockPanel.Dock="Left" Width="300">
<DockPanel DockPanel.Dock="Left" Width="400">
<Label Content="Detected SIID" DockPanel.Dock="Top" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Grid DockPanel.Dock="Bottom" Height="50">
<Grid.ColumnDefinitions>
@@ -161,7 +161,7 @@
<Button Grid.Column="2" Margin="5" x:Name="btnClearModbus" Content="Clear Table" Click="btnClearModbus_Click"/>
</Grid>
</Grid>
<DockPanel DockPanel.Dock="Left" Width="200">
<DockPanel DockPanel.Dock="Left" Width="400">
<Label Content="Connected Modbus Client" DockPanel.Dock="Top" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label Content="Count : 0" DockPanel.Dock="Bottom" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" x:Name="ConnectedModbusCount"/>
<ListBox x:Name="ConnectedModbusClients" />

View File

@@ -41,17 +41,23 @@ namespace FAtoPA.Net
// untuk values di Combobox Modbus Registers
public ObservableCollection<int> ModbusRegisters { get; set; }
// Untuk manual add SIID to FSM Table
public ObservableCollection<int> NetGroupList { get; set; }
public ObservableCollection<int> NetNodeList { get; set; }
public ObservableCollection<int> PNAList { get; set; }
public ObservableCollection<int> SINumberList { get; set; }
public ObservableCollection<int> SISubList { get; set; }
// Isi FSM Table
ObservableCollection<FSMData> FsmTableMember { get; set; }
// Isi Modbus Table
ObservableCollection<ModbusData> ModbusTableMember { get; set; }
// Isi VX Table
ObservableCollection<VXData> VXTableMember { get; set; }
public List<String> ConditionON;
public List<String> ConditionOFF;
FSMEvent fsmEvent;
public MainWindow()
{
@@ -100,6 +106,14 @@ namespace FAtoPA.Net
siType.ItemsSource = Enum.GetValues(typeof(SIType)).Cast<SIType>().ToList();
this.DataContext = this;
//TODO : Add Condition for ON and OFF
ConditionON = new List<string>();
ConditionON.Add(SILogicalState.ACTIVATION.ToString());
ConditionON.Add(SILogicalState.FIRE.ToString());
ConditionOFF = new List<string>();
ConditionOFF.Add(SILogicalState.NORMAL.ToString());
}
private void Window_Loaded(object sender, RoutedEventArgs e)
@@ -142,7 +156,11 @@ namespace FAtoPA.Net
// Load Database
database = new Database();
database.GetFSMDatas().ForEach(f => FsmTableMember.Add(f));
database.GetFSMDatas().ForEach(f =>
{
FsmTableMember.Add(f);
FSMSIID.Add(f.SIID);
});
database.GetModbusDatas().ForEach(m => ModbusTableMember.Add(m));
database.GetVXDatas().ForEach(v => VXTableMember.Add(v));
@@ -160,10 +178,12 @@ namespace FAtoPA.Net
ModbusRegisters.Clear();
for (int i = 0; i < config.Modbus_MaxRegister; i++)
{
ModbusRegisters.Add(i);
}
Debug.WriteLine($"Creating ModbusRegisters untuk Combobox, length={ModbusRegisters.Count}");
// Load Modbus Slave
modbusSlave = new ModbusSlave(new ModbusEvent(modbusstatusbar, ConnectedModbusClients, ConnectedModbusCount), config.Modbus_MaxRegister);
@@ -176,8 +196,8 @@ namespace FAtoPA.Net
fsm.AddListener(new FSMTableUpdater(FsmTableMember, DetectedSIID, DetectedSIIDCount));
fsm.AddListener(new ModbusTriggerFromFSM(FsmTableMember, ModbusTableMember, modbusSlave));
fsm.AddListener(new VXTriggerFromFSM(FsmTableMember, VXTableMember, vx3k));
fsm.AddListener(new ModbusTriggerFromFSM(FsmTableMember, ModbusTableMember, modbusSlave, ConditionON, ConditionOFF));
fsm.AddListener(new VXTriggerFromFSM(FsmTableMember, VXTableMember, vx3k, ConditionON, ConditionOFF));
}
private void Timer1s_Tick(object sender, EventArgs e)
@@ -778,13 +798,15 @@ namespace FAtoPA.Net
{
if (database != null)
{
FSMData f = new FSMData(selected.ToString());
if (database.AddFSMData(f))
{
FSMTable.ItemsSource = database.GetFSMDatas();
FSMSIID.Add(selected.ToString());
}
else MessageBox.Show("Failed to add to database");
//TODO Manual Add SIID, perlukah ?
//FSMData f = new FSMData(selected.ToString());
//if (database.AddFSMData(f))
//{
// FSMTable.ItemsSource = database.GetFSMDatas();
// FSMSIID.Add(selected.ToString());
//}
//else MessageBox.Show("Failed to add to database");
}
else MessageBox.Show("Database is null");
}
@@ -795,21 +817,21 @@ namespace FAtoPA.Net
private void btnDelSIID_Click(object sender, RoutedEventArgs e)
{
int selectedindex = FSMTable.SelectedIndex;
if (selectedindex >= 0)
if (FSMTable != null)
{
FSMData selected = (FSMData)FSMTable.Items[selectedindex];
MessageBoxResult result = MessageBox.Show("Delete SIID " + selected.ToString() + " ?", "Delete SIID", MessageBoxButton.YesNo);
var selected = FSMTable.SelectedItem as FSMData;
if (selected != null)
{
MessageBoxResult result = MessageBox.Show($"Are you sure want to delete SIID={selected.SIID}, Label={selected.Label}, Type={selected.Type} ?", "Delete SIID", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
FsmTableMember.Remove(selected);
database.RemoveFSMDatabySIID(selected.SIID);
FSMSIID.Remove(selected.SIID);
}
}
else MessageBox.Show("Select a row in table to delete");
}
}
private void btnClearSIID_Click(object sender, RoutedEventArgs e)
@@ -819,7 +841,7 @@ namespace FAtoPA.Net
{
FsmTableMember.Clear();
database.ClearFSMTable();
FSMSIID.Clear();
}
}
@@ -897,37 +919,21 @@ namespace FAtoPA.Net
private void btnDelModbus_Click(object sender, RoutedEventArgs e)
{
String ssid = "";
String reg = "";
if (ModbusSIIDComboBox.SelectedItem != null)
if (ModbusTable != null)
{
ssid = (string)ModbusSIIDComboBox.SelectedItem;
}
else
var selected = ModbusTable.SelectedItem as ModbusData;
if (selected != null)
{
MessageBox.Show("Invalid SIID"); return;
}
if (ModbusRegister.SelectedItem != null)
MessageBoxResult xx = MessageBox.Show($"Are you sure want to delete SIID={selected.SIID}, Register={selected.Register} ?", "Delete SIID", MessageBoxButton.YesNo);
if (xx == MessageBoxResult.Yes)
{
reg = (String)ModbusRegister.SelectedItem;
database.RemoveModbusDatabySIID(selected.SIID);
ModbusTableMember.Remove(selected);
}
else
{
MessageBox.Show("Invalid Register"); return;
}
MessageBoxResult result = MessageBox.Show("Delete SIID " + ssid + " Link with Register " + reg + " ?", "Delete Modbus Linkage", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
if (database != null)
{
if (database.RemoveModbusDatabySIID(ssid))
{
ModbusTable.ItemsSource = database.GetModbusDatas();
}
else MessageBox.Show("Failed to delete from database");
}
else MessageBox.Show("Database is null");
else MessageBox.Show("Select a row in Table to delete");
}
}
private void btnClearModbus_Click(object sender, RoutedEventArgs e)
@@ -935,15 +941,8 @@ namespace FAtoPA.Net
MessageBoxResult result = MessageBox.Show("Clear all Modbus Linkage ?", "Clear Modbus Linkage", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
if (database != null)
{
if (database.ClearModbusTable())
{
ModbusTable.ItemsSource = database.GetModbusDatas();
}
else MessageBox.Show("Failed to clear database");
}
else MessageBox.Show("Database is null");
database.ClearModbusTable();
ModbusTableMember.Clear();
}
}
@@ -995,45 +994,19 @@ namespace FAtoPA.Net
private void btnDelVX_Click(object sender, RoutedEventArgs e)
{
String ssid = "";
int id = 0;
int cin = 0;
if (VXSIIDComboBox.SelectedItem != null)
if (VXTable != null)
{
ssid = (string)VXSIIDComboBox.SelectedItem;
}
else
var selected = VXTable.SelectedItem as VXData;
if (selected != null)
{
MessageBox.Show("Invalid SIID"); return;
}
if (VXFrame.SelectedItem != null)
{
id = (int)VXFrame.SelectedItem;
}
else
{
MessageBox.Show("Invalid ID"); return;
}
if (VXCIN.SelectedItem != null)
{
cin = (int)VXCIN.SelectedItem;
}
else
{
MessageBox.Show("Invalid CIN"); return;
}
MessageBoxResult result = MessageBox.Show("Delete SIID " + ssid + " Link with Frame " + id + " CIN " + cin + " ?", "Delete VX Linkage", MessageBoxButton.YesNo);
var result = MessageBox.Show($"Delete SIID={selected.SIID}, ID={selected.FrameID}, CIN={selected.CIN} ?", "Delete SIID", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
if (database != null)
{
if (database.RemoveVXDatabySIID(ssid))
{
VXTable.ItemsSource = database.GetVXDatas();
database.RemoveVXDatabySIID(selected.SIID);
VXTableMember.Remove(selected);
}
else MessageBox.Show("Failed to delete from database");
}
else MessageBox.Show("Database is null");
else MessageBox.Show("Select a row in Table to delete");
}
}
@@ -1042,15 +1015,8 @@ namespace FAtoPA.Net
MessageBoxResult result = MessageBox.Show("Clear all VX Linkage ?", "Clear VX Linkage", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
if (database != null)
{
if (database.ClearVXTable())
{
VXTable.ItemsSource = database.GetVXDatas();
}
else MessageBox.Show("Failed to clear database");
}
else MessageBox.Show("Database is null");
database.ClearVXTable();
VXTableMember.Clear();
}
}
@@ -1060,19 +1026,20 @@ namespace FAtoPA.Net
switch (e.PropertyName)
{
case "SIID":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "Enable":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "Description":
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
e.Column.Width = DataGridLength.Auto;
break;
case "Value":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "LastUpdate":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
}
}
@@ -1083,19 +1050,19 @@ namespace FAtoPA.Net
switch (e.PropertyName)
{
case "SIID":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "Register":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "Description":
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
e.Column.Width = DataGridLength.Auto;
break;
case "Value":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "LastUpdate":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
}
}
@@ -1106,22 +1073,22 @@ namespace FAtoPA.Net
switch (e.PropertyName)
{
case "SIID":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "ID":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "CIN":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "Description":
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
e.Column.Width = DataGridLength.Auto;
break;
case "Value":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
case "LastUpdate":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
e.Column.Width = DataGridLength.Auto;
break;
}
}
@@ -1135,13 +1102,23 @@ namespace FAtoPA.Net
if (selected.Tag is NodeData)
{
NodeData data = (NodeData)selected.Tag;
FSMData fSMData = new FSMData(data.SIID.ToString(), true, data.Description);
FSMData fSMData = new FSMData(data.SIID.ToString(), true, data.Label, data.Description);
if (database.FSMDataHaveSIID(fSMData.SIID)!=null)
{
Debug.WriteLine($"database already have SIID={fSMData.SIID}");
} else
{
if (database.AddFSMData(fSMData))
{
FsmTableMember.Add(fSMData);
database.AddFSMData(fSMData);
FSMSIID.Add(fSMData.SIID);
Debug.WriteLine($"Added SIID={fSMData.SIID}, Label={fSMData.Label}, Type= {fSMData.Type} to database");
}
else MessageBox.Show("Failed to add to database");
else MessageBox.Show($"Failed to add SIID={fSMData.SIID}, Label={fSMData.Label}, Type={fSMData.Type} to database");
}
}
else MessageBox.Show("Selected SIID dont have NodeData");
} else MessageBox.Show("No SIID Selected");
@@ -1191,7 +1168,8 @@ namespace FAtoPA.Net
private void refresh_connectedlist()
{
Application.Current.Dispatcher.Invoke(() =>
{
connectedlist.Items.Clear();
foreach (ModbusClientRecord client in ModbusSlave)
{
@@ -1200,10 +1178,14 @@ namespace FAtoPA.Net
l.Margin = new Thickness(5, 0, 5, 0);
l.TextWrapping = TextWrapping.Wrap;
UpdateLabel(l, client);
l.Tag = client;
connectedlist.Items.Add(l);
}
if (connectedcount != null) connectedcount.Content = "Connected : " + connectedlist.Items.Count;
});
}
void ModbusSlaveEvent.Log(string msg)
@@ -1229,6 +1211,7 @@ namespace FAtoPA.Net
public void TXRXStatusUpdate(ModbusClientRecord client)
{
connectedlist.Dispatcher.Invoke(() => {
foreach (var item in connectedlist.Items)
{
TextBlock l = (TextBlock)item;
@@ -1238,12 +1221,14 @@ namespace FAtoPA.Net
return;
}
}
});
}
private void UpdateLabel(TextBlock l, ModbusClientRecord client)
{
l.Text = client.remoteEP + " TX: " + client.TXBytes + " RX: " + client.RXBytes + "TXOK: " + client.TXResponse + " RXOK: " + client.RXValidRequest;
l.Text = client.remoteEP + "/TX: " + client.TXBytes + "/RX: " + client.RXBytes + "/TXOK: " + client.TXResponse + "/RXOK: " + client.RXValidRequest+"/RXFAIL: "+client.RXInvalidRequest;
}
}
@@ -1276,16 +1261,17 @@ namespace FAtoPA.Net
public void StatisticUpdate(uint TXOK, uint RXOK, uint TXErr, uint RXerr, uint TXBytes, uint RXBytes)
{
if (statusbar != null)
{
statusbar.Dispatcher.Invoke(() =>
//if (statusbar != null)
//{
{
statusbar.Text = "FSM : TXOK: " + TXOK + " RXOK: " + RXOK + " TXErr: " + TXErr + " RXErr: " + RXerr + " TXBytes: " + TXBytes + " RXBytes: " + RXBytes;
});
// statusbar.Dispatcher.Invoke(() =>
// {
// statusbar.Text = "FSM : TXOK: " + TXOK + " RXOK: " + RXOK + " TXErr: " + TXErr + " RXErr: " + RXerr + " TXBytes: " + TXBytes + " RXBytes: " + RXBytes;
// });
}
//}
}
}
@@ -1305,12 +1291,20 @@ namespace FAtoPA.Net
public void DiscoveredSIID(string SIID, NodeData type)
{
Debug.WriteLine("Discovered SIID : " + SIID + " Type : " + type.Description);
Debug.WriteLine($"Discovered SIID={SIID} Label={type.Label} Type={type.Description}");
if (type.Label != null && type.Label.Length > 0)
{
if (type.Description != null && type.Description.Length > 0)
{
// yang punya Label dan Description saja yang masuk ke Listbox dan dihitung
Application.Current.Dispatcher.Invoke(() =>
{
listbox.Items.Add(new Label() { Content = $"{SIID} : {type.Label} : {type.Description}", Tag = type });
countlabel.Content = "Count : " + listbox.Items.Count;
});
}
}
}
@@ -1318,8 +1312,10 @@ namespace FAtoPA.Net
public void NewState(string SIID, NodeState previous, NodeState current)
{
Debug.WriteLine("New State : " + SIID + " Previous : " + previous?.LogicalState + " Current : " + current.LogicalState);
if (data != null)
{
// update yang ada di FsmTable saja
foreach (var dd in data)
{
if (dd.SIID.Equals(SIID))
@@ -1327,6 +1323,7 @@ namespace FAtoPA.Net
dd.LastUpdate = DateTime.Now.ToString();
dd.Value = current.LogicalState ?? "Unknown";
Debug.WriteLine($"Changing row in FSM Table for SIID={SIID} Value={dd.Value}");
return;
}
}
@@ -1344,11 +1341,15 @@ namespace FAtoPA.Net
ObservableCollection<FSMData> source;
ObservableCollection<ModbusData> data;
ModbusSlave slave;
public ModbusTriggerFromFSM(ObservableCollection<FSMData> source, ObservableCollection<ModbusData> data, ModbusSlave slave)
List<String> ConditionON;
List<String> ConditionOFF;
public ModbusTriggerFromFSM(ObservableCollection<FSMData> source, ObservableCollection<ModbusData> data, ModbusSlave slave, List<string> conditionON, List<string> conditionOFF)
{
this.source = source;
this.data = data;
this.slave = slave;
ConditionON = conditionON;
ConditionOFF = conditionOFF;
}
public void DiscoveredSIID(string SIID, NodeData type)
{
@@ -1358,33 +1359,71 @@ namespace FAtoPA.Net
public void NewState(string SIID, NodeState previous, NodeState current)
{
//FSMData src = source.First(d => d.SIID == SIID);
//ModbusData dt = data.First(d => d.SIID == SIID);
//if (src != null && dt != null)
//{
// if (src.Enable)
// {
// if (dt.Register >= 0)
// {
// dt.LastUpdate = DateTime.Now.ToString();
// dt.Value = current.LogicalState ?? "Unknown";
// if (slave != null)
// {
// switch (current.LogicalState)
// {
// case "ON":
// slave.SetRegister(dt.Register, 1);
// break;
// case "NORMAL":
// case "OFF":
// slave.SetRegister(dt.Register, 0);
// break;
// }
// }
// }
// }
//}
FSMData src = null;
ModbusData dt = null;
foreach(var x in source)
{
if (x.SIID.Equals(SIID))
{
src = x;
break;
}
}
foreach (var x in data)
{
if (x.SIID.Equals(SIID))
{
dt = x;
break;
}
}
if (src != null && dt != null)
{
if (src.Enable)
{
if (dt.Register >= 0)
{
if (slave != null)
{
if (ConditionON != null && ConditionOFF != null)
{
if (ConditionON.Contains(current.LogicalState))
{
Debug.WriteLine($"NewState for SIID={SIID} State={current.LogicalState} is ConditionON");
slave.SetRegister(dt.Register, 1);
dt.Value = "ON";
}
else if (ConditionOFF.Contains(current.LogicalState))
{
Debug.WriteLine($"NewState for SIID={SIID} State={current.LogicalState} is ConditionOFF");
slave.SetRegister(dt.Register, 0);
dt.Value = "OFF";
}
else
{
Debug.WriteLine($"NewState for SIID={SIID} in Modbus is ignored because Condition ON/OFF is not met");
dt.Value = "RULE NOT AVAILABLE";
}
}
else
{
Debug.WriteLine($"Condition ON/OFF for SIID={SIID} in Modbus is not set");
dt.Value = "RULE NOT SET";
}
}
else
{
Debug.WriteLine($"Not connected to Modbus Slave, NewState for SIID={SIID} in Modbus is ignored");
dt.Value = "NO MODBUS";
}
dt.LastUpdate = DateTime.Now.ToString();
} else Debug.WriteLine($"NewState for SIID={SIID} in Modbus is ignored because Register is not set");
} else Debug.WriteLine($"NewState for SIID={SIID} in Modbus is ignored because FSMData is Disabled");
} else Debug.WriteLine($"NewState for SIID={SIID} in Modbus is ignored because not registered in ModbusTable");
}
}
@@ -1394,11 +1433,15 @@ namespace FAtoPA.Net
ObservableCollection<VXData> data;
ObservableCollection<FSMData> source;
VX3K vx;
public VXTriggerFromFSM(ObservableCollection<FSMData> source, ObservableCollection<VXData> data, VX3K vx)
List<String> ConditionON;
List<String> ConditionOFF;
public VXTriggerFromFSM(ObservableCollection<FSMData> source, ObservableCollection<VXData> data, VX3K vx, List<string> conditionON, List<string> conditionOFF)
{
this.source = source;
this.data = data;
this.vx = vx;
ConditionON = conditionON;
ConditionOFF = conditionOFF;
}
public void DiscoveredSIID(string SIID, NodeData type)
{
@@ -1408,31 +1451,66 @@ namespace FAtoPA.Net
public void NewState(string SIID, NodeState previous, NodeState current)
{
//FSMData src = source.First(d => d.SIID == SIID);
//VXData dt = data.First(d => d.SIID == SIID);
//if (src != null && dt != null)
//{
// if (src.Enable)
// {
// dt.LastUpdate = DateTime.Now.ToString();
// dt.Value = current.LogicalState ?? "Unknown";
// if (vx != null && vx.IsConnected())
// {
// switch (current.LogicalState)
// {
// case "ON":
// vx.Virtual_Contact_Input(dt.FrameID, dt.CIN, true);
// break;
// case "NORMAL":
// case "OFF":
// vx.Virtual_Contact_Input(dt.FrameID, dt.CIN, false);
// break;
// }
FSMData src = null;
VXData dt = null;
foreach (var x in source)
{
if (x.SIID.Equals(SIID))
{
src = x;
break;
}
}
foreach (var x in data)
{
if (x.SIID.Equals(SIID))
{
dt = x;
break;
}
}
// }
if (src != null && dt != null)
{
if (src.Enable)
{
if (vx != null && vx.IsConnected())
{
if (ConditionON != null && ConditionOFF != null)
{
if (ConditionON.Contains(current.LogicalState))
{
Debug.WriteLine($"NewState for SIID={SIID} State={current.LogicalState} is ConditionON");
vx.Virtual_Contact_Input(dt.FrameID, dt.CIN, true);
dt.Value = "ON";
}
else if (ConditionOFF.Contains(current.LogicalState))
{
Debug.WriteLine($"NewState for SIID={SIID} State={current.LogicalState} is ConditionOFF");
vx.Virtual_Contact_Input(dt.FrameID, dt.CIN, false);
dt.Value = "OFF";
}
else
{
Debug.WriteLine($"NewState for SIID={SIID} in VX3K is ignored because Condition ON/OFF is not met");
dt.Value = "RULE NOT AVAILABLE";
}
}
else
{
Debug.WriteLine($"Condition ON/OFF for SIID={SIID} in VX3K is not set");
dt.Value = "RULE NOT SET";
}
}
else
{
Debug.WriteLine($"Not connected to VX3K, NewState for SIID={SIID} in VX3K is ignored");
dt.Value = "NO VX";
}
dt.LastUpdate = DateTime.Now.ToString();
} else Debug.WriteLine($"NewState for SIID={SIID} in VX3K is ignored because FSMData is Disabled");
} else Debug.WriteLine($"NewState for SIID={SIID} in VX3K is ignored because not registered in VXTable");
// }
//}
}
}
}

View File

@@ -301,6 +301,7 @@ namespace FAtoPA
client = null;
}
}
catch (Exception e)
{
@@ -309,8 +310,13 @@ namespace FAtoPA
if (key != null && client != null)
{
ModbusClientRecord prev = null;
if (_event!=null) _event.Log("ModbusSlave: New Connection from " + key);
ModbusClientRecord prev = clientmap[key];
if (clientmap.ContainsKey(key))
{
prev = clientmap[key];
}
if (prev != null)
{
@@ -354,9 +360,11 @@ namespace FAtoPA
// 2 byte Register Count, index [10,11]
byte[] cmd = new byte[12];
try {
int readcount = await stream.ReadAsync(new byte[12], 0, 12);
int readcount = await stream.ReadAsync(cmd, 0, 12);
if (readcount >= 12)
{
client.RXBytes += (uint)readcount;
UInt16 transactionID = GetTransactionID(cmd);
UInt16 protocolID = GetProtocolID(cmd);
@@ -482,7 +490,10 @@ namespace FAtoPA
client.TXBytes += (uint)response.Length;
client.TXResponse++;
}
if (_event != null) _event.TXRXStatusUpdate(client);
}
else if (readcount == 0) break; // connection closed
}
catch (Exception e)
{