-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.cs
69 lines (58 loc) · 2.06 KB
/
Device.cs
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
using MediaDevices;
using System;
using System.IO;
using System.Linq;
namespace GarminMtpDataBackupper
{
public class Device : IDisposable
{
private readonly string _deviceName;
private MediaDevice _connectedDevice;
public Device(Config.Config config)
{
_deviceName = config.GarminDeviceName;
}
public void Dispose()
{
_connectedDevice?.Dispose();
}
public void Connect()
{
var devices = MediaDevice.GetDevices();
_connectedDevice = devices.FirstOrDefault((device) => (device.FriendlyName == _deviceName) || (device.Description == _deviceName));
if (_connectedDevice == null)
{
var exception = new IOException($"Unable to connect to Garmin device: {_deviceName}");
Logger.Error("Connecting to the device problem.", exception);
throw exception;
}
_connectedDevice.Connect();
}
public void Disconnect()
{
_connectedDevice?.Disconnect();
_connectedDevice = null;
}
public void DownloadFile(MediaFileInfo sourceFile, string destinationFolderPath)
{
MemoryStream memoryStream = new MemoryStream();
_connectedDevice.DownloadFile(sourceFile.FullName, memoryStream);
memoryStream.Position = 0;
using (FileStream file = new FileStream($@"{destinationFolderPath}\{sourceFile.Name}", FileMode.Create, FileAccess.Write))
{
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
file.Write(bytes, 0, bytes.Length);
memoryStream.Close();
}
}
public MediaDirectoryInfo GetDirectoryInfo(string path)
{
return _connectedDevice.GetDirectoryInfo(path);
}
public bool FolderExists(string path)
{
return _connectedDevice.DirectoryExists(path);
}
}
}