|
| 1 | +import React, { useState, useRef, useEffect } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +//import ChartistGraph from '../../node_modules/react-chartist/index.js'; |
| 4 | +import moment from 'moment' |
| 5 | +import {testGood} from '../DataParsers.js' |
| 6 | +import {upperCredit, irvine} from '../rivers.js' |
| 7 | + |
| 8 | + |
| 9 | +function useInterval(callback, delay) { |
| 10 | + const savedCallback = useRef(); |
| 11 | + |
| 12 | + // Remember the latest callback. |
| 13 | + useEffect(() => { |
| 14 | + savedCallback.current = callback; |
| 15 | + }, [callback]); |
| 16 | + |
| 17 | + // Set up the interval. |
| 18 | + useEffect(() => { |
| 19 | + function tick() { |
| 20 | + savedCallback.current(); |
| 21 | + } |
| 22 | + if (delay !== null) { |
| 23 | + let id = setInterval(tick, delay); |
| 24 | + return () => clearInterval(id); |
| 25 | + } |
| 26 | + }, [delay]); |
| 27 | +} |
| 28 | +export function Counter (props) { |
| 29 | + let newClass |
| 30 | + switch (props.rating) { |
| 31 | + case "bad": newClass="error"; break; |
| 32 | + case "good": newClass="success"; break; |
| 33 | + case "shoulder": newClass="secondary" |
| 34 | + } |
| 35 | + return ( |
| 36 | + <button className={'button ' + newClass}> |
| 37 | + {props.name}: {props.level} as of {moment(props.time).format('HH:mm')} |
| 38 | + </button> |
| 39 | + ) |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +function processCurrentData (url) { |
| 44 | + return fetch(url) |
| 45 | + .then( response => response.json()) |
| 46 | + .then( json => { |
| 47 | + console.log('CURRENTDATA', json[0]); |
| 48 | + const d = json[0].data[0] |
| 49 | + return {time: d[0], level:d[1]}}) |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +export default function LevelCounters (props) { |
| 54 | + const [irv, setIrvine] = useState({}) |
| 55 | + const [credit, setCredit] = useState({}) |
| 56 | + // const [spencertown, setSpencertown] = useState({}) |
| 57 | + const cUrl = `https://waterinfo.cvc.ca/KiWIS/KiWIS?service=kisters&type=queryServices&&request=getTimeseriesValues&datasource=0&format=json&ts_id=14522010&dateformat=UNIX` |
| 58 | + const iUrl = `https://waterdata.grandriver.ca/KiWIS/KiWIS?service=kisters&type=queryServices&request=getTimeseriesValues&datasource=0&format=dajson&ts_id=8773042&dateformat=UNIX` |
| 59 | + //const sUrl = `https://waterinfo.cvc.ca/KiWIS/KiWIS?service=kisters&type=queryServices&&request=getTimeseriesValues&datasource=0&format=json&ts_id=14522010&dateformat=UNIX` |
| 60 | + |
| 61 | + function updateData () { |
| 62 | + processCurrentData (cUrl) |
| 63 | + .then( newData => setCredit(newData)) |
| 64 | + processCurrentData (iUrl) |
| 65 | + .then( newData => setIrvine(newData)) |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + useInterval( () => { |
| 70 | + updateData() |
| 71 | + }, 210000) |
| 72 | + |
| 73 | + return ( |
| 74 | + <div class="all-counters"> |
| 75 | + <Counter name="Irvine" |
| 76 | + level={Math.round(Number(irv.level))} |
| 77 | + time={irv.time} |
| 78 | + rating={testGood(irv.level, irvine)}/> |
| 79 | + <Counter name="Credit" level={credit.level} |
| 80 | + time={credit.time} |
| 81 | + rating={testGood(credit.level, upperCredit)}/> |
| 82 | + {/* <Counter name="Spencer, Town Section" level={spencertown.level} */} |
| 83 | + {/* time={spencertown.time} */} |
| 84 | + {/* rating={testGood(spencertown.level, upperCredit)}/> */} |
| 85 | + |
| 86 | + <button className = "button success" onClick={updateData}>Update</button> |
| 87 | + </div> |
| 88 | + ) |
| 89 | + |
| 90 | +} |
0 commit comments