<commit_before>/* ImonCloud ONVIF connector for nodejs 
updated: 2014/07 

NOTE: 1) 此檔要全力避免使用 event.XXXX 2)

todo:
init 要加入判斷帳號/密碼是否正確 
	
*/

/* IC method */
if (typeof(LOG) === "undefined") {
	var LOG = {};
	if (typeof(LOG.warn) === "undefined") {
		LOG.warn = function (ret) {
			console.warn(ret);
		}
	}
	if (typeof(LOG.error) === "undefined") {
		LOG.error = function (ret) {
			console.error(ret);
		}
	}
	if (typeof(LOG.stack) == "undefined") {
		LOG.stack = function (ret) {
			console.trace();
		}
	}
}

//var onDone;

var sys = require('sys'); // http://nodejs.org/api.html#_child_processes
var exec = require('child_process').exec;
var Cam = require('onvif').Cam;
var discovery = require('onvif').Discovery;
var http = require('http');
var onvif = require('onvif');
//~ var child;

//var obj = {};

function onvifc(input) {
	// the constructor
	if (typeof(input) !== "undefined") {
		this.data = input;
		this.init(input);
	}
	return this;
}

//~ var INIT = function (obj) {
onvifc.prototype.init = function (input) {
	this.data = input;
	LOG.warn('initiating: ip-' + this.data.host 
		+ ' port-' + this.data.port 
		+ ' username-' + this.data.user 
		+ ' password-' + this.data.passwd + ' ');

	input.onDone(this.__proto__);
	return this;
};

onvifc.prototype.autoScan = function(input){
	var this_wrapper = this;
	
	var cams = {};
	
	onvif.Discovery.on('device', function(cam, remoteInfo, responseXML){
	
		console.log("CAM - typeof: " + typeof(cam) + " - content - "); console.log(cam);
		console.log("RemoteInfo - typeof: " + typeof(remoteInfo) + " - content - "); console.log(remoteInfo);
		console.log("XML - typeof: " + typeof(responseXML) + " - content - "); console.log(""+responseXML);

		var myScopes = cam.probeMatches.probeMatch.scopes;
		if (myScopes._) {
			myScopes = myScopes._
		}
		var scopes = myScopes.toString().split(' ');
		var scopes_obj = {};
		Object.keys(scopes).forEach(function(key){
			var f = scopes[key].split('/')[3], b = scopes[key].split('/')[4];
			
			if (scopes_obj[f]) {
				scopes_obj[f] += (" " + b);
			} else {
				scopes_obj[f] = b;
			}
		});
		
		var address = cam.probeMatches.probeMatch.XAddrs.split('/')[2];
		var camData = {
			"IP" : address.split(':')[0],
			"Port" : address.split(':')[1],
			"endpointReference": cam.probeMatches.probeMatch.endpointReference,
			"Type": cam.probeMatches.probeMatch.types.split(':')[1],
			"Scopes": scopes_obj,
			"Path": cam.probeMatches.probeMatch.XAddrs
		};
		
		if ( camData.IP && typeof camData.Port == 'undefined') {
			camData.Port = 80
		};
		
		cams[address] = (camData);
	});
	
	
	onvif.Discovery.probe({resolve: false}, function () {
		
		input.onDone( Object.keys(cams).map(function(address) { return cams[address]; }) );
	});
	
	
	/*
	discovery.probe(function(_null, rinfo){
		
		rinfo.forEach(function(cam){
			var ip = cam.hostname;
			var found = 0;
			cams.forEach(function(outcam){
				if(outcam.hostname == ip) {
					found = 1;
				}
			});

			if(!found) {
				cams.push(cam);
			}
		});

		console.log(typeof(cams));
		var outputs = [];
		var camCount = cams.length;
		cams.forEach(function (cam) {
			var thisCam = cam;
			var output = {
				"IP" : cam.hostname,
				"Port" : cam.port,
				"Path" : cam.path
			};

			var checkEmpty = 0;
			
			
			this_wrapper.getScopes({
				address: thisCam.hostname + ":" + thisCam.port,
				onDone: function (scopes) {
					cam.getDeviceInformation(function(err, stream) {
						
						console.log(camCount);
						console.log(stream);
						
						output.scopes = scopes;
						
						if(typeof(stream) === "undefined" ) {
							checkEmpty = 1;
						} else {
							output.Manufacturer = stream.manufacturer;
							output.Model = stream.model;
							output.Serial = stream.serial;
							output.Version = stream.version;
						}
						if(checkEmpty) {
							output.check = 0;
							outputs.push(output);
						} else {
							output.check = 1;
							outputs.push(output);
						}
						if(typeof(outputs) === "undefined"){
							input.onFail({
								"Error": "NO response"
							});
						} else{
							console.log("Finalllllllllllllllllllllllllllllllllll");
							console.log(outputs);
							if(0 == --camCount) {
								input.onDone(outputs);
							}
						}
					});
				}
			});

		});
	});
	*/
};

