-
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.
- Loading branch information
1 parent
c292ebd
commit cb8f0e1
Showing
6 changed files
with
195 additions
and
11 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
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,37 @@ | ||
import type { RedisClientType, RedisModules, RedisFunctions, RedisScripts } from "redis"; | ||
import z from "zod"; | ||
|
||
export async function pubSub(redis: RedisClientType<RedisModules, RedisFunctions, RedisScripts>) { | ||
const sub = redis.duplicate(); | ||
await sub.connect(); | ||
const pub = redis.duplicate(); | ||
await pub.connect(); | ||
// return { sub, pub }; | ||
return { | ||
subscribe(channel: string, callback: (message: string, channel: string) => void) { | ||
sub.subscribe(channel, callback); | ||
}, | ||
publish(channel: string, message: string) { | ||
pub.publish(channel, message); | ||
} | ||
}; | ||
} | ||
|
||
export const PubSubToClientPacket = z.object({ | ||
type: z.literal("toClient"), | ||
serial: z.string(), | ||
data: z.any() | ||
}); | ||
|
||
export const PubSubToDevicePacket = z.object({ | ||
type: z.literal("toDevice"), | ||
serial: z.string(), | ||
data: z.any() | ||
}); | ||
|
||
export const PubSubDisconnectClientsPacket = z.object({ | ||
type: z.literal("disconnectClients"), | ||
serial: z.string() | ||
}); | ||
|
||
export const PubSubPacket = z.union([PubSubToClientPacket, PubSubToDevicePacket, PubSubDisconnectClientsPacket]); |
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,76 @@ | ||
import { adjectives, animals, uniqueNamesGenerator } from "unique-names-generator"; | ||
import { createClient } from "redis"; | ||
import { pubSub } from "./pubsub"; | ||
import { z } from "zod"; | ||
import { deleteDevice, disconnectClients, sendPacketToClients, sendPacketToDevice } from "../devices"; | ||
|
||
export const SERVERNAME = uniqueNamesGenerator({ | ||
dictionaries: [ | ||
adjectives, animals | ||
], | ||
separator: "-" | ||
}); | ||
console.log("I am " + SERVERNAME); | ||
|
||
export const redis = createClient({ | ||
url: "redis://redis:6379" | ||
}); | ||
await redis.connect(); | ||
await ensureKeys(); | ||
await ping(); | ||
|
||
export const { subscribe, publish } = await pubSub(redis); | ||
|
||
async function ensureKeys() { | ||
if(!await redis.exists("newsletter")) { | ||
await redis.json.set("newsletter", "$", []); | ||
} | ||
if(!await redis.exists("clients")) { | ||
await redis.json.set("clients", "$", []); | ||
} | ||
} | ||
|
||
async function ping() { | ||
await redis.json.set("server:" + SERVERNAME, "$", { lastPing: Date.now() }); | ||
await redis.expire("server:" + SERVERNAME, 70); | ||
} | ||
|
||
setInterval(() => { | ||
ping(); | ||
}, 60000) | ||
|
||
const PubSubToDevicePacket = z.object({ | ||
type: z.literal("toDevice"), | ||
serial: z.string(), | ||
packet: z.any() | ||
}); | ||
|
||
const PubSubToClientPacket = z.object({ | ||
type: z.literal("toClient"), | ||
serial: z.string(), | ||
packet: z.any() | ||
}); | ||
|
||
const PubSubDisconnectClientsPacket = z.object({ | ||
type: z.literal("disconnectClients"), | ||
serial: z.string() | ||
}); | ||
|
||
const PubSubPacket = z.union([PubSubToDevicePacket, PubSubToClientPacket, PubSubDisconnectClientsPacket]); | ||
|
||
subscribe(SERVERNAME, async (message, channel) => { | ||
console.log("[PUBSUB] " + message); | ||
|
||
const packet = PubSubPacket.parse(JSON.parse(message)); | ||
if(PubSubToDevicePacket.safeParse(packet).success) { | ||
const { serial, packet: data } = PubSubToDevicePacket.parse(packet); | ||
sendPacketToDevice(serial, data); | ||
} else if(PubSubToClientPacket.safeParse(packet).success) { | ||
const { serial, packet: data } = PubSubToClientPacket.parse(packet); | ||
sendPacketToClients(serial, data); | ||
} else if(PubSubDisconnectClientsPacket.safeParse(packet).success) { | ||
const { serial } = PubSubDisconnectClientsPacket.parse(packet); | ||
disconnectClients(serial); | ||
deleteDevice(serial); | ||
} | ||
}); |