468 lines
17 KiB
C#
468 lines
17 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
using System.Web;
|
|
using System.Windows.Forms;
|
|
|
|
namespace SatlocCloud.API.Sample
|
|
{
|
|
public partial class frmMain : Form
|
|
{
|
|
#region Variables
|
|
|
|
private HttpClient _httpClient;
|
|
private string _baseUrl;
|
|
private string _isAliveAPI;
|
|
private string _authenticateUserAPI;
|
|
private string _getAircraftListAPI;
|
|
private string _uploadJobAPI;
|
|
private string _getAircraftLogsAPI;
|
|
private string _getAircraftLogDataAPI;
|
|
private User _user;
|
|
private List<Aircraft> _aircraftList = new List<Aircraft>();
|
|
private Guid _selectAircraftId;
|
|
private string _selectJobFile;
|
|
private List<AircraftLog> _aircraftLogList;
|
|
private AircraftLog _selectedLogData;
|
|
|
|
private const string PMD_EXT = ".pmd";
|
|
private const string PMH_EXT = ".pmh";
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
public frmMain()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Events
|
|
|
|
private void frmMain_Load(object sender, EventArgs e)
|
|
{
|
|
// get values from App Config
|
|
_baseUrl = ConfigurationManager.AppSettings["BaseUrl"].ToString();
|
|
_isAliveAPI = ConfigurationManager.AppSettings["IsAliveAPI"].ToString();
|
|
_authenticateUserAPI = ConfigurationManager.AppSettings["AuthenticateUserAPI"].ToString();
|
|
_getAircraftListAPI = ConfigurationManager.AppSettings["GetAircraftListAPI"].ToString();
|
|
_uploadJobAPI = ConfigurationManager.AppSettings["UploadJobAPI"].ToString();
|
|
_getAircraftLogsAPI = ConfigurationManager.AppSettings["GetAircraftLogsAPI"].ToString();
|
|
_getAircraftLogDataAPI = ConfigurationManager.AppSettings["GetAircraftLogDataAPI"].ToString();
|
|
var userLogin = ConfigurationManager.AppSettings["UserLogin"].ToString();
|
|
var password = ConfigurationManager.AppSettings["Password"].ToString();
|
|
|
|
// instanciate HTTP Client
|
|
_httpClient = new HttpClient { BaseAddress = new Uri(_baseUrl) };
|
|
|
|
// check API service is available
|
|
if (IsAlive())
|
|
{
|
|
// check if Authenticate API User is successful
|
|
if(AuthenticateUser(userLogin, password))
|
|
{
|
|
// get list of aircraft associated with the Company
|
|
GetAircraftList();
|
|
|
|
|
|
// enable controls if data exist
|
|
if(_aircraftList.Count > 0)
|
|
{
|
|
// enable group box
|
|
grpAPIData.Enabled = true;
|
|
|
|
// load Aircraft List
|
|
DisplayAircraftList();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void lstAircraft_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
// select selected aircraft
|
|
_selectAircraftId = ((Aircraft)lstAircraft.SelectedItem).Id;
|
|
|
|
// load selected aircraft log list
|
|
GetAircraftLogs();
|
|
|
|
// display selected aircraft log list
|
|
DisplayAircraftLogs();
|
|
|
|
// enable download all logs
|
|
btnDownloadSelectedLog.Enabled = _selectedLogData != null && !string.IsNullOrEmpty(txtDownloadPath.Text);
|
|
|
|
// enable download all logs
|
|
btnDownloadAllLogs.Enabled = _aircraftLogList.Count > 0 && !string.IsNullOrEmpty(txtDownloadPath.Text);
|
|
}
|
|
|
|
private void btnSelectJob_Click(object sender, EventArgs e)
|
|
{
|
|
SelectJob();
|
|
}
|
|
|
|
private void btnUploadSelectedJob_Click(object sender, EventArgs e)
|
|
{
|
|
UploadSelectedJobData(_selectJobFile, _selectAircraftId, txtNotes.Text);
|
|
}
|
|
|
|
private void btnUploadMulitpleJobs_Click(object sender, EventArgs e)
|
|
{
|
|
UploadMultipeJobData();
|
|
}
|
|
|
|
private void lstLogList_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
// set seleted log data
|
|
_selectedLogData = (AircraftLog)lstLogList.SelectedItem;
|
|
|
|
// enable download log
|
|
btnDownloadSelectedLog.Enabled = !string.IsNullOrEmpty(txtDownloadPath.Text);
|
|
}
|
|
|
|
private void btnSelectPath_Click(object sender, EventArgs e)
|
|
{
|
|
using (var fldrDlg = new FolderBrowserDialog())
|
|
{
|
|
// quickly set download path
|
|
if (fldrDlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
//fldrDlg.SelectedPath -- your result
|
|
txtDownloadPath.Text = fldrDlg.SelectedPath;
|
|
|
|
// enable download selected log
|
|
btnDownloadSelectedLog.Enabled = _selectedLogData != null;
|
|
|
|
// enable download all logs
|
|
btnDownloadAllLogs.Enabled = _aircraftLogList.Count > 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnDownloadSelectedLog_Click(object sender, EventArgs e)
|
|
{
|
|
DownloadSelectedLogData();
|
|
}
|
|
|
|
private void btnDownloadAllLogs_Click(object sender, EventArgs e)
|
|
{
|
|
DownloadAllLogData();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Methods
|
|
|
|
private bool IsAlive()
|
|
{
|
|
var returnData = false;
|
|
try
|
|
{
|
|
// create uri with parameters
|
|
var builder = new UriBuilder(_baseUrl + _isAliveAPI);
|
|
|
|
// initialize http client
|
|
var client = new HttpClient();
|
|
|
|
// execute get
|
|
var response = client.GetAsync(builder.Uri).Result;
|
|
returnData = GetBooleanResponse(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Show exception
|
|
MessageBox.Show(ex.ToString());
|
|
}
|
|
|
|
return returnData;
|
|
}
|
|
|
|
private bool AuthenticateUser(string userLogin, string password)
|
|
{
|
|
var returnData = false;
|
|
try
|
|
{
|
|
// instanciate client
|
|
var client = new HttpClient { BaseAddress = new Uri(_baseUrl) };
|
|
|
|
// create uri with parameters
|
|
var builder = new UriBuilder(_baseUrl + _authenticateUserAPI);
|
|
builder.Query = "userLogin=" + HttpUtility.UrlEncode(userLogin) + "&password=" + HttpUtility.UrlEncode(password);
|
|
|
|
var response = client.GetAsync(builder.Uri).Result;
|
|
if (response.IsSuccessStatusCode) //User should be returned
|
|
{
|
|
// deserialize data local object APIUser
|
|
_user = response.Content.ReadAsAsync<User>().Result;
|
|
returnData = true;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(response.ReasonPhrase.ToString());
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Show exception
|
|
MessageBox.Show(ex.ToString());
|
|
}
|
|
return returnData;
|
|
}
|
|
|
|
public void GetAircraftList()
|
|
{
|
|
try
|
|
{
|
|
// create uri with parameters
|
|
var builder = new UriBuilder(_baseUrl + _getAircraftListAPI);
|
|
builder.Query = "userId=" + _user.UserId + "&companyId=" + _user.CompanyId;
|
|
|
|
// execute get
|
|
var response = _httpClient.GetAsync(builder.Uri).Result;
|
|
_aircraftList = response.Content.ReadAsAsync<List<Aircraft>>().Result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Show exception
|
|
MessageBox.Show(ex.ToString());
|
|
}
|
|
}
|
|
|
|
public void GetAircraftLogs()
|
|
{
|
|
try
|
|
{
|
|
// create uri with parameters
|
|
var builder = new UriBuilder(_baseUrl + _getAircraftLogsAPI);
|
|
builder.Query = "userId=" + _user.UserId + "&aircraftId=" + _selectAircraftId;
|
|
|
|
// execute get
|
|
var response = _httpClient.GetAsync(builder.Uri).Result;
|
|
_aircraftLogList = response.Content.ReadAsAsync<List<AircraftLog>>().Result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Show exception
|
|
MessageBox.Show(ex.ToString());
|
|
}
|
|
}
|
|
|
|
private void UploadSelectedJobData(string filePath, Guid aircraftId, string notes = "")
|
|
{
|
|
HttpResponseMessage uploadStatus = null;
|
|
|
|
try
|
|
{
|
|
var fileName = Path.GetFileName(filePath);
|
|
var ext = Path.GetExtension(filePath).ToLower();
|
|
|
|
// create job package and job data object
|
|
var jobPackage = new JobPackage { CompanyId = _user.CompanyId, UserId = _user.UserId };
|
|
var jobDataModel = new JobDataModel();
|
|
|
|
// check ext type
|
|
if (ext.Equals(PMD_EXT))
|
|
{
|
|
// load original data
|
|
var byteArray = File.ReadAllBytes(filePath);
|
|
jobDataModel = new JobDataModel { AircraftId = aircraftId, JobData = byteArray, JobName = fileName, Notes = notes };
|
|
jobPackage.JobDataList.Add(jobDataModel);
|
|
|
|
// check if accompanying data exist
|
|
if (File.Exists(filePath.ToLower().Replace(PMD_EXT, PMH_EXT)))
|
|
{
|
|
// load accompanying data
|
|
var stringData = File.ReadAllText(filePath.ToLower().Replace(PMD_EXT, PMH_EXT));
|
|
jobDataModel = new JobDataModel { AircraftId = aircraftId, JobData = stringData, JobName = fileName.ToLower().Replace(PMD_EXT, PMH_EXT) };
|
|
jobPackage.JobDataList.Add(jobDataModel);
|
|
}
|
|
}
|
|
else if (ext.Equals(PMH_EXT))
|
|
{
|
|
// load orginal data
|
|
var stringData = File.ReadAllText(filePath);
|
|
jobDataModel = new JobDataModel { AircraftId = aircraftId, JobData = stringData, JobName = fileName, Notes = notes };
|
|
jobPackage.JobDataList.Add(jobDataModel);
|
|
|
|
// check if accompanying data exist
|
|
if (File.Exists(filePath.ToLower().Replace(PMH_EXT, PMD_EXT)))
|
|
{
|
|
// load data
|
|
var byteArray = File.ReadAllBytes(filePath.ToLower().Replace(PMH_EXT, PMD_EXT));
|
|
jobDataModel = new JobDataModel { AircraftId = aircraftId, JobData = byteArray, JobName = fileName.ToLower().Replace(PMH_EXT, PMD_EXT) };
|
|
jobPackage.JobDataList.Add(jobDataModel);
|
|
}
|
|
}
|
|
else // handle regular job file
|
|
{
|
|
var stringData = File.ReadAllText(filePath);
|
|
jobDataModel = new JobDataModel { AircraftId = aircraftId, JobData = stringData, JobName = fileName, Notes = notes };
|
|
jobPackage.JobDataList.Add(jobDataModel);
|
|
}
|
|
|
|
// POST Json
|
|
var jsonData = JsonConvert.SerializeObject(jobPackage);
|
|
uploadStatus = _httpClient.PostAsJsonAsync(_baseUrl + _uploadJobAPI, jsonData).Result;
|
|
|
|
// display notification
|
|
if (uploadStatus.StatusCode == System.Net.HttpStatusCode.OK) UploadJobSuccessful();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Show exception
|
|
MessageBox.Show(ex.ToString());
|
|
}
|
|
}
|
|
|
|
private void UploadMultipeJobData()
|
|
{
|
|
HttpResponseMessage uploadStatus = null;
|
|
|
|
try
|
|
{
|
|
// load multiple jobs
|
|
var jobFile = File.ReadAllText(@"C:\__1 SATLOC\Projects\SampleFiles\SampleJob1.job");
|
|
var textFile = File.ReadAllText(@"C:\__1 SATLOC\Projects\SampleFiles\SampleJob3.PMH");
|
|
var byteArray = File.ReadAllBytes(@"C:\__1 SATLOC\Projects\SampleFiles\SampleJob3.PMD");
|
|
|
|
// instanciate job Package
|
|
var jobPackage = new JobPackage { CompanyId = _user.CompanyId, UserId = _user.UserId };
|
|
|
|
// instanciate job data model
|
|
var jobDataModel = new JobDataModel { AircraftId = _selectAircraftId, JobData = byteArray, JobName = "SampleJob3.PMD" };
|
|
// add to job package
|
|
jobPackage.JobDataList.Add(jobDataModel);
|
|
|
|
// instanciate another job data model
|
|
jobDataModel = new JobDataModel { AircraftId = _selectAircraftId, JobData = textFile, JobName = "SampleJob3.PMH" };
|
|
jobPackage.JobDataList.Add(jobDataModel);
|
|
|
|
jobDataModel = new JobDataModel { AircraftId = _selectAircraftId, JobData = jobFile, JobName = "SampleJob1.job" };
|
|
jobPackage.JobDataList.Add(jobDataModel);
|
|
|
|
// POST Json
|
|
var jsonData = JsonConvert.SerializeObject(jobPackage);
|
|
uploadStatus = _httpClient.PostAsJsonAsync(_baseUrl + _uploadJobAPI, jsonData).Result;
|
|
|
|
// display notification
|
|
if (uploadStatus.StatusCode == System.Net.HttpStatusCode.OK) UploadJobSuccessful();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Show exception
|
|
MessageBox.Show(ex.ToString());
|
|
}
|
|
}
|
|
|
|
private void UploadJobSuccessful()
|
|
{
|
|
MessageBox.Show("Upload Job(s) completed successfully!");
|
|
}
|
|
|
|
private void DownloadLogSuccessful()
|
|
{
|
|
MessageBox.Show("Download Log(s) completed successfully!");
|
|
}
|
|
|
|
public void DownloadSelectedLogData()
|
|
{
|
|
try
|
|
{
|
|
// create uri with parameters
|
|
var builder = new UriBuilder(_baseUrl + _getAircraftLogDataAPI);
|
|
builder.Query = "userId=" + _user.UserId + "&logId=" + _selectedLogData.Id;
|
|
|
|
// execute get
|
|
var response = _httpClient.GetAsync(builder.Uri).Result;
|
|
var aircraftLogData = response.Content.ReadAsAsync<AircraftLogData>().Result;
|
|
|
|
// write file to
|
|
File.WriteAllBytes(Path.Combine(txtDownloadPath.Text, aircraftLogData.LogFileName), aircraftLogData.LogFile);
|
|
|
|
DownloadLogSuccessful();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Show exception
|
|
MessageBox.Show(ex.ToString());
|
|
}
|
|
}
|
|
|
|
public void DownloadAllLogData()
|
|
{
|
|
try
|
|
{
|
|
// iterate thru log list
|
|
foreach (var logData in _aircraftLogList)
|
|
{
|
|
// create uri with parameters
|
|
var builder = new UriBuilder(_baseUrl + _getAircraftLogDataAPI);
|
|
builder.Query = "userId=" + _user.UserId + "&logId=" + logData.Id;
|
|
|
|
// execute get
|
|
var response = _httpClient.GetAsync(builder.Uri).Result;
|
|
var aircraftLogData = response.Content.ReadAsAsync<AircraftLogData>().Result;
|
|
|
|
// write file to
|
|
File.WriteAllBytes(Path.Combine(txtDownloadPath.Text, aircraftLogData.LogFileName), aircraftLogData.LogFile);
|
|
}
|
|
|
|
DownloadLogSuccessful();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Show exception
|
|
MessageBox.Show(ex.ToString());
|
|
}
|
|
}
|
|
|
|
|
|
private void DisplayAircraftList()
|
|
{
|
|
// populate Aircraft Combo
|
|
lstAircraft.ValueMember = "Id";
|
|
lstAircraft.DisplayMember = "TailNumber";
|
|
lstAircraft.DataSource = _aircraftList;
|
|
}
|
|
|
|
private void DisplayAircraftLogs()
|
|
{
|
|
// reset
|
|
lstLogList.DataSource = null;
|
|
|
|
// populate Aircraft Combo
|
|
lstLogList.ValueMember = "Id";
|
|
lstLogList.DisplayMember = "LogFileName";
|
|
lstLogList.DataSource = _aircraftLogList;
|
|
}
|
|
|
|
private static bool GetBooleanResponse(HttpResponseMessage response)
|
|
{
|
|
// return boolean result
|
|
return response.IsSuccessStatusCode ? JsonConvert.DeserializeObject<bool>(response.Content.ReadAsStringAsync().Result) : false;
|
|
}
|
|
|
|
private void SelectJob()
|
|
{
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
//Get the path of specified file
|
|
_selectJobFile = openFileDialog.FileName;
|
|
|
|
// set filename on textbox
|
|
txtJobName.Text = openFileDialog.SafeFileName;
|
|
|
|
// enable job upload
|
|
btnUploadSelectedJob.Enabled = _selectAircraftId != Guid.Empty;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|