Skip to content

Commit

Permalink
Merge pull request #95 from mcrossley/master
Browse files Browse the repository at this point in the history
build 3099
  • Loading branch information
mcrossley authored Dec 30, 2020
2 parents 98ca9e6 + 1dd9636 commit 5ac6d0f
Show file tree
Hide file tree
Showing 17 changed files with 1,062 additions and 633 deletions.
1 change: 0 additions & 1 deletion CumulusMX/AlarmSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public string GetAlarmSettings()
return retObject.ToJson();
}

//public string UpdateNoaaConfig(HttpListenerContext context)
public string UpdateAlarmSettings(IHttpContext context)
{
try
Expand Down
174 changes: 75 additions & 99 deletions CumulusMX/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
using System.Threading.Tasks;
using System.Web;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Modules;
using Unosquare.Labs.EmbedIO.Constants;
using Unosquare.Labs.EmbedIO.Modules;

namespace CumulusMX
{
public static class Api
{
private const string RelativePath = "/api/";
internal static WeatherStation Station;
public static StationSettings stationSettings;
public static ProgramSettings programSettings;
internal static StationSettings stationSettings;
public static InternetSettings internetSettings;
public static ExtraSensorSettings extraSensorSettings;
public static CalibrationSettings calibrationSettings;
Expand Down Expand Up @@ -48,22 +49,21 @@ public static void Setup(WebServer server)
server.Module<WebApiModule>().RegisterController<RecordsController>();
server.Module<WebApiModule>().RegisterController<TodayYestDataController>();
server.Module<WebApiModule>().RegisterController<ExtraDataController>();
server.Module<WebApiModule>().RegisterController<GetSettingsController>();
server.Module<WebApiModule>().RegisterController<SetSettingsController>();
server.Module<WebApiModule>().RegisterController<EditControllerGet>();
server.Module<WebApiModule>().RegisterController<EditControllerPost>();
server.Module<WebApiModule>().RegisterController<SettingsController>();
server.Module<WebApiModule>().RegisterController<EditController>();
server.Module<WebApiModule>().RegisterController<ReportsController>();
server.Module<WebApiModule>().RegisterController<TagController>();
}

public class EditControllerGet : WebApiController
// Get/Post Edit data
public class EditController : WebApiController
{
public EditControllerGet(IHttpContext context) : base(context)
public EditController(IHttpContext context) : base(context)
{
}

[WebApiHandler(HttpVerbs.Get, RelativePath + "edit/*")]
public async Task<bool> EditData()
public async Task<bool> GetEditData()
{
try
{
Expand Down Expand Up @@ -126,28 +126,8 @@ public async Task<bool> EditData()
}
}

private async Task<bool> HandleError(Exception ex, int statusCode)
{
var errorResponse = new
{
Title = "Unexpected Error",
ErrorCode = ex.GetType().Name,
Description = ex.Message,
};

this.Response.StatusCode = statusCode;
return await this.JsonResponseAsync(errorResponse);
}
}

public class EditControllerPost : WebApiController
{
public EditControllerPost(IHttpContext context) : base(context)
{
}

[WebApiHandler(HttpVerbs.Post, RelativePath + "edit/*")]
public async Task<bool> EditData()
public async Task<bool> PostEditData()
{
try
{
Expand Down Expand Up @@ -212,6 +192,8 @@ private async Task<bool> HandleError(Exception ex, int statusCode)
}
}


// Get log and diary Data
public class DataController : WebApiController
{
public DataController(IHttpContext context) : base(context)
Expand Down Expand Up @@ -272,7 +254,7 @@ private async Task<bool> HandleError(Exception ex, int statusCode)
}
}


// Get/Post Tag body data
public class TagController : WebApiController
{
public TagController(IHttpContext context) : base(context)
Expand Down Expand Up @@ -323,8 +305,6 @@ public async Task<bool> GetTags()
}
}



private async Task<bool> HandleError(Exception ex, int statusCode)
{
var errorResponse = new
Expand All @@ -339,6 +319,7 @@ private async Task<bool> HandleError(Exception ex, int statusCode)
}
}

// Get recent/daily graph data
public class GraphDataController : WebApiController
{
public GraphDataController(IHttpContext context) : base(context)
Expand Down Expand Up @@ -391,7 +372,6 @@ public async Task<bool> GetGraphData()
}
}


[WebApiHandler(HttpVerbs.Get, RelativePath + "dailygraphdata/*")]
public async Task<bool> GetDailyGraphData()
{
Expand Down Expand Up @@ -430,7 +410,6 @@ public async Task<bool> GetDailyGraphData()
}
}


private async Task<bool> HandleError(Exception ex, int statusCode)
{
var errorResponse = new
Expand All @@ -445,6 +424,7 @@ private async Task<bool> HandleError(Exception ex, int statusCode)
}
}

// Get Records data
public class RecordsController : WebApiController
{
public RecordsController(IHttpContext context) : base(context)
Expand Down Expand Up @@ -587,6 +567,7 @@ private async Task<bool> HandleError(Exception ex, int statusCode)
}
}

// Get today/yesterday data
public class TodayYestDataController : WebApiController
{
public TodayYestDataController(IHttpContext context) : base(context) {}
Expand Down Expand Up @@ -637,6 +618,7 @@ private async Task<bool> HandleError(Exception ex, int statusCode)
}
}

// Get Extra data
public class ExtraDataController : WebApiController
{
public ExtraDataController(IHttpContext context) : base(context) { }
Expand Down Expand Up @@ -709,71 +691,10 @@ private async Task<bool> HandleError(Exception ex, int statusCode)
}
}

