Skip to content

Commit

Permalink
알람 센싱 백 업뎃
Browse files Browse the repository at this point in the history
  • Loading branch information
jooeun921 committed Mar 3, 2024
1 parent 9891762 commit 1dd23a4
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 20 deletions.
Binary file added javascript-version/public/images/misc/ImgMap.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions javascript-version/src/pages/api/alarm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// src/pages/api/alarm.js

import axios from 'axios';

export default async function handler(req, res) {
try {

if (req.method === 'GET') {
// GET 요청: 센서 데이터를 조회하여 클라이언트에 응답
const response = await axios.get('http://localhost:8089/api/alarm');
const alarm_t = response.data;
res.status(200).json({ alarm_t });
} else if (req.method === 'POST') {
// POST 요청: 클라이언트로부터 받은 센서 정보를 데이터베이스에 추가
const { alarm_type, alarm_time, alarm_read } = req.body;
await axios.post('http://localhost:8089/api/alarm',
{
alarm_type: alarm_type,
alarm_time: alarm_time,
alarm_read: alarm_read,
});
console.log('데이터가 성공적으로 전송되었습니다.');
} else {
res.status(405).json({ message: 'Method Not Allowed' });
}
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Internal Server Error' });
}
}
32 changes: 32 additions & 0 deletions javascript-version/src/pages/api/sensing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// src/pages/api/sensing.js
//목표온도 계산하려고

import axios from 'axios';

export default async function handler(req, res) {
try {

if (req.method === 'GET') {
// GET 요청: 센서 데이터를 조회하여 클라이언트에 응답
const response = await axios.get('http://localhost:8089/api/sensing');
const sensing_t = response.data;
res.status(200).json({ sensing_t });
} else if (req.method === 'POST') {
// POST 요청: 클라이언트로부터 받은 센서 정보를 데이터베이스에 추가
const { sensing_time, sensing_goal, sensing_data, curing_date } = req.body;
await axios.post('http://localhost:8089/api/sensing',
{
sensing_time: sensing_time,
sensing_goal: sensing_goal,
sensing_data: sensing_data,
curing_date: curing_date,
});
console.log('데이터가 성공적으로 전송되었습니다.');
} else {
res.status(405).json({ message: 'Method Not Allowed' });
}
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Internal Server Error' });
}
}
2 changes: 1 addition & 1 deletion javascript-version/src/pages/api/site.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// src/pages/api/data_sensor.js
// src/pages/api/site.js

import axios from 'axios';

Expand Down
12 changes: 0 additions & 12 deletions javascript-version/src/pages/site-info/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,11 @@ const AccountSettings = () => {
</Box>
}
/>
<Tab
value='info'
label={
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<InformationOutline />
<TabName>Info</TabName>
</Box>
}
/>
</TabList>

<TabPanel sx={{ p: 0 }} value='account'>
<SiteAccount />
</TabPanel>
<TabPanel sx={{ p: 0 }} value='info'>
<SiteInfo />
</TabPanel>
</TabContext>
</Card>
)
Expand Down
79 changes: 73 additions & 6 deletions javascript-version/src/views/account-settings/SiteAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ const ImgStyled = styled('img')(({ theme }) => ({
borderRadius: theme.shape.borderRadius
}))

const ImgStyledMap = styled('img')(({ theme }) => ({
width: 1000,
height: 600,
marginRight: theme.spacing(6.25),
borderRadius: theme.shape.borderRadius
}))

const ButtonStyled = styled(Button)(({ theme }) => ({
[theme.breakpoints.down('sm')]: {
width: '100%',
Expand All @@ -50,14 +57,37 @@ const TabAccount = () => {
// ** State
const [openAlert, setOpenAlert] = useState(true)
const [imgSrc, setImgSrc] = useState('/images/misc/free-icon-building-6017722.png')
const onChange = file => {
const reader = new FileReader()
const { files } = file.target
const [site_img, setImgMap] = useState('/images/misc/ImgMap.jpg')
const onChange = async (file) => {
const reader = new FileReader();
const { files } = file.target;

if (files && files.length !== 0) {
reader.onload = () => setImgSrc(reader.result)
reader.readAsDataURL(files[0])
reader.onload = async () => {
setImgMap(reader.result);

const formData = new FormData();
formData.append('file', files[0]);

try {
const response = await axios.post('/api/site', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});

if (response.status === 200) {
console.log('Image uploaded successfully:', response.data);
// Update the database with the image URL or any other necessary action
}
} catch (error) {
console.error('Error uploading image:', error);
}
};
reader.readAsDataURL(files[0]);
}
}
};


const [site_t, setSiteT] = useState([]);

Expand All @@ -76,6 +106,17 @@ const TabAccount = () => {
} catch (error) {
console.error('Error fetching data:', error);
}

// try {
// const response = await axios.post('/api/site', {
// site_img: site_img,
// });
// console.log('데이터가 성공적으로 전송되었습니다.');
// // 성공적으로 데이터를 전송한 후에 수행할 로직 추가
// } catch (error) {
// console.error('데이터 전송 중 오류가 발생했습니다:', error);
// // 데이터 전송 중 오류가 발생했을 때의 처리 로직 추가
// }
};
fetchData();
}, []);
Expand Down Expand Up @@ -103,6 +144,32 @@ const TabAccount = () => {
<Grid item xs={12} sm={6}>
<TextField fullWidth label='종료일' placeholder={site_t[0]?.site_end} defaultValue={site_t[0]?.site_end} />
</Grid>
<Grid item xs={12} sm={6}>
<Box>
<ButtonStyled component='label' variant='contained' htmlFor='account-settings-upload-image'>
설계도 등록
<input
hidden
type='file'
onChange={onChange}
accept='image/png, image/jpeg'
id='account-settings-upload-image'
/>
</ButtonStyled>
<ResetButtonStyled color='error' variant='outlined' onClick={() => setImgMap(reader.result)}>
Reset
</ResetButtonStyled>
<Typography variant='body2' sx={{ marginTop: 3 }}>
Allowed PNG or JPEG. Max size of 800K.
</Typography>
</Box>
</Grid>
<Grid item xs={12}>
{/* 이미지 출력 */}
<Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<ImgStyledMap src={site_img} alt='Profile Pic' />
</Box>
</Grid>
</Grid>
</CardContent>
)
Expand Down
2 changes: 1 addition & 1 deletion javascript-version/src/views/account-settings/SiteInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const TabInfo = () => {
</RadioGroup>
</FormControl>
</Grid>
<Grid item xs={12} sm={6}>
<Grid>
<Box>
<ButtonStyled component='label' variant='contained' htmlFor='account-settings-upload-image'>
Upload New Photo
Expand Down

0 comments on commit 1dd23a4

Please sign in to comment.