Improvement 25/11/2024
This commit is contained in:
@@ -3,9 +3,11 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
@@ -55,6 +57,9 @@ namespace FAtoPA.Net
|
||||
// Isi VX Table
|
||||
ObservableCollection<VXData> VXTableMember { get; set; }
|
||||
|
||||
// tabel translation FSM Status String to Modbus Register Value
|
||||
public ObservableCollection<KeyValuePair<String, Int16>> ModbusTranslationTable { get; set; }
|
||||
|
||||
public List<String> ConditionON;
|
||||
public List<String> ConditionOFF;
|
||||
|
||||
@@ -65,6 +70,8 @@ namespace FAtoPA.Net
|
||||
// Di-isi dengan user input di Tab Fire Alarm
|
||||
FSMSIID = new ObservableCollection<string>();
|
||||
|
||||
|
||||
|
||||
// Isinya ada di Window_Loaded
|
||||
ModbusRegisters = new ObservableCollection<int>();
|
||||
FsmTableMember = new ObservableCollection<FSMData>();
|
||||
@@ -105,17 +112,121 @@ 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());
|
||||
|
||||
Load_ModbusTranslationTable();
|
||||
|
||||
DataContext = this;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load Modbus Translation Table from JSON File
|
||||
/// </summary>
|
||||
private void Load_ModbusTranslationTable()
|
||||
{
|
||||
if (File.Exists("ModbusTranslationTable.json"))
|
||||
{
|
||||
String loadedJson = File.ReadAllText("ModbusTranslationTable.json");
|
||||
try
|
||||
{
|
||||
ModbusTranslationTable = JsonSerializer.Deserialize<ObservableCollection<KeyValuePair<string, short>>>(loadedJson);
|
||||
Debug.WriteLine("ModbusTranslationTable loaded");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("Error loading ModbusTranslationTable : " + ex.Message);
|
||||
Create_Default_ModbusTranslationTable();
|
||||
}
|
||||
} else
|
||||
{
|
||||
Debug.WriteLine("ModbusTranslationTable.json not found, creating default");
|
||||
Create_Default_ModbusTranslationTable();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create Default Modbus Translation Table
|
||||
/// <para>It also save the default table to JSON File</para>
|
||||
/// </summary>
|
||||
private void Create_Default_ModbusTranslationTable()
|
||||
{
|
||||
ModbusTranslationTable = new ObservableCollection<KeyValuePair<string, short>>();
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.INVALID.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.NORMAL.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.TROUBLE.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.FIRE.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.FIRE_PRE.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.VERIFY_FIRE.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.HEAT.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.SUPERVISORY.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.SMOKE.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.ACTIVATION.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.ACTIVATION_FAILED.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.TAMPER.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.COVER_OPEN.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.PAPER_OUT.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.WARNING.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.TROUBLE_LIGHT.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.WATCHDOGRESTART.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.ON.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.OFF.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.POLLUTION.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.POLLUTION_LIGHT.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.MONITOR.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.WATER.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.POWERFAIL.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.MANUAL_ALARM.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.PAS_WAIT_FOR_ACK.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.PAS_INVESTIGATE.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.AC_CHANGED.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.AC_COUNTDOWN_STARTED.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.AC_TAMPER.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.FIRE_INT.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.ERROR.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.UNKNOWN.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.MATCHALL.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.NET_CONFIG_MISMATCH.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.UNKNOWN_ITEM.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.MISSING_ITEM.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.INCOMPATIBLE_SOFTWARE.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.INCOMPATIBLE_NET_PROTOCOL.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.INFO_ALARM.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.CHEMICAL.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.WARNING_HEAT.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.WARNING_SMOKE.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.WARNING_CHEMICAL.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.REBOOT_READY.ToString(), 0));
|
||||
ModbusTranslationTable.Add(new KeyValuePair<string, short>(SILogicalState.LASTLOGICAL.ToString(), 0));
|
||||
Save_ModbusTranslationTable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save Modbus Translation Table to JSON File
|
||||
/// </summary>
|
||||
private void Save_ModbusTranslationTable()
|
||||
{
|
||||
try
|
||||
{
|
||||
String json = JsonSerializer.Serialize(ModbusTranslationTable);
|
||||
File.WriteAllText("ModbusTranslationTable.json", json);
|
||||
Debug.WriteLine("ModbusTranslationTable saved");
|
||||
} catch (Exception e)
|
||||
{
|
||||
Debug.WriteLine("Error saving ModbusTranslationTable : " + e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Debug.WriteLine("Window Loaded");
|
||||
@@ -167,6 +278,7 @@ namespace FAtoPA.Net
|
||||
FSMTable.ItemsSource = FsmTableMember;
|
||||
ModbusTable.ItemsSource = ModbusTableMember;
|
||||
VXTable.ItemsSource = VXTableMember;
|
||||
FSMtoModbusTranslationTable.ItemsSource = ModbusTranslationTable;
|
||||
|
||||
// Load FSM
|
||||
fsmEvent = new FSMEvent(this.firealarmstatusbar);
|
||||
@@ -198,6 +310,7 @@ namespace FAtoPA.Net
|
||||
fsm.AddListener(new FSMTableUpdater(FsmTableMember, DetectedSIID, DetectedSIIDCount));
|
||||
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)
|
||||
@@ -788,33 +901,6 @@ namespace FAtoPA.Net
|
||||
}
|
||||
}
|
||||
|
||||
private void btnAddSIID_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SIID selected = GetSIID();
|
||||
if (selected != null)
|
||||
{
|
||||
MessageBoxResult result = MessageBox.Show("Add SIID " + selected.ToString() + " ?", "Add SIID", MessageBoxButton.YesNo);
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
if (database != null)
|
||||
{
|
||||
//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");
|
||||
}
|
||||
}
|
||||
else MessageBox.Show("Invalid Selection");
|
||||
|
||||
}
|
||||
|
||||
private void btnDelSIID_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (FSMTable != null)
|
||||
@@ -846,39 +932,6 @@ namespace FAtoPA.Net
|
||||
}
|
||||
}
|
||||
|
||||
private SIID GetSIID()
|
||||
{
|
||||
int netgroup = 0;
|
||||
int netnode = 0;
|
||||
SIType sitype = 0;
|
||||
int sinumber = 0;
|
||||
int sisub = 0;
|
||||
|
||||
if (netGroupNumber.SelectedItem != null)
|
||||
{
|
||||
netgroup = (int)netGroupNumber.SelectedItem;
|
||||
if (netNodeNumber.SelectedItem != null)
|
||||
{
|
||||
netnode = (int)netNodeNumber.SelectedItem;
|
||||
if (siType.SelectedItem != null)
|
||||
{
|
||||
sitype = (SIType)siType.SelectedItem;
|
||||
if (siNumber.SelectedItem != null)
|
||||
{
|
||||
sinumber = (int)siNumber.SelectedItem;
|
||||
if (siSub.SelectedItem != null)
|
||||
{
|
||||
sisub = (int)siSub.SelectedItem;
|
||||
|
||||
return new SIID((byte)netgroup, (byte)netnode, sitype, (ushort)sinumber, (byte)sisub);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
private void btnAddModbus_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
@@ -1123,9 +1176,68 @@ namespace FAtoPA.Net
|
||||
else MessageBox.Show("Selected SIID dont have NodeData");
|
||||
} else MessageBox.Show("No SIID Selected");
|
||||
}
|
||||
|
||||
|
||||
private void FSMtoModbusTranslationTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
|
||||
{
|
||||
Debug.WriteLine("FSMtoModbusTranslationTable_AutoGeneratingColumn : " + e.PropertyName);
|
||||
switch (e.PropertyName)
|
||||
{
|
||||
case "Key":
|
||||
e.Column.Width = DataGridLength.Auto;
|
||||
break;
|
||||
case "Value":
|
||||
e.Column.Width = DataGridLength.Auto;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void FSMtoModbusTranslationTable_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
var selected = (sender as DataGrid).SelectedItem;
|
||||
|
||||
if (selected != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
KeyValuePair<String, Int16> vv = (KeyValuePair<String, Int16>)selected;
|
||||
Debug.WriteLine($"Selected Key={vv.Key}, Value={vv.Value}");
|
||||
//TODO create InputBox to select Value for Key
|
||||
} catch(Exception exception)
|
||||
{
|
||||
Debug.WriteLine("FSMtoModbusTranslationTable_MouseDoubleClick Error : " + exception.Message);
|
||||
}
|
||||
|
||||
}
|
||||
else Debug.WriteLine("Not selected yet");
|
||||
}
|
||||
|
||||
private void ResetFSMtoModbusTranslationTable_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var result = MessageBox.Show($"Reset FSM to Modbus Translation Table ?", "Reset FSM to Modbus Translation Table", MessageBoxButton.YesNo);
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
Create_Default_ModbusTranslationTable();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void SetFSMtoModbusTranslationTable_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var result = MessageBox.Show($"Save FSM to Modbus Translation Table ?", "Save FSM to Modbus Translation Table", MessageBoxButton.YesNo);
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
Save_ModbusTranslationTable();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// event handler for VX3K
|
||||
/// <summary>
|
||||
/// Event Handler for VX3K
|
||||
/// </summary>
|
||||
class VX3KEvent : EventInterface
|
||||
{
|
||||
private TextBlock statusbar;
|
||||
@@ -1150,6 +1262,9 @@ namespace FAtoPA.Net
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event Handler for Modbus
|
||||
/// </summary>
|
||||
class ModbusEvent : ModbusSlaveEvent
|
||||
{
|
||||
private TextBlock statusbar;
|
||||
@@ -1173,12 +1288,8 @@ namespace FAtoPA.Net
|
||||
connectedlist.Items.Clear();
|
||||
foreach (ModbusClientRecord client in ModbusSlave)
|
||||
{
|
||||
TextBlock l = new TextBlock();
|
||||
l.Width = connectedlist.Width;
|
||||
l.Margin = new Thickness(5, 0, 5, 0);
|
||||
l.TextWrapping = TextWrapping.Wrap;
|
||||
TextBlock l = new TextBlock() { Margin=new Thickness(5,0,5,0), TextWrapping=TextWrapping.Wrap, Width=connectedlist.Width, Tag = client, Height=50 };
|
||||
UpdateLabel(l, client);
|
||||
l.Tag = client;
|
||||
connectedlist.Items.Add(l);
|
||||
|
||||
}
|
||||
@@ -1233,6 +1344,9 @@ namespace FAtoPA.Net
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event Handler for Bosch FSM
|
||||
/// </summary>
|
||||
class FSMEvent : EventInterface
|
||||
{
|
||||
private TextBlock statusbar;
|
||||
@@ -1275,7 +1389,9 @@ namespace FAtoPA.Net
|
||||
}
|
||||
}
|
||||
|
||||
// Class ini untuk update Table FSM
|
||||
/// <summary>
|
||||
/// Class ini untuk update Table FSM
|
||||
/// </summary>
|
||||
class FSMTableUpdater : FSMResultInterface
|
||||
{
|
||||
// dari database
|
||||
@@ -1289,6 +1405,11 @@ namespace FAtoPA.Net
|
||||
this.countlabel = countlabel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method ini untuk menambahkan SIID yang muncul dari FSM ke Listbox
|
||||
/// </summary>
|
||||
/// <param name="SIID">SIID yang muncul</param>
|
||||
/// <param name="type">Tipe nya</param>
|
||||
public void DiscoveredSIID(string SIID, NodeData type)
|
||||
{
|
||||
Debug.WriteLine($"Discovered SIID={SIID} Label={type.Label} Type={type.Description}");
|
||||
@@ -1299,7 +1420,7 @@ namespace FAtoPA.Net
|
||||
// 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 });
|
||||
listbox.Items.Add(new Label() { Content = $"{SIID} : {type.Label} : {type.Description}", Tag = type, Height=50 });
|
||||
countlabel.Content = "Count : " + listbox.Items.Count;
|
||||
});
|
||||
}
|
||||
@@ -1308,7 +1429,12 @@ namespace FAtoPA.Net
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Method ini untuk mengupdate State SIID yang ada di FSM
|
||||
/// </summary>
|
||||
/// <param name="SIID">SIID yang muncul</param>
|
||||
/// <param name="previous">Previous State kalau ada</param>
|
||||
/// <param name="current">State sekarang</param>
|
||||
public void NewState(string SIID, NodeState previous, NodeState current)
|
||||
{
|
||||
Debug.WriteLine("New State : " + SIID + " Previous : " + previous?.LogicalState + " Current : " + current.LogicalState);
|
||||
@@ -1334,7 +1460,9 @@ namespace FAtoPA.Net
|
||||
|
||||
}
|
||||
|
||||
// Class ini untuk Update Modbus Register dari FSM Update
|
||||
/// <summary>
|
||||
/// Class ini untuk Update Modbus Register dari FSM Update
|
||||
/// </summary>
|
||||
class ModbusTriggerFromFSM : FSMResultInterface
|
||||
{
|
||||
// dari database
|
||||
@@ -1351,12 +1479,19 @@ namespace FAtoPA.Net
|
||||
ConditionON = conditionON;
|
||||
ConditionOFF = conditionOFF;
|
||||
}
|
||||
public void DiscoveredSIID(string SIID, NodeData type)
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Tidak dipakai
|
||||
/// </summary>
|
||||
void FSMResultInterface.DiscoveredSIID(string SIID, NodeData type) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Ada State baru dari FSM, Action to Modbus
|
||||
/// </summary>
|
||||
/// <param name="SIID">SIID yang muncul</param>
|
||||
/// <param name="previous">Previous State kalau ada</param>
|
||||
/// <param name="current">State sekarang</param>
|
||||
public void NewState(string SIID, NodeState previous, NodeState current)
|
||||
{
|
||||
|
||||
@@ -1427,7 +1562,9 @@ namespace FAtoPA.Net
|
||||
}
|
||||
}
|
||||
|
||||
// Class ini untuk Trigger VX3K dari FSM Update
|
||||
/// <summary>
|
||||
/// Class ini untuk Trigger VX3K dari FSM Update
|
||||
/// </summary>
|
||||
class VXTriggerFromFSM : FSMResultInterface
|
||||
{
|
||||
ObservableCollection<VXData> data;
|
||||
@@ -1443,12 +1580,19 @@ namespace FAtoPA.Net
|
||||
ConditionON = conditionON;
|
||||
ConditionOFF = conditionOFF;
|
||||
}
|
||||
public void DiscoveredSIID(string SIID, NodeData type)
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Event Discovered SIID, tidak dipakai di sini
|
||||
/// </summary>
|
||||
void FSMResultInterface.DiscoveredSIID(string SIID, NodeData type) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Ada State baru dari FSM, Action to VX 3K
|
||||
/// </summary>
|
||||
/// <param name="SIID">SIID yang muncul</param>
|
||||
/// <param name="previous">Previous state kalau ada</param>
|
||||
/// <param name="current">State sekarang</param>
|
||||
public void NewState(string SIID, NodeState previous, NodeState current)
|
||||
{
|
||||
FSMData src = null;
|
||||
|
||||
Reference in New Issue
Block a user