import { buildLogger, Logger } from '@f2/log';

import { SshClient } from './@f2/ssh-client/ssh-client';

import { PmacUnsolicitedAutoClient } from './unsolicited-auto-client';


async function main(): Promise<void>{

    const rootLogger = await buildLogger({
        console: true,
        rotate: 'size',
        maxsize: 10 * 1024 * 1024,
        maxfiles: 6 * 30 // 6 mois
    });

    const appLogger = rootLogger.getLogger('App');

    const sshClient = new SshClient('192.168.3.10', {
        user: 'root',
        password: 'deltatau'
    });

    const bufLoggers: {[key: string]: Logger} = {};

    const buffers = [0, 1, 2, 3, 4, 5, 6, 7];

    buffers.forEach((buf) => {

        bufLoggers[buf] = rootLogger.getLogger(`b${buf}`);
    });

    const client = new PmacUnsolicitedAutoClient(sshClient, buffers);

    client.start();
    
    client.on('message', (msg, buffer) => {

        bufLoggers[buffer].info(msg);
    });

    client.on('connect', () => {
        appLogger.info('Client connecté');
    })

    client.on('disconnect', () => {
        appLogger.info('Client déconnecté');
    });

    client.on('error', (err) => {

        appLogger.error(err);
    })
}

main().catch(console.error);