-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: send MQ message when device is created/updated/deleted * feature: send MQ message when device is created/updated/deleted * chore: bump npm packages
- Loading branch information
1 parent
bcbbd5f
commit b794609
Showing
10 changed files
with
448 additions
and
240 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"sensepro-admin": patch | ||
--- | ||
|
||
feature: send MQ message when device is created/updated/deleted |
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,5 @@ | ||
--- | ||
"sensepro-admin": patch | ||
--- | ||
|
||
chore: bump npm packages |
Large diffs are not rendered by default.
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
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
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,64 @@ | ||
"use server"; | ||
|
||
import "server-only"; | ||
|
||
import { auth } from "@/lib/auth"; | ||
import { headers } from "next/headers"; | ||
import { redirect } from "next/navigation"; | ||
import { getDeviceTypes } from "@/data-access/deviceType"; | ||
import { getCustomer } from "@/data-access/customer"; | ||
import { getDevicesByLocationId } from "@/data-access/device"; | ||
import { getRulesByLocationId } from "@/data-access/rule"; | ||
import { getLocation } from "@/data-access/location"; | ||
|
||
export async function getLocationPageData(locationId: string) { | ||
const session = await auth.api.getSession({ | ||
headers: await headers(), | ||
}); | ||
|
||
if (!session) { | ||
return redirect("/"); | ||
} | ||
|
||
const location = await getLocation(locationId); | ||
|
||
// TODO improve this | ||
const deviceTypes = await getDeviceTypes(); | ||
const customer = await getCustomer(location!.customerId!); | ||
const devices = await getDevicesByLocationId(locationId); | ||
const rules = await getRulesByLocationId(locationId); | ||
const deviceGroups = devices | ||
.filter((device) => device.deviceTypeId === "controller") | ||
.map((controller) => ({ | ||
controllerId: controller.id, | ||
controller, | ||
devices: devices.filter( | ||
(device) => device.controllerId === controller.id, | ||
), | ||
rules: rules | ||
.filter((rule) => rule.controllerId === controller.id) | ||
.map((rule) => ({ | ||
rule: { | ||
id: rule.id, | ||
name: rule.name, | ||
type: rule.type, | ||
controllerId: rule.controllerId, | ||
locationId: rule.locationId, | ||
}, | ||
devices: rule.devices | ||
.map((ruleDevice) => | ||
devices.find((device) => device.id === ruleDevice.deviceId), | ||
) | ||
.filter((device) => device !== undefined), | ||
selectedDevices: rule.devices.map((device) => device.deviceId), | ||
})), | ||
devicesAllowedInRules: devices.filter( | ||
(device) => | ||
device.controllerId === controller.id && | ||
deviceTypes.find( | ||
(deviceType) => deviceType.id === device.deviceTypeId, | ||
)?.allowInRules, | ||
), | ||
})); | ||
return { customer, deviceGroups, deviceTypes }; | ||
} |
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,32 @@ | ||
"use server"; | ||
|
||
import "server-only"; | ||
|
||
import amqp from "amqplib"; | ||
|
||
export async function publishMessage(queue: string, message: string) { | ||
let connection; | ||
let channel; | ||
|
||
try { | ||
// Step 1: Create a connection | ||
connection = await amqp.connect(process.env.RABBITMQ_URL as string); | ||
|
||
// Step 2: Create a channel | ||
channel = await connection.createChannel(); | ||
|
||
// Step 3: Assert the queue (ensures the queue exists) | ||
await channel.assertQueue(queue, { durable: true, maxLength: 1 }); | ||
|
||
// Step 4: Send the message to the queue | ||
channel.sendToQueue(queue, Buffer.from(message)); | ||
console.log(`Message sent to queue "${queue}"`); | ||
|
||
// Step 5: Close the channel and connection | ||
} catch (error) { | ||
console.error("Failed to send message:", error); | ||
} finally { | ||
if (channel) await channel.close(); | ||
if (connection) await connection.close(); | ||
} | ||
} |