-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdeviceSlice.ts
More file actions
158 lines (132 loc) · 4.17 KB
/
Copy pathdeviceSlice.ts
File metadata and controls
158 lines (132 loc) · 4.17 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
*/
import { type DeviceCore } from '@nordicsemiconductor/pc-nrfconnect-shared/nrfutil/device';
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
import { type RootState } from '../../app/store';
import { type DeviceWithSerialnumber } from './deviceLib';
export interface Firmware {
core?: DeviceCore;
coreLabel?: string;
file: string;
link?: { label: string; href: string };
}
interface FirmwareNote {
title: string;
content: string;
}
export enum SDKType {
nRFConnectSDK = 'nRF Connect SDK', // Must match the description on nrfutil SDK types.
nRFConnectSDKBareMetal = 'nRF Connect SDK Bare Metal', // Must match the description on nrfutil SDK types.
}
interface ChoiceInfo {
name: string;
description: string;
documentation: { label: string; href: string };
firmwareNote: FirmwareNote | undefined;
ncsAddon?: string;
sdk?: {
type: SDKType;
version: string;
};
}
interface BatchChoice extends ChoiceInfo {
type: 'jlink-batch';
programmingOptions: {
firmwareList: Firmware[];
};
}
interface ProgrammingAction {
type: 'program';
firmware: Firmware;
}
interface WaitAction {
type: 'wait';
durationMs: number;
}
interface ProgramModemFirmwareAction {
type: 'program-modem-firmware';
firmware: Firmware;
version: string;
vComIndex: number;
}
interface ResetAction {
type: 'reset';
}
export type ActionListEntry =
| ProgrammingAction
| WaitAction
| ProgramModemFirmwareAction
| ResetAction;
interface ActionListChoice extends ChoiceInfo {
type: 'action-list';
programmingOptions: {
actions: ActionListEntry[];
};
}
export type Choice = BatchChoice | ActionListChoice;
interface State {
choice?: Choice;
connectedDevices: Map<string, DeviceWithSerialnumber>;
selectedDevice?: DeviceWithSerialnumber;
}
const initialState: State = {
connectedDevices: new Map(),
};
const slice = createSlice({
name: 'device',
initialState,
reducers: {
addDevice: (
state,
{ payload: device }: PayloadAction<DeviceWithSerialnumber>,
) => {
if (state.selectedDevice?.serialNumber === device.serialNumber) {
state.selectedDevice = device as DeviceWithSerialnumber;
}
state.connectedDevices.set(device.serialNumber, device);
},
removeDevice: (state, { payload: deviceId }: PayloadAction<number>) => {
state.connectedDevices.forEach(device => {
if (device.id === deviceId) {
state.connectedDevices.delete(device.serialNumber);
}
});
},
selectDevice: (
state,
{
payload: device,
}: PayloadAction<DeviceWithSerialnumber | undefined>,
) => {
state.selectedDevice = device;
},
setChoice: (
state,
{ payload: choice }: PayloadAction<Choice | undefined>,
) => {
state.choice = choice;
},
},
});
export const { addDevice, removeDevice, selectDevice, setChoice } =
slice.actions;
export const getConnectedDevices = (state: RootState) => [
...state.device.connectedDevices.values(),
];
export const getSelectedDevice = (state: RootState) =>
state.device.selectedDevice;
export const selectedDeviceIsConnected = (state: RootState) =>
state.device.connectedDevices.has(
state.device.selectedDevice?.serialNumber ?? '',
);
export const getSelectedDeviceUnsafely = (state: RootState) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- Because we are certain based on the step that a device is selected
state.device.selectedDevice!;
export const getChoice = (state: RootState) => state.device.choice;
export const getChoiceUnsafely = (state: RootState) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- Because we are certain based on the step that the user made a choice
state.device.choice!;
export default slice.reducer;