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