timelapse/Timelapse.Api/Extension.cs

236 lines
7.9 KiB
C#
Raw Normal View History

2021-12-28 07:25:39 +00:00
using System;
using System.Collections.Generic;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Net;
namespace Timelapse.Api
{
2021-12-30 04:07:31 +00:00
public class ProjectUnloadedException : Exception
{
public ProjectUnloadedException() : base("Project not Loaded")
{
}
}
2021-12-28 07:25:39 +00:00
public abstract class TimelapseExtension
{
internal string __id;
internal bool _recording;
internal bool _onex;
2021-12-30 04:07:31 +00:00
/// <summary>
/// Recording bit
/// </summary>
/// <returns>Whether project is recording</returns>
2021-12-28 07:25:39 +00:00
protected bool Recording {get {return _recording;}set{if(value){StartRecording();} else{StopRecording();}}}
2021-12-30 04:07:31 +00:00
/// <summary>
/// OneX bit
/// </summary>
/// <returns>Whether project is recording</returns>
protected bool OneX {get {return _onex;}set{if(value) {EnableOneX();} else {DisableOneX();} }}
/// <summary>
/// Called when Extension is Loaded
/// </summary>
2021-12-28 07:25:39 +00:00
public abstract void OnInit();
2021-12-30 04:07:31 +00:00
/// <summary>
/// Name of Extension (retreved from Attribute)
/// </summary>
/// <value>The Name</value>
2021-12-28 07:25:39 +00:00
public string Name {get;internal set;}
2021-12-30 04:07:31 +00:00
/// <summary>
/// When a frame is taken by webcam and is about to be saved (async)
/// </summary>
/// <param name="frame">a mutable SixLabors.ImageSharp object</param>
/// <returns>Task but no data</returns>
2021-12-28 07:25:39 +00:00
public virtual async Task OnNewFrame(Image<Rgb24> frame)
{
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// You could manipulate the project and detect project load
/// </summary>
/// <param name="project">TimelapseProject object</param>
2021-12-28 07:25:39 +00:00
public virtual void OnLoadProject(TimelapseProject project)
{
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Register Razor Component
/// </summary>
/// <param name="href">the url for the razor component</param>
/// <typeparam name="T">The type</typeparam>
public void RegisterComponentAsPage<T>(string href)
{
Type typ= typeof(T);
Extensions._components.Add(GET_URL(href),typ);
}
///<summary>
///URL for in razor pages
2021-12-28 07:25:39 +00:00
///</summary>
public string GET_URL(string href)
{
2021-12-30 04:07:31 +00:00
return $"/ExtensionPage/{WebUtility.UrlEncode(__id)}/{WebUtility.UrlEncode(href)}";
}
/// <summary>
/// Write Object (as json)
/// </summary>
/// <param name="key">Path to file (must use forward slash '/' and can start with '/'</param>
/// <param name="project">whether you want to get project specific path</param>
/// <param name="contents">data to be writen to file</param>
public void WriteObject(string key,object contents,bool project=false)
{
WriteAllText(key,Newtonsoft.Json.JsonConvert.SerializeObject(contents),project);
2021-12-28 07:25:39 +00:00
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Read Object (as json)
/// </summary>
/// <param name="key">Path to file (must use forward slash '/' and can start with '/'</param>
/// <param name="project">whether you want to get project specific path</param>
/// <typeparam name="T">object type</typeparam>
/// <returns>object filled with data</returns>
public T ReadObject<T>(string key,bool project)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(ReadAllText(key,project));
}
/// <summary>
/// Write all text
/// </summary>
/// <param name="key">Path to file (must use forward slash '/' and can start with '/'</param>
/// <param name="project">whether you want to get project specific path</param>
/// <param name="contents">data to be writen to file</param>
public void WriteAllText(string key,string contents,bool project=false)
{
string file=GetPath(key,project);
File.WriteAllText(file,contents);
}
/// <summary>
/// Read all text
/// </summary>
/// <param name="key">Path to file (must use forward slash '/' and can start with '/'</param>
/// <param name="project">whether you want to get project specific path</param>
/// <returns>file contents</returns>
public string ReadAllText(string key,bool project=false)
{
string file=GetPath(key,project);
return File.ReadAllText(file);
}
/// <summary>
/// Get Path to data file for extension
/// </summary>
/// <param name="key">Path to file (must use forward slash '/' and can start with '/'</param>
/// <param name="project">whether you want to get project specific path</param>
/// <returns>Path to file</returns>
public string GetPath(string key,bool project=false)
{
string v = key.TrimStart('/');
if(Path.DirectorySeparatorChar == '\\')
{
v = v.Replace('/','\\');
}
if(project)
{
return Path.Combine(GetExtensionProjectData(),v);
}else{
return Path.Combine(GetExtensionData(),v);
}
}
///<summary>
/// Get Project Specific Data for extension (Directory Path)
///</summary>
/// <returns>Path</returns>
2021-12-28 07:25:39 +00:00
protected string GetExtensionProjectData()
{
if(!Extensions.Callbacks.ProjectLoaded())
{
2021-12-30 04:07:31 +00:00
throw new ProjectUnloadedException();
2021-12-28 07:25:39 +00:00
}
string p = Path.Combine(Extensions.Callbacks.GetProjectDirectoryLocation(),"ExtensionData",__id);
Directory.CreateDirectory(p);
return p;
}
2021-12-30 04:07:31 +00:00
///<summary>
/// Get Data for extension (Directory Path)
///</summary>
/// <returns>Path</returns>
2021-12-28 07:25:39 +00:00
protected string GetExtensionData()
{
string p = Path.Combine(Extensions.UserData,"ExtensionData",__id);
Directory.CreateDirectory(p);
return p;
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Project Loaded
/// </summary>
/// <returns>returns true if project is loaded otherwise false</returns>
2021-12-28 07:25:39 +00:00
public bool IsProjectLoaded()
{
return Extensions.Callbacks.ProjectLoaded();
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Triggered when timelapse starts recording
/// </summary>
2021-12-28 07:25:39 +00:00
public virtual void OnStartRecording()
{
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Triggered when timelapse stops recording
/// </summary>
2021-12-28 07:25:39 +00:00
public virtual void OnStopRecording()
{
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Call this to start recording
/// </summary>
2021-12-28 07:25:39 +00:00
protected void StartRecording()
{
Extensions.Callbacks.StartRecording();
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Call this to stop recording
/// </summary>
2021-12-28 07:25:39 +00:00
protected void StopRecording()
{
Extensions.Callbacks.StopRecording();
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Call this to Enable OneX (interval of 1)
/// </summary>
2021-12-28 07:25:39 +00:00
protected void EnableOneX()
{
Extensions.Callbacks.EnableOneX();
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Call this to Disable OneX (no longer the interval of 1)
/// </summary>
2021-12-28 07:25:39 +00:00
protected void DisableOneX()
{
Extensions.Callbacks.DisableOneX();
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Triggered when timelapse starts onex mode
/// </summary>
2021-12-28 07:25:39 +00:00
public virtual void OnEnableOneX()
{
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Triggered when timelapse exits onex mode
/// </summary>
2021-12-28 07:25:39 +00:00
public virtual void OnDisableOneX()
{
}
}
}