Statistiques
| Branche: | Révision:

teotestbluetooth / TestXamConnections / TestXamConnections / ViewModels / TeoDeviceViewModel.cs @ 6f503a5a

Historique | Voir | Annoter | Télécharger (3,369 ko)

1
using System;
2
using System.Collections.ObjectModel;
3
using System.Threading.Tasks;
4
using System.Windows.Input;
5
using TestXamConnections.Connection;
6
using TestXamConnections.Device;
7
using TestXamConnections.Device.Balance;
8
using TestXamConnections.Device.Balance.Balea;
9
using TestXamConnections.Models;
10
using Xamarin.Forms;
11

    
12
namespace TestXamConnections.ViewModels
13
{
14
    public class TeoDeviceViewModel : ViewModelBase
15
    {
16
        private IBalance teoScale;
17
        private bool isConnected;
18
        private string data;
19
        private bool isVisibleData;
20
        private bool showError;
21
        private string errorMessage;
22

    
23
        public IBalance TeoScale
24
        {
25
            get => teoScale;
26
            set => SetProperty(ref teoScale, value);
27
        }
28

    
29
        public bool IsVisibleData
30
        {
31
            get => isVisibleData;
32
            set => SetProperty(ref isVisibleData, value);
33
        }
34

    
35
        public bool IsConnected
36
        {
37
            get => isConnected;
38
            set => SetProperty(ref isConnected, value);
39
        }
40

    
41
        public string Data
42
        {
43
            get => data;
44
            set => SetProperty(ref data, value);
45
        }
46

    
47
        public bool ShowError
48
        {
49
            get => showError;
50
            set => SetProperty(ref showError, value);
51
        }
52

    
53
        public string ErrorMessage
54
        {
55
            get => errorMessage;
56
            set => SetProperty(ref errorMessage, value);
57
        }
58

    
59
        public ObservableCollection<CommandItemViewModel> AvailableCommandsList { get; set; }
60

    
61
        public async Task<bool> ConnectToDevice()
62
        {
63
            ShowError = false;
64
            IsConnected = await TeoScale.ConnectToBalanceAsync();
65
            return IsConnected;
66
        }
67

    
68
        public ICommand ConnectToDeviceCommand { private set; get; }
69
        public ICommand SendValueCommand { private set; get; }
70

    
71
        public void Init()
72
        {
73
            // Init View Model
74
            ShowError = false;
75
            IsConnected = false;
76
            IsVisibleData = false;
77
            ConnectToDeviceCommand = new Command(async () => await ConnectToDevice());
78
            SendValueCommand = new Command(async (cmd) => await TeoScale.SendCommandAsync((TeoConstants.TeoCommandType)cmd));
79
            TeoScale.PeseeReceivedEvent += PeseeReceived;
80

    
81
            // Chargement des commandes
82
            AvailableCommandsList = new ObservableCollection<CommandItemViewModel>();
83
            foreach (TeoConstants.TeoCommandType _cmd in Enum.GetValues(typeof(TeoConstants.TeoCommandType)))
84
            {
85
                AvailableCommandsList.Add(new CommandItemViewModel(_cmd));
86
            }
87

    
88
        }
89

    
90
        public TeoDeviceViewModel(DeviceInfo device)
91
        {
92
            // Appel au Balance provider pour récupérer l'item TeoBalancea avec les fonctions d'initiation
93
            TeoScale = BalanceProvider.GetBalance(Balance.TeoBalance, ConnectionType.Bluetooth, device);
94
            TeoScale.SendCommandAsync(TeoConstants.TeoCommandType.EmissionPoids)
95
            Init();
96
        }
97

    
98
        ~TeoDeviceViewModel()
99
        {
100
            TeoScale.PeseeReceivedEvent -= PeseeReceived;
101
        }
102

    
103
        public void PeseeReceived(object sender, ReponsePesee args)
104
        {
105
            if (args.Err == false)
106
            {
107
                Data = args.PoidsMesure;
108
            } else
109
            {
110
                Data = "Erreur de lecture";
111
            }
112
        }
113
    }
114
}