Added: BFFH Simulation

This commit is contained in:
TheJoKlLa 2020-11-17 23:40:33 +01:00
parent f0bb467e77
commit a21fa2a913
8 changed files with 522 additions and 38 deletions

View File

@ -3,6 +3,7 @@ using Prism.Ioc;
using Borepin.PageModel;
using Borepin.Page;
using Xamarin.Forms;
using Borepin.Service;
namespace Borepin
{
@ -32,6 +33,9 @@ namespace Borepin
containerRegistry.RegisterForNavigation<MachinesPage, MachinesPageModel>();
containerRegistry.RegisterForNavigation<SettingsPage>();
containerRegistry.RegisterForNavigation<MachinePage, MachinePageModel>();
// Use Admin as Default
containerRegistry.RegisterInstance(typeof(BFFHInterface), BFFHService.Auth(BFFHService.AdminID));
}
}
}

View File

@ -73,6 +73,5 @@
<ItemGroup>
<Folder Include="Base\" />
<Folder Include="Behaviour\" />
<Folder Include="Service\" />
</ItemGroup>
</Project>

View File

@ -2,6 +2,16 @@
namespace Borepin.Model
{
public enum MachineStates
{
Free,
InUse,
ToCheck,
Blocked,
Disabled,
Reserved
}
public class Machine : BindableBase
{
private string _ID;
@ -11,8 +21,8 @@ namespace Borepin.Model
set => SetProperty(ref _ID, value);
}
private string _State;
public string State
private MachineStates _State;
public MachineStates State
{
get => _State;
set => SetProperty(ref _State, value);
@ -25,11 +35,20 @@ namespace Borepin.Model
set => SetProperty(ref _Description, value);
}
private string _User;
public string User
private User _User;
public User User
{
get => _User;
set => SetProperty(ref _User, value);
}
}
public class User
{
public string ID { get; set; }
public string Name { get; set; }
public string OriginalWorkshop { get; set; }
}
}

View File

@ -1,18 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Borepin.Page.MachinePage">
x:Class="Borepin.Page.MachinePage"
Title="{Binding Machine.ID}">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Label Text="MachineID=" TextDecorations="Underline"></Label>
<Label Text="{Binding Machine.MachineID}"/>
<Label Text="Description=" TextDecorations="Underline"></Label>
<Label Text="MachineID" FontAttributes="Bold"></Label>
<Label Text="{Binding Machine.ID}"/>
<Label Text="Description" FontAttributes="Bold"></Label>
<Label Text="{Binding Machine.Description}"/>
<Label Text="State=" TextDecorations="Underline"></Label>
<Label Text="State" FontAttributes="Bold"></Label>
<Label Text="{Binding Machine.State}"/>
<Button Text="Reserve" Command="{Binding ReserveMachineCommand}" IsVisible="{Binding CanReserve}"/>
<Button Text="Use" Command="{Binding UseMachineCommand}" IsVisible="{Binding CanUse}"/>
<Button Text="GiveBack" Command="{Binding GiveBackMachineCommand}" IsVisible="{Binding CanGiveBack}"/>
<Button Text="Disable" Command="{Binding DisableMachineCommand}" IsVisible="{Binding IsAdmin}"/>
<Button Text="Block" Command="{Binding BlockMachineCommand}" IsVisible="{Binding IsAdmin}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

View File

@ -5,13 +5,14 @@
Title="Machines">
<ContentPage.Content>
<StackLayout>
<ListView ItemsSource="{Binding MachineList}">
<ListView x:Name="MachineList" ItemsSource="{Binding MachineList}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding ID}" FontAttributes="Bold" />
<Label Grid.Column="1" Text="{Binding State}" />
<Label Grid.Column="0" Text="{Binding ID, StringFormat='{0}'}" />
<Label Grid.Column="1" Text="{Binding State, StringFormat='{0}'}" />
<Button Grid.Column="2" Text="->" Command="{Binding BindingContext.GoToMachineCommand, Source={x:Reference MachineList}}" CommandParameter="{Binding .}"/>
</Grid>
</ViewCell>
</DataTemplate>

View File

