<commit_before>#!/usr/bin/env node
/*
 * Copyright (c) 2015, Daliworks. All rights reserved.
 *
 * Reproduction and/or distribution in source and binary forms 
 * without the written consent of Daliworks, Inc. is prohibited.
 *
 */
'use strict';
var jsonrpc = require('jsonrpc-tcp'),
    _ = require('lodash'),
    os = require('os'),
    log4js = require('log4js');

var Lcd = require('./grovePiLcd'),
    grovePiSensors = require('./grovePiSensors');

//log4js.configure(__dirname + '/logger_cfg.json', {
//  reloadSecs: 30,
//  cwd: './log'
//});

var logger = log4js.getLogger('Main');

/**
 * Configuration
 **/
var
JSONRPC_PORT = 50800,     // JSON-RPC server port
STATUS_INTERVAL = 60000,  // status report interval; less than gateway one.

setNotiTable = {}, // notification is enabled or not for each sensor
clientConnection,  // the connection(assuming only one) from client.
device0Id = '0',   // device address, any string allowed for url

/* 
 * Devices and its sensors info to be used for discovery response.
 *
 * Note: Sensor id can be any url unreserved characters on condition 
 *       that it is uniq across your devices under the gateway.
 */
DEVICES = [{
  deviceAddress: device0Id,
  sensors: [
    // Analog
    {
      id: [device0Id, 'sound'].join('-'),
      type: 'noise',
      name: 'sound',
      notification: false
    },
    {
      id: [device0Id, 'light'].join('-'),
      type: 'light',
      name: 'light',
      notification: false
    },
    {
      id: [device0Id, 'rotary'].join('-'),
      type: 'rotaryAngle',
      name: 'rotary',
      notification: false
    },
//    {
//      id: [device0Id, 'vib'].join('-'),
//      type: 'vibration',
//      name: 'vibration',
//      notification: false
//    },
    // Digital
    { // actuator
      id: [device0Id, 'buzz'].join('-'),
      type: 'buzzer',
      name: 'buzzer',
      notification: false
    },
    { // actuator
      id: [device0Id, 'led'].join('-'),
      type: 'led',
      name: 'led',
      notification: false
    },
    { // actuator
      id: [device0Id, 'relay'].join('-'),
      type: 'powerSwitch',
      name: 'relay',
      notification: false
    },
    { //event
      id: [device0Id, 'button'].join('-'),
      type: 'onoff',
      name: 'button',
      notification: true
    },
    { //event
      id: [device0Id, 'ultra'].join('-'),
      type: 'onoff',
      name: 'ultrasonic',
      notification: true
    },
    { //series
      id: [device0Id, 'temp'].join('-'),
      type: 'temperature',
      name: 'temperature',
      notification: false
    },
    { //series
      id: [device0Id, 'humi'].join('-'),
      type: 'humidity',
      name: 'humidity',
      notification: false
    },
    { // actuator
      id: [device0Id, 'lcd'].join('-'),
      type: 'lcd',
      name: 'lcd',
      notification: false
    }
  ]
}];

var sensorNames = [];
var grovePiLcd = new Lcd();

// util function: find target sensor from DEVICES
function getSensorInfo(cond) {
  var found;
  _.each(DEVICES, function(device) {
    var sensor = _.find(device.sensors, cond);
    found = sensor;

    if (found) {
      return false;
    }
  });
  return found || {};
}

/**
 * JSON-RPC server setup 
 *
 */
