-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdeviceEvents.tsx
207 lines (186 loc) · 7.89 KB
/
deviceEvents.tsx
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/***********************************************************
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License
**********************************************************/
import * as React from 'react';
import { Stack } from '@fluentui/react';
import { useLocation } from 'react-router-dom';
import { ResourceKeys } from '../../../../localization/resourceKeys';
import { getDeviceIdFromQueryString, getModuleIdentityIdFromQueryString } from '../../../shared/utils/queryStringHelper';
import { SynchronizationStatus } from '../../../api/models/synchronizationStatus';
import { MonitorEventsParameters } from '../../../api/parameters/deviceParameters';
import { DEFAULT_CONSUMER_GROUP, WEBSOCKET_ENDPOINT } from '../../../constants/apiConstants';
import { HeaderView } from '../../../shared/components/headerView';
import { useDeviceEventsStateContext } from '../context/deviceEventsStateContext';
import { usePnpStateContext } from '../../pnp/context/pnpStateContext';
import { MultiLineShimmer } from '../../../shared/components/multiLineShimmer';
import { DeviceSimulationPanel } from './deviceSimulationPanel';
import { Commands } from './commands';
import { CustomEventHub } from './customEventHub';
import { ConsumerGroup } from './consumerGroup';
import { DeviceContentTypePanel } from './deviceContentTypePanel';
import { Loader } from './loader';
import { EventsContent } from './eventsContent';
import { SystemPropertyCheckBox } from './systemPropertyCheckBox';
import './deviceEvents.scss';
let client: WebSocket;
export const DeviceEvents: React.FC = () => {
const { search } = useLocation();
const deviceId = getDeviceIdFromQueryString(search);
const moduleId = getModuleIdentityIdFromQueryString(search);
const [ state, api ] = useDeviceEventsStateContext();
const decoderPrototype = state.contentType.decoderPrototype;
// event hub settings
const [consumerGroup, setConsumerGroup] = React.useState(DEFAULT_CONSUMER_GROUP);
const [useBuiltInEventHub, setUseBuiltInEventHub] = React.useState<boolean>(true);
const [customEventHubConnectionString, setCustomEventHubConnectionString] = React.useState<string>(undefined);
const [showSystemProperties, setShowSystemProperties] = React.useState<boolean>(false);
// event message state
const [monitoringData, setMonitoringData] = React.useState<boolean>(false);
const [startDisabled, setStartDisabled] = React.useState<boolean>(false);
const [hasError, setHasError] = React.useState<boolean>(false);
// pnp events specific
const { pnpState, } = usePnpStateContext();
const isLoading = pnpState.modelDefinitionWithSource.synchronizationStatus === SynchronizationStatus.working;
const [showPnpModeledEvents, setShowPnpModeledEvents] = React.useState(false);
// simulation specific
const [showSimulationPanel, setShowSimulationPanel] = React.useState(false);
// message content type specific
const [showContentTypePanel, setShowContentTypePanel] = React.useState(false);
React.useEffect(
() => {
return () => {
stopMonitoring();
client?.close();
};
},
[]);
React.useEffect(
() => {
client = new WebSocket(WEBSOCKET_ENDPOINT);
},
[]);
React.useEffect(
() => {
if (monitoringData) {
client.onmessage = message => {
api.setEvents(JSON.parse(message.data));
};
}
},
[monitoringData]);
React.useEffect( // tslint:disable-next-line: cyclomatic-complexity
() => {
if (state.formMode === 'updating' ||
// when using custom event hub, both valid connection string and name need to be provided
(!useBuiltInEventHub && (!customEventHubConnectionString || hasError))) {
setStartDisabled(true);
}
else {
setStartDisabled(false);
}
},
[hasError, state.formMode, useBuiltInEventHub, customEventHubConnectionString]);
const onSystemPropertyCheckBoxChange = (ev?: React.FormEvent<HTMLElement | HTMLInputElement>, checked?: boolean) => {
setShowSystemProperties(!!checked);
};
const renderCommands = () => {
return (
<Commands
startDisabled={startDisabled}
monitoringData={monitoringData}
showPnpModeledEvents={showPnpModeledEvents}
showSimulationPanel={showSimulationPanel}
showContentTypePanel={showContentTypePanel}
setMonitoringData={setMonitoringData}
setShowPnpModeledEvents={setShowPnpModeledEvents}
setShowSimulationPanel={setShowSimulationPanel}
setShowContentTypePanel={setShowContentTypePanel}
fetchData={fetchData}
stopFetching={stopMonitoring}
/>
);
};
const renderConsumerGroup = () => {
return (
<div className="horizontal-item">
<ConsumerGroup
monitoringData={monitoringData}
consumerGroup={consumerGroup}
setConsumerGroup={setConsumerGroup}
/>
</div>
);
};
const renderCustomEventHub = () => {
return (
<div className="horizontal-item">
<CustomEventHub
monitoringData={monitoringData}
useBuiltInEventHub={useBuiltInEventHub}
customEventHubConnectionString={customEventHubConnectionString}
setUseBuiltInEventHub={setUseBuiltInEventHub}
setCustomEventHubConnectionString={setCustomEventHubConnectionString}
setHasError={setHasError}
/>
</div>
);
};
const stopMonitoring = () => {
api.stopEventsMonitoring();
};
const fetchData = () => {
client.onopen = () => { // intentionally blank
};
let parameters: MonitorEventsParameters = {
consumerGroup,
decoderPrototype,
deviceId,
moduleId
};
if (!useBuiltInEventHub) {
parameters = {
...parameters,
customEventHubConnectionString
};
}
api.startEventsMonitoring(parameters);
};
const onToggleSimulationPanel = () => {
setShowSimulationPanel(!showSimulationPanel);
};
const onToggleContentTypePanel = () => {
setShowContentTypePanel(!showContentTypePanel);
};
if (isLoading) {
return <MultiLineShimmer />;
}
return (
<Stack className="device-events" key="device-events">
{renderCommands()}
<HeaderView
headerText={ResourceKeys.deviceEvents.headerText}
tooltip={ResourceKeys.deviceEvents.tooltip}
/>
{renderConsumerGroup()}
{renderCustomEventHub()}
<DeviceSimulationPanel
showSimulationPanel={showSimulationPanel}
onToggleSimulationPanel={onToggleSimulationPanel}
/>
<DeviceContentTypePanel
showContentTypePanel={showContentTypePanel}
onToggleContentTypePanel={onToggleContentTypePanel}
/>
<div className="device-events-container">
<SystemPropertyCheckBox
showSystemProperties={showSystemProperties}
showPnpModeledEvents={showPnpModeledEvents}
setShowSystemProperties={onSystemPropertyCheckBoxChange}
/>
<Loader monitoringData={monitoringData}/>
<EventsContent showPnpModeledEvents={showPnpModeledEvents} showSystemProperties={showSystemProperties}/>
</div>
</Stack>
);
};