iotdeviceadd
iotdeviceadd
iotdeviceadd
useEffect(() => {
if (id) {
const loadIotDevice = async () => {
const deviceData = await fetchIotDevicebyId(id);
if (deviceData) {
setIOTdeviceData({
...deviceData,
yardUuid: deviceData.yardUuid || yardUuid,
});
}
};
loadIotDevice();
}
}, [id, yardUuid]);
// useEffect(() => {
// if (iotDeviceData.deviceType) {
// // Fetch machine options based on device type
// axios
// .get(`http://localhost:9092/api/machines/get-machines/$
{iotDeviceData.deviceType}`)
// .then((response) => {
// if (response.data) {
// setMachineOptions(response.data);
// }
// })
// .catch((error) => {
// console.error("Error fetching machines:", error);
// });
// }
// }, [iotDeviceData.deviceType]);
useEffect(() => {
if (iotDeviceData.deviceType) {
// Use the service to fetch machine options
fetchMachineOptions(iotDeviceData.deviceType)
.then((options) => {
setMachineOptions(options); // Update machine options state
})
.catch((error) => {
console.error("Error fetching machines:", error);
});
}
}, [iotDeviceData.deviceType]);
useEffect(() => {
if (selectedMachine) {
// Fetch the assigned status of the machine
axios
.get(`http://192.168.1.21:9092/api/machines/${selectedMachine}/$
{iotDeviceData.deviceType}/assigned`)
.then((response) => {
if (response.data !== undefined) {
setIsAssigned(response.data); // This will be either `true` or `false`
}
if (response.data === true) {
setShow01(true); // Show `show01` if the machine is not assigned
} else {
setShow01(false); // Hide `show01` if the machine is assigned
}
})
.catch((error) => {
console.error("Error fetching machines:", error);
});
}
}, [selectedMachine]);
// useEffect(() => {
// if (selectedMachine) {
// // Use the service to fetch the machine's assigned status
// fetchMachineAssignedStatus(selectedMachine, iotDeviceData.deviceType)
// .then((status) => {
// setIsAssigned(status); // Update assigned status
// setShow01(!status); // Show `show01` based on assigned status
// })
// .catch((error) => {
// console.error("Error fetching machines:", error);
// });
// }
// }, [selectedMachine, iotDeviceData.deviceType]);
return (
<React.Fragment>
<div
className={isToggled ? "inner-content p-3 expand-inner-content" : "inner-
content p-3"}
>
<Container>
<Breadcrumb>
<Breadcrumb.Item href="/pages/dashboard">
<FontAwesomeIcon icon={faHouseChimney} />
</Breadcrumb.Item>
<Breadcrumb.Item href="/pages/iot-device-list">
IOT Device List
</Breadcrumb.Item>
<Breadcrumb.Item active>
{iotDeviceData.id ? "Edit Device" : "Add Device"}
</Breadcrumb.Item>
</Breadcrumb>
<Row>
<Col lg={12}>
<div className="title">
<h2 className="mb-0">
{iotDeviceData.id ? "Edit Device" : "Add Device"}
</h2>
</div>
</Col>
</Row>
<div className="form-wrap">
<Form noValidate validated={validated} onSubmit={handleSubmit}>
<Row className="mb-3">
{/* Iot Device Name */}
{/* Iot Device Name */}
<Col md="4">
<Form.Group
className="form-group"
controlId="validationCustom01"
>
<Form.Label>Device Name</Form.Label>
<Form.Control
name="deviceName"
value={iotDeviceData.deviceName}
onChange={handleChange}
required
type="text"
placeholder="Enter Device Name"
pattern="^[A-Za-z\s]+$"
/>
<Form.Control.Feedback type="invalid">
Please enter a valid Machine Name.
</Form.Control.Feedback>
</Form.Group>
</Col>
<Col md="4">
<Form.Group className="form-group" controlId="machine">
<div className="card flex justify-content-center border-0">
<Form.Label>Machine</Form.Label>
<Dropdown
value={selectedMachine}
options={machineOptions.map((machine) => ({
label: machine.machineName,
value: machine.uuid,
}))}
onChange={handleMachineChange}
placeholder="Select Machine"
required
/>
</div>
</Form.Group>
</Col>
</Row>
<Modal centered show={show01} onHide={handleClose}
className="success-modal">
<Modal.Body>
<Modal.Title className="fs-6 text-black text-center">
<SealCheck
size={50}
className={isError ? "text-danger d-block mx-auto mb-2" :
"text-success d-block mx-auto mb-2"}
/>
The device is already assigned. Do you want to unassign the
previous device and continue with the current one?
</Modal.Title>
<div className="text-center">
<Button variant="secondary" onClick={handleClose}
className="me-2">
Cancel
</Button>
<Button variant="danger" onClick={handleConfirm}>
Unassign and Continue
</Button>
</div>
</Modal.Body>
</Modal>