-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLightSensorDeviceWatcher.cpp
More file actions
102 lines (87 loc) · 2.94 KB
/
Copy pathLightSensorDeviceWatcher.cpp
File metadata and controls
102 lines (87 loc) · 2.94 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
95
96
97
98
99
100
101
102
#include "LightSensorDeviceWatcher.h"
#include <winrt/windows.devices.sensors.h>
#include <winrt/windows.foundation.h>
#include <winrt/windows.foundation.collections.h>
using namespace winrt;
using namespace Windows::Devices::Sensors;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
LightSensorDeviceWatcher::~LightSensorDeviceWatcher()
{
Stop();
}
auto LightSensorDeviceWatcher::Start(std::function<void(const std::wstring&)> handler) noexcept -> void
{
_deviceWatcher = DeviceInformation::CreateWatcher(LightSensor::GetDeviceSelector());
if (_deviceWatcher == nullptr)
return;
// デバイスが追加されたときのイベントハンドラ
auto addedHandler = [&handler](const DeviceWatcher&, const DeviceInformation&)
{
handler(L"Added.");
};
// デバイスが削除されたときのイベントハンドラ
auto removedHandler = [&handler](const DeviceWatcher&, const DeviceInformationUpdate&)
{
handler(L"Removed.");
};
// 列挙が完了したときのイベントハンドラ
auto enumerationCompletedHandler = [&handler](const DeviceWatcher&, const IInspectable&)
{
handler(L"Enumaration Completed.");
};
// デバイスの更新があったときのイベントハンドラ
auto updatedHandler = [&handler](const DeviceWatcher&, const DeviceInformationUpdate&)
{
handler(L"Update.");
};
auto stoppedHandler = [&handler](const DeviceWatcher&, const IInspectable&)
{
handler(L"Stopped");
};
// イベントハンドラを設定
// すべてのイベントハンドラを購読しないとイベントが飛んでこない
_tokenAdded = _deviceWatcher.Added(addedHandler);
_tokenRemoved = _deviceWatcher.Removed(removedHandler);
_tokenEnumerationCompleted = _deviceWatcher.EnumerationCompleted(enumerationCompletedHandler);
_tokenUpdated = _deviceWatcher.Updated(updatedHandler);
_tokenStopped = _deviceWatcher.Stopped(stoppedHandler);
_deviceWatcher.Start();
}
auto LightSensorDeviceWatcher::Stop() const noexcept -> void
{
if (_deviceWatcher == nullptr)
return;
if (_tokenAdded)
{
_deviceWatcher.Added(_tokenAdded);
}
if (_tokenRemoved)
{
_deviceWatcher.Removed(_tokenRemoved);
}
if (_tokenEnumerationCompleted)
{
_deviceWatcher.EnumerationCompleted(_tokenEnumerationCompleted);
}
if (_tokenUpdated)
{
_deviceWatcher.Updated(_tokenUpdated);
}
if (_tokenStopped)
{
_deviceWatcher.Stopped(_tokenStopped);
}
if (IsStarted())
{
_deviceWatcher.Stop();
}
}
auto LightSensorDeviceWatcher::IsStarted() const noexcept -> bool
{
if (_deviceWatcher == nullptr)
return false;
auto status = _deviceWatcher.Status();
return (status == DeviceWatcherStatus::Started) ||
(status == DeviceWatcherStatus::EnumerationCompleted);
}