This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mqtt): mqtt topic & add provider
- Loading branch information
Showing
13 changed files
with
123 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use client"; | ||
|
||
import { MqttProvider, useMqttSubscription } from "@ubus/mqtt"; | ||
|
||
interface BusLocationPageProps { | ||
params: { | ||
bussId: string; | ||
}; | ||
} | ||
|
||
const BusLocationPage = (props: BusLocationPageProps) => { | ||
return ( | ||
<MqttProvider> | ||
<BusLocationContent {...props} /> | ||
</MqttProvider> | ||
); | ||
}; | ||
|
||
const BusLocationContent = (props: BusLocationPageProps) => { | ||
const { data: location, error } = useMqttSubscription(props.params.bussId); | ||
|
||
// if (isLoading) return <p>Loading...</p>; | ||
if (error) | ||
return ( | ||
<p className="grid h-dvh place-content-center place-items-center gap-4 text-destructive"> | ||
An error has occurred: {error.message} | ||
</p> | ||
); | ||
|
||
return ( | ||
<div className="grid h-dvh place-content-center place-items-center gap-4"> | ||
<h1>Bus Location</h1> | ||
<p> | ||
{location ? `Current Location: ${location}` : "Waiting for location..."} | ||
</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default BusLocationPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
import type { MqttClient } from "mqtt"; | ||
import type { IClientOptions, MqttClient } from "mqtt"; | ||
import mqtt from "mqtt"; | ||
|
||
import { MQTT_HOST, MqttOptions } from "./mqtt.config"; | ||
import { env } from "./env"; | ||
|
||
export const mqttClient: MqttClient = mqtt.connect(MQTT_HOST, MqttOptions); | ||
export const MqttOptions: IClientOptions = { | ||
clientId: `mqtt_${Math.random().toString(16).slice(3)}`, | ||
username: env.NEXT_PUBLIC_MQTT_USERNAME, | ||
password: env.NEXT_PUBLIC_MQTT_PASSWORD, | ||
clean: true, | ||
connectTimeout: 4000, | ||
reconnectPeriod: 1000, | ||
}; | ||
|
||
export const mqttClient: MqttClient = mqtt.connect( | ||
env.NEXT_PUBLIC_MQTT_HOST, | ||
MqttOptions, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createEnv } from "@t3-oss/env-nextjs"; | ||
import { z } from "zod"; | ||
|
||
export const env = createEnv({ | ||
server: { | ||
NODE_ENV: z.enum(["development", "production"]).optional(), | ||
}, | ||
client: { | ||
NEXT_PUBLIC_MQTT_HOST: z.string().min(1), | ||
NEXT_PUBLIC_MQTT_USERNAME: z.string().min(1), | ||
NEXT_PUBLIC_MQTT_PASSWORD: z.string().min(1), | ||
NEXT_PUBLIC_MQTT_TOPIC: z.string().min(1), | ||
}, | ||
experimental__runtimeEnv: { | ||
NEXT_PUBLIC_MQTT_HOST: process.env.NEXT_PUBLIC_MQTT_HOST, | ||
NEXT_PUBLIC_MQTT_USERNAME: process.env.NEXT_PUBLIC_MQTT_USERNAME, | ||
NEXT_PUBLIC_MQTT_PASSWORD: process.env.NEXT_PUBLIC_MQTT_PASSWORD, | ||
NEXT_PUBLIC_MQTT_TOPIC: process.env.NEXT_PUBLIC_MQTT_TOPIC, | ||
}, | ||
skipValidation: | ||
!!process.env.CI || process.env.npm_lifecycle_event === "lint", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./client"; | ||
export * from "./provider"; | ||
export * from "./service"; | ||
export * from "./topics"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from "react"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
|
||
interface MqttProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
export const MqttProvider = (props: MqttProviderProps) => { | ||
return React.createElement( | ||
QueryClientProvider, | ||
{ client: queryClient }, | ||
props.children, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export const getBusLocationTopic = (id: string) => `buses/${id}/location`; | ||
import { env } from "./env"; | ||
|
||
export const getBusLocationTopic = (busId: string) => | ||
`${env.NEXT_PUBLIC_MQTT_TOPIC}/${busId}`; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters