// @flow

import {
  DeviceAttributeFileRepository,
  DeviceServer,
  EventPublisher,
  ServerConfigFileRepository,
} from 'spark-protocol';
import utilities from './lib/utilities';
import logger from './lib/logger';
import createApp from './app';
import settings from './settings';

const NODE_PORT = process.env.NODE_PORT || 8080;

// TODO wny do we need this? (Anton Puko)
global._socket_counter = 1;

// TODO: something better here
process.on('uncaughtException', (exception: Error) => {
  let details = '';
  try {
    details = JSON.stringify(exception);
  } catch (stringifyException) {
    logger.error(`Caught exception: ${stringifyException}`);
  }
  logger.error(`Caught exception: ${exception.toString()} ${exception.stack}`);
});

const eventPublisher = new EventPublisher();

const deviceServer = new DeviceServer(
  {
    coreKeysDir: settings.coreKeysDir,
    deviceAttributeRepository: new DeviceAttributeFileRepository(
      settings.coreKeysDir,
    ),
    host: settings.HOST,
    port: settings.PORT,
    serverConfigRepository: new ServerConfigFileRepository(
      settings.serverKeyFile,
    ),
    serverKeyFile: settings.serverKeyFile,
    serverKeyPassEnvVar: settings.serverKeyPassEnvVar,
    serverKeyPassFile: settings.serverKeyPassFile,
  },
  eventPublisher,
);

global.server = deviceServer;
deviceServer.start();

const app = createApp(settings, deviceServer, eventPublisher);

app.listen(
  NODE_PORT,
  (): void => console.log(`express server started on port ${NODE_PORT}`),
);

utilities.getIPAddresses().forEach((ip: string): void =>
  console.log(`Your device server IP address is: ${ip}`),
);

@@ -45,16 +45,43 @@
 sitory,%0A
+  DeviceKeyFileRepository,%0A
   Device
@@ -989,24 +989,113 @@
 Dir,%0A    ),%0A
+    deviceKeyRepository: new DeviceKeyFileRepository(%0A      settings.coreKeysDir,%0A    ),%0A
     host: se

add deviceKeyRepository to deviceServerConfig