Migrate to .NET8 GenCycleVDP

Migrate generator to .NET8
This commit is contained in:
2024-09-19 13:26:06 +03:00
parent 242d4bda3e
commit 9dbd0f39fe
306 changed files with 511 additions and 20041 deletions

23
GenCycleVDP/Db/DbCycle.cs Normal file
View File

@@ -0,0 +1,23 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace GenCycleVDP.Db
{
[Table("Cycles")]
internal class DbCycle
{
[Column("idCycle"), Required, Key]
public int IdCycle { get; set; }
[Column("numVdp")]
public int NumVdp { get; set; }
[Column("numCycle")]
public int NumCycle { get; set; }
[Column("factStart")]
public DateTime FactStart { get; set; }
[Column("thinkEnd")]
public DateTime ThinkEnd { get; set; }
[Column("factEnd")]
public DateTime FactEnd { get; set; }
}
}

View File

@@ -0,0 +1,23 @@
using System;
using Microsoft.EntityFrameworkCore;
namespace GenCycleVDP.Db
{
internal class DbFurnace : DbContext
{
public DbSet<DbCycle> Cycles { get; set; }
public DbFurnace()
{
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql("server=127.0.0.1;user=diplom;password=diplom;database=VDPCycles;", new MySqlServerVersion(new Version(8, 0)));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
}

152
GenCycleVDP/GenCycle.cs Normal file
View File

@@ -0,0 +1,152 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using GenCycleVDP.Resources;
namespace GenCycleVDP
{
internal class GenCycle(int vdp)
{
private readonly int vdp = vdp;
private CycleStatus currCycle = CycleStatus.EndTechCycle;
private DateTime factStart = DateTime.Now;
private DateTime factEnd = DateTime.Now;
private DateTime thinkEnd = DateTime.Now;
private bool cycle = false;
private Task taskCycle = Task.CompletedTask;
public void Start()
{
cycle = true;
taskCycle = this.Cycle();
}
public void Stop()
{
cycle = false;
taskCycle.Wait();
}
async public Task Cycle()
{
while (cycle)
{
if (GetCurrCycle()) //Can get info form DB.
{
if (DateTime.Now >= factEnd)
{
GetNextCycle();
GetTimeStart();
GetTimeThinkEnd();
GetTimeFactEnd();
while (!SaveToDB())
{
Console.WriteLine("VDP " + vdp.ToString("D2") + ": Can't connect to DB.");
await Task.Delay(5000);
}
}
}
else //It's new Cycle.
{
GetTimeStart();
GetTimeThinkEnd();
GetTimeFactEnd();
while (!SaveToDB())
{
Console.WriteLine("VDP " + vdp.ToString("D2") + ": Can't connect to DB.");
await Task.Delay(5000);
}
}
while (cycle && (DateTime.Now < factEnd))
{
var secAwait = (factEnd - DateTime.Now).TotalSeconds;
if (secAwait >= 5)
{
await Task.Delay(5000);
}
else
{
await Task.Delay(Convert.ToInt32(Math.Ceiling(secAwait)) * 1000);
}
}
}
}
public bool GetCurrCycle()
{
try
{
using var db = new Db.DbFurnace();
var tmp = (from u in db.Cycles
where
u.NumVdp == vdp
orderby u.FactStart descending
select u).FirstOrDefault();
if (tmp == null)
{
currCycle = CycleStatus.EndTechCycle;
return false;
}
currCycle = Enum.IsDefined(typeof(CycleStatus), tmp.NumCycle)
? (CycleStatus)tmp.NumCycle
: CycleStatus.EndTechCycle;
factStart = tmp.FactStart;
factEnd = tmp.FactEnd;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
public void GetNextCycle()
{
currCycle = GenData.GetNextCycle(currCycle);
}
public void GetTimeStart()
{
factStart = factEnd;
}
public void GetTimeThinkEnd()
{
thinkEnd = factStart.AddMinutes(GenData.GetDuration(currCycle));
}
public void GetTimeFactEnd()
{
factEnd = thinkEnd.AddSeconds(GenData.GetDeviation(currCycle));
}
public bool SaveToDB()
{
try
{
using (var db = new Db.DbFurnace())
{
var tmp = new Db.DbCycle()
{
NumVdp = vdp,
NumCycle = (int)currCycle,
FactStart = factStart,
FactEnd = factEnd,
ThinkEnd = thinkEnd
};
db.Cycles.Add(tmp);
db.SaveChanges();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
public string GetStatus()
{
return vdp.ToString("D2") + "-" + currCycle.ToString();
}
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
</ItemGroup>
</Project>

54
GenCycleVDP/Program.cs Normal file
View File

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GenCycleVDP
{
internal class Program
{
private static bool isExiting = false;
private static List<GenCycle> tasks = [];
static void Main(string[] args)
{
for (var i = 1; i <= 48; i++)
{
var a = new GenCycle(i);
a.Start();
tasks.Add(a);
}
Task.Delay(1000 * 5).Wait();
int count = 0;
while (!isExiting)
{
if (count > 600)
{
for (var i = 0; i < tasks.Count; i++)
{
if (i % 10 == 0 && i != 0)
Console.WriteLine();
Console.Write(tasks[i].GetStatus() + "|");
}
Console.WriteLine();
count = 0;
}
else
{
count++;
}
Task.Delay(1000).Wait();
}
foreach(var furance in tasks)
{
furance.Stop();
}
}
private static void OnExit(object sender, ConsoleCancelEventArgs e)
{
isExiting = true;
e.Cancel = true;
}
}
}

View File

@@ -0,0 +1,20 @@
namespace GenCycleVDP.Resources
{
public enum CycleStatus : ushort
{
EndTechCycle = 0,
LoadUnload = 1,
VacForWelding = 2,
Welding = 5,
CoolingWelding = 6,
CheckWelding = 7,
VacForMelting = 8,
DilutionVat = 9,
Melting = 10,
BringShrinkageCavity = 11,
CoolingIngot = 12,
VacForMeltingScarp = 14,
MeltingScarp = 15,
CoolingMeltingScarp = 16
}
}

View File

@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
namespace GenCycleVDP.Resources
{
internal static class GenData
{
private static readonly Random random = new();
private static readonly Dictionary<CycleStatus, (Func<CycleStatus> NextState, int Duration, (int min, int offset))> genDataMap = new()
{
{
CycleStatus.EndTechCycle,
(() => random.Next(100) < 50
? CycleStatus.VacForMeltingScarp
: CycleStatus.VacForWelding,
15,
(7 * 60, 5 * 60))
},
{
CycleStatus.LoadUnload,
(() => CycleStatus.VacForWelding,
15,
(3 * 60, 2 * 60))
},
{
CycleStatus.VacForWelding,
(() => CycleStatus.Welding,
10,
(11 * 60, 1 * 60))
},
{
CycleStatus.Welding,
(() => CycleStatus.CoolingWelding,
13,
(4 * 60, 3 * 60))
},
{
CycleStatus.CoolingWelding,
(() => CycleStatus.CheckWelding,
7,
(4 * 60, 3 * 60))
},
{
CycleStatus.CheckWelding,
(() => random.Next(100) < 20
? CycleStatus.Welding
: CycleStatus.VacForMelting,
5,
(7 * 60, 2 * 60))
},
{
CycleStatus.VacForMelting,
(() => CycleStatus.DilutionVat,
10,
(11 * 60, 1 * 60))
},
{
CycleStatus.DilutionVat,
(() => CycleStatus.Melting,
5,
(2 * 60, 1 * 60))
},
{
CycleStatus.Melting,
(() => CycleStatus.BringShrinkageCavity,
60,
(40 * 60, 30 * 60))
},
{
CycleStatus.BringShrinkageCavity,
(() => CycleStatus.CoolingIngot,
15,
(5 * 60, 3 * 60))
},
{
CycleStatus.CoolingIngot,
(() => CycleStatus.EndTechCycle,
30,
(20 * 60, 10 * 60))
},
{
CycleStatus.VacForMeltingScarp,
(() => CycleStatus.MeltingScarp,
10,
(11 * 60, 1 * 60))
},
{
CycleStatus.MeltingScarp,
(() => CycleStatus.CoolingMeltingScarp,
20,
(4 * 60, 3 * 60))
},
{
CycleStatus.CoolingMeltingScarp,
(() => CycleStatus.LoadUnload,
15,
(4 * 60, 3 * 60))
},
};
public static CycleStatus GetNextCycle(CycleStatus currCycle)
{
CycleStatus nextCycle = CycleStatus.EndTechCycle;
if(genDataMap.TryGetValue(currCycle, out var data))
{
nextCycle = data.NextState();
}
return nextCycle;
}
public static int GetDuration(CycleStatus currCycle)
{
int duration = 15;
if (genDataMap.TryGetValue(currCycle, out var data))
{
duration = data.Duration;
}
return duration;
}
public static int GetDeviation(CycleStatus currCycle)
{
int deviation = random.Next(7 * 60) - (5 * 60);
if (genDataMap.TryGetValue(currCycle, out var data))
{
deviation = random.Next(data.Item3.min) - data.Item3.offset;
}
return deviation;
}
}
}