ProjectFile.prototype._FileTransmission_= function(that, file)
{
	var ssocket = io.connect();
	var stream = ss.createStream();
	var blobStream = ss.createBlobReadStream(file);
	
	blobStream.on('data', function(chunk) { 
		console.log('data chunk.length:',chunk.length);
	});
	blobStream.on('end', function() {
		$(that).closest(".Row").find(".Loading").eq(0).hide();
		//$(that).closest(".Row").find(".Message").eq(0).show();
		$(that).closest("div").find(":file").val("");
	});
	
	blobStream.pipe(stream);
	ss(ssocket).emit('UploadFile', stream, {name: file.name});
}

ProjectFile.prototype._CheckProjectFileName_ = function(that, FileName)
{
	var Return=true;
	if(!(FileName.indexOf("Project") != -1 && FileName.indexOf(".tar") != -1))
	{
		$(that).closest(".Row").find(".Loading").hide();
		$(that).closest(".Row").find(".Message").eq(1).show();
		Return=false;
		return Return;
	}
	$(that).closest(".Row").find(".Message").hide();
	return Return;
};

ProjectFile.prototype._UploadProjectFile_ = function(that, Event)
{
	var file = Event.target.files[0];
	if(file == null)	{return 0;}
	
	$(that).closest(".Row").find(".Message").hide();
	$(that).closest(".Row").find(".Loading").eq(0).show();
	var FileName = file.name;
	$(that).closest("div").find(":text").val(file.name);
	if(ProjectFile.prototype._CheckProjectFileName_(that, FileName) == false)		{return 0;}
	ProjectFile.prototype._FileTransmission_(that, file);
};

ProjectFile.prototype._UpdateProjectFile_ = function(that)
{
	StepFlow.prototype._LockAllUi_();
	$(that).closest(".Row").find(".Message").hide();
	$(that).closest(".Row").find(".Loading").eq(1).show();
	FileName = $(that).closest("div").find(":text").val();
	$(that).closest("div").find(":text").val("");
	var ssocket = io.connect();
	ssocket.emit("UpdateProjectFile", FileName);
	ssocket.on("UpdateProjectFile", function (Return) {
		if(Return == true)
		{
			var Class = new Main();
		    $.when(Class.AsynchronousInitializeGlobalVariables())
		    .done(function(res){
			if(res == true)
			{
				//RTU data Synchronize
				var RtuModuleIndex = Object.keys(Global_RtuVariable);
				for(var i=0; i<RtuModuleIndex.length; i++)
				{
					RtuModuleSecondPage.prototype._SynchronizeOtherServiceData_(RtuModuleIndex[i]);
				}
				//TCP data Synchronize
				var TcpModuleIndex = Object.keys(Global_TcpVariable);
				for(var i=0; i<TcpModuleIndex.length; i++)
				{
					TcpModuleSecondPage.prototype._SynchronizeOtherServiceData_(TcpModuleIndex[i]);
				}
				//ASCII data Synchronize
				var AsciiModuleIndex = Object.keys(Global_AsciiVariable);
				for(var i=0; i<AsciiModuleIndex.length; i++)
				{
					AsciiModuleSecondPage.prototype._SynchronizeOtherServiceData_(AsciiModuleIndex[i]);
				}
				//MQTT data Synchronize
				var MqttModuleIndex = Object.keys(Global_MqttVariable);
				for(var i=0; i<MqttModuleIndex.length; i++)
				{
					MqttModuleSecondPage.prototype._SynchronizeOtherServiceData_(MqttModuleIndex[i]);
				}
				//Save All Global variable
				var Result1={"Input":null};  var Result2={"Input":null};  var Result3={"Input":null};
    			$.when(RtuModuleFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_(Result1))
    			.then(function(res){
      			if(res){
      				return TcpModuleFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_(Result2);
      			}})
    			.then(function(res){
      			if(res == true){
					return MqttModuleFirstPage.prototype._SaveDataLayerAndServiceLayerVariables_(Result3);
      			}})
    			.then(function(res){
      			if(res == true){
					//show message
					$(that).closest(".Row").find(".Loading").hide();
					$(that).closest(".Row").find(".Message").eq(3).show();
					StepFlow.prototype._UnLockAllUi_();
					
					ProjectFile.prototype._SaveXml_(that);
					ProjectFile.prototype._UpdateBootService_(that);
      			}});
			}
			});
		}
		else{
			$(that).closest(".Row").find(".Loading").hide();
			$(that).closest(".Row").find(".Message").eq(4).show();
		}
		ssocket.emit("SocketClose");
	});
};

