import { useState } from 'react'
import viteLogo from '/vite.svg'
import './App.css'
import mqtt from "precompiled-mqtt";

function App() {
  const [count, setCount] = useState(0)
  const topic = "value/#";
  const brokerUrl = 'wss://random.pigne.org:443';
  const sensorDataUrl = '/sensors_data.json';
  const sensors = {};
  const client  = mqtt.connect(brokerUrl);
  
  client.on('connect', () => {
    console.log(`Connected to MQTT broker ${brokerUrl}`);
    client.subscribe(topic, (err) => {
      if (err) {
        console.error('Error subscribing to topic', err);
      } else {
        console.log(`Subscribed to topic ${topic}`);
      }
    });
  });
  
  client.on('message', (topic, message) => {
    const sensorData = JSON.parse(message.toString());
    console.log(`Received sensor data from topic ${topic}:`, sensorData);
    // Update state with sensor data here
  });
  

  //client.on('message', (topic,value) => {console.log(topic, JSON.parse(value.toString()))})
  return (
    <div className="App">
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src={viteLogo} className="logo" alt="Vite logo" />
        </a>
      </div>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
        <p>
          Edit <code>src/App.jsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </div>
  )
}

export default App