public class SetSettingsController : WebApiController
{
public SetSettingsController(IHttpContext context) : base(context) { }

[WebApiHandler(HttpVerbs.Post, RelativePath + "setsettings/*")]
public async Task<bool> SettingsSet()
{
try
{
// read the last segment of the URL to determine what data the caller wants
var lastSegment = Request.Url.Segments.Last();

switch (lastSegment)
{
case "updatestationconfig.json":
return await this.JsonResponseAsync(stationSettings.UpdateStationConfig(this));
case "updateinternetconfig.json":
return await this.JsonResponseAsync(internetSettings.UpdateInternetConfig(this));
case "updateextrasensorconfig.json":
return await this.JsonResponseAsync(extraSensorSettings.UpdateExtraSensorConfig(this));
case "updatecalibrationconfig.json":
return await this.JsonResponseAsync(calibrationSettings.UpdateCalibrationConfig(this));
case "updatenoaaconfig.json":
return await this.JsonResponseAsync(noaaSettings.UpdateNoaaConfig(this));
case "updateextrawebfiles.html":
return await this.JsonResponseAsync(internetSettings.UpdateExtraWebFiles(this));
case "updatemysqlconfig.json":
return await this.JsonResponseAsync(mySqlSettings.UpdateMysqlConfig(this));
case "createmonthlysql.json":
return await this.JsonResponseAsync(mySqlSettings.CreateMonthlySQL(this));
case "createdayfilesql.json":
return await this.JsonResponseAsync(mySqlSettings.CreateDayfileSQL(this));
case "createrealtimesql.json":
return await this.JsonResponseAsync(mySqlSettings.CreateRealtimeSQL(this));
case "updatealarmconfig.json":
return await this.JsonResponseAsync(alarmSettings.UpdateAlarmSettings(this));
case "ftpnow.json":
return await this.JsonResponseAsync(stationSettings.FtpNow());
}

throw new KeyNotFoundException("Key Not Found: " + lastSegment);
}
catch (Exception ex)
{
return await HandleError(ex, 404);
}
}

private async Task<bool> HandleError(Exception ex, int statusCode)
{
var errorResponse = new
{
Title = "Unexpected Error",
ErrorCode = ex.GetType().Name,
Description = ex.Message,
};

this.Response.StatusCode = statusCode;
return await this.JsonResponseAsync(errorResponse);
}
}

public class GetSettingsController : WebApiController
// Get/Post settings data
public class SettingsController : WebApiController
{
public GetSettingsController(IHttpContext context) : base(context) { }
public SettingsController(IHttpContext context) : base(context) { }

[WebApiHandler(HttpVerbs.Get, RelativePath + "settings/*")]
public async Task<bool> SettingsGet()
Expand Down Expand Up @@ -811,6 +732,13 @@ public async Task<bool> SettingsGet()

switch (lastSegment)
{
case "programdata.json":
return await this.JsonResponseAsync(programSettings.GetProgramAlpacaFormData());
case "programoptions.json":
return await this.JsonResponseAsync(programSettings.GetProgramAlpacaFormOptions());
case "programschema.json":
return await this.JsonResponseAsync(programSettings.GetProgramAlpacaFormSchema());

case "stationdata.json":
return await this.JsonResponseAsync(stationSettings.GetStationAlpacaFormData());
case "stationoptions.json":
Expand Down Expand Up @@ -872,6 +800,53 @@ public async Task<bool> SettingsGet()
}
}

[WebApiHandler(HttpVerbs.Post, RelativePath + "setsettings/*")]
public async Task<bool> SettingsSet()
{
try
{
// read the last segment of the URL to determine what data the caller wants
var lastSegment = Request.Url.Segments.Last();

switch (lastSegment)
{
case "updateprogramconfig.json":
return await this.JsonResponseAsync(programSettings.UpdateProgramConfig(this));

case "updatestationconfig.json":
return await this.JsonResponseAsync(stationSettings.UpdateStationConfig(this));
case "updateinternetconfig.json":
return await this.JsonResponseAsync(internetSettings.UpdateInternetConfig(this));
case "updateextrasensorconfig.json":
return await this.JsonResponseAsync(extraSensorSettings.UpdateExtraSensorConfig(this));
case "updatecalibrationconfig.json":
return await this.JsonResponseAsync(calibrationSettings.UpdateCalibrationConfig(this));
case "updatenoaaconfig.json":
return await this.JsonResponseAsync(noaaSettings.UpdateNoaaConfig(this));
case "updateextrawebfiles.html":
return await this.JsonResponseAsync(internetSettings.UpdateExtraWebFiles(this));
case "updatemysqlconfig.json":
return await this.JsonResponseAsync(mySqlSettings.UpdateMysqlConfig(this));
case "createmonthlysql.json":
return await this.JsonResponseAsync(mySqlSettings.CreateMonthlySQL(this));
case "createdayfilesql.json":
return await this.JsonResponseAsync(mySqlSettings.CreateDayfileSQL(this));
case "createrealtimesql.json":
return await this.JsonResponseAsync(mySqlSettings.CreateRealtimeSQL(this));
case "updatealarmconfig.json":
return await this.JsonResponseAsync(alarmSettings.UpdateAlarmSettings(this));
case "ftpnow.json":
return await this.JsonResponseAsync(stationSettings.FtpNow(this));
}

throw new KeyNotFoundException("Key Not Found: " + lastSegment);
}
catch (Exception ex)
{
return await HandleError(ex, 404);
}
}

private async Task<bool> HandleError(Exception ex, int statusCode)
{
var errorResponse = new
Expand All @@ -886,6 +861,7 @@ private async Task<bool> HandleError(Exception ex, int statusCode)
}
}

// Get reports data
public class ReportsController : WebApiController
{
public ReportsController(IHttpContext context) : base(context)
Expand Down
Loading

0 comments on commit 5ac6d0f

Please sign in to comment.