Innovenergy_trunk/csharp/app/Meiringen/Program.cs

140 lines
3.8 KiB
C#

using System.Reactive.Linq;
using System.Text.Json;
using Flurl.Http;
using InnovEnergy.S3.Drivers;
using InnovEnergy.Lib.Utils;
using static System.StringSplitOptions;
namespace InnovEnergy.Meiringen;
/*
dotnet publish Meiringen.csproj -c Release -r linux-arm -p:PublishSingleFile=true --self-contained false && \
rsync -av bin/Release/net5.0/linux-arm/publish/ debian@10.2.1.87:/home/debian/Monitor && \
ssh debian@10.2.1.87 /home/debian/Monitor/Meiringen /home/debian/trumpf/csvData
*/
class Program
{
//private static UdpClient _incomingSocket = new UdpClient(Settings.IncomingEndPoint);
private static readonly S3Config S3Config = new S3Config
{
Bucket = "meiringen",
Region = "sos-ch-dk-2",
Provider = "exo.io",
ContentType = "text/plain; charset=utf-8",
//Key = "EXO821fc7a9a3ea6d7365b0c6b8",
//Secret = "xiCpn5f9sFeyXL81CG7iTqDGkTekNsrJ-_bJRiyRp9Q"
Key = "EXO36c6c169fbe39800742f4f56",
Secret = "MA5svEdwwNF9XaLO-8PKj_P325Ovog5aeihPAc88R1w"
};
public static void Main(String[] args)
{
var path = args.FirstOrDefault() ?? ".";
Console.WriteLine("Starting");
Observable.Interval(TimeSpan.FromSeconds(2))
.SelectMany(_ => TryUploadData(path))
.Subscribe(Console.WriteLine);
Console.ReadLine();
}
private static async Task<String> TryUploadData(String path)
{
try
{
return await UploadData(path);
}
catch (Exception e)
{
return e.ToString();
}
}
private static async Task<String> UploadData(String path)
{
// var pv = ReadFile(path, "PV.csv").FirstOrDefault();
// var grid = ReadFile(path, "gridPower.csv").FirstOrDefault();
// var inverter = -ReadFile(path, "inverterPower.csv").FirstOrDefault();
// var acLoad = ReadFile(path, "loadPower.csv").FirstOrDefault();
// var socBattery = ReadFile(path, "battery.csv");
//
// var soc = socBattery.ElementAtOrDefault(0);
// var battery = -socBattery.ElementAtOrDefault(1);
var battery = 0;
var soc = 1;
var pv = 2;
var grid = 3;
var inverter = 4;
var acLoad = 5;
var data = new
{
battery,
soc,
pv,
grid,
inverter,
acLoad,
};
var json = JsonSerializer.Serialize(data);
var unixTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds() / 2 * 2;
var s3Path = "/" + unixTime;
var request = S3Config.CreatePutRequest(s3Path);
var response = await request.PutAsync(new StringContent(json));
if (response.StatusCode == 200)
return "PUT " + s3Path;
var error = await response.GetStringAsync();
return "ERROR: " + error;
}
private static IReadOnlyList<Double> ReadFile(String path, String file)
{
var fullPath = Path.Combine(path, file);
return File
.ReadAllText(fullPath)
.Trim()
.Split(',', TrimEntries | RemoveEmptyEntries)
.Select(ParseDouble)
.ToList();
Double ParseDouble(String s) => Double.TryParse(s, out var value) ? value : 0;
}
private static IEnumerable<Double> FakeDataSource(Double min, Double max)
{
var amplitude = (max - min)/2;
return EnumerableUtils
.InfinitelyMany(0)
.Select(_ => Random.Shared.NextDouble() * 2 - 1)
.IntegrateNormalize()
.Select(x => x * .5) // those constants have been drawn out of thin air to make it look good
.IntegrateNormalize()
.Select(x => x * .1)
.IntegrateNormalize()
.Select(x => (x + 1) * amplitude + min);
}
}