onvifc.prototype.getScopes = function (input) {
/*
	this.execute(
		'GetScopes',
		'',
		'',
		input.onDone,
		input.onFail
	);
*/
	var this_wrapper = this;
	new Cam(
		{
			hostname: this_wrapper.data.host,
			port: this_wrapper.data.port,
			username: this_wrapper.data.user,
			password: <PASSWORD>
		},function(err){
			this.getScopes(function(err, stream){
				if (stream) {
					input.onDone(stream);
				} else {
					input.onFail("password error");
				}
			
			});
		}
	);
		
};

onvifc.prototype.getHostname = function (input) {
	var this_wrapper = this;
	new Cam(
		{
			hostname: this_wrapper.data.host,
			port: this_wrapper.data.port,
			username: this_wrapper.data.user,
			password: <PASSWORD>
		},function(err){
			this.getHostname(function(err, stream){
				if (stream) {
					input.onDone(stream);
				} else {
					input.onFail("password error");
				}
			
			});
		}
	);
		
};

onvifc.prototype.getSystemDateAndTime = function (input) {
	var this_wrapper = this;
	new Cam(
		{
			hostname: this_wrapper.data.host,
			port: this_wrapper.data.port,
			username: this_wrapper.data.user,
			password: <PASSWORD>
		},function(err){
			this.getSystemDateandTime(function(err, stream){
				if (stream) {
					input.onDone(stream);
				} else {
					input.onFail("password error");
				}
			
			});
		}
	);
	
};

