git_bsmd/AIS/AISAdmin/Main.cs

237 lines
8.9 KiB
C#

using bsmd.AIS2Service;
using System.ComponentModel;
namespace AISAdmin
{
public partial class Main : Form
{
private AIS_SQLiteStorage? _storage;
private BindingList<MonitorGroup> _monitorGroups = new();
private MonitorGroup? _currentGroup;
private readonly Dictionary<MonitorZone, List<MonitorAssignment>> _zoneAssignmentDict = new();
private readonly List<AIS_Target> _allTargets = new();
public Main()
{
InitializeComponent();
_currentGroup = null;
}
private void buttonZoneUp_Click(object sender, EventArgs e)
{
MonitorZone mz = (MonitorZone)this.listBoxZones.SelectedItem;
int i = this.listBoxZones.SelectedIndex;
BindingList<MonitorZone> zones = (BindingList<MonitorZone>)this.listBoxZones.DataSource;
if (mz != null && (i > 0))
{
MonitorZone prevZone = zones[i - 1];
mz.Sequence -= 1;
prevZone.Sequence += 1;
zones[i - 1] = mz;
zones[i] = prevZone;
this.listBoxZones.SelectedIndex = i - 1;
}
}
private void buttonZoneDown_Click(object sender, EventArgs e)
{
MonitorZone mz = (MonitorZone)this.listBoxZones.SelectedItem;
int i = this.listBoxZones.SelectedIndex;
BindingList<MonitorZone> zones = (BindingList<MonitorZone>) this.listBoxZones.DataSource;
if (mz != null && (i < (zones.Count - 1)))
{
MonitorZone nextZone = zones[i + 1];
mz.Sequence += 1;
nextZone.Sequence -= 1;
zones[i + 1] = mz;
zones[i] = nextZone;
this.listBoxZones.SelectedIndex = i + 1;
}
}
private void buttonImportZone_Click(object sender, EventArgs e)
{
using OpenFileDialog ofd = new();
ofd.Filter = "Google Earth KML files (*.kml)|*.kml|All files (*.*)|*.*";
ofd.RestoreDirectory = true;
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
_monitorGroups = new BindingList<MonitorGroup>(MonitorGroup.LoadGroups(ofd.FileName));
this.comboBoxGroup.DataSource = _monitorGroups;
if (_monitorGroups.Count > 0)
this.comboBoxGroup.SelectedIndex = 0;
this.buttonSaveGroup.Enabled = _monitorGroups.Count > 0;
this.listBoxZones.Enabled = _monitorGroups.Count > 0;
}
}
private void buttonEditZone_Click(object sender, EventArgs e)
{
}
private void buttonDeleteZone_Click(object sender, EventArgs e)
{
}
private void buttonNewGroup_Click(object sender, EventArgs e)
{
this.textBoxGroup.ReadOnly = false;
this.buttonSaveGroup.Enabled = true;
_currentGroup = new MonitorGroup(-1, "");
this.textBoxGroup.ResetText();
}
private void buttonDeleteGroup_Click(object sender, EventArgs e)
{
}
private void buttonSaveGroup_Click(object sender, EventArgs e)
{
// save everything
foreach(MonitorGroup mg in this._monitorGroups)
{
this._storage?.Save(mg);
foreach(MonitorZone mz in mg.Zones)
{
if (mz.MonitorGroupId < 1) mz.MonitorGroupId = mg.Id;
this._storage?.Save(mz);
foreach(GeoPoint vertex in mz.Vertices)
{
if(vertex.MonitorZoneId < 1) vertex.MonitorZoneId = mz.Id;
this._storage?.Save(vertex);
}
}
}
}
private async void Main_Load(object sender, EventArgs e)
{
_storage = new AIS_SQLiteStorage(Properties.Settings.Default.SQLiteDBConnectionString);
_monitorGroups = new BindingList<MonitorGroup>(_storage.LoadGroups());
List<MonitorZone> zones = _storage.LoadMonitorZones();
foreach(MonitorGroup mg in _monitorGroups)
{
foreach(MonitorZone mz in zones)
{
if (mz.MonitorGroupId == mg.Id)
mg.Zones.Add(mz);
}
}
this.comboBoxGroup.DataSource = _monitorGroups;
this.comboBoxGroup.DisplayMember = "Name";
if (_monitorGroups.Count > 0)
this.comboBoxGroup.SelectedIndex = 0;
this.buttonSaveGroup.Enabled = _monitorGroups.Count > 0;
this.listBoxZones.Enabled = _monitorGroups.Count > 0;
Dictionary<int, AIS_Target> allTargets = await _storage.LoadTargets();
foreach (int mmsi in allTargets.Keys)
this._allTargets.Add(allTargets[mmsi]);
this._allTargets.Sort();
this.listBoxTargets.DataSource = this._allTargets;
if (this._allTargets.Count > 0)
this.listBoxTargets.Enabled = true;
}
private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
{
_currentGroup = (MonitorGroup) this.comboBoxGroup.SelectedItem;
if (_currentGroup != null)
{
_currentGroup.Zones.Sort();
this.listBoxZones.DataSource = new BindingList<MonitorZone>(_currentGroup.Zones);
foreach(MonitorZone zone in _currentGroup.Zones)
{
_zoneAssignmentDict[zone] = _storage?.LoadAssignmentsForZone(zone.Id) ?? new();
}
}
else
{
this.listBoxZones.DataSource = null;
this.textBoxGroup.ResetText();
this.buttonSaveGroup.Enabled = false;
this.textBoxGroup.ReadOnly = true;
}
this.buttonZoneDown.Enabled = (this._currentGroup != null) && (this._currentGroup.Zones.Count > 1);
this.buttonZoneUp.Enabled = (this._currentGroup != null) && (this._currentGroup.Zones.Count > 1);
}
private void checkBoxZoneActive_CheckedChanged(object sender, EventArgs e)
{
MonitorZone mz = (MonitorZone)this.listBoxZones.SelectedItem;
if (mz != null)
{
mz.Active = this.checkBoxZoneActive.Checked;
}
}
private void listBoxZones_SelectedIndexChanged(object sender, EventArgs e)
{
MonitorZone mz = (MonitorZone) this.listBoxZones.SelectedItem;
if(mz != null)
{
this.checkBoxZoneActive.Checked = mz.Active;
this.textBoxZone.Text = mz.Name;
if (this._zoneAssignmentDict.ContainsKey(mz))
this.listBoxAssignment.DataSource = new BindingList<MonitorAssignment>(this._zoneAssignmentDict[mz]);
else
this.listBoxAssignment.DataSource = null;
}
}
private void buttonNewAssignment_Click(object sender, EventArgs e)
{
MonitorAssignment ma = new(-1);
using EditAssignmentDialog ead = new EditAssignmentDialog();
ead.Assignment = ma;
if(ead.ShowDialog() == DialogResult.OK)
{
this.AddOrUpdateAssignmentForAllGroupZones(ma.MMSI, ma.MonitorType);
}
}
private void AddOrUpdateAssignmentForAllGroupZones(int mMSI, MonitorAssignment.ZoneMonitorType monitorType)
{
if (this._currentGroup == null) return;
foreach (MonitorZone mz in this._currentGroup.Zones)
{
if (!this._zoneAssignmentDict.ContainsKey(mz))
this._zoneAssignmentDict[mz] = new();
MonitorAssignment? ma = this._zoneAssignmentDict[mz].FirstOrDefault((z) => z.MMSI == mMSI);
if (ma == null)
{
ma = new MonitorAssignment(-1);
ma.MonitorZoneId = mz.Id;
this._zoneAssignmentDict[mz].Add(ma);
}
ma.MMSI = mMSI;
ma.MonitorType = monitorType;
_storage?.Save(ma);
}
}
private void buttonDeleteAssignment_Click(object sender, EventArgs e)
{
MonitorAssignment? ma = (MonitorAssignment)this.listBoxAssignment.SelectedItem;
if(ma != null)
{
}
}
private void buttonAssignSelectedTarget_Click(object sender, EventArgs e)
{
foreach (AIS_Target target in this.listBoxTargets.SelectedItems)
{
this.AddOrUpdateAssignmentForAllGroupZones(target.MMSI, MonitorAssignment.ZoneMonitorType.ENTER);
}
}
}
}