ProjectFile.prototype._RunMainProgram_ = function(that)
{
	var ssocket = io.connect();
	ssocket.emit('StartMainSystem');
	ssocket.on('StartMainSystem', function (Return) {
		if(Return == true)
		{
			$(that).closest(".Row").find(".Message").eq(3).show();
		}
		else
		{
			$(that).closest(".Row").find(".Message").eq(4).show();
		}
	});
}

ProjectFile.prototype._StopMainProgram_ = function(that)
{
	var ssocket = io.connect();
	ssocket.emit('StopMainSystem');
	ssocket.on('StopMainSystem', function (Return) {
		if(Return == true)
		{
			$(that).closest(".Row").find(".Message").eq(3).show();
		}
		else
		{
			$(that).closest(".Row").find(".Message").eq(4).show();
		}
	});
}

ProjectFile.prototype._RemoveProjectFile_ = function(that)
{
	$(that).closest('.Row').find(".Loading").show();
	$(that).closest('.Row').find(".Message").hide();
	var ssocket = io.connect();
	ssocket.emit("RemoveProjectFile");
	ssocket.on("RemoveProjectFile", function (Return) {
		if(Return == true)
		{
			var Class = new Main();
			Class.AsynchronousInitializeGlobalVariables();
			Class=null;
			$(that).closest('.Row').find(".Loading").hide();
			$(that).closest('.Row').find(".Message").eq(0).show();
		}
		else
		{
			$(that).closest('.Row').find(".Loading").hide();
			$(that).closest('.Row').find(".Message").eq(1).show();
		}
		ssocket.emit("SocketClose");
	});
};

ProjectFile.prototype._DownloadProjectFile_ = function(that)
{
	$(that).closest('.Row').find(".Loading").show();
	$(that).closest('.Row').find(".Message").hide();
	var ssocket = io.connect();
	ssocket.emit("DownloadProjectFile");
	ssocket.on("DownloadProjectFile", function (Return) {
		if(Return == true)
		{
			location.href='/DownloadFile/Project_'+location.hostname+'_.tar';
			var array = new Array();
			array[0]=300;
			ssocket.emit("DeleteTarFile", array);
		}
		ssocket.emit("SocketClose");
	});
};

ProjectFile.prototype._UpdateBootService_ = function(that)
{
    var Result = {
        "Input": null
    };
    var Class = new LoadFile();
    Class.ReadFile("./File/Boot.json", new Object(), Result);
    var SetInterval = setInterval(function() {
        if (Result["Input"] != null) {
            clearInterval(SetInterval);
            Class = null;
            var array = new Array();
            array[0] = Result["Input"]["Main System Run at startup"];
            array[1] = Result["Input"]["MQTT Broker Run at startup"];
            array[2] = Result["Input"]["DDNS Run at startup"];
            var ssocket = io.connect();
            ssocket.emit('SaveBootAllData', array);
        }
    }, 500);
};

ProjectFile.prototype._SaveXml_ = function(that)
{
	var Result_1={"Input":null}, Result_2={"Input":null};
	var Class = new ControllerServiceSetting();
	Class.SaveDeviceDri(Result_1);
	Class.SaveServiceXml(Result_2);
	var SetInterval = setInterval(function()
	{
		if(Result_1["Input"]==true && Result_2["Input"]==true)
		{
			clearInterval(SetInterval);
			ProjectFile.prototype._RunMainProgram_(that);
		}
	},500);
};