text: const Roomba = require('./roomba');
  const express = require('express');
  const log4js = require('log4js');
  
  // setup logger
  log4js.configure({
      appenders: {
          console: {
              type: 'console'
          },
          system: {
              type: 'file',
              filename: '/var/log/roomba/roomba.log',
              maxLogSize: 5120000, // 5MB
              backup: 3
          }
      },
      categories: {
          default: {
              appenders: ['console', 'system'],
              level: 'info'
          }
      }
  });
  const logger = log4js.getLogger('default');
  
  class HttpServer {
      constructor() {
          const env = process.env;
          // setup roomba
          this.robot = new Roomba(env.USERNAME, env.PASSWORD, env.IP_ADDRESS, env.FIRMWARE_VERSION);
          this.port = env.SERVER_PORT || 10520;
          // setup http server
          this.app = express();
          this.app.use(express.json());
          this.configuration();
      }
  
      configuration() {
          // pre-process
          this.app.use((req, res, next) => {
              next();
          });
          // main process
          this.app.post('/', (req, res, next) => {
              const param = req.body;
  
              try {
                  const method = param['method'];
                  const message = param['message'];
  
                  this.robot.execute(method).then(() => {
                      res.locals.statusCode = 200;
                      res.locals.message = message;
                      next();
                  }).catch((err) => {
                      res.locals.statusCode = 506;
                      res.locals.message = err.message;
                      next(err);
                  });
              }
              catch(err) {
                  res.locals.statusCode = 500;
                  res.locals.message = err.message;
                  next(err);
              }
          });
          // post-process
          this.app.use((req, res) => {
              const statusCode = res.locals.statusCode;
              const message = res.locals.message;
              logger.info(`Success(${statusCode}): ${message}`);
              res.status(statusCode || 200).json({status_code: statusCode, message: message});
          });
          this.app.use((err, req, res, next) => {
              const statusCode = res.locals.statusCode;
              const message = res.locals.message;
              logger.error(`Error(${statusCode}):   ${message}`);
              res.status(statusCode || 500).json({status_code: statusCode, message: message});
          });
      }
  
      run() {
          this.app.listen(this.port, () => logger.info('Start HTTP Server'));
      }
  }
  
  server = new HttpServer();
  server.run();
  