mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 01:24:54 +00:00
Add PowerClamp module with controller and service for energy data retrieval
This commit is contained in:
@ -482,7 +482,15 @@ export class ControllerRoute {
|
||||
'This endpoint retrieves all devices in a specified group within a space, based on the group name and space UUID.';
|
||||
};
|
||||
};
|
||||
static PowerClamp = class {
|
||||
public static readonly ROUTE = 'power-clamp';
|
||||
|
||||
static ACTIONS = class {
|
||||
public static readonly GET_ENERGY_SUMMARY = 'Get power clamp data';
|
||||
public static readonly GET_ENERGY_DESCRIPTION =
|
||||
'This endpoint retrieves power clamp data for a specific device.';
|
||||
};
|
||||
};
|
||||
static DEVICE = class {
|
||||
public static readonly ROUTE = 'devices';
|
||||
|
||||
|
||||
1
libs/common/src/constants/sql-query-path.ts
Normal file
1
libs/common/src/constants/sql-query-path.ts
Normal file
@ -0,0 +1 @@
|
||||
export const SQL_QUERIES_PATH = 'libs/common/src/sql/queries';
|
||||
28
libs/common/src/helper/services/sql-loader.service.ts
Normal file
28
libs/common/src/helper/services/sql-loader.service.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { SQL_QUERIES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
@Injectable()
|
||||
export class SqlLoaderService {
|
||||
private readonly logger = new Logger(SqlLoaderService.name);
|
||||
private readonly sqlRootPath = join(__dirname, '../sql/queries');
|
||||
|
||||
loadQuery(module: string, queryName: string): string {
|
||||
const filePath = join(
|
||||
process.cwd(),
|
||||
SQL_QUERIES_PATH,
|
||||
module,
|
||||
`${queryName}.sql`,
|
||||
);
|
||||
try {
|
||||
return readFileSync(filePath, 'utf8');
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to load SQL query: ${module}/${queryName}`,
|
||||
error.stack,
|
||||
);
|
||||
throw new Error(`SQL query not found: ${module}/${queryName}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -33,7 +33,7 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||
})
|
||||
isActive: boolean;
|
||||
|
||||
@ManyToOne(() => UserEntity, (user) => user.userSpaces, { nullable: false })
|
||||
@ManyToOne(() => UserEntity, (user) => user.userSpaces, { nullable: true })
|
||||
user: UserEntity;
|
||||
|
||||
@OneToMany(
|
||||
|
||||
@ -0,0 +1,65 @@
|
||||
-- model shows the energy consumed per day per device
|
||||
|
||||
WITH total_energy AS (
|
||||
SELECT
|
||||
device_id,
|
||||
event_time::date AS date,
|
||||
MIN(value)::integer AS min_value,
|
||||
MAX(value)::integer AS max_value
|
||||
FROM "device-status-log"
|
||||
where code='EnergyConsumed'
|
||||
GROUP BY device_id, date
|
||||
)
|
||||
|
||||
, energy_phase_A AS (
|
||||
SELECT
|
||||
device_id,
|
||||
event_time::date AS date,
|
||||
MIN(value)::integer AS min_value,
|
||||
MAX(value)::integer AS max_value
|
||||
FROM "device-status-log"
|
||||
where code='EnergyConsumedA'
|
||||
GROUP BY device_id, date
|
||||
)
|
||||
|
||||
, energy_phase_B AS (
|
||||
SELECT
|
||||
device_id,
|
||||
event_time::date AS date,
|
||||
MIN(value)::integer AS min_value,
|
||||
MAX(value)::integer AS max_value
|
||||
FROM "device-status-log"
|
||||
where code='EnergyConsumedB'
|
||||
GROUP BY device_id, date
|
||||
)
|
||||
|
||||
, energy_phase_C AS (
|
||||
SELECT
|
||||
device_id,
|
||||
event_time::date AS date,
|
||||
MIN(value)::integer AS min_value,
|
||||
MAX(value)::integer AS max_value
|
||||
FROM "device-status-log"
|
||||
where code='EnergyConsumedC'
|
||||
GROUP BY device_id, date
|
||||
)
|
||||
|
||||
|
||||
SELECT
|
||||
total_energy.device_id,
|
||||
total_energy.date,
|
||||
(total_energy.max_value-total_energy.min_value) as energy_consumed_kW,
|
||||
(energy_phase_A.max_value-energy_phase_A.min_value) as energy_consumed_A,
|
||||
(energy_phase_B.max_value-energy_phase_B.min_value) as energy_consumed_B,
|
||||
(energy_phase_C.max_value-energy_phase_C.min_value) as energy_consumed_C
|
||||
FROM total_energy
|
||||
JOIN energy_phase_A
|
||||
ON total_energy.device_id=energy_phase_A.device_id
|
||||
and total_energy.date=energy_phase_A.date
|
||||
JOIN energy_phase_B
|
||||
ON total_energy.device_id=energy_phase_B.device_id
|
||||
and total_energy.date=energy_phase_B.date
|
||||
JOIN energy_phase_C
|
||||
ON total_energy.device_id=energy_phase_C.device_id
|
||||
and total_energy.date=energy_phase_C.date
|
||||
|
||||
Reference in New Issue
Block a user