@ -1,35 +1,248 @@
using Borepin.Model;
using Borepin.Service;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace Borepin.PageModel
{
public class MachinePageModel : BindableBase
public class MachinePageModel : BindableBase, INavigationAware
{
private INavigationService _NavigationService;
private BFFHInterface _BFFHInterface;
public MachinePageModel(string machineID, INavigationService navigationService)
public MachinePageModel(INavigationService navigationService, BFFHInterface bffhInterface)
{
_NavigationService = navigationService;
_BFFHInterface = bffhInterface;
Machine = new Machine()
{
ID = machineID,
Description = "This is a Discription",
User = "Test User"
};
ReserveMachineCommand = new Command(ReserveMachineCommandExecuted);
UseMachineCommand = new Command(UseMachineCommandExecuted);
GiveBackMachineCommand = new Command(GiveBackMachineCommandExecuted);
BlockMachineCommand = new Command(BlockMachineCommandExecuted);
DisableMachineCommand = new Command(DisableMachineCommandExecuted);
}
private Machine _Machine;
public Machine Machine
{
get => Machine;
get => _Machine;
set => SetProperty(ref _Machine, value);
}
private bool _IsValid = false;
public bool IsValid
{
get => _IsValid;
set => SetProperty(ref _IsValid, value);
}
private ICommand _ReserveMachineCommand;
public ICommand ReserveMachineCommand
{
get => _ReserveMachineCommand;
set => SetProperty(ref _ReserveMachineCommand, value);
}
private void ReserveMachineCommandExecuted()
{
_BFFHInterface.ReserveMachine(Machine.ID);
UpdateMachine();
}
private ICommand _UseMachineCommand;
public ICommand UseMachineCommand
{
get => _UseMachineCommand;
set => SetProperty(ref _UseMachineCommand, value);
}
private void UseMachineCommandExecuted()
{
_BFFHInterface.UseMachine(Machine.ID);
UpdateMachine();
}
private ICommand _GiveBackMachineCommand;
public ICommand GiveBackMachineCommand
{
get => _GiveBackMachineCommand;
set => SetProperty(ref _GiveBackMachineCommand, value);
}
private void GiveBackMachineCommandExecuted()
{
_BFFHInterface.GiveBackMachine(Machine.ID);
UpdateMachine();
}
private ICommand _DisableMachineCommand;
public ICommand DisableMachineCommand
{
get => _DisableMachineCommand;
set => SetProperty(ref _DisableMachineCommand, value);
}
private void DisableMachineCommandExecuted()
{
if(Machine.State == MachineStates.Disabled)
{
_BFFHInterface.SetStateForce(Machine.ID, MachineStates.Free);
}
else
{
_BFFHInterface.SetStateForce(Machine.ID, MachineStates.Disabled);
}
UpdateMachine();
}
private ICommand _BlockMachineCommand;
public ICommand BlockMachineCommand
{
get => _BlockMachineCommand;
set => SetProperty(ref _BlockMachineCommand, value);
}
private void BlockMachineCommandExecuted()
{
if (Machine.State == MachineStates.Blocked)
{
_BFFHInterface.SetStateForce(Machine.ID, MachineStates.Free);
}
else
{
_BFFHInterface.SetStateForce(Machine.ID, MachineStates.Blocked);
}
UpdateMachine();
}
public bool CanUse
{
get
{
if(Machine == null)
{
return false;
}
if(Machine.State == MachineStates.Free)
{
return true;
}
if(Machine.State == MachineStates.Reserved)
{
if(Machine.User == _BFFHInterface.ActiveUser)
{
return true;
}
else if(_BFFHInterface.IsAdmin())
{
return true;
}
}
return false;
}
}
public bool CanReserve
{
get
{
if (Machine == null)
{
return false;
}
if (Machine.State == MachineStates.Free)
{
return true;
}
return false;
}
}
public bool CanGiveBack
{
get
{
if (Machine == null)
{
return false;
}
if (Machine.State == MachineStates.InUse && Machine.User == _BFFHInterface.ActiveUser)
{
return true;
}
return false;
}
}
public bool IsAdmin
{
get
{
if (_BFFHInterface == null)
{
return false;
}
if (_BFFHInterface.IsAdmin())
{
return true;
}
return false;
}
}
public void UpdateMachine()
{
Machine = BFFHService.GetMachine(Machine.ID);
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("CanUse"));
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("CanReserve"));
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("CanGiveBack"));
}
public void OnNavigatedFrom(INavigationParameters parameters)
{
}
public void OnNavigatedTo(INavigationParameters parameters)
{
string machineID = parameters["machineID"] as string;
if(machineID == null)
{
_IsValid = false;
return;
}
try
{
Machine = BFFHService.GetMachine(machineID);
IsValid = true;
UpdateMachine();
}
catch
{
_IsValid = false;
}
}
}
}

