<commit_before>/**
 * Copyright 2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

module.exports = function(RED) {
	"use strict";
	var path = require('path');

	function awsNodeBroker(n) {
		RED.nodes.createNode(this, n);
		this.clientId = n.clientId;
		this.endpoint = n.endpoint;
		var self = this;

		this.connect = function(clientId, reconnect, callback) {
			clientId = clientId || n.clientId;
			clientId = clientId.trim();
			var awsCerts = n.awscerts || ".";
			if (!self.device || reconnect) {
				self.log("Attempt to connect to " + n.mode + " with " + clientId + " from: " + awsCerts);
				var keyPath = path.join(awsCerts, '/' + clientId + '.private.key');
				var certPath = path.join(awsCerts, '/' + clientId + '.cert.pem');
				var caPath = path.join(awsCerts, '/root-CA.crt');
				self.log("Using the certificates that are presented as follows:");
				self.log(" - keyPath   : " + keyPath);
				self.log(" - certPath  : " + certPath);
				self.log(" - caPath    : " + caPath);
				if (n.mode == "shadow") {
					self.device = require('aws-iot-device-sdk').thingShadow({
						keyPath : keyPath,
						certPath : certPath,
						caPath : caPath,
						clientId : clientId,
						host : self.endpoint,
						protocol: 'mqtts'
					});
				} else {
					self.device = require('aws-iot-device-sdk').device({
						keyPath : keyPath,
						certPath : certPath,
						caPath : caPath,
						clientId : clientId,
						host : self.endpoint,
						protocol: 'mqtts'
					});
				}
				if (self.device) {
					self.device.on('connect', function() {
						callback('connected');
					});
					self.device.on('reconnect', function() {
						callback('reconnected');
					});
					self.device.on('error', function(error) {
						callback('error', error);
					});
					self.device.on('offline', function() {
						callback('offline');
					});
				}
				callback('created');
			} else if (self.device) {
				callback('ready');
			}
		};

		self.on('close', function() {
			self.log("closed " + self.clientId + " ok");
			if (self.device) {
				if (n.mode == "shadow") {
					self.device.unregister(self.clientId);
					self.device.end();
				} else {
					self.device.end();
				}
			}
			self.device = null;
		});
	}


	RED.nodes.registerType("aws-iot-device", awsNodeBroker);

	function awsMqttNodeIn(n) {
		RED.nodes.createNode(this, n);
		this.myDevice = n.device;
		this.awsIot = RED.nodes.getNode(this.myDevice);
		var self = this;
		var options = {
			qos: parseInt(n.qos) || 0
		};
		if (self.awsIot) {
			self.status({
				fill : "yellow",
				shape : "ring",
				text : "subscribing..."
			});
			self.awsIot.connect(null, null, function(event, error) {
				if (event == "created") {
					self.awsIot.device.unsubscribe(n.topic);
					self.awsIot.device.subscribe(n.topic, options, function(error, granted) {
						if (!error) {
							self.status({
								fill : "green",
								shape : "ring",
								text : "subscribed"
							});
							self.awsIot.device.on('message', function(topic, payload) {
								if ( typeof payload === "string") {
								} else {
									payload = payload.toString();
								}								
								console.log("RECV<", topic, payload);
								self.send({
									topic : topic,
									payload : payload
								});
							});
						} else {
							self.status({
								fill : "error",
								shape : "ring",
								text : error.message
							});
						}
					});
				}
			});
		} else {
			self.error("aws-mqtt in is not configured");
			self.status({
				fill : "red",
				shape : "ring",
				text : "not configured"
			});
		}
	}


	RED.nodes.registerType("aws-mqtt in", awsMqttNodeIn);

	function awsMqttNodeOut(n) {
		RED.nodes.createNode(this, n);
		this.myDevice = n.device;
		this.awsIot = RED.nodes.getNode(this.myDevice);
		var self = this;
		var options = {
			qos: parseInt(n.qos) || 0
		};
		self.on("input", function(msg) {
			if (self.awsIot) {
				self.awsIot.connect(msg.clientId, msg.reconnect, function(event, error) {
					if ((event == "ready" || event == "created")) {
						if (!Buffer.isBuffer(msg.payload)) {
							if ( typeof msg.payload === "object") {
								msg.payload = JSON.stringify(msg.payload);
							} else if ( typeof msg.payload !== "string") {
								msg.payload = msg.payload.toString();
							}
							msg.payload = new Buffer(msg.payload, "utf-8");
						}
						self.status({
							fill : "blue",
							shape : "ring",
							text : "sending..."
						});
						console.log("SEND>", msg.topic || n.topic, msg.payload);
						self.awsIot.device.publish(msg.topic || n.topic, msg.payload, options, function(error) {
							if (error) {
								self.status({
									fill : "red",
									shape : "ring",<commit_msg>replaced console.log with self.log<commit_after>/**
 * Copyright 2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

module.exports = function(RED) {
	"use strict";
	var path = require('path');

	function awsNodeBroker(n) {
		RED.nodes.createNode(this, n);
		this.clientId = n.clientId;
		this.endpoint = n.endpoint;
		var self = this;

		this.connect = function(clientId, reconnect, callback) {
			clientId = clientId || n.clientId;
			clientId = clientId.trim();
			var awsCerts = n.awscerts || ".";
			if (!self.device || reconnect) {
				self.log("Attempt to connect to " + n.mode + " with " + clientId + " from: " + awsCerts);
				var keyPath = path.join(awsCerts, '/' + clientId + '.private.key');
				var certPath = path.join(awsCerts, '/' + clientId + '.cert.pem');
				var caPath = path.join(awsCerts, '/root-CA.crt');
				self.log("Using the certificates that are presented as follows:");
				self.log(" - keyPath   : " + keyPath);
				self.log(" - certPath  : " + certPath);
				self.log(" - caPath    : " + caPath);
				if (n.mode == "shadow") {
					self.device = require('aws-iot-device-sdk').thingShadow({
						keyPath : keyPath,
						certPath : certPath,
						caPath : caPath,
						clientId : clientId,
						host : self.endpoint,
						protocol: 'mqtts'
					});
				} else {
					self.device = require('aws-iot-device-sdk').device({
						keyPath : keyPath,
						certPath : certPath,
						caPath : caPath,
						clientId : clientId,
						host : self.endpoint,
						protocol: 'mqtts'
					});
				}
				if (self.device) {
					self.device.on('connect', function() {
						callback('connected');
					});
					self.device.on('reconnect', function() {
						callback('reconnected');
					});
					self.device.on('error', function(error) {
						callback('error', error);
					});
					self.device.on('offline', function() {
						callback('offline');
					});
				}
				callback('created');
			} else if (self.device) {
				callback('ready');
			}
		};

		self.on('close', function() {
			self.log("closed " + self.clientId + " ok");
			if (self.device) {
				if (n.mode == "shadow") {
					self.device.unregister(self.clientId);
					self.device.end();
				} else {
					self.device.end();
				}
			}
			self.device = null;
		});
	}


	RED.nodes.registerType("aws-iot-device", awsNodeBroker);

	function awsMqttNodeIn(n) {
		RED.nodes.createNode(this, n);
		this.myDevice = n.device;
		this.awsIot = RED.nodes.getNode(this.myDevice);
		var self = this;
		var options = {
			qos: parseInt(n.qos) || 0
		};
		if (self.awsIot) {
			self.status({
				fill : "yellow",
				shape : "ring",
				text : "subscribing..."
			});
			self.awsIot.connect(null, null, function(event, error) {
				if (event == "created") {
					self.awsIot.device.unsubscribe(n.topic);
					self.awsIot.device.subscribe(n.topic, options, function(error, granted) {
						if (!error) {
							self.status({
								fill : "green",
								shape : "ring",
								text : "subscribed"
							});
							self.awsIot.device.on('message', function(topic, payload) {
								if ( typeof payload === "string") {
								} else {
									payload = payload.toString();
								}								
								self.log("RECV< " + topic + payload);
								self.send({
									topic : topic,
									payload : payload
								});
							});
						} else {
							self.status({
								fill : "error",
								shape : "ring",
								text : error.message
							});
						}
					});
				}
			});
		} else {
			self.error("aws-mqtt in is not configured");
			self.status({
				fill : "red",
				shape : "ring",
				text : "not configured"
			});
		}
	}


	RED.nodes.registerType("aws-mqtt in", awsMqttNodeIn);

	function awsMqttNodeOut(n) {
		RED.nodes.createNode(this, n);
		this.myDevice = n.device;
		this.awsIot = RED.nodes.getNode(this.myDevice);
		var self = this;
		var options = {
			qos: parseInt(n.qos) || 0
		};
		self.on("input", function(msg) {
			if (self.awsIot) {
				self.awsIot.connect(msg.clientId, msg.reconnect, function(event, error) {
					if ((event == "ready" || event == "created")) {
						if (!Buffer.isBuffer(msg.payload)) {
							if ( typeof msg.payload === "object") {
								msg.payload = JSON.stringify(msg.payload);
							} else if ( typeof msg.payload !== "string") {
								msg.payload = msg.payload.toString();
							}
							msg.payload = new Buffer(msg.payload, "utf-8");
						}
						self.status({
							fill : "blue",
							shape : "ring",
							text : "sending..."
						});
						self.log("SEND> " + msg.topic || n.topic + " " +msg.payload);
						self.awsIot.device.publish(msg.topic || n.topic, msg.payload, options, function(error) {
							if (error) {
								self.status({
									fill : "red",
									shape : "ring",
{'fraction_non_alphanumeric': 0.16086377473363775, 'fraction_numerical': 0.0020928462709284626, 'mean_word_length': 2.4266623207301175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2161109', 'n_tokens_mistral': 4608, 'n_tokens_neox': 3424, 'n_words': 774}