-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.cs
More file actions
94 lines (84 loc) · 2.75 KB
/
Cube.cs
File metadata and controls
94 lines (84 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
namespace virtual_cube
{
public abstract class Cube : INotifyPropertyChanged
{
public string Name { get; set; }
public ulong BluetoothAddress { get; set; }
public String FormattedBluetoothAddress { get; set; }
private DateTime _LastAdvertisement;
public DateTime LastAdvertisement
{
get { return _LastAdvertisement; }
set
{
_LastAdvertisement = value;
OnPropertyChanged("LastAdvertisement");
}
}
private ConnectionStatus _ConnectionStatus;
public ConnectionStatus ConnectionStatus
{
get { return _ConnectionStatus; }
set
{
_ConnectionStatus = value;
OnPropertyChanged("ConnectionStatus");
}
}
private int? _BatteryLevel;
public int? BatteryLevel
{
get { return _BatteryLevel; }
set
{
_BatteryLevel = value;
OnPropertyChanged("BatteryLevel");
}
}
public event EventHandler<Move> Moves;
public event PropertyChangedEventHandler PropertyChanged;
public Cube(string name, ulong bluetoothAddress)
{
Name = name;
BluetoothAddress = bluetoothAddress;
FormattedBluetoothAddress = MAC802DOT3(bluetoothAddress);
ConnectionStatus = ConnectionStatus.DISCONNECTED;
}
public abstract String GetTypeName();
public abstract Task RequestBatteryLevelAsync();
public abstract Task ConnectAsync();
public abstract Task DisconnectAsync();
protected virtual void OnNewMove(Move move)
{
EventHandler<Move> handler = Moves;
if (handler != null)
{
handler(this, move);
}
}
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public override bool Equals(object obj)
{
return obj is Cube cube &&
BluetoothAddress == cube.BluetoothAddress;
}
public override int GetHashCode()
{
return 963345907 + BluetoothAddress.GetHashCode();
}
public static string MAC802DOT3(ulong macAddress)
{
return string.Join(":",
BitConverter.GetBytes(macAddress).Reverse()
.Select(b => b.ToString("X2"))).Substring(6);
}
}
}