View File

@ -1,4 +1,5 @@
using Borepin.Model;
using Borepin.Service;
using Prism.Mvvm;
using Prism.Navigation;
using System;
@ -10,11 +11,6 @@ using Xamarin.Forms;
namespace Borepin.PageModel
{
public class Employee
{
public string DisplayName { get; set; }
}
public class MachinesPageModel : BindableBase
{
private INavigationService _navigationService;
@ -22,13 +18,9 @@ namespace Borepin.PageModel
public MachinesPageModel(INavigationService navigationService)
{
_navigationService = navigationService;
GoToMachineCommand = new Command<string>(GoToMachineCommandExecuted);
GoToMachineCommand = new Command<object>(GoToMachineCommandExecuted);
MachineList = new ObservableCollection<Machine>();
MachineList.Add(new Machine() { ID = "Machine 1", State = "InUse" });
MachineList.Add(new Machine() { ID = "Machine 1", State = "InUse" });
MachineList.Add(new Machine() { ID = "Machine 1", State = "InUse" });
MachineList.Add(new Machine() { ID = "Machine 1", State = "InUse" });
MachineList = new ObservableCollection<Machine>(BFFHService.GetMachines());
}
private ICommand _GoToMachineCommand;
@ -38,9 +30,14 @@ namespace Borepin.PageModel
set => SetProperty(ref _GoToMachineCommand, value);
}
private async void GoToMachineCommandExecuted(string machineID)
private async void GoToMachineCommandExecuted(object obj)
{
var result = await _navigationService.NavigateAsync($"NavigationPage/MachinePage/MachinePage?machineID={ machineID }");
Machine machine = obj as Machine;
NavigationParameters navigationParams = new NavigationParameters();
navigationParams.Add("machineID", machine.ID);
INavigationResult result = await _navigationService.NavigateAsync($"MachinePage", navigationParams);
if (!result.Success)
{
System.Diagnostics.Debugger.Break();

View File

@ -0,0 +1,245 @@
using Borepin.Model;
using System;
using System.Collections.Generic;
using System.Text;
namespace Borepin.Service
{
public static class BFFHDataService
{
public static List<Machine> _Machines;
public static List<User> _Users;
static BFFHDataService()
{
_Users = new List<User>()
{
new User()
{
ID = "0",
Name = "Nick Wise (Admin)",
OriginalWorkshop = "fvm.fab-access.org"
},
new User()
{
ID = "1",
Name = "Tamia Langley",
OriginalWorkshop = "fvm.fab-access.org"
},
new User()
{
ID = "2",
Name = "Jay James",
OriginalWorkshop = "fvm.fab-access.org"
},
new User()
{
ID = "3",
Name = "Darragh Ellwood",
OriginalWorkshop = "happylab.fab-access.org"
},
new User()
{
ID = "4",
Name = "Mila-Rose Mcknight",
OriginalWorkshop = "motionlab.fab-access.org"
}
};
_Machines = new List<Machine>()
{
new Machine()
{
ID = "MakerRobo UP",
Description = "MakerRobo UP is a 3D Printer from Bangladesh",
State = MachineStates.Free,
User = null
},
new Machine()
{
ID = "LS-1080-K 150W",
Description = "Laser cutting and engraving" + "\n" +
"Cutting Area: 1000 x 800 mm" + "\n" +
"Materials: wood, cardboard, PMMA, textiles, etc." + "\n" +
"Supported files: DXF, AI, EPS, JPG, BMP",
State = MachineStates.InUse,
User = _Users.Find(x => x.ID == "1")
},
new Machine()
{
ID = "Fireball V90",
Description = "The V90 is a high performance, general purpose machine, suitable for many uses such as clock making, plaques, RC aircraft parts and more. 12x18x3 working space.",
State = MachineStates.ToCheck,
User = _Users.Find(x => x.ID == "0")
},
new Machine()
{
ID = "IRB 1600-145 kg",
Description = "Robotic Arm",
State = MachineStates.Blocked,
User = null
},
new Machine()
{
ID = "Carmine 1.09",
Description = "Primesense",
State = MachineStates.Disabled,
User = null
},
new Machine()
{
ID = "Zmorph 2.0SX Multitool",
Description = "FabLab in a box ",
State = MachineStates.Reserved,
User = _Users.Find(x => x.ID == "2")
},
new Machine()
{
ID = "CubePro Duo",
Description = "The CubePro features the largest-in-class build platform with ultra high-resolution. With prints 2.5 times larger than any other desktop prosumer and hobbyist printer (11.2 x 10.6 x 9.06 or 285.4mm x 270.4mm x 230mm) with ultra high-resolution settings of 70-micron thin print layers, professional quality printing has never been so large or easy.",
State = MachineStates.Free,
User = null
},
};
}
}
public class BFFHInterface
{
public readonly User ActiveUser;
public BFFHInterface(string userID)
{
ActiveUser = BFFHDataService._Users.Find(x => x.ID == userID);
}
public void UseMachine(string machineID)
{
Machine machinetouse = BFFHDataService._Machines.Find(x => x.ID == machineID);
if (machinetouse == null)
{
throw new Exception("Unknown Machine");
}
int index = BFFHDataService._Machines.IndexOf(machinetouse);
if (machinetouse.State == MachineStates.Free)
{
machinetouse.State = MachineStates.InUse;
machinetouse.User = ActiveUser;
}
if(machinetouse.State == MachineStates.Reserved && machinetouse.User == ActiveUser)
{
machinetouse.State = MachineStates.InUse;
machinetouse.User = ActiveUser;
}
BFFHDataService._Machines[index] = machinetouse;
}
public void ReserveMachine(string machineID)
{
Machine machinetouse = BFFHDataService._Machines.Find(x => x.ID == machineID);
if (machinetouse == null)
{
throw new Exception("Unknown Machine");
}
int index = BFFHDataService._Machines.IndexOf(machinetouse);
if (machinetouse.State == MachineStates.Free)
{
machinetouse.State = MachineStates.Reserved;
machinetouse.User = ActiveUser;
}
BFFHDataService._Machines[index] = machinetouse;
}
public void GiveBackMachine(string machineID)
{
Machine machinetogiveback = BFFHDataService._Machines.Find(x => x.ID == machineID);
if (machinetogiveback == null)
{
throw new Exception("Unknown Machine");
}
int index = BFFHDataService._Machines.IndexOf(machinetogiveback);
if (machinetogiveback.State == MachineStates.InUse)
{
machinetogiveback.State = MachineStates.Free;
machinetogiveback.User = null;
}
BFFHDataService._Machines[index] = machinetogiveback;
}
public void SetStateForce(string machineID, MachineStates state)
{
if(IsAdmin())
{
Machine machinetosetstate = BFFHDataService._Machines.Find(x => x.ID == machineID);
if (machinetosetstate == null)
{
throw new Exception("Unknown Machine");
}
int index = BFFHDataService._Machines.IndexOf(machinetosetstate);
machinetosetstate.State = state;
BFFHDataService._Machines[index] = machinetosetstate;
}
else
{
throw new Exception("Forbidden");
}
}
public bool IsAdmin()
{
return ActiveUser.ID == BFFHService.AdminID;
}
}
public static class BFFHService
{
public static readonly string Host = "fvm.fab-access.org";
public static readonly string AdminID = "0";
public static BFFHInterface Auth(string userID)
{
if(BFFHDataService._Users.Find(x => x.ID == userID) == null)
{
throw new Exception("Authentication failed");
}
return new BFFHInterface(userID);
}
public static List<Machine> GetMachines()
{
return BFFHDataService._Machines;
}
public static List<User> GetUsers()
{
return BFFHDataService._Users;
}
public static Machine GetMachine(string machineID)
{
Machine m = BFFHDataService._Machines.Find(x => x.ID == machineID);
if (m == null)
{
throw new Exception("Unknown Machine");
}
return m;
}
}
}