|
1 |
| -import { Injectable } from '@nestjs/common'; |
2 |
| -import { PrismaService } from '../prisma/prisma.service'; |
| 1 | +import { Injectable } from "@nestjs/common"; |
| 2 | +import { PrismaService } from "../prisma/prisma.service"; |
| 3 | +import { CreateCommandInput } from "./dto/command.input"; |
| 4 | +import { CommandResponse } from "./entities/command.entity"; |
| 5 | +import { Time } from "./dto/commandAnalyze.output"; |
3 | 6 |
|
4 | 7 | @Injectable()
|
5 | 8 | export class CommandService {
|
6 | 9 | constructor(private readonly prisma: PrismaService) {}
|
7 | 10 |
|
8 |
| - async command(userID: number) { |
9 |
| - return this.prisma.command.findFirst({ |
10 |
| - where: { userID: userID }, |
11 |
| - orderBy: { created_at: "desc" } |
12 |
| - }); |
| 11 | + async commandsByUser(userID: number) { |
| 12 | + try { |
| 13 | + const list_commands = await this.prisma.command.findMany({ |
| 14 | + where: { userID: userID }, |
| 15 | + orderBy: { created_at: "desc" }, |
| 16 | + }); |
| 17 | + if (list_commands.length === 0) { |
| 18 | + return { commands: [] }; |
| 19 | + } |
| 20 | + return { commands: list_commands }; |
| 21 | + } catch (error) { |
| 22 | + throw error; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + async commandsByDevice(deviceID: number) { |
| 27 | + try { |
| 28 | + const list_commands = await this.prisma.command.findMany({ |
| 29 | + where: { |
| 30 | + deviceID: deviceID, |
| 31 | + }, |
| 32 | + orderBy: { created_at: "desc" }, |
| 33 | + }); |
| 34 | + if (list_commands.length === 0) { |
| 35 | + return { commands: [] }; |
| 36 | + } |
| 37 | + return { commands: list_commands }; |
| 38 | + } catch (error) { |
| 39 | + throw error; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + calculateActiveHours(commands: CommandResponse[], days: string[]) { |
| 44 | + var activeHours: Time[] = [{ hours: 0, minutes: 0, seconds: 0 }]; |
| 45 | + for (var i = 0; i < commands.length - 1; i++) { |
| 46 | + const dateOne = commands[i].created_at.toISOString().split("T")[0]; |
| 47 | + const dateTwo = commands[i + 1].created_at.toISOString().split("T")[0]; |
| 48 | + if (commands[i].command === "ON" && commands[i + 1].command === "OFF" && dateOne === dateTwo) { |
| 49 | + var diff = Math.abs(Number(commands[i].created_at) - Number(commands[i + 1].created_at)); |
| 50 | + console.log("Diff:", diff); |
| 51 | + const hour = Math.floor(diff / (1000 * 60 * 60)); |
| 52 | + diff = diff % (1000 * 60 * 60); |
| 53 | + const minute = Math.floor(diff / (1000 * 60)); |
| 54 | + diff = diff % (1000 * 60); |
| 55 | + const second = Math.floor(diff / 1000); |
| 56 | + days.forEach((day, index) => { |
| 57 | + if (day === dateOne) { |
| 58 | + activeHours[index].hours += hour; |
| 59 | + activeHours[index].minutes += minute; |
| 60 | + activeHours[index].seconds += second; |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + console.log("Active hours:", activeHours); |
| 66 | + if (commands[commands.length - 1].command === "ON" && days[days.length - 1] === commands[commands.length - 1].created_at.toISOString().split("T")[0]) { |
| 67 | + const date = new Date(commands[commands.length - 1].created_at); |
| 68 | + const currentDate = new Date(); |
| 69 | + const diff = Math.abs(currentDate.getTime() - date.getTime()); |
| 70 | + const hours = Math.floor(diff / (1000 * 60 * 60)); |
| 71 | + const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); |
| 72 | + const seconds = Math.floor((diff % (1000 * 60)) / 1000); |
| 73 | + activeHours[days.length - 1].hours += hours; |
| 74 | + activeHours[days.length - 1].minutes += minutes; |
| 75 | + activeHours[days.length - 1].seconds += seconds; |
| 76 | + } |
| 77 | + console.log("Active hours:", activeHours); |
| 78 | + return activeHours; |
| 79 | + } |
| 80 | + |
| 81 | + async analyzeCommandsByDevice(deviceID: number) { |
| 82 | + try { |
| 83 | + const list_commands = await this.prisma.command.findMany({ |
| 84 | + where: { |
| 85 | + deviceID: deviceID, |
| 86 | + }, |
| 87 | + orderBy: { created_at: "asc" }, |
| 88 | + }); |
| 89 | + if (list_commands.length === 0) { |
| 90 | + return { commands: [] }; |
| 91 | + } |
| 92 | + const days: string[] = []; |
| 93 | + list_commands.forEach((command, index) => { |
| 94 | + const date = new Date(command.created_at); |
| 95 | + const month = date.getMonth() + 1; |
| 96 | + const dayx = date.getDate(); |
| 97 | + const hours = date.getHours(); |
| 98 | + const minutes = date.getMinutes(); |
| 99 | + console.log(`${index} - Date: ${month}/${dayx} Time: ${hours}:${minutes} - ${command.command}`); |
| 100 | + const day = command.created_at.toISOString().split("T")[0]; |
| 101 | + if (!days.includes(day)) { |
| 102 | + days.push(day); |
| 103 | + } |
| 104 | + }); |
| 105 | + const activeHours = this.calculateActiveHours(list_commands, days); |
| 106 | + console.log(days); |
| 107 | + return { days: days, hoursPerDay: activeHours }; |
| 108 | + } catch (error) { |
| 109 | + throw error; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + async createCommand(input: CreateCommandInput) { |
| 114 | + try { |
| 115 | + const data = await this.prisma.command.create({ |
| 116 | + data: { |
| 117 | + deviceID: input.deviceID, |
| 118 | + command: input.command, |
| 119 | + sender: input.sender, |
| 120 | + receiver: input.receiver, |
| 121 | + userID: input.userID, |
| 122 | + created_at: input.created_at, |
| 123 | + }, |
| 124 | + }); |
| 125 | + if (data) { |
| 126 | + return data; |
| 127 | + } |
| 128 | + throw new Error("Failed to create command"); |
| 129 | + } catch (error) { |
| 130 | + throw error; |
| 131 | + } |
13 | 132 | }
|
14 | 133 | }
|
0 commit comments