PTMagic/Monitor/Pages/ManageSMS.cshtml.cs

121 lines
4.0 KiB
C#
Raw Normal View History

2018-05-22 10:11:50 +02:00
using System;
2020-09-09 08:39:26 +02:00
using System.Collections;
2018-05-22 10:11:50 +02:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
2024-01-27 18:39:47 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
2018-05-22 10:11:50 +02:00
using Core.Main;
using Core.Main.DataObjects.PTMagicData;
using Newtonsoft.Json;
namespace Monitor.Pages
{
public class ManageSMSModel : _Internal.BasePageModelSecure
{
2018-05-22 10:11:50 +02:00
public List<SingleMarketSettingSummary> SingleMarketSettingSummaries = new List<SingleMarketSettingSummary>();
2024-01-27 18:39:47 +01:00
private readonly IWebHostEnvironment _hostingEnvironment;
2018-05-22 10:11:50 +02:00
2024-01-27 18:39:47 +01:00
public ManageSMSModel(IWebHostEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public void OnGet()
{
2018-05-22 10:11:50 +02:00
base.Init();
BindData();
}
2020-09-09 08:39:26 +02:00
public List<string> smsList = new List<string>();
public void CreateSmsList ()
{
foreach (Core.Main.DataObjects.PTMagicData.SingleMarketSettingSummary smsSummary in SingleMarketSettingSummaries)
{
if (!smsList.Contains(smsSummary.SingleMarketSetting.SettingName))
{
smsList.Add(smsSummary.SingleMarketSetting.SettingName);
}
}
}
2024-01-27 18:39:47 +01:00
public IActionResult OnPostDeleteFile(string filename)
{
string webRootParent = Directory.GetParent(_hostingEnvironment.WebRootPath).FullName;
string ptMagicRoot = Directory.GetParent(webRootParent).FullName;
string analyzerStatePath = Path.Combine(ptMagicRoot, "_data", "AnalyzerState");
// Read the AnalyzerState file
if (System.IO.File.Exists(analyzerStatePath))
{
string state = System.IO.File.ReadAllText(analyzerStatePath);
if (state != "0")
{
return new JsonResult(new { success = false, message = "Tha Analyzer is in the middle of a run. Try again in a moment." });
}
}
// If state is "0", proceed to delete the file
try
{
string path = Path.Combine(ptMagicRoot, "_data", filename);
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
return new JsonResult(new { success = true, message = "All SMS settings reset!" });
}
catch (Exception ex)
{
return new JsonResult(new { success = false, message = ex.Message });
}
}
2020-09-09 08:39:26 +02:00
private void BindData()
{
if (System.IO.File.Exists(PTMagicBasePath + Constants.PTMagicPathData + Path.DirectorySeparatorChar + "SingleMarketSettingSummary.json"))
{
try
{
2018-05-22 10:11:50 +02:00
SingleMarketSettingSummaries = JsonConvert.DeserializeObject<List<SingleMarketSettingSummary>>(System.IO.File.ReadAllText(PTMagicBasePath + Constants.PTMagicPathData + Path.DirectorySeparatorChar + "SingleMarketSettingSummary.json"));
}
catch { }
2018-05-22 10:11:50 +02:00
}
string notification = GetStringParameter("n", "");
if (notification.Equals("SettingReset"))
{
2018-05-22 10:11:50 +02:00
NotifyHeadline = "Setting Reset!";
NotifyMessage = "The setting will get reset on the next interval!";
NotifyType = "success";
}
}
public double GetTrendChange(string marketTrend, MarketPairSummary mps, TriggerSnapshot ts, string marketTrendRelation)
{
2018-05-22 10:11:50 +02:00
double result = 0;
if (mps.MarketTrendChanges.ContainsKey(marketTrend))
{
2018-05-22 10:11:50 +02:00
result = mps.MarketTrendChanges[marketTrend];
double averageMarketTrendChange = Summary.MarketTrendChanges[marketTrend].OrderByDescending(mtc => mtc.TrendDateTime).First().TrendChange;
if (marketTrendRelation.Equals(Constants.MarketTrendRelationAbsolute, StringComparison.InvariantCulture))
{
2018-05-22 10:11:50 +02:00
result = result - averageMarketTrendChange;
}
else if (marketTrendRelation.Equals(Constants.MarketTrendRelationRelativeTrigger, StringComparison.InvariantCulture))
{
2018-05-22 10:11:50 +02:00
double currentPrice = mps.LatestPrice;
double triggerPrice = ts.LastPrice;
double triggerTrend = (currentPrice - triggerPrice) / triggerPrice * 100;
result = triggerTrend;
}
}
return result;
}
}
}