//JSON-RPC service functions: get/set/setNotification
var Sensor = {
  set: function (id, cmd, options, result) { 
    logger.info('[set actuator] id=%s cmd=%s options=%j', id, cmd, options);
    var target = getSensorInfo({id: id});

    if (target.name === 'lcd') {
      grovePiLcd.doCommand(target.name, cmd, options);
    } else {
      grovePiSensors.doCommand(target.name, cmd, options);
    }
    result(null, options || 'success');
  },

  setNotification: function (id, result) { 
    if (!setNotiTable[id]) {
      setNotiTable[id] = true;
    }
    result(null, 'success');<commit_msg>return 'ok' when relay actuating success<commit_after>#!/usr/bin/env node
/*
 * Copyright (c) 2015, Daliworks. All rights reserved.
 *
 * Reproduction and/or distribution in source and binary forms 
 * without the written consent of Daliworks, Inc. is prohibited.
 *
 */
'use strict';
var jsonrpc = require('jsonrpc-tcp'),
    _ = require('lodash'),
    os = require('os'),
    log4js = require('log4js');

var Lcd = require('./grovePiLcd'),
    grovePiSensors = require('./grovePiSensors');

//log4js.configure(__dirname + '/logger_cfg.json', {
//  reloadSecs: 30,
//  cwd: './log'
//});

var logger = log4js.getLogger('Main');

/**
 * Configuration
 **/
var
JSONRPC_PORT = 50800,     // JSON-RPC server port
STATUS_INTERVAL = 60000,  // status report interval; less than gateway one.

setNotiTable = {}, // notification is enabled or not for each sensor
clientConnection,  // the connection(assuming only one) from client.
device0Id = '0',   // device address, any string allowed for url

/* 
 * Devices and its sensors info to be used for discovery response.
 *
 * Note: Sensor id can be any url unreserved characters on condition 
 *       that it is uniq across your devices under the gateway.
 */
DEVICES = [{
  deviceAddress: device0Id,
  sensors: [
    // Analog
    {
      id: [device0Id, 'sound'].join('-'),
      type: 'noise',
      name: 'sound',
      notification: false
    },
    {
      id: [device0Id, 'light'].join('-'),
      type: 'light',
      name: 'light',
      notification: false
    },
    {
      id: [device0Id, 'rotary'].join('-'),
      type: 'rotaryAngle',
      name: 'rotary',
      notification: false
    },
//    {
//      id: [device0Id, 'vib'].join('-'),
//      type: 'vibration',
//      name: 'vibration',
//      notification: false
//    },
    // Digital
    { // actuator
      id: [device0Id, 'buzz'].join('-'),
      type: 'buzzer',
      name: 'buzzer',
      notification: false
    },
    { // actuator
      id: [device0Id, 'led'].join('-'),
      type: 'led',
      name: 'led',
      notification: false
    },
    { // actuator
      id: [device0Id, 'relay'].join('-'),
      type: 'powerSwitch',
      name: 'relay',
      notification: false
    },
    { //event
      id: [device0Id, 'button'].join('-'),
      type: 'onoff',
      name: 'button',
      notification: true
    },
    { //event
      id: [device0Id, 'ultra'].join('-'),
      type: 'onoff',
      name: 'ultrasonic',
      notification: true
    },
    { //series
      id: [device0Id, 'temp'].join('-'),
      type: 'temperature',
      name: 'temperature',
      notification: false
    },
    { //series
      id: [device0Id, 'humi'].join('-'),
      type: 'humidity',
      name: 'humidity',
      notification: false
    },
    { // actuator
      id: [device0Id, 'lcd'].join('-'),
      type: 'lcd',
      name: 'lcd',
      notification: false
    }
  ]
}];

var sensorNames = [];
var grovePiLcd = new Lcd();

// util function: find target sensor from DEVICES
function getSensorInfo(cond) {
  var found;
  _.each(DEVICES, function(device) {
    var sensor = _.find(device.sensors, cond);
    found = sensor;

    if (found) {
      return false;
    }
  });
  return found || {};
}

/**
 * JSON-RPC server setup 
 *
 */
//JSON-RPC service functions: get/set/setNotification
var Sensor = {
  set: function (id, cmd, options, result) { 
    logger.info('[set actuator] id=%s cmd=%s options=%j', id, cmd, options);
    var target = getSensorInfo({id: id});

    if (target.name === 'lcd') {
      grovePiLcd.doCommand(target.name, cmd, options);
    } else {
      grovePiSensors.doCommand(target.name, cmd, options);
    }

    if (target.name === 'relay') {
      result(null, 'ok');
    } else {
      result(null, options || 'success');
    }
  },

  setNotification: function (id, result) { 
    if (!setNotiTable[id]) {
      setNotiTable[id] = true;
    }
    result(null, 'success');
{'fraction_non_alphanumeric': 0.1688210961737332, 'fraction_numerical': 0.00904860392967942, 'mean_word_length': 2.4280017722640674, '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': '5511128', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2514, 'n_words': 662}