Initial Commit
This commit is contained in:
commit
15cb1a03b6
130
.gitignore
vendored
Normal file
130
.gitignore
vendored
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
.pnpm-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
.stylelintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
.env
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.env.local
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
.cache/
|
||||||
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# vuepress v2.x temp and cache directory
|
||||||
|
.temp
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v2
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/unplugged
|
||||||
|
.yarn/build-state.yml
|
||||||
|
.yarn/install-state.gz
|
||||||
|
.pnp.*
|
||||||
21
app.js
Normal file
21
app.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
const log4js = require("log4js");
|
||||||
|
const os = require("os");
|
||||||
|
const config = require("./config.json");
|
||||||
|
|
||||||
|
const WebSocketServer = require("./src/websockets.js").WebSocketServer;
|
||||||
|
|
||||||
|
const logger = log4js.getLogger("App");
|
||||||
|
logger.level = config.logLevel;
|
||||||
|
|
||||||
|
function showSystemInfo() {
|
||||||
|
logger.info("=== System Info ===")
|
||||||
|
logger.info("Hostname:", os.hostname());
|
||||||
|
logger.info("Memory:", os.totalmem() / 1024 / 1024 + "mb");
|
||||||
|
console.log("");
|
||||||
|
}
|
||||||
|
|
||||||
|
showSystemInfo();
|
||||||
|
logger.info("Web Socket Server Starting...");
|
||||||
|
|
||||||
|
var server = new WebSocketServer();
|
||||||
|
server.start(5050);
|
||||||
5
config.json
Normal file
5
config.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"logLevel": "trace",
|
||||||
|
"jwt_secret": "secret123",
|
||||||
|
"mongo_uri": "mongodb://localhost:27017/socketio"
|
||||||
|
}
|
||||||
5246
package-lock.json
generated
Normal file
5246
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
package.json
Normal file
26
package.json
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"name": "farmcontrol-ws",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Farmcontrol Web Socket microservice",
|
||||||
|
"main": "app.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"start": "node app.js",
|
||||||
|
"dev": "nodemon app.js"
|
||||||
|
},
|
||||||
|
"author": "Tom Butcher",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.19.2",
|
||||||
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"log4js": "^6.9.1",
|
||||||
|
"mongodb": "^6.8.0",
|
||||||
|
"mongoose": "^8.5.1",
|
||||||
|
"socket.io": "^4.7.5",
|
||||||
|
"socket.io-adapter-mongo": "^2.0.5",
|
||||||
|
"socketio-jwt": "^4.6.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"standard": "^17.1.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
436
src/websockets.js
Normal file
436
src/websockets.js
Normal file
@ -0,0 +1,436 @@
|
|||||||
|
// WebSocketServer.js
|
||||||
|
|
||||||
|
const express = require("express");
|
||||||
|
const http = require("http");
|
||||||
|
const socketIo = require("socket.io");
|
||||||
|
const { MongoClient } = require("mongodb");
|
||||||
|
const socketioJwt = require("socketio-jwt");
|
||||||
|
const log4js = require("log4js");
|
||||||
|
const config = require("../config.json");
|
||||||
|
const { trace } = require("console");
|
||||||
|
|
||||||
|
const logger = log4js.getLogger("Web Sockets");
|
||||||
|
logger.level = config.logLevel;
|
||||||
|
|
||||||
|
class WebSocketServer {
|
||||||
|
constructor() {
|
||||||
|
this.app = express();
|
||||||
|
this.server = http.createServer(this.app);
|
||||||
|
this.io = socketIo(this.server, {
|
||||||
|
cors: {
|
||||||
|
origin: "*",
|
||||||
|
},
|
||||||
|
maxHttpBufferSize: 1e8
|
||||||
|
});
|
||||||
|
this.JWT_SECRET = process.env.JWT_SECRET || config.jwt_secret;
|
||||||
|
this.mongoUri = process.env.MONGO_URI || config.mongo_uri;
|
||||||
|
this.dbName = "farmcontrol"; // DB Name
|
||||||
|
|
||||||
|
this.hostSockets = [];
|
||||||
|
this.userSockets = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
async connectToMongo() {
|
||||||
|
try {
|
||||||
|
const client = new MongoClient(this.mongoUri, {});
|
||||||
|
await client.connect();
|
||||||
|
this.db = client.db(this.dbName);
|
||||||
|
logger.info("Connected to MongoDB");
|
||||||
|
this.clearHostsCollection();
|
||||||
|
} catch (err) {
|
||||||
|
logger.error("MongoDB connection error:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
configureMiddleware() {
|
||||||
|
// Middleware for JWT authentication
|
||||||
|
this.io.use(
|
||||||
|
socketioJwt.authorize({
|
||||||
|
secret: this.JWT_SECRET,
|
||||||
|
handshake: true,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
setupSocketIO() {
|
||||||
|
this.io.on("connection", (socket) => {
|
||||||
|
var connectionType = "user";
|
||||||
|
var hostId;
|
||||||
|
var id, email;
|
||||||
|
if (socket.decoded_token.hasOwnProperty("hostId")) {
|
||||||
|
var connectionType = "host";
|
||||||
|
hostId = socket.decoded_token.hostId;
|
||||||
|
logger.info("Host connected:", hostId);
|
||||||
|
this.hostSockets.push(socket.id);
|
||||||
|
this.handleHostOnlineEvent({ hostId });
|
||||||
|
} else {
|
||||||
|
id = socket.decoded_token.id;
|
||||||
|
email = socket.decoded_token.email;
|
||||||
|
logger.info("User connected:", id);
|
||||||
|
this.userSockets.push(socket.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.on("disconnect", () => {
|
||||||
|
if (connectionType == "host") {
|
||||||
|
logger.info("Host " + socket.decoded_token.hostId + " disconnected.");
|
||||||
|
this.hostSockets = this.hostSockets.filter(id => id !== socket.id);
|
||||||
|
this.handleHostOfflineEvent({ hostId: hostId });
|
||||||
|
} else {
|
||||||
|
logger.info("User " + socket.decoded_token.id + " disconnected.")
|
||||||
|
this.userSockets = this.userSockets.filter(id => id !== socket.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("status", (data) => {
|
||||||
|
logger.info("Setting", data.remoteAddress, "status to", data.status);
|
||||||
|
if (data.type == "printer") {
|
||||||
|
this.handlePrinterStatusEvent(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("online", (data) => {
|
||||||
|
logger.info("Setting", data.remoteAddress, "to online.");
|
||||||
|
if (data.type == "printer") {
|
||||||
|
this.handlePrinterOnlineEvent(data, socket);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("offline", (data) => {
|
||||||
|
logger.info("Setting", data.remoteAddress, "to offline.");
|
||||||
|
if (data.type == "printer") {
|
||||||
|
this.handlePrinterOfflineEvent(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("join", (data) => {
|
||||||
|
if (data.remoteAddress) {
|
||||||
|
logger.trace("Joining room", data.remoteAddress);
|
||||||
|
socket.join(data.remoteAddress);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("leave", (data) => {
|
||||||
|
if (data.remoteAddress) {
|
||||||
|
logger.trace("Leaving room", data.remoteAddress);
|
||||||
|
socket.leave(data.remoteAddress);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("temperature", (data) => {
|
||||||
|
if (connectionType == "host") {
|
||||||
|
socket.to(data.remoteAddress).emit("temperature", data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("command", (data) => {
|
||||||
|
if (connectionType == "user") {
|
||||||
|
socket.to(data.remoteAddress).emit(data.type, data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async clearHostsCollection() {
|
||||||
|
try {
|
||||||
|
if (!this.db) {
|
||||||
|
await this.connectToMongo();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete all documents from hosts collection
|
||||||
|
const deleteResult = await this.db.collection("hosts").deleteMany({});
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
`Deleted ${deleteResult.deletedCount} documents from hosts collection`
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error clearing hosts collection:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePrinterOnlineEvent(data, socket) {
|
||||||
|
try {
|
||||||
|
if (!this.db) {
|
||||||
|
await this.connectToMongo();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if data.remoteAddress exists in printers collection
|
||||||
|
const existingPrinter = await this.db
|
||||||
|
.collection("printers")
|
||||||
|
.findOne({ remoteAddress: data.remoteAddress });
|
||||||
|
|
||||||
|
if (existingPrinter) {
|
||||||
|
// If exists, update the document
|
||||||
|
const updateResult = await this.db.collection("printers").updateOne(
|
||||||
|
{ remoteAddress: data.remoteAddress },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
online: true,
|
||||||
|
status: { type: "Online" },
|
||||||
|
connectedAt: new Date(),
|
||||||
|
hostId: data.hostId, // Assuming hostId is passed as a parameter to handleIdentifyEvent
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (updateResult.modifiedCount > 0) {
|
||||||
|
logger.info(`Printer updated: ${data.remoteAddress}`);
|
||||||
|
} else {
|
||||||
|
logger.warn(`Printer not updated: ${data.remoteAddress}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If not exists, insert a new document
|
||||||
|
const insertData = {
|
||||||
|
remoteAddress: data.remoteAddress,
|
||||||
|
hostId: data.hostId,
|
||||||
|
online: true,
|
||||||
|
status: { type: "Online" },
|
||||||
|
connectedAt: new Date(),
|
||||||
|
friendlyName: "",
|
||||||
|
loadedFillament: null,
|
||||||
|
};
|
||||||
|
const insertResult = await this.db
|
||||||
|
.collection("printers")
|
||||||
|
.insertOne(insertData);
|
||||||
|
|
||||||
|
if (insertResult.insertedCount > 0) {
|
||||||
|
logger.info(`New printer added: ${data.remoteAddress}`);
|
||||||
|
} else {
|
||||||
|
logger.warn(`Failed to add new printer: ${data.remoteAddress}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onlineData = {
|
||||||
|
remoteAddress: data.remoteAddress,
|
||||||
|
hostId: data.hostId,
|
||||||
|
online: true,
|
||||||
|
status: { type: "Online" },
|
||||||
|
connectedAt: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.trace("Sending online data", onlineData)
|
||||||
|
this.sendStatusToUserSockets(onlineData);
|
||||||
|
|
||||||
|
// Join socket room using remoteAddress as name
|
||||||
|
//logger.trace("Joining room", data.remoteAddress);
|
||||||
|
logger.trace("Joining room", data.remoteAddress);
|
||||||
|
socket.join(data.remoteAddress);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error handling online event:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePrinterStatusEvent(data) {
|
||||||
|
try {
|
||||||
|
if (!this.db) {
|
||||||
|
await this.connectToMongo();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if data.remoteAddress exists in printers collection
|
||||||
|
const existingPrinter = await this.db
|
||||||
|
.collection("printers")
|
||||||
|
.findOne({ remoteAddress: data.remoteAddress });
|
||||||
|
|
||||||
|
if (existingPrinter) {
|
||||||
|
// If exists, update the document
|
||||||
|
const updateResult = await this.db.collection("printers").updateOne(
|
||||||
|
{ remoteAddress: data.remoteAddress },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
status: data.status,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (updateResult.modifiedCount > 0) {
|
||||||
|
logger.info(`Printer updated: ${data.remoteAddress}`);
|
||||||
|
} else {
|
||||||
|
logger.warn(`Printer not updated: ${data.remoteAddress}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const onlineData = {
|
||||||
|
remoteAddress: data.remoteAddress,
|
||||||
|
hostId: data.hostId,
|
||||||
|
online: true,
|
||||||
|
connectedAt: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.trace("Sending status data", onlineData)
|
||||||
|
this.sendStatusToUserSockets(data);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error handling status event:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async handlePrinterOfflineEvent(data) {
|
||||||
|
try {
|
||||||
|
if (!this.db) {
|
||||||
|
await this.connectToMongo();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if data.remoteAddress exists in printers collection
|
||||||
|
const existingPrinter = await this.db
|
||||||
|
.collection("printers")
|
||||||
|
.findOne({ remoteAddress: data.remoteAddress });
|
||||||
|
|
||||||
|
if (existingPrinter) {
|
||||||
|
// If exists, update the document
|
||||||
|
const updateResult = await this.db.collection("printers").updateOne(
|
||||||
|
{ remoteAddress: data.remoteAddress },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
online: false,
|
||||||
|
status: { type: "Offline" },
|
||||||
|
connectedAt: null,
|
||||||
|
hostId: data.hostId, // Assuming hostId is passed as a parameter to handleIdentifyEvent
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (updateResult.modifiedCount > 0) {
|
||||||
|
logger.info(`Printer updated: ${data.remoteAddress}`);
|
||||||
|
} else {
|
||||||
|
logger.warn(`Printer not updated: ${data.remoteAddress}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If not exists, insert a new document
|
||||||
|
const insertResult = await this.db.collection("printers").insertOne({
|
||||||
|
remoteAddress: data.remoteAddress,
|
||||||
|
hostId: data.hostId,
|
||||||
|
online: false,
|
||||||
|
status: { type: "Offline" },
|
||||||
|
connectedAt: null,
|
||||||
|
friendlyName: "",
|
||||||
|
loadedFillament: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (insertResult.insertedCount > 0) {
|
||||||
|
logger.info(`New printer added: ${data.remoteAddress}`);
|
||||||
|
} else {
|
||||||
|
logger.warn(`Failed to add new printer: ${data.remoteAddress}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const offlineData = {
|
||||||
|
remoteAddress: data.remoteAddress,
|
||||||
|
hostId: data.hostId,
|
||||||
|
online: false,
|
||||||
|
status: { type: "Offline" },
|
||||||
|
connectedAt: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.trace("Sending offline data", offlineData)
|
||||||
|
this.sendStatusToUserSockets(offlineData);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error handling offline event:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleHostOnlineEvent(data) {
|
||||||
|
try {
|
||||||
|
if (!this.db) {
|
||||||
|
await this.connectToMongo();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new entry to hosts collection.
|
||||||
|
const insertResult = await this.db.collection("hosts").insertOne({
|
||||||
|
hostId: data.hostId,
|
||||||
|
online: true,
|
||||||
|
connectedAt: new Date(),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (insertResult.insertedCount != 0) {
|
||||||
|
logger.info(`New host added: ${data.hostId}`);
|
||||||
|
} else {
|
||||||
|
logger.warn(`Failed to add new host: ${data.hostId}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error handling online event:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleHostOfflineEvent(data) {
|
||||||
|
//try {
|
||||||
|
if (!this.db) {
|
||||||
|
await this.connectToMongo();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete user from hosts collection
|
||||||
|
const deleteUserResult = await this.db
|
||||||
|
.collection("hosts")
|
||||||
|
.deleteOne({ hostId: data.hostId });
|
||||||
|
|
||||||
|
if (deleteUserResult.deletedCount > 0) {
|
||||||
|
logger.info(`User deleted from hosts collection: ${data.hostId}`);
|
||||||
|
} else {
|
||||||
|
logger.warn(`User not found in hosts collection: ${data.hostId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update all printers with matching hostId
|
||||||
|
const updatePrintersResult = await this.db
|
||||||
|
.collection("printers")
|
||||||
|
.updateMany(
|
||||||
|
{ hostId: data.hostId },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
online: false,
|
||||||
|
status: { type: "Offline" },
|
||||||
|
connectedAt: null,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const listPrintersResult = await this.db
|
||||||
|
.collection("printers")
|
||||||
|
.find({ hostId: data.hostId }).toArray();
|
||||||
|
|
||||||
|
if (updatePrintersResult.modifiedCount > 0) {
|
||||||
|
logger.info(
|
||||||
|
`Updated ${updatePrintersResult.modifiedCount} printers for user: ${data.hostId}`
|
||||||
|
);
|
||||||
|
for (let i = 0; i < listPrintersResult.length; i++) {
|
||||||
|
const printer = listPrintersResult[i];
|
||||||
|
const offlineData = {
|
||||||
|
remoteAddress: printer.remoteAddress,
|
||||||
|
hostId: printer.hostId,
|
||||||
|
online: false,
|
||||||
|
status: { type: "Offline" },
|
||||||
|
connectedAt: null,
|
||||||
|
};
|
||||||
|
logger.info(
|
||||||
|
`Sending offline status for: ${printer.remoteAddress}`
|
||||||
|
);
|
||||||
|
this.sendStatusToUserSockets(offlineData)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.warn(`No printers found for user: ${data.hostId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//} catch (error) {
|
||||||
|
// logger.error('Error handling user offline event:', error);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
sendStatusToUserSockets(data) {
|
||||||
|
this.userSockets.forEach(id => {
|
||||||
|
this.io.to(id).emit("status", data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async start(port = 3000) {
|
||||||
|
await this.connectToMongo();
|
||||||
|
this.configureMiddleware();
|
||||||
|
this.setupSocketIO();
|
||||||
|
|
||||||
|
this.server.listen(port, () => {
|
||||||
|
logger.info(`Server is running on port ${port}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { WebSocketServer };
|
||||||
Loading…
x
Reference in New Issue
Block a user