All files DotBots.js

86.2% Statements 50/58
88.88% Branches 24/27
72.72% Functions 16/22
90.56% Lines 48/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                        1x 1x     1x   21x           1x   1x               1x 9x 9x 9x   9x 12x 4x     4x         9x 2x 2x 2x 1x         9x 2x 2x 1x   2x     9x 3x 3x 3x 3x 3x 3x         9x 1x 1x     9x 1x   1x 1x       9x 1x   1x       9x         9x 4x 1x       18x   9x                                     21x                                                                    
import React from "react";
import { useCallback, useEffect, useState } from "react";
import { RgbColorPicker } from "react-colorful";
import useWebSocket from 'react-use-websocket';
 
import { Joystick } from "./Joystick";
import {
  apiUpdateActiveDotbotAddress, apiFetchActiveDotbotAddress,
  apiFetchDotbots, apiUpdateRgbLed
} from "./rest";
 
 
const websocketUrl = `${process.env.REACT_APP_DOTBOTS_WS_URL}/controller/ws/status`;
const inactiveAddress = "0000000000000000";
 
 
const DotBotRow = (props) => {
 
  return (
    <tr>
      <td>{`${props.dotbot.address}`}</td>
      <td>
      {
        props.dotbot.address === props.activeDotbot ? (
          <button className="badge text-bg-success text-light border-0" onClick={() => props.controlsClicked(props.dotbot.address)}>active</button>
        ) : (
          <button className="badge text-bg-primary text-light border-0" onClick={() => props.controlsClicked(props.dotbot.address)}>activate</button>
        )
      }
      </td>
    </tr>
  )
}
 
const DotBots = () => {
  const [ dotbots, setDotbots ] = useState();
  const [ activeDotbot, setActiveDotbot ] = useState(inactiveAddress);
  const [ color, setColor ] = useState({ r: 0, g: 0, b: 0 });
 
  const updateColor = useCallback((data, address) => {
    const dotbot = data.filter(db => db.address === address)[0];
    Iif (dotbot && dotbot.rgb_led) {
      setColor({r: dotbot.rgb_led.red, g: dotbot.rgb_led.green, b: dotbot.rgb_led.blue,});
    } else {
      setColor({r: 0, g: 0, b: 0,});
    }
  }, [setColor]
  );
 
  const updateActive = useCallback(async (address) => {
    await apiUpdateActiveDotbotAddress(address).catch((error) => console.error(error));
    setActiveDotbot(address);
    if (dotbots && address !== inactiveAddress) {
      updateColor(dotbots, address);
    }
  }, [dotbots, setActiveDotbot, updateColor]
  );
 
  const switchActive = async (address) => {
    let newAddress = address;
    if (address === activeDotbot) {
      newAddress = inactiveAddress
    }
    await updateActive(newAddress);
  };
 
  const fetchDotBots = useCallback(async () => {
    const data = await apiFetchDotbots().catch(error => console.log(error));
    setDotbots(data);
    const active = await apiFetchActiveDotbotAddress().catch(error => console.log(error));
    setActiveDotbot(active);
    Eif (data && active !== inactiveAddress) {
      updateColor(data, active);
    }
  }, [setDotbots, updateColor]
  );
 
  const onWsOpen = () => {
    console.log('websocket opened');
    fetchDotBots();
  };
 
  const onWsMessage = (event) => {
    const message = JSON.parse(event.data);
 
    Eif (message.cmd === "reload") {
      fetchDotBots();
    }
  };
 
  useWebSocket(websocketUrl, {
    onOpen: () => onWsOpen(),
    onClose: () => console.log("websocket closed"),
    onMessage: (event) => onWsMessage(event),
    shouldReconnect: (event) => true,
  });
 
  const applyColor = async () => {
    await apiUpdateRgbLed(activeDotbot, color.r, color.g, color.b);
    await fetchDotBots();
  }
 
  useEffect(() => {
    if (!dotbots) {
      fetchDotBots();
    }
  }, [dotbots, fetchDotBots]);
 
  const controlsVisible = activeDotbot !== inactiveAddress && dotbots && dotbots.filter(dotbot => dotbot.address === activeDotbot).length > 0;
 
  return (
    <>
    <nav className="navbar navbar-expand-lg bg-dark">
      <div className="container-fluid">
        <a className="navbar-brand text-light" href="http://www.dotbots.org">DotBots</a>
      </div>
    </nav>
    <div className="container">
      <div className="card m-1">
        <div className="card-header">Available DotBots</div>
        <div className="card-body p-0">
          <table id="table" className="table table-striped align-middle">
            <thead>
              <tr>
                <th>Address</th>
                <th>Controls</th>
              </tr>
            </thead>
            <tbody>
            {dotbots && dotbots.map(dotbot => <DotBotRow key={dotbot.address} dotbot={dotbot} activeDotbot={activeDotbot} controlsClicked={switchActive}/>)}
            </tbody>
          </table>
        </div>
      </div>
      <div className={`card m-1 ${controlsVisible ? "visible" : "invisible"}`}>
        <div className="card-body">
          <div className="row">
            <div className="col d-flex justify-content-center">
              <Joystick address={activeDotbot} />
            </div>
            <div className="col m-2">
              <div className="row">
                <div className="col">
                  <div className="d-flex justify-content-center">
                    <RgbColorPicker color={color} onChange={setColor} />
                  </div>
                </div>
              </div>
              <div className="col m-2">
                <div className="d-flex justify-content-center">
                  <button className="btn btn-primary m-1" onClick={applyColor}>Apply color</button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    </>
  );
}
 
export default DotBots;