From 73e68ca9ef48534c88da59562d02f6e27efcc616 Mon Sep 17 00:00:00 2001 From: Vite Date: Tue, 6 Aug 2024 21:30:30 -0500 Subject: [PATCH] feat: simulation time is now dynamic --- package.json | 1 + pnpm-lock.yaml | 323 ++++++++++++++++++++++ src/@types/global.d.ts | 3 +- src/@types/route.d.ts | 4 +- src/jobs/fakePub.ts | 155 ++++++----- src/network/mqtt/router.ts | 11 +- src/network/mqtt/routes/aq.ts | 1 + src/network/mqtt/routes/co2.ts | 1 + src/network/mqtt/routes/date.ts | 1 + src/network/mqtt/routes/humidity.ts | 1 + src/network/mqtt/routes/photo.ts | 1 + src/network/mqtt/routes/pm2.5.ts | 1 + src/network/mqtt/routes/pressure.ts | 1 + src/network/mqtt/routes/simulationTIme.ts | 43 +++ src/network/mqtt/routes/temperature.ts | 1 + src/network/mqtt/server.ts | 7 +- src/network/server.ts | 3 + 17 files changed, 474 insertions(+), 84 deletions(-) create mode 100644 src/network/mqtt/routes/simulationTIme.ts diff --git a/package.json b/package.json index d4c8f67..c22b139 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "dependencies": { "@prisma/client": "5.2.0", "debug": "4.3.4", + "fastify": "^4.28.1", "firebase-admin": "11.10.1", "mqtt": "5.0.4", "node-cron": "3.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f5a0478..0c7b6da 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: debug: specifier: 4.3.4 version: 4.3.4 + fastify: + specifier: ^4.28.1 + version: 4.28.1 firebase-admin: specifier: 11.10.1 version: 11.10.1 @@ -300,10 +303,22 @@ packages: resolution: {integrity: sha512-ZSjtmelB7IJfWD2Fvb7+Z+ChTIKWq6kjda95fLcQKNS5aheVHn4IkfgRQE3sIIzTcSLwLcLZUD9UBt+V7+h+Pw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + '@fastify/ajv-compiler@3.6.0': + resolution: {integrity: sha512-LwdXQJjmMD+GwLOkP7TVC68qa+pSSogeWWmznRJ/coyTcfe9qA05AHFSe1eZFwK6q+xVRpChnvFUkf1iYaSZsQ==} + '@fastify/busboy@1.2.1': resolution: {integrity: sha512-7PQA7EH43S0CxcOa9OeAnaeA0oQ+e/DHNPZwSQM9CQHW76jle5+OvLdibRp/Aafs9KXbLhxyjOTkRjWUbQEd3Q==} engines: {node: '>=14'} + '@fastify/error@3.4.1': + resolution: {integrity: sha512-wWSvph+29GR783IhmvdwWnN4bUxTD01Vm5Xad4i7i1VuAOItLvbPAb69sb0IQ2N57yprvhNIwAP5B6xfKTmjmQ==} + + '@fastify/fast-json-stringify-compiler@4.3.0': + resolution: {integrity: sha512-aZAXGYo6m22Fk1zZzEUKBvut/CIIQe/BapEORnxiD5Qr0kPHqqI69NtEMCme74h+at72sPhbkb4ZrLd1W3KRLA==} + + '@fastify/merge-json-schemas@0.1.1': + resolution: {integrity: sha512-fERDVz7topgNjtXsJTTW1JKLy0rhuLRcquYqNR9rF7OcVpCa2OVW49ZPDIhaRRCaUuvVxI+N416xUoF76HNSXA==} + '@firebase/app-types@0.9.0': resolution: {integrity: sha512-AeweANOIo0Mb8GiYm3xhTEBVCmPwTYAu9Hcd2qSkLuga/6+j9b1Jskl5bpiSQWy9eJ/j5pavxj6eYogmnuzm+Q==} @@ -833,6 +848,9 @@ packages: resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==} engines: {node: '>=6.5'} + abstract-logging@2.0.1: + resolution: {integrity: sha512-2BjRTZxTPvheOvGbBslFSYOUkr+SjPtOnrLP33f+VIWLzezQpZcqVg7ja3L4dBXmzzgwT+a029jRx5PCi3JuiA==} + accepts@1.3.8: resolution: {integrity: sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==} engines: {node: '>= 0.6'} @@ -860,6 +878,22 @@ packages: resolution: {integrity: sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==} engines: {node: '>= 6.0.0'} + ajv-formats@2.1.1: + resolution: {integrity: sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==} + peerDependencies: + ajv: ^8.0.0 + peerDependenciesMeta: + ajv: + optional: true + + ajv-formats@3.0.1: + resolution: {integrity: sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==} + peerDependencies: + ajv: ^8.0.0 + peerDependenciesMeta: + ajv: + optional: true + ajv-keywords@3.5.2: resolution: {integrity: sha512-5p6WTN0DdTGVQk6VjcEju19IgaHudalcfabD7yhDGeA6bcQnmL+CpveLJq/3hvfwd1aof6L386Ougkx6RfyMIQ==} peerDependencies: @@ -868,6 +902,9 @@ packages: ajv@6.12.6: resolution: {integrity: sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==} + ajv@8.17.1: + resolution: {integrity: sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==} + ansi-escapes@4.3.2: resolution: {integrity: sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ==} engines: {node: '>=8'} @@ -936,10 +973,17 @@ packages: async-retry@1.3.3: resolution: {integrity: sha512-wfr/jstw9xNi/0teMHrRW7dsz3Lt5ARhYNZ2ewpadnhaIp5mbALhOAP+EAdsC7t4Z6wqsDVv9+W6gm1Dk9mEyw==} + atomic-sleep@1.0.0: + resolution: {integrity: sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==} + engines: {node: '>=8.0.0'} + available-typed-arrays@1.0.7: resolution: {integrity: sha512-wvUjBtSGN7+7SjNpq/9M2Tg350UZD3q62IFZLbRAR1bSMlCo1ZaeW+BJ+D090e4hIIZLBcTDWe4Mh4jvUDajzQ==} engines: {node: '>= 0.4'} + avvio@8.3.2: + resolution: {integrity: sha512-st8e519GWHa/azv8S87mcJvZs4WsgTBjOw/Ih1CP6u+8SZvcOeAYNG6JbsIrAUUJJ7JfmrnOkR8ipDS+u9SIRQ==} + babel-jest@29.7.0: resolution: {integrity: sha512-BrvGY3xZSwEcCzKvKsCi2GgHqDqsYkOP4/by5xCgIwGXQxIEh+8ew3gmrE1y7XRR6LHZIj6yLYnUi/mm2KXKBg==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -1120,6 +1164,10 @@ packages: resolution: {integrity: sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==} engines: {node: '>= 0.6'} + cookie@0.6.0: + resolution: {integrity: sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==} + engines: {node: '>= 0.6'} + cors@2.8.5: resolution: {integrity: sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==} engines: {node: '>= 0.10'} @@ -1498,6 +1546,12 @@ packages: extend@3.0.2: resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==} + fast-content-type-parse@1.1.0: + resolution: {integrity: sha512-fBHHqSTFLVnR61C+gltJuE5GkVQMV0S2nqUO8TJ+5Z3qAKG8vAx4FKai1s5jq/inV1+sREynIWSuQ6HgoSXpDQ==} + + fast-decode-uri-component@1.0.1: + resolution: {integrity: sha512-WKgKWg5eUxvRZGwW8FvfbaH7AXSh2cL+3j5fMGzUMCxWBJ3dV3a7Wz8y2f/uQ0e3B6WmodD3oS54jTQ9HVTIIg==} + fast-deep-equal@3.1.3: resolution: {integrity: sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==} @@ -1511,16 +1565,35 @@ packages: fast-json-stable-stringify@2.1.0: resolution: {integrity: sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==} + fast-json-stringify@5.16.1: + resolution: {integrity: sha512-KAdnLvy1yu/XrRtP+LJnxbBGrhN+xXu+gt3EUvZhYGKCr3lFHq/7UFJHHFgmJKoqlh6B40bZLEv7w46B0mqn1g==} + fast-levenshtein@2.0.6: resolution: {integrity: sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==} + fast-querystring@1.1.2: + resolution: {integrity: sha512-g6KuKWmFXc0fID8WWH0jit4g0AGBoJhCkJMb1RmbsSEUNvQ+ZC8D6CUZ+GtF8nMzSPXnhiePyyqqipzNNEnHjg==} + + fast-redact@3.5.0: + resolution: {integrity: sha512-dwsoQlS7h9hMeYUq1W++23NDcBLV4KqONnITDV9DjfS3q1SgDGVrBdvvTLUotWtPSD7asWDV9/CmsZPy8Hf70A==} + engines: {node: '>=6'} + fast-text-encoding@1.0.6: resolution: {integrity: sha512-VhXlQgj9ioXCqGstD37E/HBeqEGV/qOD/kmbVG8h5xKBYvM1L3lR1Zn4555cQ8GkYbJa8aJSipLPndE1k6zK2w==} + fast-uri@2.4.0: + resolution: {integrity: sha512-ypuAmmMKInk5q7XcepxlnUWDLWv4GFtaJqAzWKqn62IpQ3pejtr5dTVbt3vwqVaMKmkNR55sTT+CqUKIaT21BA==} + + fast-uri@3.0.1: + resolution: {integrity: sha512-MWipKbbYiYI0UC7cl8m/i/IWTqfC8YXsqjzybjddLsFjStroQzsHXkc73JutMvBiXmOvapk+axIl79ig5t55Bw==} + fast-xml-parser@4.4.1: resolution: {integrity: sha512-xkjOecfnKGkSsOwtZ5Pz7Us/T6mrbPQrq0nh+aCO5V9nk5NLWmasAHumTKjiPJPWANe+kAZ84Jc8ooJkzZ88Sw==} hasBin: true + fastify@4.28.1: + resolution: {integrity: sha512-kFWUtpNr4i7t5vY2EJPCN2KgMVpuqfU4NjnJNCgiNB900oiDeYqaNDRcAfeBbOF5hGixixxcKnOU4KN9z6QncQ==} + fastq@1.17.1: resolution: {integrity: sha512-sRVD3lWVIXWg6By68ZN7vho9a1pQcN/WBFaAAsDDFzlJjvoGx0P8z7V1t72grFJfJhu3YPZBuu25f7Kaw2jN1w==} @@ -1539,6 +1612,10 @@ packages: resolution: {integrity: sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==} engines: {node: '>=8'} + find-my-way@8.2.0: + resolution: {integrity: sha512-HdWXgFYc6b1BJcOBDBwjqWuHJj1WYiqrxSh25qtU4DabpMFdj/gSunNBQb83t+8Zt67D7CXEzJWTkxaShMTMOA==} + engines: {node: '>=14'} + find-up@4.1.0: resolution: {integrity: sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==} engines: {node: '>=8'} @@ -1561,6 +1638,10 @@ packages: for-each@0.3.3: resolution: {integrity: sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==} + forwarded@0.2.0: + resolution: {integrity: sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==} + engines: {node: '>= 0.6'} + fs.realpath@1.0.0: resolution: {integrity: sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==} @@ -1767,6 +1848,10 @@ packages: resolution: {integrity: sha512-NGnrKwXzSms2qUUih/ILZ5JBqNTSa1+ZmP6flaIp6KmSElgE9qdndzS3cqjrDovwFdmwsGsLdeFgB6suw+1e9g==} engines: {node: '>= 0.4'} + ipaddr.js@1.9.1: + resolution: {integrity: sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==} + engines: {node: '>= 0.10'} + is-array-buffer@3.0.4: resolution: {integrity: sha512-wcjaerHw0ydZwfhiKbXJWLDY8A7yV7KhjQOpb83hGgGfId/aQa4TOvwyzn2PuswW2gPCYEL/nEAiSVpdOj1lXw==} engines: {node: '>= 0.4'} @@ -2078,9 +2163,15 @@ packages: json-parse-even-better-errors@2.3.1: resolution: {integrity: sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==} + json-schema-ref-resolver@1.0.1: + resolution: {integrity: sha512-EJAj1pgHc1hxF6vo2Z3s69fMjO1INq6eGHXZ8Z6wCQeldCuwxGK9Sxf4/cScGn3FZubCVUehfWtcDM/PLteCQw==} + json-schema-traverse@0.4.1: resolution: {integrity: sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==} + json-schema-traverse@1.0.0: + resolution: {integrity: sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==} + json-stable-stringify-without-jsonify@1.0.1: resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==} @@ -2135,6 +2226,9 @@ packages: resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==} engines: {node: '>= 0.8.0'} + light-my-request@5.13.0: + resolution: {integrity: sha512-9IjUN9ZyCS9pTG+KqTDEQo68Sui2lHsYBrfMyVUTTZ3XhH8PMZq7xO94Kr+eP9dhi/kcKsx4N41p2IXEBil1pQ==} + limiter@1.1.5: resolution: {integrity: sha512-FWWMIEOxz3GwUI4Ts/IvgVy6LPvoMPgjMdQ185nN6psJyBJ4yOpzqm695/h5umdLJg2vW3GR5iG11MAkR2AzJA==} @@ -2379,6 +2473,10 @@ packages: resolution: {integrity: sha512-yBYjY9QX2hnRmZHAjG/f13MzmBzxzYgQhFrke06TTyKY5zSTEqkOeukBzIdVA3j3ulu8Qa3MbVFShV7T2RmGtQ==} engines: {node: '>= 0.4'} + on-exit-leak-free@2.1.2: + resolution: {integrity: sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA==} + engines: {node: '>=14.0.0'} + once@1.4.0: resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==} @@ -2448,6 +2546,16 @@ packages: resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==} engines: {node: '>=8.6'} + pino-abstract-transport@1.2.0: + resolution: {integrity: sha512-Guhh8EZfPCfH+PMXAb6rKOjGQEoy0xlAIn+irODG5kgfYV+BQ0rGYYWTIel3P5mmyXqkYkPmdIkywsn6QKUR1Q==} + + pino-std-serializers@7.0.0: + resolution: {integrity: sha512-e906FRY0+tV27iq4juKzSYPbUj2do2X2JX4EzSca1631EB2QJQUqGbDuERal7LCtOpxl6x3+nvo9NPZcmjkiFA==} + + pino@9.3.2: + resolution: {integrity: sha512-WtARBjgZ7LNEkrGWxMBN/jvlFiE17LTbBoH0konmBU684Kd0uIiDwBXlcTCW7iJnA6HfIKwUssS/2AC6cDEanw==} + hasBin: true + pirates@4.0.6: resolution: {integrity: sha512-saLsH7WeYYPiD25LDuLRRY/i+6HaPYr6G1OUlN39otzkSTxKnubR9RTxS3/Kk50s1g2JTgFwWQDQyplC5/SHZg==} engines: {node: '>= 6'} @@ -2489,6 +2597,12 @@ packages: process-nextick-args@2.0.1: resolution: {integrity: sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==} + process-warning@3.0.0: + resolution: {integrity: sha512-mqn0kFRl0EoqhnL0GQ0veqFHyIN1yig9RHh/InzORTUiZHFRAur+aMtRkELNwGs9aNwKS6tg/An4NYBPGwvtzQ==} + + process-warning@4.0.0: + resolution: {integrity: sha512-/MyYDxttz7DfGMMHiysAsFE4qF+pQYAA8ziO/3NcRVrQ5fSk+Mns4QZA/oRPFzvcqNoVJXQNWNAsdwBXLUkQKw==} + process@0.11.10: resolution: {integrity: sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==} engines: {node: '>= 0.6.0'} @@ -2516,6 +2630,10 @@ packages: resolution: {integrity: sha512-RXyHaACeqXeqAKGLDl68rQKbmObRsTIn4TYVUUug1KfS47YWCo5MacGITEryugIgZqORCvJWEk4l449POg5Txg==} engines: {node: '>=12.0.0'} + proxy-addr@2.0.7: + resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==} + engines: {node: '>= 0.10'} + pstree.remy@1.1.8: resolution: {integrity: sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==} @@ -2536,6 +2654,9 @@ packages: queue-microtask@1.2.3: resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==} + quick-format-unescaped@4.0.4: + resolution: {integrity: sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==} + randombytes@2.1.0: resolution: {integrity: sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==} @@ -2554,6 +2675,10 @@ packages: resolution: {integrity: sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==} engines: {node: '>=8.10.0'} + real-require@0.2.0: + resolution: {integrity: sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg==} + engines: {node: '>= 12.13.0'} + reflect-metadata@0.1.13: resolution: {integrity: sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==} @@ -2572,6 +2697,10 @@ packages: resolution: {integrity: sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==} engines: {node: '>=0.10.0'} + require-from-string@2.0.2: + resolution: {integrity: sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==} + engines: {node: '>=0.10.0'} + requizzle@0.2.4: resolution: {integrity: sha512-JRrFk1D4OQ4SqovXOgdav+K8EAhSB/LJZqCz8tbX0KObcdeM15Ss59ozWMBWmmINMagCwmqn4ZNryUGpBsl6Jw==} @@ -2595,6 +2724,10 @@ packages: resolution: {integrity: sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==} hasBin: true + ret@0.4.3: + resolution: {integrity: sha512-0f4Memo5QP7WQyUEAYUO3esD/XjOc3Zjjg5CPsAq1p8sIu0XPeMbHJemKA0BO7tV0X7+A0FoEpbmHXWxPyD3wQ==} + engines: {node: '>=10'} + retry-request@5.0.2: resolution: {integrity: sha512-wfI3pk7EE80lCIXprqh7ym48IHYdwmAAzESdbU8Q9l7pnRCk9LEhpbOTNKjz6FARLm/Bl5m+4F0ABxOkYUujSQ==} engines: {node: '>=12'} @@ -2632,10 +2765,20 @@ packages: resolution: {integrity: sha512-CdASjNJPvRa7roO6Ra/gLYBTzYzzPyyBXxIMdGW3USQLyjWEls2RgW5UBTXaQVp+OrpeCK3bLem8smtmheoRuw==} engines: {node: '>= 0.4'} + safe-regex2@3.1.0: + resolution: {integrity: sha512-RAAZAGbap2kBfbVhvmnTFv73NWLMvDGOITFYTZBAaY8eR+Ir4ef7Up/e7amo+y1+AH+3PtLkrt9mvcTsG9LXug==} + + safe-stable-stringify@2.4.3: + resolution: {integrity: sha512-e2bDA2WJT0wxseVd4lsDP4+3ONX6HpMXQa1ZhFQ7SU+GjvORCmShbCMltrtIDfkYhVHrOcPtj+KhmDBdPdZD1g==} + engines: {node: '>=10'} + schema-utils@3.3.0: resolution: {integrity: sha512-pN/yOAvcC+5rQ5nERGuwrjLlYvLTbCibnZ1I7B1LaiAz9BRBlE9GMgE/eqV30P7aJQUf7Ddimy/RsbYO/GrVGg==} engines: {node: '>= 10.13.0'} + secure-json-parse@2.7.0: + resolution: {integrity: sha512-6aU+Rwsezw7VR8/nyvKTx8QpWH9FrcYiXXlqC4z5d5XQBDRqtbfsRjnwGyqbi3gddNtWHuEk9OANUotL26qKUw==} + semver@6.3.1: resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==} hasBin: true @@ -2648,6 +2791,9 @@ packages: serialize-javascript@6.0.2: resolution: {integrity: sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==} + set-cookie-parser@2.7.0: + resolution: {integrity: sha512-lXLOiqpkUumhRdFF3k1osNXCy9akgx/dyPZ5p8qAg9seJzXr5ZrlqZuWIMuY6ejOsVLE6flJ5/h3lsn57fQ/PQ==} + set-function-length@1.2.2: resolution: {integrity: sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==} engines: {node: '>= 0.4'} @@ -2693,6 +2839,9 @@ packages: resolution: {integrity: sha512-bvKVS29/I5fl2FGLNHuXlQaUH/BlzX1IN6S+NKLNZpBsPZIDH+90eQmCs2Railn4YUiww4SzUedJ6+uzwFnKLw==} engines: {node: '>=10.2.0'} + sonic-boom@4.0.1: + resolution: {integrity: sha512-hTSD/6JMLyT4r9zeof6UtuBDpjJ9sO08/nmS5djaA9eozT9oOlNdpXSnzcgj4FTqpk3nkLrs61l4gip9r1HCrQ==} + source-map-support@0.5.13: resolution: {integrity: sha512-SHSKFHadjVA5oR4PPqhtAVdcBWwRYVd6g6cAXnIbRiIwc2EhPrTuKUBdSLvlEKyIP3GCf89fltvcZiP9MMFA1w==} @@ -2827,6 +2976,9 @@ packages: text-table@0.2.0: resolution: {integrity: sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==} + thread-stream@3.1.0: + resolution: {integrity: sha512-OqyPZ9u96VohAyMfJykzmivOrY2wfMSf3C5TtFJVgN+Hm6aj+voFhlK+kZEIv2FBh1X6Xp3DlnCOfEQ3B2J86A==} + tmp@0.2.3: resolution: {integrity: sha512-nZD7m9iCPC5g0pYmcaxogYKggSfLsdxl8of3Q/oIbqCqLLIO9IAF0GWjX1z9NZRHPiXv8Wex4yDCaZsgEw0Y8w==} engines: {node: '>=14.14'} @@ -2842,6 +2994,10 @@ packages: resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==} engines: {node: '>=8.0'} + toad-cache@3.7.0: + resolution: {integrity: sha512-/m8M+2BJUpoJdgAHoG+baCwBT+tf2VraSfkBgl0Y00qIWt41DJ8R5B8nsEw0I58YwF5IZH6z24/2TobDKnqSWw==} + engines: {node: '>=12'} + touch@3.1.1: resolution: {integrity: sha512-r0eojU4bI8MnHr8c5bNo7lJDdI2qXlWWJk6a9EAFG7vbhTjElYhBVS3/miuE0uOuoLdb8Mc/rVfsmm6eo5o9GA==} hasBin: true @@ -3355,10 +3511,26 @@ snapshots: '@eslint/js@8.48.0': {} + '@fastify/ajv-compiler@3.6.0': + dependencies: + ajv: 8.17.1 + ajv-formats: 2.1.1(ajv@8.17.1) + fast-uri: 2.4.0 + '@fastify/busboy@1.2.1': dependencies: text-decoding: 1.0.0 + '@fastify/error@3.4.1': {} + + '@fastify/fast-json-stringify-compiler@4.3.0': + dependencies: + fast-json-stringify: 5.16.1 + + '@fastify/merge-json-schemas@0.1.1': + dependencies: + fast-deep-equal: 3.1.3 + '@firebase/app-types@0.9.0': {} '@firebase/auth-interop-types@0.2.1': {} @@ -4134,6 +4306,8 @@ snapshots: dependencies: event-target-shim: 5.0.1 + abstract-logging@2.0.1: {} + accepts@1.3.8: dependencies: mime-types: 2.1.35 @@ -4160,6 +4334,14 @@ snapshots: - supports-color optional: true + ajv-formats@2.1.1(ajv@8.17.1): + optionalDependencies: + ajv: 8.17.1 + + ajv-formats@3.0.1(ajv@8.17.1): + optionalDependencies: + ajv: 8.17.1 + ajv-keywords@3.5.2(ajv@6.12.6): dependencies: ajv: 6.12.6 @@ -4171,6 +4353,13 @@ snapshots: json-schema-traverse: 0.4.1 uri-js: 4.4.1 + ajv@8.17.1: + dependencies: + fast-deep-equal: 3.1.3 + fast-uri: 3.0.1 + json-schema-traverse: 1.0.0 + require-from-string: 2.0.2 + ansi-escapes@4.3.2: dependencies: type-fest: 0.21.3 @@ -4258,10 +4447,17 @@ snapshots: retry: 0.13.1 optional: true + atomic-sleep@1.0.0: {} + available-typed-arrays@1.0.7: dependencies: possible-typed-array-names: 1.0.0 + avvio@8.3.2: + dependencies: + '@fastify/error': 3.4.1 + fastq: 1.17.1 + babel-jest@29.7.0(@babel/core@7.25.2): dependencies: '@babel/core': 7.25.2 @@ -4471,6 +4667,8 @@ snapshots: cookie@0.4.2: {} + cookie@0.6.0: {} + cors@2.8.5: dependencies: object-assign: 4.1.1 @@ -4949,6 +5147,10 @@ snapshots: extend@3.0.2: optional: true + fast-content-type-parse@1.1.0: {} + + fast-decode-uri-component@1.0.1: {} + fast-deep-equal@3.1.3: {} fast-diff@1.3.0: {} @@ -4963,16 +5165,55 @@ snapshots: fast-json-stable-stringify@2.1.0: {} + fast-json-stringify@5.16.1: + dependencies: + '@fastify/merge-json-schemas': 0.1.1 + ajv: 8.17.1 + ajv-formats: 3.0.1(ajv@8.17.1) + fast-deep-equal: 3.1.3 + fast-uri: 2.4.0 + json-schema-ref-resolver: 1.0.1 + rfdc: 1.4.1 + fast-levenshtein@2.0.6: {} + fast-querystring@1.1.2: + dependencies: + fast-decode-uri-component: 1.0.1 + + fast-redact@3.5.0: {} + fast-text-encoding@1.0.6: optional: true + fast-uri@2.4.0: {} + + fast-uri@3.0.1: {} + fast-xml-parser@4.4.1: dependencies: strnum: 1.0.5 optional: true + fastify@4.28.1: + dependencies: + '@fastify/ajv-compiler': 3.6.0 + '@fastify/error': 3.4.1 + '@fastify/fast-json-stringify-compiler': 4.3.0 + abstract-logging: 2.0.1 + avvio: 8.3.2 + fast-content-type-parse: 1.1.0 + fast-json-stringify: 5.16.1 + find-my-way: 8.2.0 + light-my-request: 5.13.0 + pino: 9.3.2 + process-warning: 3.0.0 + proxy-addr: 2.0.7 + rfdc: 1.4.1 + secure-json-parse: 2.7.0 + semver: 7.6.3 + toad-cache: 3.7.0 + fastq@1.17.1: dependencies: reusify: 1.0.4 @@ -4993,6 +5234,12 @@ snapshots: dependencies: to-regex-range: 5.0.1 + find-my-way@8.2.0: + dependencies: + fast-deep-equal: 3.1.3 + fast-querystring: 1.1.2 + safe-regex2: 3.1.0 + find-up@4.1.0: dependencies: locate-path: 5.0.0 @@ -5032,6 +5279,8 @@ snapshots: dependencies: is-callable: 1.2.7 + forwarded@0.2.0: {} + fs.realpath@1.0.0: {} fsevents@2.3.3: @@ -5284,6 +5533,8 @@ snapshots: hasown: 2.0.2 side-channel: 1.0.6 + ipaddr.js@1.9.1: {} + is-array-buffer@3.0.4: dependencies: call-bind: 1.0.7 @@ -5790,8 +6041,14 @@ snapshots: json-parse-even-better-errors@2.3.1: {} + json-schema-ref-resolver@1.0.1: + dependencies: + fast-deep-equal: 3.1.3 + json-schema-traverse@0.4.1: {} + json-schema-traverse@1.0.0: {} + json-stable-stringify-without-jsonify@1.0.1: {} json5@1.0.2: @@ -5872,6 +6129,12 @@ snapshots: prelude-ls: 1.2.1 type-check: 0.4.0 + light-my-request@5.13.0: + dependencies: + cookie: 0.6.0 + process-warning: 3.0.0 + set-cookie-parser: 2.7.0 + limiter@1.1.5: {} lines-and-columns@1.2.4: {} @@ -6120,6 +6383,8 @@ snapshots: define-properties: 1.2.1 es-object-atoms: 1.0.0 + on-exit-leak-free@2.1.2: {} + once@1.4.0: dependencies: wrappy: 1.0.2 @@ -6190,6 +6455,27 @@ snapshots: picomatch@2.3.1: {} + pino-abstract-transport@1.2.0: + dependencies: + readable-stream: 4.5.2 + split2: 4.2.0 + + pino-std-serializers@7.0.0: {} + + pino@9.3.2: + dependencies: + atomic-sleep: 1.0.0 + fast-redact: 3.5.0 + on-exit-leak-free: 2.1.2 + pino-abstract-transport: 1.2.0 + pino-std-serializers: 7.0.0 + process-warning: 4.0.0 + quick-format-unescaped: 4.0.4 + real-require: 0.2.0 + safe-stable-stringify: 2.4.3 + sonic-boom: 4.0.1 + thread-stream: 3.1.0 + pirates@4.0.6: {} pkg-dir@4.2.0: @@ -6221,6 +6507,10 @@ snapshots: process-nextick-args@2.0.1: {} + process-warning@3.0.0: {} + + process-warning@4.0.0: {} + process@0.11.10: {} prompts@2.4.2: @@ -6280,6 +6570,11 @@ snapshots: long: 5.2.3 optional: true + proxy-addr@2.0.7: + dependencies: + forwarded: 0.2.0 + ipaddr.js: 1.9.1 + pstree.remy@1.1.8: {} punycode.js@2.3.1: @@ -6294,6 +6589,8 @@ snapshots: queue-microtask@1.2.3: {} + quick-format-unescaped@4.0.4: {} + randombytes@2.1.0: dependencies: safe-buffer: 5.2.1 @@ -6318,6 +6615,8 @@ snapshots: dependencies: picomatch: 2.3.1 + real-require@0.2.0: {} + reflect-metadata@0.1.13: {} regexp.prototype.flags@1.5.2: @@ -6333,6 +6632,8 @@ snapshots: require-directory@2.1.1: {} + require-from-string@2.0.2: {} + requizzle@0.2.4: dependencies: lodash: 4.17.21 @@ -6354,6 +6655,8 @@ snapshots: path-parse: 1.0.7 supports-preserve-symlinks-flag: 1.0.0 + ret@0.4.3: {} + retry-request@5.0.2: dependencies: debug: 4.3.4 @@ -6394,12 +6697,20 @@ snapshots: es-errors: 1.3.0 is-regex: 1.1.4 + safe-regex2@3.1.0: + dependencies: + ret: 0.4.3 + + safe-stable-stringify@2.4.3: {} + schema-utils@3.3.0: dependencies: '@types/json-schema': 7.0.15 ajv: 6.12.6 ajv-keywords: 3.5.2(ajv@6.12.6) + secure-json-parse@2.7.0: {} + semver@6.3.1: {} semver@7.6.3: {} @@ -6408,6 +6719,8 @@ snapshots: dependencies: randombytes: 2.1.0 + set-cookie-parser@2.7.0: {} + set-function-length@1.2.2: dependencies: define-data-property: 1.1.4 @@ -6477,6 +6790,10 @@ snapshots: - supports-color - utf-8-validate + sonic-boom@4.0.1: + dependencies: + atomic-sleep: 1.0.0 + source-map-support@0.5.13: dependencies: buffer-from: 1.1.2 @@ -6615,6 +6932,10 @@ snapshots: text-table@0.2.0: {} + thread-stream@3.1.0: + dependencies: + real-require: 0.2.0 + tmp@0.2.3: optional: true @@ -6626,6 +6947,8 @@ snapshots: dependencies: is-number: 7.0.0 + toad-cache@3.7.0: {} + touch@3.1.1: {} tr46@0.0.3: diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts index bebacd7..8c348dc 100644 --- a/src/@types/global.d.ts +++ b/src/@types/global.d.ts @@ -1,6 +1,7 @@ +/* eslint-disable no-var */ declare global { - // eslint-disable-next-line no-var var __io__: import('socket.io').Server + var __SIMULATION_TIME__: number } export {} diff --git a/src/@types/route.d.ts b/src/@types/route.d.ts index bc48890..74e2c6e 100644 --- a/src/@types/route.d.ts +++ b/src/@types/route.d.ts @@ -1,4 +1,6 @@ interface Route { - sub: (client: import('mqtt').MqttClient) => void + type: 'mqtt' | 'http' + sub?: (client: import('mqtt').MqttClient) => void + init?: (app: FastifyInstance) => void SUB_TOPIC: string } diff --git a/src/jobs/fakePub.ts b/src/jobs/fakePub.ts index 6224294..810e5e0 100644 --- a/src/jobs/fakePub.ts +++ b/src/jobs/fakePub.ts @@ -41,6 +41,8 @@ const clientPublish = ({ cb?.() } +global.__SIMULATION_TIME__ = 10 + const updateData = (client: MqttClient) => { const pubDebug = debug(`${MAIN_TOPIC}:Mqtt:demo:pub`) @@ -50,83 +52,86 @@ const updateData = (client: MqttClient) => { return } - cron.schedule('*/30 * * * * *', async (): Promise => { - pubDebug(`Job started at: ${new Date().toISOString()}`) + cron.schedule( + `*/${global.__SIMULATION_TIME__} * * * * *`, + async (): Promise => { + pubDebug(`Job started at: ${new Date().toISOString()}`) - const { id, moduleId, sensorId } = DEMO_CLIENT - const currentIsoTime = new Date().toISOString() + const { id, moduleId, sensorId } = DEMO_CLIENT + const currentIsoTime = new Date().toISOString() - pubDebug(`Publishing messages at: ${currentIsoTime}`) - clientPublish({ - client, - value: randomInInterval(200, 450), - id, - moduleId, - sensorId, - topic: 'aq', - cb: () => { - clientPublish({ - client, - value: parseFloat(randomInInterval(500, 800)), - id, - moduleId, - sensorId, - topic: 'co2', - cb: () => { - clientPublish({ - client, - value: randomInInterval(1, 20), - id, - moduleId, - sensorId, - topic: 'humidity', - cb: () => { - clientPublish({ - client, - value: randomInInterval(40, 70), - id, - moduleId, - sensorId, - topic: 'pm2.5', - cb: () => { - clientPublish({ - client, - value: 998, - id, - moduleId, - sensorId, - topic: 'pressure', - cb: () => { - clientPublish({ - client, - value: randomInInterval(23, 27, 1), - id, - moduleId, - sensorId, - topic: 'temperature', - cb: () => { - clientPublish({ - client, - value: currentIsoTime, - id, - moduleId, - sensorId, - topic: 'date', - demo: true - }) - } - }) - } - }) - } - }) - } - }) - } - }) - } - }) - }) + pubDebug(`Publishing messages at: ${currentIsoTime}`) + clientPublish({ + client, + value: randomInInterval(200, 450), + id, + moduleId, + sensorId, + topic: 'aq', + cb: () => { + clientPublish({ + client, + value: parseFloat(randomInInterval(500, 800)), + id, + moduleId, + sensorId, + topic: 'co2', + cb: () => { + clientPublish({ + client, + value: randomInInterval(1, 20), + id, + moduleId, + sensorId, + topic: 'humidity', + cb: () => { + clientPublish({ + client, + value: randomInInterval(40, 70), + id, + moduleId, + sensorId, + topic: 'pm2.5', + cb: () => { + clientPublish({ + client, + value: 998, + id, + moduleId, + sensorId, + topic: 'pressure', + cb: () => { + clientPublish({ + client, + value: randomInInterval(23, 27, 1), + id, + moduleId, + sensorId, + topic: 'temperature', + cb: () => { + clientPublish({ + client, + value: currentIsoTime, + id, + moduleId, + sensorId, + topic: 'date', + demo: true + }) + } + }) + } + }) + } + }) + } + }) + } + }) + } + }) + } + ) } export { updateData } diff --git a/src/network/mqtt/router.ts b/src/network/mqtt/router.ts index e06842b..ff3ec81 100644 --- a/src/network/mqtt/router.ts +++ b/src/network/mqtt/router.ts @@ -1,10 +1,13 @@ import { MqttClient } from 'mqtt' - +import { FastifyInstance } from 'fastify' import * as Routes from './routes' -const applyRoutes = (client: MqttClient) => { - ;(Object.keys(Routes) as (keyof typeof Routes)[]).forEach(route => { - Routes[route].sub(client) +const applyRoutes = (client: MqttClient, app: FastifyInstance) => { + ;(Object.keys(Routes) as (keyof typeof Routes)[]).forEach(routeKey => { + const route = Routes[routeKey] as Route + + if (route.type === 'mqtt' && route.sub) route.sub(client) + else if (route.type === 'http' && route.init) route.init(app) }) } diff --git a/src/network/mqtt/routes/aq.ts b/src/network/mqtt/routes/aq.ts index b732be0..d24b678 100644 --- a/src/network/mqtt/routes/aq.ts +++ b/src/network/mqtt/routes/aq.ts @@ -34,6 +34,7 @@ const sub = (client: MqttClient) => { } const aq: Route = { + type: 'mqtt', sub, SUB_TOPIC } diff --git a/src/network/mqtt/routes/co2.ts b/src/network/mqtt/routes/co2.ts index 59cf7e0..9830ccb 100644 --- a/src/network/mqtt/routes/co2.ts +++ b/src/network/mqtt/routes/co2.ts @@ -35,6 +35,7 @@ const sub = (client: MqttClient) => { } const co2: Route = { + type: 'mqtt', sub, SUB_TOPIC } diff --git a/src/network/mqtt/routes/date.ts b/src/network/mqtt/routes/date.ts index fcbd1be..1d68abb 100644 --- a/src/network/mqtt/routes/date.ts +++ b/src/network/mqtt/routes/date.ts @@ -45,6 +45,7 @@ const sub = (client: MqttClient) => { } const date: Route = { + type: 'mqtt', sub, SUB_TOPIC } diff --git a/src/network/mqtt/routes/humidity.ts b/src/network/mqtt/routes/humidity.ts index f924137..eb816ca 100644 --- a/src/network/mqtt/routes/humidity.ts +++ b/src/network/mqtt/routes/humidity.ts @@ -36,6 +36,7 @@ const sub = (client: MqttClient) => { } const humidity: Route = { + type: 'mqtt', sub, SUB_TOPIC } diff --git a/src/network/mqtt/routes/photo.ts b/src/network/mqtt/routes/photo.ts index f390e87..c752e07 100644 --- a/src/network/mqtt/routes/photo.ts +++ b/src/network/mqtt/routes/photo.ts @@ -67,6 +67,7 @@ const sub = (client: MqttClient) => { } const receivePhoto: Route = { + type: 'mqtt', sub, SUB_TOPIC } diff --git a/src/network/mqtt/routes/pm2.5.ts b/src/network/mqtt/routes/pm2.5.ts index acc7d98..bf27a73 100644 --- a/src/network/mqtt/routes/pm2.5.ts +++ b/src/network/mqtt/routes/pm2.5.ts @@ -36,6 +36,7 @@ const sub = (client: MqttClient) => { // eslint-disable-next-line camelcase const pm2_5: Route = { + type: 'mqtt', sub, SUB_TOPIC } diff --git a/src/network/mqtt/routes/pressure.ts b/src/network/mqtt/routes/pressure.ts index 310e1fb..aef0969 100644 --- a/src/network/mqtt/routes/pressure.ts +++ b/src/network/mqtt/routes/pressure.ts @@ -36,6 +36,7 @@ const sub = (client: MqttClient) => { } const pressure: Route = { + type: 'mqtt', sub, SUB_TOPIC } diff --git a/src/network/mqtt/routes/simulationTIme.ts b/src/network/mqtt/routes/simulationTIme.ts new file mode 100644 index 0000000..1e60bb4 --- /dev/null +++ b/src/network/mqtt/routes/simulationTIme.ts @@ -0,0 +1,43 @@ +import debug from 'debug' +import { FastifyInstance } from 'fastify' +import { MAIN_TOPIC } from 'utils' + +const TOPIC = 'simulationTIme' +const SUB_TOPIC = `${MAIN_TOPIC}/${TOPIC}` + +const updateSimulationTime = (app: FastifyInstance) => { + const simDebug = debug('simulation:time:update') + + app.post<{ Params: { time: number } }>( + '/simulationTime/:time', + async (req, reply) => { + const { + headers: { authorization }, + params: { time } + } = req + + if (authorization !== process.env.API_KEY) + return reply.status(401).send({ message: 'Unauthorized' }) + + if (isNaN(Number(time))) + return reply.status(400).send({ message: 'Time is not a number' }) + + global.__SIMULATION_TIME__ = time * 1000 + simDebug( + `Updated simulation time: ${new Date( + global.__SIMULATION_TIME__ + ).toISOString()}` + ) + + return { message: 'Simulation time updated', newTime: `${time}s` } + } + ) +} + +const simulationTime: Route = { + type: 'http', + init: updateSimulationTime, + SUB_TOPIC +} + +export { simulationTime } diff --git a/src/network/mqtt/routes/temperature.ts b/src/network/mqtt/routes/temperature.ts index 01c1427..2bd44e4 100644 --- a/src/network/mqtt/routes/temperature.ts +++ b/src/network/mqtt/routes/temperature.ts @@ -42,6 +42,7 @@ const sub = (client: MqttClient) => { } const temperature: Route = { + type: 'mqtt', sub, SUB_TOPIC } diff --git a/src/network/mqtt/server.ts b/src/network/mqtt/server.ts index 3b3162e..f196028 100644 --- a/src/network/mqtt/server.ts +++ b/src/network/mqtt/server.ts @@ -1,6 +1,6 @@ import { connect, type IClientOptions, type MqttClient } from 'mqtt' import { Debugger } from 'debug' - +import fastify from 'fastify' declare global { // eslint-disable-next-line no-var var __mqttClient__: MqttClient @@ -36,8 +36,9 @@ const getClient = (d?: Debugger) => { const start = async (d: Debugger) => { const { applyRoutes } = await import('./router') - - applyRoutes(getClient(d)) + const app = fastify() + // updateSimulationTime(app) + applyRoutes(getClient(d), app) } const stop = async () => { diff --git a/src/network/server.ts b/src/network/server.ts index c4a02a1..dc3616c 100644 --- a/src/network/server.ts +++ b/src/network/server.ts @@ -4,6 +4,7 @@ import { dbConnection, firebaseConnection } from 'database' import { MAIN_TOPIC } from 'utils' import { getClient, start as startMqtt } from './mqtt' import { socketConnection } from './socket' +import { updateData } from 'jobs/fakePub' const namespace = `${MAIN_TOPIC}:Mqtt:Server` const serverDebug = debug(namespace) @@ -15,6 +16,8 @@ const start = async () => { await dbConnection(serverDebug).connect() }) + updateData(getClient(serverDebug)) + if (process.env.NODE_ENV !== 'local') { const jobs = await import('../jobs')