From 849c61e20345c2c3311327f1eb13d82aff15d57e Mon Sep 17 00:00:00 2001 From: Hajar RAHMOUNI Date: Sun, 7 May 2023 14:02:17 +0200 Subject: [PATCH 1/4] Feat : layout page using css grid layouts --- src/App.css | 42 ------------------ src/index.css | 69 ----------------------------- src/index.module.css | 100 +++++++++++++++++++++++++++++++++++++++++++ src/main.jsx | 16 +++++-- src/routes/rout.jsx | 34 +++++++++++++++ 5 files changed, 147 insertions(+), 114 deletions(-) delete mode 100644 src/App.css delete mode 100644 src/index.css create mode 100644 src/index.module.css create mode 100644 src/routes/rout.jsx diff --git a/src/App.css b/src/App.css deleted file mode 100644 index b9d355d..0000000 --- a/src/App.css +++ /dev/null @@ -1,42 +0,0 @@ -#root { - max-width: 1280px; - margin: 0 auto; - padding: 2rem; - text-align: center; -} - -.logo { - height: 6em; - padding: 1.5em; - will-change: filter; - transition: filter 300ms; -} -.logo:hover { - filter: drop-shadow(0 0 2em #646cffaa); -} -.logo.react:hover { - filter: drop-shadow(0 0 2em #61dafbaa); -} - -@keyframes logo-spin { - from { - transform: rotate(0deg); - } - to { - transform: rotate(360deg); - } -} - -@media (prefers-reduced-motion: no-preference) { - a:nth-of-type(2) .logo { - animation: logo-spin infinite 20s linear; - } -} - -.card { - padding: 2em; -} - -.read-the-docs { - color: #888; -} diff --git a/src/index.css b/src/index.css deleted file mode 100644 index 2c3fac6..0000000 --- a/src/index.css +++ /dev/null @@ -1,69 +0,0 @@ -:root { - font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; - line-height: 1.5; - font-weight: 400; - - color-scheme: light dark; - color: rgba(255, 255, 255, 0.87); - background-color: #242424; - - font-synthesis: none; - text-rendering: optimizeLegibility; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; - -webkit-text-size-adjust: 100%; -} - -a { - font-weight: 500; - color: #646cff; - text-decoration: inherit; -} -a:hover { - color: #535bf2; -} - -body { - margin: 0; - display: flex; - place-items: center; - min-width: 320px; - min-height: 100vh; -} - -h1 { - font-size: 3.2em; - line-height: 1.1; -} - -button { - border-radius: 8px; - border: 1px solid transparent; - padding: 0.6em 1.2em; - font-size: 1em; - font-weight: 500; - font-family: inherit; - background-color: #1a1a1a; - cursor: pointer; - transition: border-color 0.25s; -} -button:hover { - border-color: #646cff; -} -button:focus, -button:focus-visible { - outline: 4px auto -webkit-focus-ring-color; -} - -@media (prefers-color-scheme: light) { - :root { - color: #213547; - background-color: #ffffff; - } - a:hover { - color: #747bff; - } - button { - background-color: #f9f9f9; - } -} diff --git a/src/index.module.css b/src/index.module.css new file mode 100644 index 0000000..5fbcbb7 --- /dev/null +++ b/src/index.module.css @@ -0,0 +1,100 @@ +html { + box-sizing: border-box; +} +*, +*:before, +*:after { + box-sizing: inherit; +} + +body { + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", + "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", + sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", + monospace; +} + +html, +body { + height: 100%; + margin: 0; + line-height: 1.5; + color: #121212; +} +.wrapper { + display: grid; + grid-template-columns: 20% auto; + grid-template-rows: 20% auto; + height: 100vh; + margin: 0; +} +.navbar { + height: 20%; + display: flex; + align-items: center; + justify-content: flex-start;; + background-color: #222; + padding: 0 20px; + color: white; +} + +.navbar a { + margin-right: 20px; + color: white; + text-decoration: none; + font-size: 20px; +} +.navbar a:last-child { + margin-right: 0; +} +.navbar a:active { + color: rgb(62, 104, 217); +} +.header { + grid-column: 1 / 3; + display: flex; + flex-direction: column; + justify-content: flex-end; + background-color: #555; +} + +.sidebar { + grid-row: 2 / 3; +} +.sidebar h1{ + margin-left: 10%; +} +.listSensors { + padding-left: 20%; +} + +nav ul { + list-style: none; + margin: 0; + padding: 0; +} + +nav li a { + display: block; + padding: 10px; + color: black; + text-decoration: none; +} + +nav li a:hover { + background-color: #6b4d4d; +} + +.details { + display: flex; + justify-content: center; + margin-top: 20%; + grid-row: 2 / 3; + grid-column: 2 / 3; +} diff --git a/src/main.jsx b/src/main.jsx index 5cc5991..24242dd 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -1,10 +1,20 @@ import React from 'react' import ReactDOM from 'react-dom/client' -import App from './App' -import './index.css' +import Root from "./routes/rout"; +import { + createBrowserRouter, + RouterProvider, +} from "react-router-dom"; + +const router = createBrowserRouter([ + { + path: "/", + element: , + }, +]); ReactDOM.createRoot(document.getElementById('root')).render( - + , ) diff --git a/src/routes/rout.jsx b/src/routes/rout.jsx new file mode 100644 index 0000000..28c4dcf --- /dev/null +++ b/src/routes/rout.jsx @@ -0,0 +1,34 @@ +import styles from "../index.module.css"; +export default function Root() { + return ( + <> +
+
+
+ Real time sensors + Other + Foo +
+
+
+

