-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathreducers.ts
142 lines (119 loc) · 3.49 KB
/
reducers.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: MIT
// Copyright (c) 2020-2024 The Pybricks Authors
//
// Manages state for the Bluetooth Low Energy connection.
// This assumes that there is only one global connection to a single device.
import { Reducer, combineReducers } from 'redux';
import {
bleDIServiceDidReceiveFirmwareRevision,
bleDIServiceDidReceivePnPId,
} from '../ble-device-info-service/actions';
import { getHubTypeName } from '../ble-device-info-service/protocol';
import { didReceiveStatusReport } from '../ble-pybricks-service/actions';
import { Status, statusToFlag } from '../ble-pybricks-service/protocol';
import {
bleConnectPybricks,
bleDidConnectPybricks,
bleDidDisconnectPybricks,
bleDidFailToConnectPybricks,
bleDidFailToDisconnectPybricks,
bleDisconnectPybricks,
} from './actions';
/**
* Describes the state of the BLE connection.
*/
export enum BleConnectionState {
/**
* No device is connected.
*/
Disconnected = 'ble.connection.state.disconnected',
/**
* Connecting to a device.
*/
Connecting = 'ble.connection.state.connecting',
/**
* Connected to a device.
*/
Connected = 'ble.connection.state.connected',
/**
* Disconnecting from a device.
*/
Disconnecting = 'ble.connection.state.disconnecting',
}
const connection: Reducer<BleConnectionState> = (
state = BleConnectionState.Disconnected,
action,
) => {
if (bleConnectPybricks.matches(action)) {
return BleConnectionState.Connecting;
}
if (
bleDidConnectPybricks.matches(action) ||
bleDidFailToDisconnectPybricks.matches(action)
) {
return BleConnectionState.Connected;
}
if (bleDisconnectPybricks.matches(action)) {
return BleConnectionState.Disconnecting;
}
if (
bleDidFailToConnectPybricks.matches(action) ||
bleDidDisconnectPybricks.matches(action)
) {
return BleConnectionState.Disconnected;
}
return state;
};
const deviceName: Reducer<string> = (state = '', action) => {
if (bleDidDisconnectPybricks.matches(action)) {
return '';
}
if (bleDidConnectPybricks.matches(action)) {
return action.name;
}
return state;
};
const deviceType: Reducer<string> = (state = '', action) => {
if (bleDidDisconnectPybricks.matches(action)) {
return '';
}
if (bleDIServiceDidReceivePnPId.matches(action)) {
return getHubTypeName(action.pnpId);
}
return state;
};
const deviceFirmwareVersion: Reducer<string> = (state = '', action) => {
if (bleDidDisconnectPybricks.matches(action)) {
return '';
}
if (bleDIServiceDidReceiveFirmwareRevision.matches(action)) {
return action.version;
}
return state;
};
const deviceLowBatteryWarning: Reducer<boolean> = (state = false, action) => {
if (bleDidDisconnectPybricks.matches(action)) {
return false;
}
if (didReceiveStatusReport.matches(action)) {
return Boolean(
action.statusFlags & statusToFlag(Status.BatteryLowVoltageWarning),
);
}
return state;
};
const deviceBatteryCharging: Reducer<boolean> = (state = false, action) => {
if (bleDidDisconnectPybricks.matches(action)) {
return false;
}
// TODO: hub does not currently have a status flag for this
return state;
};
export default combineReducers({
connection,
deviceName,
deviceType,
deviceFirmwareVersion,
deviceLowBatteryWarning,
deviceBatteryCharging,
});