Skip to content

Commit 5e68a15

Browse files
authored
Enabling ACPU UI to provide a way for user to enable/disable ACPU (#234)
* Enabling ACPU UI to provide a way for user to enable/disable ACPU * fixing eslint issue on ubuntu latest
1 parent f137fd7 commit 5e68a15

File tree

2 files changed

+121
-29
lines changed

2 files changed

+121
-29
lines changed

src/components/Tables/ACPUTable.js

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ function ACPUTable({ device, update, notify }) {
3333
const [disable, setDisable] = React.useState(true);
3434
const loadActivity = GetOptions('A45_Load');
3535

36+
// Toggle handler to enable/disable ACPU fields and freeze parameters
37+
const handleToggle = () => {
38+
if (!device || !href) {
39+
setDisable(true);
40+
} else if (device && href) {
41+
setDisable(!disable); // Toggle the fields' disable state
42+
}
43+
};
44+
3645
function fetchPort(port, link) {
3746
server.GET(server.peripheralPath(device, `${link}/${port.href}`), (data) => {
3847
const ep = parseInt(port.href.slice(-1), 10);
@@ -46,7 +55,6 @@ function ACPUTable({ device, update, notify }) {
4655
function fetchAcpuData(link) {
4756
if (link !== '') {
4857
server.GET(server.peripheralPath(device, link), (data) => {
49-
// resolve cycling
5058
if (data.name !== acpuData.name
5159
|| data.frequency !== acpuData.frequency
5260
|| data.load !== acpuData.load) {
@@ -56,9 +64,7 @@ function ACPUTable({ device, update, notify }) {
5664
load: data.load,
5765
});
5866
}
59-
setPowerData([
60-
['Block Power', data.consumption.block_power, 0],
61-
]);
67+
setPowerData([['Block Power', data.consumption.block_power, 0]]);
6268
data.ports.forEach((port) => fetchPort(port, link));
6369
});
6470
}
@@ -75,7 +81,7 @@ function ACPUTable({ device, update, notify }) {
7581
function fetchData() {
7682
server.GET(server.api.fetch(server.Elem.peripherals, device), (data) => {
7783
const acpu = getPeripherals(data, 'acpu');
78-
setDisable(acpu.length === 0);
84+
setDisable(true);
7985
if (acpu.length > 0) {
8086
const link = acpu[0].href;
8187
setHref(link);
@@ -124,7 +130,6 @@ function ACPUTable({ device, update, notify }) {
124130
}
125131

126132
const deleteRow = (index) => {
127-
// no delete method for acpu. this is just clear name of the endpoint which mean disable
128133
const val = endpoints[index].data;
129134
val.name = '';
130135
server.PATCH(server.peripheralPath(device, `${href}/ep/${endpoints[index].ep}`), val, () => fetchAcpuData(href));
@@ -147,9 +152,25 @@ function ACPUTable({ device, update, notify }) {
147152

148153
const powerHeader = ['Power', '%'];
149154
const title = 'ACPU';
155+
150156
return (
151157
<div className="component-table-head">
152158
<ComponentLabel name={title} />
159+
160+
{/* Toggle Switch for ACPU */}
161+
<div className="toggle-container">
162+
<label htmlFor="acpu-toggle">ACPU Power</label>
163+
<label className="toggle-switch">
164+
<input
165+
type="checkbox"
166+
onChange={handleToggle}
167+
checked={!disable}
168+
disabled={!device || !href} // Disable toggle based on device and ACPU selection
169+
/>
170+
<span className="slider" />
171+
</label>
172+
</div>
173+
153174
<div className="cpu-container">
154175
<PowerTable
155176
title="ACPU power"
@@ -161,39 +182,58 @@ function ACPUTable({ device, update, notify }) {
161182
<div className="acpu-group-container">
162183
<div className="acpu-group">
163184
<label>ACPU name</label>
164-
<input type="text" onChange={(e) => handleChange('name', e.target.value)} value={acpuData.name} disabled={disable} />
185+
<input
186+
type="text"
187+
onChange={(e) => handleChange('name', e.target.value)}
188+
value={acpuData.name}
189+
disabled={disable} // Freeze when toggle is off
190+
/>
165191
</div>
166192
<div className="acpu-group">
167193
<label>Frequency</label>
168-
<input type="number" min={0} step={1} onChange={(e) => handleChange('frequency', e.target.value)} value={acpuData.frequency} disabled={disable} />
194+
<input
195+
type="number"
196+
min={0}
197+
step={1}
198+
onChange={(e) => handleChange('frequency', e.target.value)}
199+
value={acpuData.frequency}
200+
disabled={disable} // Freeze when toggle is off
201+
/>
169202
</div>
170203
<div className="acpu-group">
171204
<label>Load</label>
172-
<Dropdown value={acpuData.load} onChangeHandler={(value) => handleChange('load', value)} items={loadActivity} disabled={disable} />
205+
<Dropdown
206+
value={acpuData.load}
207+
onChangeHandler={(value) => handleChange('load', value)}
208+
items={loadActivity}
209+
disabled={disable} // Freeze when toggle is off
210+
/>
173211
</div>
174212
</div>
213+
175214
<TableBase header={header} disabled={disable} onClick={() => setModalOpen(true)}>
176-
{
177-
endpoints.map((row, index) => (
178-
(row.data !== undefined && row.data.name !== '')
179-
&& (
180-
<tr key={row.ep}>
181-
<StatusColumn messages={row.data.consumption.messages} />
182-
<Actions
183-
onEditClick={() => { setEditIndex(index); setModalOpen(true); }}
184-
onDeleteClick={() => deleteRow(index)}
185-
/>
186-
<td>{row.data.name}</td>
187-
<SelectionCell val={row.data.activity} values={loadActivity} />
188-
<PercentsCell val={row.data.read_write_rate} />
189-
<PercentsCell val={row.data.toggle_rate} precition={1} />
190-
<BandwidthCell val={row.data.consumption.calculated_bandwidth} />
191-
<PowerCell val={row.data.consumption.noc_power} />
192-
</tr>
193-
)
194-
))
195-
}
215+
{endpoints.map((row, index) => (
216+
row.data !== undefined && row.data.name !== ''
217+
&& (
218+
<tr key={row.ep}>
219+
<StatusColumn messages={row.data.consumption.messages} />
220+
<Actions
221+
onEditClick={() => { if (!disable) { setEditIndex(index); setModalOpen(true); } }}
222+
onDeleteClick={() => { if (!disable) { deleteRow(index); } }}
223+
/>
224+
<td>{row.data.name}</td>
225+
<SelectionCell val={row.data.activity} values={loadActivity} disabled={disable} />
226+
{' '}
227+
{/* Freeze selection */}
228+
<PercentsCell val={row.data.read_write_rate} disabled={disable} />
229+
<PercentsCell val={row.data.toggle_rate} precition={1} disabled={disable} />
230+
<BandwidthCell val={row.data.consumption.calculated_bandwidth} disabled={disable} />
231+
<PowerCell val={row.data.consumption.noc_power} disabled={disable} />
232+
</tr>
233+
)
234+
))}
196235
</TableBase>
236+
197237
{modalOpen && (
198238
<ABCPUModal
199239
title={title}

src/components/style/ACPUTable.css

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,55 @@
2626
padding-right: 10px;
2727
padding-left: 10px;
2828
}
29+
30+
.toggle-container {
31+
display: flex;
32+
align-items: center;
33+
gap: 0.5rem;
34+
margin-bottom: 0.5rem;
35+
margin-left: 0.3rem;
36+
width: 100%;
37+
}
38+
39+
.toggle-label {
40+
flex-grow: 1;
41+
margin-left: 0.5rem;
42+
}
43+
44+
.toggle-switch {
45+
position: relative;
46+
width: 35px;
47+
height: 18px;
48+
}
49+
50+
.slider {
51+
position: absolute;
52+
cursor: pointer;
53+
top: 0;
54+
left: 0;
55+
right: 0;
56+
bottom: 0;
57+
background-color: #ccc;
58+
transition: 0.4s;
59+
border-radius: 18px;
60+
}
61+
62+
.slider:before {
63+
position: absolute;
64+
content: "";
65+
height: 14px;
66+
width: 14px;
67+
border-radius: 50%;
68+
left: 2px;
69+
bottom: 2px;
70+
background-color: white;
71+
transition: 0.4s;
72+
}
73+
74+
input:checked + .slider {
75+
background-color: #4CAF50;
76+
}
77+
78+
input:checked + .slider:before {
79+
transform: translateX(17px);
80+
}

0 commit comments

Comments
 (0)