<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


var nodejsWebsocket = require('nodejs-websocket');
var sysap_external = require('../freeathome/sysap-external');
var data = require('../data');
var helper = require('../helper');
var md5 = require('md5');

var valid = ['set', 'info', 'structure', 'raw', 'status', 'update', 'logLevel', 'logFilter', 'weather', 'message', 'powermeter', 'daynight'];

var socket = nodejsWebsocket.createServer(function (conn) {
	helper.log.info('[' + conn.socket.remoteAddress + '] websocket started');
	conn.on('text', function (str) {
		var parts = str.split('/');
		if (valid.indexOf(parts[0]) != -1) {
			set(parts, conn);
		} else {
			conn.sendText(JSON.stringify({'error': 'invalid command: ' + parts[0] + ' (' + str + ')'}));
			helper.log.debug('[' + conn.socket.remoteAddress + '] invalid websocket command: ' + parts[0] + ' (' + str + ')');
		}
	});
	conn.on('close', function (code, reason) {
		helper.log.info('websocket closed');
	});
	conn.on('error', function (err) {
		switch (err.code) {
			// most of these errors are cause by a sudden client disconnect
			// e.g. closing the browser window or turning off the computer
			case 'ECONNRESET':
				helper.log.error('client has exited ungracefully (ECONNRESET)');
				break;
			case 'EHOSTUNREACH':
				helper.log.error('where has the client gone? (EHOSTUNREACH)');
				break;
			case 'ETIMEDOUT':
				helper.log.error('where has the client gone? (ETIMEDOUT)');
				break;
			case 'EHOSTDOWN':
				helper.log.error('where has the client gone? (EHOSTDOWN)');
				break;
			case 'EPIPE':
				helper.log.error('where has the client gone? (EPIPE)');
				break;
			default:
				helper.log.error(err.code);
				throw err;
		}
	})
}).listen(8001);

function set (d, conn) {
	var command = d.shift();
	switch (command) {
		case 'info':
				helper.log.debug('[' + conn.socket.remoteAddress + '] info call');
				var d = data.getData('actuators');
				conn.sendText(JSON.stringify({'result': d}));
			break;
		case 'set':
			if (d.length == 4 || d.length == 5) {
				helper.log.debug('set channel ' + d[2] + ' of ' + d[0] + ' ' + d[1] + ' to ' + d[3] + (d.length == 5 ? ' (' + d[4] + ')' : ''));
				var status = sysap_external.parse(d[0], d[1], d[2], d[3], (d.length == 5 ? d[4] : null));
				conn.sendText(JSON.stringify({'result': status}));
			} else {
				conn.sendText(JSON.stringify({'error': 'invalid set command: ' + d.join('/')}));
				helper.log.error('[' + conn.socket.remoteAddress + '] invalid set command: ' + d.join('/'));
			}
			break;
<commit_msg>add parameters to info command<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


var nodejsWebsocket = require('nodejs-websocket');
var sysap_external = require('../freeathome/sysap-external');
var data = require('../data');
var helper = require('../helper');
var md5 = require('md5');

var valid = ['set', 'info', 'structure', 'raw', 'status', 'update', 'logLevel', 'logFilter', 'weather', 'message', 'powermeter', 'daynight'];

var socket = nodejsWebsocket.createServer(function (conn) {
	helper.log.info('[' + conn.socket.remoteAddress + '] websocket started');
	conn.on('text', function (str) {
		var parts = str.split('/');
		if (valid.indexOf(parts[0]) != -1) {
			set(parts, conn);
		} else {
			conn.sendText(JSON.stringify({'error': 'invalid command: ' + parts[0] + ' (' + str + ')'}));
			helper.log.debug('[' + conn.socket.remoteAddress + '] invalid websocket command: ' + parts[0] + ' (' + str + ')');
		}
	});
	conn.on('close', function (code, reason) {
		helper.log.info('websocket closed');
	});
	conn.on('error', function (err) {
		switch (err.code) {
			// most of these errors are cause by a sudden client disconnect
			// e.g. closing the browser window or turning off the computer
			case 'ECONNRESET':
				helper.log.error('client has exited ungracefully (ECONNRESET)');
				break;
			case 'EHOSTUNREACH':
				helper.log.error('where has the client gone? (EHOSTUNREACH)');
				break;
			case 'ETIMEDOUT':
				helper.log.error('where has the client gone? (ETIMEDOUT)');
				break;
			case 'EHOSTDOWN':
				helper.log.error('where has the client gone? (EHOSTDOWN)');
				break;
			case 'EPIPE':
				helper.log.error('where has the client gone? (EPIPE)');
				break;
			default:
				helper.log.error(err.code);
				throw err;
		}
	})
}).listen(8001);

function set (d, conn) {
	var command = d.shift();
	switch (command) {
		case 'info':
				helper.log.debug('[' + conn.socket.remoteAddress + '] info call');
				var actuators = data.getData('actuators');

				if (d.length >= 1 && actuators[d[0]]) {
					actuators = actuators[d[0]];
					if (d.length >= 2 && actuators.channels[d[1]]) {
						actuators = actuators.channels[d[1]];
						if (d.length >= 3 && actuators.datapoints[d[2]]) {
							actuators = actuators.datapoints[d[2]];
						}
					}
				}

				conn.sendText(JSON.stringify({'result': actuators}));
			break;
		case 'set':
			if (d.length == 4 || d.length == 5) {
				helper.log.debug('set channel ' + d[2] + ' of ' + d[0] + ' ' + d[1] + ' to ' + d[3] + (d.length == 5 ? ' (' + d[4] + ')' : ''));
				var status = sysap_external.parse(d[0], d[1], d[2], d[3], (d.length == 5 ? d[4] : null));
				conn.sendText(JSON.stringify({'result': status}));
			} else {
				conn.sendText(JSON.stringify({'error': 'invalid set command: ' + d.join('/')}));
				helper.log.error('[' + conn.socket.remoteAddress + '] invalid set command: ' + d.join('/'));
			}
			break;

{'fraction_non_alphanumeric': 0.19685447632215694, 'fraction_numerical': 0.01123401313515382, 'mean_word_length': 3.510522213561964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '2716869', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2003, 'n_words': 504}