diff --git a/netxml2kml/Data/DatabaseContext.cs b/netxml2kml/Data/DatabaseContext.cs index 126f1fc..19c8d8c 100644 --- a/netxml2kml/Data/DatabaseContext.cs +++ b/netxml2kml/Data/DatabaseContext.cs @@ -7,6 +7,7 @@ public sealed class DatabaseContext : DbContext { public DbSet WirelessNetworks { get; set; } = null!; public DbSet WirelessClients { get; set; } = null!; + public DbSet WirelessConnections { get; set; } = null!; private string DbPath { get; } @@ -23,6 +24,12 @@ public sealed class DatabaseContext : DbContext DbPath = Path.Join(path, "netxml2kml.sqlite3.db"); } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasKey(wc => + new {wc.WirelessNetworkBssid, wc.WirelessClientMac}); + } + protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data Source={DbPath}"); } \ No newline at end of file diff --git a/netxml2kml/Migrations/20220810152156_InitialCreate.cs b/netxml2kml/Migrations/20220810152156_InitialCreate.cs deleted file mode 100644 index 422abc1..0000000 --- a/netxml2kml/Migrations/20220810152156_InitialCreate.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace netxml2kml.Migrations -{ - public partial class InitialCreate : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "WirelessClients", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Mac = table.Column(type: "TEXT", nullable: false), - Manufacturer = table.Column(type: "TEXT", nullable: false), - TotalPackets = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_WirelessClients", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "WirelessNetworks", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Ssid = table.Column(type: "TEXT", nullable: false), - IsCloaked = table.Column(type: "INTEGER", nullable: false), - Encryption = table.Column(type: "TEXT", nullable: false), - Bssid = table.Column(type: "TEXT", nullable: false), - Manufacturer = table.Column(type: "TEXT", nullable: false), - Channel = table.Column(type: "INTEGER", nullable: false), - FrequencyMhz = table.Column(type: "INTEGER", nullable: false), - MaxSignalDbm = table.Column(type: "INTEGER", nullable: false), - MaxLatitude = table.Column(type: "REAL", nullable: false), - MaxLongitude = table.Column(type: "REAL", nullable: false), - MaxAltitude = table.Column(type: "REAL", nullable: false), - FirstSeen = table.Column(type: "TEXT", nullable: false), - LastUpdate = table.Column(type: "TEXT", nullable: false), - TotalPackets = table.Column(type: "INTEGER", nullable: false), - WirelessClientId = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_WirelessNetworks", x => x.Id); - table.ForeignKey( - name: "FK_WirelessNetworks_WirelessClients_WirelessClientId", - column: x => x.WirelessClientId, - principalTable: "WirelessClients", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_WirelessNetworks_WirelessClientId", - table: "WirelessNetworks", - column: "WirelessClientId"); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "WirelessNetworks"); - - migrationBuilder.DropTable( - name: "WirelessClients"); - } - } -} diff --git a/netxml2kml/Migrations/20220810152156_InitialCreate.Designer.cs b/netxml2kml/Migrations/20220812182106_InitialCreate.Designer.cs similarity index 53% rename from netxml2kml/Migrations/20220810152156_InitialCreate.Designer.cs rename to netxml2kml/Migrations/20220812182106_InitialCreate.Designer.cs index b5ab709..7b99d05 100644 --- a/netxml2kml/Migrations/20220810152156_InitialCreate.Designer.cs +++ b/netxml2kml/Migrations/20220812182106_InitialCreate.Designer.cs @@ -11,7 +11,7 @@ using netxml2kml.Data; namespace netxml2kml.Migrations { [DbContext(typeof(DatabaseContext))] - [Migration("20220810152156_InitialCreate")] + [Migration("20220812182106_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -21,53 +21,63 @@ namespace netxml2kml.Migrations modelBuilder.Entity("netxml2kml.Models.WirelessClient", b => { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - b.Property("Mac") - .IsRequired() + .HasColumnType("TEXT"); + + b.Property("FirstSeenDate") + .HasColumnType("TEXT"); + + b.Property("LastUpdateDate") .HasColumnType("TEXT"); b.Property("Manufacturer") .IsRequired() .HasColumnType("TEXT"); - b.Property("TotalPackets") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); + b.HasKey("Mac"); b.ToTable("WirelessClients"); }); + modelBuilder.Entity("netxml2kml.Models.WirelessConnection", b => + { + b.Property("WirelessNetworkBssid") + .HasColumnType("TEXT"); + + b.Property("WirelessClientMac") + .HasColumnType("TEXT"); + + b.Property("FirstSeenDate") + .HasColumnType("TEXT"); + + b.Property("LastUpdateDate") + .HasColumnType("TEXT"); + + b.HasKey("WirelessNetworkBssid", "WirelessClientMac"); + + b.HasIndex("WirelessClientMac"); + + b.ToTable("WirelessConnections"); + }); + modelBuilder.Entity("netxml2kml.Models.WirelessNetwork", b => { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - b.Property("Bssid") - .IsRequired() .HasColumnType("TEXT"); - b.Property("Channel") - .HasColumnType("INTEGER"); - b.Property("Encryption") - .IsRequired() .HasColumnType("TEXT"); - b.Property("FirstSeen") + b.Property("Essid") .HasColumnType("TEXT"); - b.Property("FrequencyMhz") - .HasColumnType("INTEGER"); + b.Property("FirstSeenDate") + .HasColumnType("TEXT"); - b.Property("IsCloaked") - .HasColumnType("INTEGER"); + b.Property("FrequencyMhz") + .HasColumnType("REAL"); - b.Property("LastUpdate") + b.Property("LastUpdateDate") .HasColumnType("TEXT"); b.Property("Manufacturer") @@ -86,32 +96,38 @@ namespace netxml2kml.Migrations b.Property("MaxSignalDbm") .HasColumnType("INTEGER"); - b.Property("Ssid") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("TotalPackets") - .HasColumnType("INTEGER"); - - b.Property("WirelessClientId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("WirelessClientId"); + b.HasKey("Bssid"); b.ToTable("WirelessNetworks"); }); - modelBuilder.Entity("netxml2kml.Models.WirelessNetwork", b => + modelBuilder.Entity("netxml2kml.Models.WirelessConnection", b => { b.HasOne("netxml2kml.Models.WirelessClient", "WirelessClient") - .WithMany() - .HasForeignKey("WirelessClientId") + .WithMany("WirelessConnections") + .HasForeignKey("WirelessClientMac") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("netxml2kml.Models.WirelessNetwork", "WirelessNetwork") + .WithMany("WirelessConnections") + .HasForeignKey("WirelessNetworkBssid") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.Navigation("WirelessClient"); + + b.Navigation("WirelessNetwork"); + }); + + modelBuilder.Entity("netxml2kml.Models.WirelessClient", b => + { + b.Navigation("WirelessConnections"); + }); + + modelBuilder.Entity("netxml2kml.Models.WirelessNetwork", b => + { + b.Navigation("WirelessConnections"); }); #pragma warning restore 612, 618 } diff --git a/netxml2kml/Migrations/20220812182106_InitialCreate.cs b/netxml2kml/Migrations/20220812182106_InitialCreate.cs new file mode 100644 index 0000000..6e9b387 --- /dev/null +++ b/netxml2kml/Migrations/20220812182106_InitialCreate.cs @@ -0,0 +1,91 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace netxml2kml.Migrations +{ + public partial class InitialCreate : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "WirelessClients", + columns: table => new + { + Mac = table.Column(type: "TEXT", nullable: false), + Manufacturer = table.Column(type: "TEXT", nullable: false), + FirstSeenDate = table.Column(type: "TEXT", nullable: false), + LastUpdateDate = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_WirelessClients", x => x.Mac); + }); + + migrationBuilder.CreateTable( + name: "WirelessNetworks", + columns: table => new + { + Bssid = table.Column(type: "TEXT", nullable: false), + Essid = table.Column(type: "TEXT", nullable: true), + Manufacturer = table.Column(type: "TEXT", nullable: false), + Encryption = table.Column(type: "TEXT", nullable: true), + FrequencyMhz = table.Column(type: "REAL", nullable: false), + MaxSignalDbm = table.Column(type: "INTEGER", nullable: false), + MaxLatitude = table.Column(type: "REAL", nullable: false), + MaxLongitude = table.Column(type: "REAL", nullable: false), + MaxAltitude = table.Column(type: "REAL", nullable: false), + FirstSeenDate = table.Column(type: "TEXT", nullable: false), + LastUpdateDate = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_WirelessNetworks", x => x.Bssid); + }); + + migrationBuilder.CreateTable( + name: "WirelessConnections", + columns: table => new + { + WirelessNetworkBssid = table.Column(type: "TEXT", nullable: false), + WirelessClientMac = table.Column(type: "TEXT", nullable: false), + FirstSeenDate = table.Column(type: "TEXT", nullable: false), + LastUpdateDate = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_WirelessConnections", x => new { x.WirelessNetworkBssid, x.WirelessClientMac }); + table.ForeignKey( + name: "FK_WirelessConnections_WirelessClients_WirelessClientMac", + column: x => x.WirelessClientMac, + principalTable: "WirelessClients", + principalColumn: "Mac", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_WirelessConnections_WirelessNetworks_WirelessNetworkBssid", + column: x => x.WirelessNetworkBssid, + principalTable: "WirelessNetworks", + principalColumn: "Bssid", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_WirelessConnections_WirelessClientMac", + table: "WirelessConnections", + column: "WirelessClientMac"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "WirelessConnections"); + + migrationBuilder.DropTable( + name: "WirelessClients"); + + migrationBuilder.DropTable( + name: "WirelessNetworks"); + } + } +} diff --git a/netxml2kml/Migrations/DatabaseContextModelSnapshot.cs b/netxml2kml/Migrations/DatabaseContextModelSnapshot.cs index 99afa3c..0a40f51 100644 --- a/netxml2kml/Migrations/DatabaseContextModelSnapshot.cs +++ b/netxml2kml/Migrations/DatabaseContextModelSnapshot.cs @@ -19,53 +19,63 @@ namespace netxml2kml.Migrations modelBuilder.Entity("netxml2kml.Models.WirelessClient", b => { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - b.Property("Mac") - .IsRequired() + .HasColumnType("TEXT"); + + b.Property("FirstSeenDate") + .HasColumnType("TEXT"); + + b.Property("LastUpdateDate") .HasColumnType("TEXT"); b.Property("Manufacturer") .IsRequired() .HasColumnType("TEXT"); - b.Property("TotalPackets") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); + b.HasKey("Mac"); b.ToTable("WirelessClients"); }); + modelBuilder.Entity("netxml2kml.Models.WirelessConnection", b => + { + b.Property("WirelessNetworkBssid") + .HasColumnType("TEXT"); + + b.Property("WirelessClientMac") + .HasColumnType("TEXT"); + + b.Property("FirstSeenDate") + .HasColumnType("TEXT"); + + b.Property("LastUpdateDate") + .HasColumnType("TEXT"); + + b.HasKey("WirelessNetworkBssid", "WirelessClientMac"); + + b.HasIndex("WirelessClientMac"); + + b.ToTable("WirelessConnections"); + }); + modelBuilder.Entity("netxml2kml.Models.WirelessNetwork", b => { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - b.Property("Bssid") - .IsRequired() .HasColumnType("TEXT"); - b.Property("Channel") - .HasColumnType("INTEGER"); - b.Property("Encryption") - .IsRequired() .HasColumnType("TEXT"); - b.Property("FirstSeen") + b.Property("Essid") .HasColumnType("TEXT"); - b.Property("FrequencyMhz") - .HasColumnType("INTEGER"); + b.Property("FirstSeenDate") + .HasColumnType("TEXT"); - b.Property("IsCloaked") - .HasColumnType("INTEGER"); + b.Property("FrequencyMhz") + .HasColumnType("REAL"); - b.Property("LastUpdate") + b.Property("LastUpdateDate") .HasColumnType("TEXT"); b.Property("Manufacturer") @@ -84,32 +94,38 @@ namespace netxml2kml.Migrations b.Property("MaxSignalDbm") .HasColumnType("INTEGER"); - b.Property("Ssid") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("TotalPackets") - .HasColumnType("INTEGER"); - - b.Property("WirelessClientId") - .HasColumnType("INTEGER"); - - b.HasKey("Id"); - - b.HasIndex("WirelessClientId"); + b.HasKey("Bssid"); b.ToTable("WirelessNetworks"); }); - modelBuilder.Entity("netxml2kml.Models.WirelessNetwork", b => + modelBuilder.Entity("netxml2kml.Models.WirelessConnection", b => { b.HasOne("netxml2kml.Models.WirelessClient", "WirelessClient") - .WithMany() - .HasForeignKey("WirelessClientId") + .WithMany("WirelessConnections") + .HasForeignKey("WirelessClientMac") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("netxml2kml.Models.WirelessNetwork", "WirelessNetwork") + .WithMany("WirelessConnections") + .HasForeignKey("WirelessNetworkBssid") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.Navigation("WirelessClient"); + + b.Navigation("WirelessNetwork"); + }); + + modelBuilder.Entity("netxml2kml.Models.WirelessClient", b => + { + b.Navigation("WirelessConnections"); + }); + + modelBuilder.Entity("netxml2kml.Models.WirelessNetwork", b => + { + b.Navigation("WirelessConnections"); }); #pragma warning restore 612, 618 } diff --git a/netxml2kml/Models/WirelessClient.cs b/netxml2kml/Models/WirelessClient.cs index e0f9d3c..dca0ac7 100644 --- a/netxml2kml/Models/WirelessClient.cs +++ b/netxml2kml/Models/WirelessClient.cs @@ -1,10 +1,15 @@ +using System.ComponentModel.DataAnnotations; + namespace netxml2kml.Models; public class WirelessClient { - public int Id { get; set; } - + [Key] public string Mac { get; set; } = null!; public string Manufacturer { get; set; } = null!; - public int TotalPackets { get; set; } + + public DateTime FirstSeenDate { get; set; } + public DateTime LastUpdateDate { get; set; } + + public List WirelessConnections { get; set; } = null!; } \ No newline at end of file diff --git a/netxml2kml/Models/WirelessConnection.cs b/netxml2kml/Models/WirelessConnection.cs new file mode 100644 index 0000000..061eb75 --- /dev/null +++ b/netxml2kml/Models/WirelessConnection.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations.Schema; + +namespace netxml2kml.Models; + +public class WirelessConnection +{ + [ForeignKey("WirelessNetwork")] + public string WirelessNetworkBssid { get; set; } = null!; + public WirelessNetwork WirelessNetwork { get; set; } = null!; + + [ForeignKey("WirelessClient")] + public string WirelessClientMac { get; set; } = null!; + public WirelessClient WirelessClient { get; set; } = null!; + + public DateTime FirstSeenDate { get; set; } + public DateTime LastUpdateDate { get; set; } +} \ No newline at end of file diff --git a/netxml2kml/Models/WirelessNetwork.cs b/netxml2kml/Models/WirelessNetwork.cs index 387aaa8..8ed6d38 100644 --- a/netxml2kml/Models/WirelessNetwork.cs +++ b/netxml2kml/Models/WirelessNetwork.cs @@ -1,19 +1,16 @@ +using System.ComponentModel.DataAnnotations; + namespace netxml2kml.Models; public class WirelessNetwork { - public int Id { get; set; } - - public string Ssid { get; set; } = null!; - public bool IsCloaked { get; set; } - public string Encryption { get; set; } = null!; - + [Key] public string Bssid { get; set; } = null!; - - public string Manufacturer { get; set; } = null!; - public int Channel { get; set; } - public int FrequencyMhz { get; set; } + public string? Essid { get; set; } + public string Manufacturer { get; set; } = null!; + public string? Encryption { get; set; } + public double FrequencyMhz { get; set; } public int MaxSignalDbm { get; set; } @@ -21,10 +18,10 @@ public class WirelessNetwork public double MaxLongitude { get; set; } public double MaxAltitude { get; set; } - public DateTime FirstSeen { get; set; } - public DateTime LastUpdate { get; set; } - - public int TotalPackets { get; set; } - - public WirelessClient WirelessClient { get; set; } = null!; + public DateTime FirstSeenDate { get; set; } + public DateTime LastUpdateDate { get; set; } + + public List? WirelessConnections { get; set; } + + public bool IsCloaked => Essid == null; } \ No newline at end of file