onvifc.prototype.setSystemDateAndTime = function (input) {
	var options = {};
	options.dateTimeType = input.DateTimeType;
	options.daylightSavings = input.DaylightSavings;
	options.timezone = input.TimeZone;
	options.year = input.Year;
	options.month = input.Month;
	options.day = input.Day;
	options.hour = input.Hour;
	options.minute = input.Minute;
	options.second = input.Second;

	var this_wrapper = this;
	new Cam(
		{
			hostname: this_wrapper.data.host,
			port: this_wrapper.data.port,
			username: this_wrapper.data.user,
			password: <PASSWORD>
		},function(err){
			this.setSystemDateAndTime(options, function(err, stream){
				if (stream) {
					input.onDone(stream);
				} else {<commit_msg>fix month rule and error word<commit_after>/* ImonCloud ONVIF connector for nodejs 
updated: 2014/07 

NOTE: 1) 此檔要全力避免使用 event.XXXX 2)

todo:
init 要加入判斷帳號/密碼是否正確 
	
*/

/* IC method */
if (typeof(LOG) === "undefined") {
	var LOG = {};
	if (typeof(LOG.warn) === "undefined") {
		LOG.warn = function (ret) {
			console.warn(ret);
		}
	}
	if (typeof(LOG.error) === "undefined") {
		LOG.error = function (ret) {
			console.error(ret);
		}
	}
	if (typeof(LOG.stack) == "undefined") {
		LOG.stack = function (ret) {
			console.trace();
		}
	}
}

//var onDone;

var sys = require('sys'); // http://nodejs.org/api.html#_child_processes
var exec = require('child_process').exec;
var Cam = require('onvif').Cam;
var discovery = require('onvif').Discovery;
var http = require('http');
var onvif = require('onvif');
//~ var child;

//var obj = {};

function onvifc(input) {
	// the constructor
	if (typeof(input) !== "undefined") {
		this.data = input;
		this.init(input);
	}
	return this;
}

//~ var INIT = function (obj) {
onvifc.prototype.init = function (input) {
	this.data = input;
	LOG.warn('initiating: ip-' + this.data.host 
		+ ' port-' + this.data.port 
		+ ' username-' + this.data.user 
		+ ' password-' + this.data.passwd + ' ');

	input.onDone(this.__proto__);
	return this;
};

onvifc.prototype.autoScan = function(input){
	var this_wrapper = this;
	
	var cams = {};
	
	onvif.Discovery.on('device', function(cam, remoteInfo, responseXML){
	
		console.log("CAM - typeof: " + typeof(cam) + " - content - "); console.log(cam);
		console.log("RemoteInfo - typeof: " + typeof(remoteInfo) + " - content - "); console.log(remoteInfo);
		console.log("XML - typeof: " + typeof(responseXML) + " - content - "); console.log(""+responseXML);

		var myScopes = cam.probeMatches.probeMatch.scopes;
		if (myScopes._) {
			myScopes = myScopes._
		}
		var scopes = myScopes.toString().split(' ');
		var scopes_obj = {};
		Object.keys(scopes).forEach(function(key){
			var f = scopes[key].split('/')[3], b = scopes[key].split('/')[4];
			
			if (scopes_obj[f]) {
				scopes_obj[f] += (" " + b);
			} else {
				scopes_obj[f] = b;
			}
		});
		
		var address = cam.probeMatches.probeMatch.XAddrs.split('/')[2];
		var camData = {
			"IP" : address.split(':')[0],
			"Port" : address.split(':')[1],
			"endpointReference": cam.probeMatches.probeMatch.endpointReference,
			"Type": cam.probeMatches.probeMatch.types.split(':')[1],
			"Scopes": scopes_obj,
			"Path": cam.probeMatches.probeMatch.XAddrs
		};
		
		if ( camData.IP && typeof camData.Port == 'undefined') {
			camData.Port = 80
		};
		
		cams[address] = (camData);
	});
	
	
	onvif.Discovery.probe({resolve: false}, function () {
		
		input.onDone( Object.keys(cams).map(function(address) { return cams[address]; }) );
	});
	
	
	/*
	discovery.probe(function(_null, rinfo){
		
		rinfo.forEach(function(cam){
			var ip = cam.hostname;
			var found = 0;
			cams.forEach(function(outcam){
				if(outcam.hostname == ip) {
					found = 1;
				}
			});

			if(!found) {
				cams.push(cam);
			}
		});

		console.log(typeof(cams));
		var outputs = [];
		var camCount = cams.length;
		cams.forEach(function (cam) {
			var thisCam = cam;
			var output = {
				"IP" : cam.hostname,
				"Port" : cam.port,
				"Path" : cam.path
			};

			var checkEmpty = 0;
			
			
			this_wrapper.getScopes({
				address: thisCam.hostname + ":" + thisCam.port,
				onDone: function (scopes) {
					cam.getDeviceInformation(function(err, stream) {
						
						console.log(camCount);
						console.log(stream);
						
						output.scopes = scopes;
						
						if(typeof(stream) === "undefined" ) {
							checkEmpty = 1;
						} else {
							output.Manufacturer = stream.manufacturer;
							output.Model = stream.model;
							output.Serial = stream.serial;
							output.Version = stream.version;
						}
						if(checkEmpty) {
							output.check = 0;
							outputs.push(output);
						} else {
							output.check = 1;
							outputs.push(output);
						}
						if(typeof(outputs) === "undefined"){
							input.onFail({
								"Error": "NO response"
							});
						} else{
							console.log("Finalllllllllllllllllllllllllllllllllll");
							console.log(outputs);
							if(0 == --camCount) {
								input.onDone(outputs);
							}
						}
					});
				}
			});

		});
	});
	*/
};

onvifc.prototype.getScopes = function (input) {
/*
	this.execute(
		'GetScopes',
		'',
		'',
		input.onDone,
		input.onFail
	);
*/
	var this_wrapper = this;
	new Cam(
		{
			hostname: this_wrapper.data.host,
			port: this_wrapper.data.port,
			username: this_wrapper.data.user,
			password: <PASSWORD>
		},function(err){
			this.getScopes(function(err, stream){
				if (stream) {
					input.onDone(stream);
				} else {
					input.onFail("password error");
				}
			
			});
		}
	);
		
};

onvifc.prototype.getHostname = function (input) {
	var this_wrapper = this;
	new Cam(
		{
			hostname: this_wrapper.data.host,
			port: this_wrapper.data.port,
			username: this_wrapper.data.user,
			password: <PASSWORD>
		},function(err){
			this.getHostname(function(err, stream){
				if (stream) {
					input.onDone(stream);
				} else {
					input.onFail("password error");
				}
			
			});
		}
	);
		
};

onvifc.prototype.getSystemDateAndTime = function (input) {
	var this_wrapper = this;
	new Cam(
		{
			hostname: this_wrapper.data.host,
			port: this_wrapper.data.port,
			username: this_wrapper.data.user,
			password: <PASSWORD>
		},function(err){
			this.getSystemDateAndTime(function(err, stream){
				if (stream) {
					input.onDone(stream);
				} else {
					input.onFail("password error");
				}
			
			});
		}
	);
	
};

onvifc.prototype.setSystemDateAndTime = function (input) {
	var options = {};
	options.dateTimeType = input.DateTimeType;
	options.daylightSavings = input.DaylightSavings;
	options.timezone = input.TimeZone;
	options.year = input.Year;
	if (input.Month == '1') {
		options.month = '12';
	} else {
		options.month = input.Month - 1;
	}
	options.day = input.Day;
	options.hour = input.Hour;
	options.minute = input.Minute;
	options.second = input.Second;

	var this_wrapper = this;
	new Cam(
		{
			hostname: this_wrapper.data.host,
			port: this_wrapper.data.port,
			username: this_wrapper.data.user,
			password: <PASSWORD>
		},function(err){
			this.setSystemDateAndTime(options, function(err, stream){
				if (stream) {
					input.onDone(stream);
				} else {
{'fraction_non_alphanumeric': 0.16809565012192243, 'fraction_numerical': 0.003932981986942499, 'mean_word_length': 2.935004642525534, 'pattern_counts': {'":': 12, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2176849', 'n_tokens_mistral': 5629, 'n_tokens_neox': 4584, 'n_words': 798}