mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-23 09:07:54 +00:00
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import * as fs from 'fs';
|
|
import * as csv from 'csv-parser';
|
|
|
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { DeviceService } from 'src/device/services';
|
|
|
|
@Injectable()
|
|
export class DeviceCommissionService {
|
|
constructor(
|
|
private readonly tuyaService: TuyaService,
|
|
private readonly deviceService: DeviceService,
|
|
) {}
|
|
|
|
async processCsv(filePath: string): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
const results = [];
|
|
|
|
fs.createReadStream(filePath)
|
|
.pipe(csv())
|
|
.on('data', async (row) => {
|
|
console.log(`Device: ${JSON.stringify(row)}`);
|
|
const deviceId = row.deviceId?.trim();
|
|
|
|
if (!deviceId) {
|
|
console.error('Missing deviceId or deviceName in row:', row);
|
|
return;
|
|
} else {
|
|
const device = await this.tuyaService.getDeviceDetails(
|
|
row.deviceId,
|
|
);
|
|
console.log(device);
|
|
}
|
|
})
|
|
.on('end', () => {
|
|
console.log(`Finished processing ${results.length} devices.`);
|
|
resolve();
|
|
})
|
|
.on('error', (error) => {
|
|
console.error('Error reading CSV', error);
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
}
|