Statistiques
| Branche: | Révision:

teotestbluetooth / TestBLE / TestBLE / ViewModels / DeviceViewModel.cs @ 60882b56

Historique | Voir | Annoter | Télécharger (2,486 ko)

1 3c62e059 Martin Toutant
using System;
2
using System.Collections.Generic;
3
using System.Collections.ObjectModel;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Windows.Input;
7
using TestBLE.Models;
8
using TestBLE.Services;
9
using Xamarin.Forms;
10 4fda4a87 Toutance
using static TestBLE.Models.TeoBluetoothCommand;
11 3c62e059 Martin Toutant
12
namespace TestBLE.ViewModels
13
{
14
    public class DeviceViewModel : ViewModelBase
15
    {
16
        private bool isConnected;
17
        private string data;
18
        private BTDevice device;
19
20
        private bool isVisibleData;
21
22
        public bool IsVisibleData
23
        {
24
            get { return isVisibleData; }
25
            set { SetProperty(ref isVisibleData, value); }
26
        }
27
28
        public bool IsConnected
29
        {
30
            get { return isConnected; }
31
            set { SetProperty(ref isConnected, value); }
32
        }
33
34
        public BTDevice Device
35
        {
36
            get { return device; }
37
            set { SetProperty(ref device, value); }
38
        }
39
40
        public string Data
41
        {
42
            get { return data; }
43
            set { SetProperty(ref data, value); }
44
        }
45
46
        public IBluetoothService BluetoothService { get; set; }
47
        
48
        public ObservableCollection<CommandItemViewModel> AvailableCommandsList { get; set; }
49
50
        public async Task<bool> ConnectToDevice()
51
        {
52
            await BluetoothService.Connect(Device);
53
            IsConnected = true;
54
            return true;
55
        }
56
        public ICommand ConnectToDeviceCommand { private set; get; }
57
58
        public DeviceViewModel(BTDevice device)
59
        {
60
            Device = device;
61
            IsConnected = false;
62
            IsVisibleData = false;
63
            BluetoothService = DependencyService.Get<IBluetoothService>();
64
            BluetoothService.DataReceivedEvent += (s, args) => DataReceived(args);
65
            ConnectToDeviceCommand = new Command(async () => await ConnectToDevice());
66
67
            // Ajout manuel des commandes
68
            AvailableCommandsList = new ObservableCollection<CommandItemViewModel>();
69 60882b56 Martin Toutant
            foreach (CommandType _cmd in Enum.GetValues(typeof(CommandType)))
70 3c62e059 Martin Toutant
            {
71 60882b56 Martin Toutant
                AvailableCommandsList.Add(new CommandItemViewModel(_cmd));
72 4fda4a87 Toutance
            }
73 3c62e059 Martin Toutant
        }
74
75
        ~DeviceViewModel()
76
        {
77
            BluetoothService.DataReceivedEvent -= (s, args) => DataReceived(args);
78
        }
79
80
        private void DataReceived(string recData)
81
        {
82
            IsVisibleData = true;
83
            string cleanData = recData.Trim();
84
            Data += cleanData + "\n";
85
        }
86
87
    }
88
}