Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 48 additions & 49 deletions ViewModels/SupervisorViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,109 +1,91 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Core;
using AutoMapper;
using Manufacturing.Framework.Dto;
using Manufacturing.WinApp.Common;
using Manufacturing.WinApp.Common.Clients;
using Manufacturing.WinApp.Views.Supervisor;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Manufacturing.WinApp.ViewModels
{
public class DataRecord : DatasourceRecord
{
public string Id { get; set; }
public bool IsSelected { get; set; }

public DataRecord(DatasourceRecord datasourceRecord)
{
Mapper.CreateMap<DatasourceRecord, DataRecord>();
Mapper.Map(datasourceRecord, this);
}

public string Id { get; set; }

public bool IsSelected { get; set; }
}

public class Datasource : DatasourceConfiguration
{
public bool IsSelected { get; set; }

public Datasource(DatasourceConfiguration datasourceConfiguration)
{
Mapper.CreateMap<DatasourceConfiguration, Datasource>();
Mapper.Map(datasourceConfiguration, this);
}

public bool IsSelected { get; set; }
}

[MenuScreen(typeof(SupervisorPage), "Supervisor Demo")]
[RequiredRole("Supervisor")]
[MenuScreen(typeof(SupervisorPage), "Supervisor Demo"), RequiredRole("Supervisor")]
public class SupervisorViewModel : BaseViewModel
{
private readonly ApiClient _apiClient;

private readonly DatasourceRecordReceiver _subscriptionClient;

private RelayCommand _dataRecordSelectedCommand;

private CoreDispatcher _dispatcher;

public ObservableCollection<DatasourceConfiguration> Datasources { get; set; }
public ObservableCollection<DataRecord> DataRecords { get; set; }
public ObservableCollection<DataRecord> SelectedDataRecords { get; set; }
private string _errorMessage;

public SupervisorViewModel()
{
//TODO: Move this into a ctor variable to be injected by IoC
var apiUrl = string.Format("{0}/api/", ConfigSettings.ApiServiceUrl);
_apiClient = new ApiClient(apiUrl, App.BearerToken);

try
{
// _subscriptionClient = new DatasourceRecordReceiver(App.BearerToken, "http://localhost:3184/signalr", "DatasourceRecord", "notify", "1");
}
catch (Exception ex)
{

throw;
}


Datasources = new ObservableCollection<DatasourceConfiguration>();
DataRecords = new ObservableCollection<DataRecord>();
}

public void Load()
{
//Subscriptions = new ObservableCollection<string>();
//Subscriptions.Add("Item1");
//Subscriptions.Add("Item2");
//Subscriptions.Add("Item3");

_dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
_subscriptionClient = new DatasourceRecordReceiver(App.BearerToken, "http://localhost:3184/signalr",
"DatasourceRecordHub", "notify", "1");
_subscriptionClient.DataReceived += (sender, args) => DataRecords.Add(new DataRecord(args.Message));
}

SelectedDataRecords = new ObservableCollection<DataRecord>();
LoadDataRecords();
public ObservableCollection<DatasourceConfiguration> Datasources { get; set; }

// Move this into subscription code, you'll need to track these manually for now
//var subscriber = new DatasourceRecordReceiver("GIVE ME AUTH TOKEN!", "ENDPOINT URL; these come from Cloud.json, not sure how that's being injected", "HUB ID", "notify",
// "id you want to look for; still need to set up filtering");
// subscribe to event and have it push into data records
//subscriber.DataReceived += (sender, args) => DataRecords.Add(args.Message);
}
public ObservableCollection<DataRecord> DataRecords { get; set; }

public ObservableCollection<DataRecord> SelectedDataRecords { get; set; }

private string _errorMessage;
public string ErrorMessage
{
get { return _errorMessage; }
set { this.SetProperty(ref _errorMessage, value); }
get
{
return _errorMessage;
}
set
{
SetProperty(ref _errorMessage, value);
}
}

RelayCommand _dataRecordSelectedCommand;
public RelayCommand DataRecordSelectedCommand
{
get
{
if (_dataRecordSelectedCommand == null)
{
_dataRecordSelectedCommand = new RelayCommand(
x => this.PerformDataRecordSelectedCommand(x));
_dataRecordSelectedCommand = new RelayCommand(x => PerformDataRecordSelectedCommand(x));
}
return _dataRecordSelectedCommand;
}
Expand All @@ -113,15 +95,32 @@ public RelayCommand DataRecordSelectedCommand
}
}

public void Load()
{
//Subscriptions = new ObservableCollection<string>();
//Subscriptions.Add("Item1");
//Subscriptions.Add("Item2");
//Subscriptions.Add("Item3");

_dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;

SelectedDataRecords = new ObservableCollection<DataRecord>();
LoadDataRecords();
}

public virtual void PerformDataRecordSelectedCommand(object state)
{
var dr = (DataRecord)state;
if (dr != null)
{
if (dr.IsSelected)
{
SelectedDataRecords.Add(dr);
}
else if (SelectedDataRecords.Any(x => x.Id == dr.Id))
{
SelectedDataRecords.Remove(SelectedDataRecords.SingleOrDefault(x => x.Id == dr.Id));
}
}
}

Expand All @@ -138,7 +137,7 @@ private async void LoadDatasources()
});
}

async void LoadDataRecords()
private async void LoadDataRecords()
{
var records = await _apiClient.GetData<IEnumerable<DatasourceRecord>>("data?datasourceId=1");

Expand Down