<commit_before>var devices = require('ev3-js-devices')
var Device = require('ev3-js-device')

var fs = require('fs')
var Emitter = require('component-emitter')
var motor = require('./motor')
var moveSteering = require('./moveSteering')
var foreach = require('@f/foreach')

var readingChannels = {}
var sensors = {}
var motors = {}
var reading = false
var sensorInterval
var interval

exports.sensor_mode = sensorMode
exports.sensor_subscribe = sensorSubscribe,
exports.sensor_unsubscribe = sensorUnsubscribe,
exports.motor_read = motorRead
exports.motor_write = motor
exports.motors_write = moveSteering
exports.ping = ping
exports.reading = getReading
exports.setPaths = setPaths

var paths = {
  motor: '/sys/class/tacho-motor/',
  sensor: '/sys/class/lego-sensor/'
}

function setPaths (path) {
  paths = path
}

var messages = exports.messages = new Emitter()

function getReading () {
  return reading
}

messages.on('close', function (socketId) {
  sensorUnsubscribe({socketId: socketId}, function () {})
})

function readPorts () {
  sensors = getSensors()
  motors = getMotors()
}

function sensorSubscribe (data, cb) {
  if (!reading) {
    reading = true
    readPorts()
    sensorInterval = setInterval(readPorts, 5000)
    interval = setInterval(function () { sensorRead(sensors, motors) }, 150)
  }
  readingChannels[data.socketId] = true
  cb(null, true)
}

function sensorUnsubscribe (data, cb) {
  delete readingChannels[data.socketId]
  if (!Object.keys(readingChannels).length) {
    reading = false
    try {
      interval.close()
      sensorInterval.close()
    } catch (e) {}
    clearInterval(interval)
    clearInterval(sensorInterval)
  }
  return cb(null, true)
}

function sensorMode (data, cb) {
  var port = data.port
  var command = data.command
  var device = new Device(devices(port, paths))

  device.write('mode', command, cb)
}

function motorRead (data, cb, path) {
  var port = data.port
  var value = data.value

  var device = new Device(devices(port, path))

  cb(null, device.read(value))
}

function ping (data, cb) {
  cb(null, true)
}

function sensorRead (sensors, motors) {
  var sensorData = {}
  for (var port in sensors) {
    if (sensors[port].type === 'ir' && sensors[port].mode === 'IR-SEEK'){
      try {
        var data = readIR(sensors[port].device)
      } catch (e) {
        console.warn('sensor disconnected')
      }
    } else {
      try {
        var data = sensors[port].device.read('value0')
      } catch (e) {<commit_msg>update sensors when mode changes<commit_after>var devices = require('ev3-js-devices')
var Device = require('ev3-js-device')

var fs = require('fs')
var Emitter = require('component-emitter')
var motor = require('./motor')
var moveSteering = require('./moveSteering')
var foreach = require('@f/foreach')

var readingChannels = {}
var sensors = {}
var motors = {}
var reading = false
var sensorInterval
var interval

exports.sensor_mode = sensorMode
exports.sensor_subscribe = sensorSubscribe,
exports.sensor_unsubscribe = sensorUnsubscribe,
exports.motor_read = motorRead
exports.motor_write = motor
exports.motors_write = moveSteering
exports.ping = ping
exports.reading = getReading
exports.setPaths = setPaths

var paths = {
  motor: '/sys/class/tacho-motor/',
  sensor: '/sys/class/lego-sensor/'
}

function setPaths (path) {
  paths = path
}

var messages = exports.messages = new Emitter()

function getReading () {
  return reading
}

messages.on('close', function (socketId) {
  sensorUnsubscribe({socketId: socketId}, function () {})
})

function readPorts () {
  sensors = getSensors()
  motors = getMotors()
}

function sensorSubscribe (data, cb) {
  if (!reading) {
    reading = true
    readPorts()
    sensorInterval = setInterval(readPorts, 5000)
    interval = setInterval(function () { sensorRead(sensors, motors) }, 150)
  }
  readingChannels[data.socketId] = true
  cb(null, true)
}

function sensorUnsubscribe (data, cb) {
  delete readingChannels[data.socketId]
  if (!Object.keys(readingChannels).length) {
    reading = false
    try {
      interval.close()
      sensorInterval.close()
    } catch (e) {}
    clearInterval(interval)
    clearInterval(sensorInterval)
  }
  return cb(null, true)
}

function sensorMode (data, cb) {
  var port = data.port
  var command = data.command
  var device = new Device(devices(port, paths))

  device.write('mode', command, function (err) {
    sensors = getSensors()
    cb(err)
  })
}

function motorRead (data, cb, path) {
  var port = data.port
  var value = data.value

  var device = new Device(devices(port, path))

  cb(null, device.read(value))
}

function ping (data, cb) {
  cb(null, true)
}

function sensorRead (sensors, motors) {
  var sensorData = {}
  for (var port in sensors) {
    if (sensors[port].type === 'ir' && sensors[port].mode === 'IR-SEEK'){
      try {
        var data = readIR(sensors[port].device)
      } catch (e) {
        console.warn('sensor disconnected')
      }
    } else {
      try {
        var data = sensors[port].device.read('value0')
      } catch (e) {
{'fraction_non_alphanumeric': 0.13310917721518986, 'fraction_numerical': 0.003955696202531646, 'mean_word_length': 3.770754716981132, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1243966', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1586, 'n_words': 404}