import React, {useEffect, useState} from 'react';
import { useForm } from "react-hook-form";
import './App.css';
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faSync, faSave } from "@fortawesome/free-solid-svg-icons";
import Amplify, {API} from 'aws-amplify';
import awsconfig from './aws-exports';
import * as mutations from './graphql/mutations';
import * as queries from './graphql/queries';
import ddblogo from './images/Amazon-DynamoDB_lgt.png'
import toast from "light-toast";
import { yupResolver } from '@hookform/resolvers/yup';
import * as yup from "yup";
import {
ResponsiveContainer, Tooltip, XAxis, YAxis, CartesianGrid, LineChart, Line,
} from 'recharts';
Amplify.configure(awsconfig);
function Windspeed() {
const [mylist, setMyList] = useState([]);
const [myIndex, setMyIndex] = useState();
const [mySortedlist, setMySortedList] = useState([]);
const [loop, setLoop] = useState(10);
const [lower, setLower] = useState(25);
const [upper, setUpper] = useState(70);
const [animation, setAnimation]= useState("false")
useEffect(() => {
updateVals();
}, []);
const updateVals = async() =>{
const result = await API.graphql({
query: queries.listWindspeeds,
fetchPolicy: "no-cache",
});
setMyList(result.data.listWindspeeds.items);
setMyIndex(result.data.listWindspeeds.items.length);
};
async function genValues () {
for (let i = 0, j=myIndex; i < loop; i++) {
const generateValues = {
index: i+j,
deviceID: "device" + (Math.floor(Math.random() * 5) + 1),
value: Math.floor(Math.random() * (upper-lower)) + lower
};
await API.graphql({ query: mutations.createWindspeed, variables: { input: generateValues}});
setMyIndex(myIndex+1);
}
}
function generate () {
genValues();
toast.success(`Generating ${loop} values between ${lower} and ${upper}`, 1000, () => {
updateVals();
});
}
const refresh = () => {
toast.success(`Fetcing values from DynamoDB`, 1000, () => {
updateVals();
});
}
const myChartArray = [];
const generateKeyValePairs = (arraytoupdated) => {
mylist.map((item) => {
var objNeeded = { name: 'init', value: -1};
objNeeded['name'] = item.index;
objNeeded['value'] = item.value;
arraytoupdated.push(objNeeded);
});
}
generateKeyValePairs(myChartArray);
function CustomizedLabel(props) {
const { x, y, stroke, value } = props;
if (value > 55 || value < 30) {
return (
Every wind turbine has a range of wind speeds, typically around 30 to 55 mph. Speed other than this range is detected anomalous{" "} (Source)