Skip to content

Commit

Permalink
refactor(log-services): 🎉 update log services and camera libs
Browse files Browse the repository at this point in the history
  • Loading branch information
gokhangunduz committed Oct 23, 2023
1 parent 063f51a commit 2402da9
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import appRouters from "./src/routes/app.routes";
import bodyParser from "body-parser";
import cors from "cors";
import logRouters from "./src/routes/log.routes";
import foto from "./src/functions/foto";

async function app() {
const app = express();
Expand All @@ -25,6 +26,8 @@ async function app() {

app.use("/", appRouters);

foto();

app.use("/log", logRouters);

const server = app.listen(env.robot.port, async function () {
Expand Down
20 changes: 20 additions & 0 deletions app/src/functions/foto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as NodeWebcam from "node-webcam";

export default async function cam() {
const opts = {
width: 1280,
height: 720,
quality: 100,
device: "/dev/video0",
};

const Webcam = NodeWebcam.create(opts);

Webcam.capture("/home/gokhangunduz/Desktop/test.jpg", (err, data) => {
if (err) {
console.error(err);
} else {
console.log("Kamera başarıyla açıldı.");
}
});
}
29 changes: 29 additions & 0 deletions app/src/functions/video.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// import cv from "opencv4nodejs";

// // Video kaydı için kullanılacak dosya adı ve FPS (kare hızı)
// const videoFileName = "output.mp4";
// const fps = 30;

// // Video çekimine başlayın
// const camera = new cv.VideoCapture(0); // 0, varsayılan kamera cihazını temsil eder

// const videoWriter = new cv.VideoWriter(
// videoFileName,
// cv.VideoWriter.fourcc("X", "2", "6", "4"),
// fps,
// new cv.Size(640, 480),
// true
// );

// const delay = 1000 / fps;

// function captureFrame() {
// const frame = camera.read();
// if (!frame.empty) {
// videoWriter.write(frame);
// }
// setTimeout(captureFrame, delay);
// }

// console.log(`Video kaydediliyor: ${videoFileName}`);
// captureFrame();
6 changes: 6 additions & 0 deletions app/src/routes/log.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ const router = express.Router();

router.get("/", logServices.get);

router.get("/:name", logServices.getWithName);

router.delete("/", logServices.remove);

router.delete("/:name", logServices.removeWithName);

export default router;
60 changes: 55 additions & 5 deletions app/src/services/log.services.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
import { Request, Response } from "express";
import setResponse from "../helpers/setResponse.helper";
import getRosLogs from "../functions/getRosLogs.function";
import { RobotLog } from "../types/types";
import fs from "fs";

async function get(req: Request, res: Response) {
const files = await getRosLogs();
try {
const files: RobotLog[] | null = await getRosLogs();

if (files) {
setResponse(res, 200, "ROS logs retrieved successfully.", files);
} else {
setResponse(res, 500, "Error while getting ROS logs.", null);
if (files) {
setResponse(res, 200, "ROS logs retrieved successfully.", files);
} else {
setResponse(res, 500, "Error while getting ROS logs.", null);
}
} catch (error) {
setResponse(res, 500, "An error occurred", null);
}
}

async function getWithName(req: Request, res: Response) {
fs.readFile(
`/home/${process.env.USER}/.ros/log/${req.params.name}`,
"utf8",
function (err: any, data: any) {
if (err) {
setResponse(res, 500, "Error while getting ROS logs.", null);
return;
}
setResponse(res, 200, "ROS logs retrieved successfully.", data);
}
);
}

async function remove(req: Request, res: Response) {
try {
const files: RobotLog[] | null = await getRosLogs();

files?.map((file) => {
try {
fs.rmSync(`/home/${process.env.USER}/.ros/log/${file.name}`);
} catch (error) {
throw error;
}
});

setResponse(res, 200, "ROS logs removed successfully.", null);
} catch (error) {
setResponse(res, 500, "Error while removing ROS logs.", null);
}
}

async function removeWithName(req: Request, res: Response) {
try {
fs.rmSync(`/home/${process.env.USER}/.ros/log/${req.params.name}`);
setResponse(res, 200, "ROS log removed successfully.", null);
} catch (error) {
setResponse(res, 500, "Error while removing ROS log.", null);
}
}

export default {
get,
getWithName,
remove,
removeWithName,
};
9 changes: 9 additions & 0 deletions app/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ export type Topic = {
name: string;
listener?: any;
};

export type RobotLog = {
name: string;
isDirectory: boolean;
path: string;
size: number;
createdAt: number;
updatedAt: number;
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@types/cors": "^2.8.14",
"@types/express": "^4.17.19",
"@types/node": "^20.8.4",
"@types/node-webcam": "^0.8.2",
"express": "^4.18.2",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
Expand All @@ -31,6 +32,7 @@
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"kafkajs": "^2.2.4",
"node-webcam": "^0.8.2",
"rclnodejs": "^0.23.1"
}
}

0 comments on commit 2402da9

Please sign in to comment.