<commit_before>/*
 * Convert jpg to svg to gcode.
 *
 * d3000 - <EMAIL>
 */

var path = require('path'),
  fs = require('fs'),
  root = require('../root'),
  exec = require('child_process').exec,
  osc = new (require(root.communication + '/osc')),
  util = require('util'),

  Communication = require(root.communication + '/communication'),
  GcodeConverter = require(root.process + '/GcodeConverter'),
  Watcher = require(root.common + '/Watcher');
  WebServer = require(root.web + '/web');

var drawbot = new Communication();
var gcodeConverter = new GcodeConverter({feedrate: 2});
var webServer = new WebServer(drawbot);
webServer.startServer();

var gcodeFiles = [];
var processedGcodeFiles = [];
var currentGcodeFile;

var isProcessStarted = false;

var bmpWatcher = new Watcher({
  folder: path.resolve(root.data_bmp),
  extensions: ['bmp', 'BMP']
});

var jsonWatcher = new Watcher({
  folder: path.resolve(root.data_json),
  extensions: ['json']
});



drawbot.getSerialPortList(function(ports) {
  try {
    drawbot.connect(process.argv[2] || ports[0].comName);
  } catch(e) {}

  drawbot.on('connected', function() {
  });

  //processGcodeFile();

  bmpWatcher.on('fileAdded', function(evt) {
    var fileName = evt.path.split('/');
    fileName = fileName[fileName.length-1].split('.');
    fileName = fileName[fileName.length-2];
    var jsonFileName = fileName + '.json';

    // potrace -i -b geojson -k 0.4 -t 60 -o outputXXX.json bitmapXXX.BMP

    var potrace = 'potrace -i -b geojson -k 0.4 -t 60 -o ' + (root.data_json + '/' + jsonFileName) + ' ' + evt.path;
    var compare = 'compare -metric mae ' + root.process + '/background.bmp ' + evt.path + ' ' + root.data_temp + '/diff.bmp';

    exec(compare, function(error, stdout, strerr){
      if((error && error !== 'null'))
      {
        drawbot.Log.error('compare : error : ' + error);
        return;
      }else
      {
        drawbot.Log.debug('compare says: ' + stdout.split(' '));
        if(stdout.split(' ')[0] > 1250)
        {
          drawbot.Log.debug('gotit');
          exec(potrace, function(error, stdout, stderr) {
            if((error && error !== 'null') || stderr) {
              drawbot.Log.error('potrace ' + error);
              return;
            }
            //drawbot.log('-- Json file created: ' + jsonFileName);
          });
        }else
        {
          //fs.unlinkSync(evt.path);<commit_msg>tuning compare<commit_after>/*
 * Convert jpg to svg to gcode.
 *
 * d3000 - <EMAIL>
 */

var path = require('path'),
  fs = require('fs'),
  root = require('../root'),
  exec = require('child_process').exec,
  osc = new (require(root.communication + '/osc')),
  util = require('util'),

  Communication = require(root.communication + '/communication'),
  GcodeConverter = require(root.process + '/GcodeConverter'),
  Watcher = require(root.common + '/Watcher');
  WebServer = require(root.web + '/web');

var drawbot = new Communication();
var gcodeConverter = new GcodeConverter({feedrate: 2});
var webServer = new WebServer(drawbot);
webServer.startServer();

var gcodeFiles = [];
var processedGcodeFiles = [];
var currentGcodeFile;

var isProcessStarted = false;

var bmpWatcher = new Watcher({
  folder: path.resolve(root.data_bmp),
  extensions: ['bmp', 'BMP']
});

var jsonWatcher = new Watcher({
  folder: path.resolve(root.data_json),
  extensions: ['json']
});



drawbot.getSerialPortList(function(ports) {
  try {
    drawbot.connect(process.argv[2] || ports[0].comName);
  } catch(e) {}

  drawbot.on('connected', function() {
  });

  //processGcodeFile();

  bmpWatcher.on('fileAdded', function(evt) {
    var fileName = evt.path.split('/');
    fileName = fileName[fileName.length-1].split('.');
    fileName = fileName[fileName.length-2];
    var jsonFileName = fileName + '.json';

    // potrace -i -b geojson -k 0.4 -t 60 -o outputXXX.json bitmapXXX.BMP

    var potrace = 'potrace -i -b geojson -k 0.4 -t 60 -o ' + (root.data_json + '/' + jsonFileName) + ' ' + evt.path;
    var compare = 'compare -metric mae ' + root.process + '/background.bmp ' + evt.path + ' ' + root.data_temp + '/diff.bmp';

    exec(compare, function(error, stdout, strerr){
      if((error && error !== 'null'))
      {
        drawbot.Log.error('compare : error : ' + error);
        return;
      }else
      {
        drawbot.Log.debug('compare says: ' + stdout;
        if(stdout.split(' ')[0] > 1250)
        {
          drawbot.Log.debug('gotit');
          exec(potrace, function(error, stdout, stderr) {
            if((error && error !== 'null') || stderr) {
              drawbot.Log.error('potrace ' + error);
              return;
            }
            //drawbot.log('-- Json file created: ' + jsonFileName);
          });
        }else
        {
          //fs.unlinkSync(evt.path);
{'fraction_non_alphanumeric': 0.17329722974380338, 'fraction_numerical': 0.009164757342220371, 'mean_word_length': 2.8416, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '295065', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1574, 'n_words': 338}