Sensors

+
+ +
+
+
+ Hello +
+
+ + ); + } \ No newline at end of file -- GitLab From acb2e6dde34a8a2501b18f2365406120b129989d Mon Sep 17 00:00:00 2001 From: Hajar RAHMOUNI Date: Mon, 8 May 2023 20:00:54 +0200 Subject: [PATCH 2/4] Feat : route sensor added --- src/main.jsx | 12 ++++- src/routes/rout.jsx | 123 +++++++++++++++++++++++++++++++++----------- 2 files changed, 102 insertions(+), 33 deletions(-) diff --git a/src/main.jsx b/src/main.jsx index 24242dd..e75a2bb 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -1,15 +1,23 @@ -import React from 'react' -import ReactDOM from 'react-dom/client' +import React from 'react'; +import ReactDOM from 'react-dom/client'; +import Sensor from "./routes/sensor"; import Root from "./routes/rout"; import { createBrowserRouter, RouterProvider, } from "react-router-dom"; + const router = createBrowserRouter([ { path: "/", element: , + children: [ + { + path: "/:sensorName", + element: , + }, + ], }, ]); diff --git a/src/routes/rout.jsx b/src/routes/rout.jsx index 28c4dcf..9537193 100644 --- a/src/routes/rout.jsx +++ b/src/routes/rout.jsx @@ -1,34 +1,95 @@ import styles from "../index.module.css"; +import React, { useState, useEffect } from 'react'; +import { Outlet, Link, useLoaderData } from "react-router-dom"; +import mqtt from 'precompiled-mqtt'; export default function Root() { - return ( - <> -
- -
-

Sensors

-
- -
-
-
- Hello -
+ const [sensors, setSensors] = useState([]); + const [brokerUrl, setBrokerUrl] = useState(''); + + const topic = 'value/#'; + const client = mqtt.connect(brokerUrl); + + useEffect(() => { + 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()); + setSensors((prevSensors) => { + const existingSensor = prevSensors.find((sensor) => sensor.id === sensorData.id); + if (existingSensor) { + return prevSensors.map((sensor) => (sensor.id === sensorData.id ? sensorData : sensor)); + } else { + return [...prevSensors, sensorData]; + } + }); + console.log(`Received sensor data from topic ${topic}:`, sensorData); + }); + + return () => { + client.end(); + }; + }, [brokerUrl]); // effect will run whenever brokerUrl changes + + const handleUrlChange = (e) => { + const url = e.target.value; + setBrokerUrl(url); + }; + + return ( + <> +
+ - - ); - } \ No newline at end of file +
+

Sensors

+
+ +
+
+
+

URL :

+ +
+
+ +
+
+ + ); +} -- GitLab From 6f4e17e3aa56f062efae9a4b9ad94aac80042b8d Mon Sep 17 00:00:00 2001 From: Hajar RAHMOUNI Date: Mon, 8 May 2023 20:29:43 +0200 Subject: [PATCH 3/4] sensor details component --- src/index.module.css | 16 +++++++++++++++- src/routes/sensor.jsx | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/routes/sensor.jsx diff --git a/src/index.module.css b/src/index.module.css index 5fbcbb7..ff4614c 100644 --- a/src/index.module.css +++ b/src/index.module.css @@ -43,7 +43,21 @@ body { padding: 0 20px; color: white; } - +.divUrl { + grid-column: 2 / 3; + grid-row: 2/ 3; + margin-left: 20%; +} +h4, input[type="text"] { + display: inline-block; + vertical-align: middle; +} +h4 { + margin-right: 10px; /* add some margin between the elements */ +} +.urlInput { + width: 50%; +} .navbar a { margin-right: 20px; color: white; diff --git a/src/routes/sensor.jsx b/src/routes/sensor.jsx new file mode 100644 index 0000000..c23e53f --- /dev/null +++ b/src/routes/sensor.jsx @@ -0,0 +1,23 @@ +import styles from "../index.module.css"; + +export default function Sensor(){ + const sensor = { + name: "sensor name", + type: "PERCENT", + value: 0.2, + }; + + return( +
+

{sensor.name}

+
+ {sensor.value} +
+

Historique

+
+ +
+
+ ); + +} \ No newline at end of file -- GitLab From 9de7efa92e3e3575162ae9585fb5352a9a3d3d32 Mon Sep 17 00:00:00 2001 From: Hajar RAHMOUNI Date: Mon, 8 May 2023 21:04:16 +0200 Subject: [PATCH 4/4] Fix : view sensor updated --- src/index.module.css | 3 ++- src/routes/sensor.jsx | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/index.module.css b/src/index.module.css index ff4614c..a85e609 100644 --- a/src/index.module.css +++ b/src/index.module.css @@ -108,7 +108,8 @@ nav li a:hover { .details { display: flex; justify-content: center; - margin-top: 20%; + text-align: center; + margin-top: 5%; grid-row: 2 / 3; grid-column: 2 / 3; } diff --git a/src/routes/sensor.jsx b/src/routes/sensor.jsx index c23e53f..acfed8a 100644 --- a/src/routes/sensor.jsx +++ b/src/routes/sensor.jsx @@ -2,7 +2,7 @@ import styles from "../index.module.css"; export default function Sensor(){ const sensor = { - name: "sensor name", + name: "Exemple sensor name", type: "PERCENT", value: 0.2, }; @@ -10,6 +10,7 @@ export default function Sensor(){ return(

{sensor.name}

+

Valeur actuelle

{sensor.value}
-- GitLab