timelapsenow/TimelapseApi/TimelapseProject.cs

166 lines
4.8 KiB
C#
Raw Normal View History

2022-05-20 02:06:35 +00:00
using Newtonsoft.Json;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace TimelapseApi
{
public class TimelapseProject
{
public TimelapseProject()
{
SectionName="Default";
Interval = TimeSpan.FromSeconds(1);
}
public void Save()
{
if(ProjectLocation !=null)
{
string name=Path.GetFileName(ProjectLocation.CurrentDirectory);
var fs2=ProjectLocation-1;
fs2.WriteAllText($"{name}.tlnp",JsonConvert.SerializeObject(this));
}
}
public static TimelapseProject? Open(SubFileSystem fs)
{
TimelapseProject? proj;
string name=Path.GetFileName(fs.CurrentDirectory);
SubFileSystem fs2=fs-1;
proj=JsonConvert.DeserializeObject<TimelapseProject>(fs2.ReadAllText($"{name}.tlnp"));
if(proj == null)
{
return proj;
}
proj.ProjectLocation = fs;
return proj;
}
[JsonIgnore]
public SubFileSystem? ProjectLocation {get;set;}
[JsonIgnore]
private string _SectionForFrame="";
[JsonIgnore]
private int _cframe;
[JsonIgnore]
private int CurrentFrame {get {
SetCFName();
return _cframe++;
}}
public int FrameCount {get {
SetCFName();
return _cframe;
}}
[JsonIgnore]
public DateTime LastFrameTaken {get;set;}
public string SectionName {get;set;}
public TimelapseInterval Interval {get;set;}
private void SetCFName()
{
if(ProjectLocation == null)
{
_cframe=-1;
return;
}
if(!SectionName.Equals(_SectionForFrame,StringComparison.Ordinal))
{
_SectionForFrame=SectionName;
if(!ProjectLocation.DirectoryExists($"Sections/{SectionName}"))
{
ProjectLocation.CreateDirectory($"Sections/{SectionName}");
}
_cframe=ProjectLocation.GetNumberOfFiles($"Sections/{SectionName}","*.png");
}
}
public async Task Save(Image<Rgb24> img)
{
if(ProjectLocation == null)
{
return;
}
string p=$"Sections/{SectionName}/{CurrentFrame}.png";
if(!ProjectLocation.FileExists(p))
{
using(var f = ProjectLocation.Open(p,FileMode.Create,FileAccess.Write,FileShare.None))
{
await img.SaveAsPngAsync(f);
}
}
}
}
public class TimelapseInterval
{
public TimelapseInterval()
{
UseEstimated=false;
EstimatedVideoLength=TimeSpan.FromSeconds(1);
EstimatedProjectLength =TimeSpan.FromSeconds(1);
}
public TimelapseInterval(TimeSpan estVidLen,TimeSpan estProjLen)
{
UseEstimated = true;
EstimatedVideoLength=estVidLen;
EstimatedProjectLength=estProjLen;
}
public TimelapseInterval(TimeSpan interval)
{
UseEstimated=false;
EstimatedVideoLength =TimeSpan.FromSeconds(1);
EstimatedProjectLength=interval;
}
public bool UseEstimated {get;set;}
public TimeSpan EstimatedVideoLength {get;set;}
public TimeSpan EstimatedProjectLength {get;set;}
public TimeSpan GetTimeSpan()
{
if(UseEstimated && EstimatedProjectLength.TotalSeconds != 0.0)
{
return TimeSpan.FromSeconds(EstimatedProjectLength.TotalSeconds/EstimatedVideoLength.TotalSeconds);
}
return EstimatedVideoLength;
}
public static explicit operator double(TimelapseInterval interval)
{
return interval.GetTimeSpan().TotalSeconds;
}
public static implicit operator TimeSpan(TimelapseInterval interval)
{
return interval.GetTimeSpan();
}
public static implicit operator (TimeSpan estVidLen,TimeSpan estProjLen)(TimelapseInterval span)
{
return (span.EstimatedProjectLength,span.EstimatedVideoLength);
}
public static implicit operator TimelapseInterval(TimeSpan span)
{
return new TimelapseInterval(span);
}
public static implicit operator TimelapseInterval((TimeSpan span1,TimeSpan span2) span)
{
return new TimelapseInterval(span.span1,span.span2);
}
}
}