<commit_before>
const appUtil = require('./util.js');

appUtil.autoUpdate();

process.on('SIGINT', () => {
	console.log('Received SIGINT.');
	shutdown();
});
process.on('SIGTERM', () => {
	console.log('Received SIGTERM.');
	shutdown();
});

/* ----------------------------------------------------------------------- */

const mqtt = require('mqtt');
let clientMqtt	= mqtt.connect('mqtt://200.145.148.226', {
	will: {
		topic		: appUtil.hostId,
		payload		: `Last will: Host ${appUtil.hostId} is down from ${JSON.stringify(appUtil.ips)}`,
		qos			: 2,
		retain		: true
	}
})


function doReport() {
	let report = {
		host		: appUtil.hostId,
		uptime		: process.uptime(),
		sensor		: tshark.getReport(),
	};
	clientMqtt.publish('devices/report', JSON.stringify(report))
}

function doDeiviceReport(macAddress) {
	let report = {
		host		: appUtil.hostId,
		uptime		: process.uptime(),
		sensor		: tshark.getDeviceReport(macAddress),
	};
	clientMqtt.publish('devices/report', JSON.stringify(report))
}

clientMqtt.on('connect', function () {
	clientMqtt.subscribe('ADMIN');
	clientMqtt.subscribe('devices');

	clientMqtt.publish('ADMIN', `Hello  ${appUtil.hostId}: got ips: ${JSON.stringify(appUtil.ips)}`)
})

clientMqtt.on('message', function (topic, message) {
	// message is Buffer
	console.log(message.toString());
	switch (topic.toString()){
		case 'devices':
			switch (message.toString()) {
				case 'report':
					doReport();
					break;
				default:
					doDeiviceReport(message.toString());
			}
			break;
		case 'ADMIN':
			switch (message.toString()) {
				case 'shutdown':
					shutdown();
					break;
				case 'echo':
					clientMqtt.publish(topic, message);
					break;
				default:
			}
			break;
		default:
	}
})

/* ----------------------------------------------------------------------- */

const Tshark = require('./tshark.js');
let tshark;
try {
	tshark = Tshark();
	tshark.emitterInstance.on('newDevice', (device) => {
		try {
			clientMqtt.publish(appUtil.hostId, `Got new device with MAC ${JSON.stringify(device)}`);
		} catch (e){
			console.error('lost MQTT connection');
		}
	});
} catch (e) {
	clientMqtt.publish('ADMIN' , e.message);
	shutdown();
}

function shutdown(){
	console.log('Shutdown by remote call');
	try {
		clientMqtt.end();
		clientMqtt.publish('ADMIN', util.hostId + ' is going down.');
	} catch (e){
		console.error(e.message + 'while app MQTT shutdown');<commit_msg>[FIX] MQTT good bye messsage;<commit_after>
const appUtil = require('./util.js');

appUtil.autoUpdate();

process.on('SIGINT', () => {
	console.log('Received SIGINT.');
	shutdown();
});
process.on('SIGTERM', () => {
	console.log('Received SIGTERM.');
	shutdown();
});

/* ----------------------------------------------------------------------- */

const mqtt = require('mqtt');
let clientMqtt	= mqtt.connect('mqtt://200.145.148.226', {
	will: {
		topic		: appUtil.hostId,
		payload		: `Last will: Host ${appUtil.hostId} is down from ${JSON.stringify(appUtil.ips)}`,
		qos			: 2,
		retain		: true
	}
})


function doReport() {
	let report = {
		host		: appUtil.hostId,
		uptime		: process.uptime(),
		sensor		: tshark.getReport(),
	};
	clientMqtt.publish('devices/report', JSON.stringify(report))
}

function doDeiviceReport(macAddress) {
	let report = {
		host		: appUtil.hostId,
		uptime		: process.uptime(),
		sensor		: tshark.getDeviceReport(macAddress),
	};
	clientMqtt.publish('devices/report', JSON.stringify(report))
}

clientMqtt.on('connect', function () {
	clientMqtt.subscribe('ADMIN');
	clientMqtt.subscribe('devices');

	clientMqtt.publish('ADMIN', `Hello  ${appUtil.hostId}: got ips: ${JSON.stringify(appUtil.ips)}`)
})

clientMqtt.on('message', function (topic, message) {
	// message is Buffer
	console.log(message.toString());
	switch (topic.toString()){
		case 'devices':
			switch (message.toString()) {
				case 'report':
					doReport();
					break;
				default:
					doDeiviceReport(message.toString());
			}
			break;
		case 'ADMIN':
			switch (message.toString()) {
				case 'shutdown':
					shutdown();
					break;
				case 'echo':
					clientMqtt.publish(topic, message);
					break;
				default:
			}
			break;
		default:
	}
})

/* ----------------------------------------------------------------------- */

const Tshark = require('./tshark.js');
let tshark;
try {
	tshark = Tshark();
	tshark.emitterInstance.on('newDevice', (device) => {
		try {
			clientMqtt.publish(appUtil.hostId, `Got new device with MAC ${JSON.stringify(device)}`);
		} catch (e){
			console.error('lost MQTT connection');
		}
	});
} catch (e) {
	clientMqtt.publish('ADMIN' , e.message);
	shutdown();
}

function shutdown(){
	console.log('Shutdown by remote call');
	try {
		clientMqtt.end();
		clientMqtt.publish('ADMIN', appUtil.hostId + ' is going down.');
	} catch (e){
		console.error(e.message + 'while app MQTT shutdown');
{'fraction_non_alphanumeric': 0.22917958255837984, 'fraction_numerical': 0.0053730109526761724, 'mean_word_length': 2.983539094650206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2504579', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1674, 'n_words': 311}