-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
periodically update the status of the system
- Loading branch information
1 parent
8ac7906
commit cc98fdb
Showing
4 changed files
with
69 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { updateSystemStatus } from '../store/slices/SystemStatus'; | ||
import { useWaterPumpAPI } from './WaterPumpAPIContext'; | ||
import { useNotificationsSystem } from './NotificationsContext'; | ||
|
||
const FETCH_INTERVAL = 5000; | ||
const CHECK_INTERVAL = Math.round(FETCH_INTERVAL / 10); | ||
|
||
function WaterPumpStatusProviderComoponent({ children, updateStatus, systemStatus }) { | ||
const { API } = useWaterPumpAPI(); | ||
const NotificationsSystem = useNotificationsSystem(); | ||
const nextFetchTime = React.useRef(0); | ||
|
||
// Function to fetch water pump status | ||
const fetchStatus = React.useCallback(async () => { | ||
const now = Date.now(); | ||
if(now < nextFetchTime.current) return; | ||
if(null == API) return; | ||
|
||
nextFetchTime.current = Number.MAX_SAFE_INTEGER; // prevent concurrent fetches | ||
try { | ||
const status = await API.status(); | ||
updateStatus(status); | ||
} catch (error) { | ||
NotificationsSystem.alert('Error fetching system status: ' + error.message); | ||
updateStatus(null); | ||
} | ||
nextFetchTime.current = Date.now() + FETCH_INTERVAL; | ||
}, | ||
[API, NotificationsSystem, updateStatus, nextFetchTime] | ||
); | ||
|
||
// Effect to start fetching periodically and when API changes | ||
React.useEffect(() => { | ||
const timer = setInterval(fetchStatus, CHECK_INTERVAL); | ||
return () => { clearInterval(timer); }; | ||
}, [fetchStatus]); | ||
|
||
// Effect to reset timer when system status changes | ||
React.useEffect(() => { | ||
// reset timer if not fetching | ||
const now = Date.now(); | ||
if(now < nextFetchTime.current) { | ||
nextFetchTime.current = 0; | ||
} | ||
}, [API, systemStatus, nextFetchTime]); | ||
|
||
return ( | ||
<React.Fragment> | ||
{children} | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default connect( | ||
(state) => ({ | ||
systemStatus: state.systemStatus | ||
}), { | ||
updateStatus: updateSystemStatus | ||
|
||
} | ||
)(WaterPumpStatusProviderComoponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters