var compression    = require('compression');
var express        = require('express');
var app            = express();
var http           = require('http').Server(app);
var io             = require('socket.io')(http);
var ss             = require('socket.io-stream');
var path           = require('path');
var fs             = require('fs');
var InstructionSet = require('./Server/ArmAngstromInstructionSet.js');
var ModuleInformation = require('./Server/ModuleInformation.js');
var aesjs = require('aes-js');

//DBus
var DBus           = require('./node_modules/DBus');
var Bus            = DBus.getBus('session');
var Dbus           = require('./Server/DBus.js');

//全域變數
var CommandFunction = new Object();
CommandFunction     = InstructionSet.Initialization();
var ModuleData      = new Object();
ModuleData          = ModuleInformation.Initialization();

//初始化
var MainPageConnection      = false;

var BusObject             = new Object();
BusObject["BusInterface"] = undefined;
var DbusFunction          = new Object();
DbusFunction              = Dbus.Initialization();
DbusFunction["GetDbusInterface"](Bus, BusObject);





io.on('connection', function(socket) {

  socket.on('SocketClose', function(ParameterArray) {
    socket.disconnect();
    socket = null; delete socket;
  });
// 取得Flash 資訊
socket.on('GetFlashVersionInformation', function(ParameterArray) {
  var Return = CommandFunction["GetFlashVersionInformation"](ParameterArray);
    socket.emit('GetFlashVersionInformation', Return);
  });
//憑證清單
socket.on('MQTTCertificateList', function(ParameterArray) {
  var Return = CommandFunction["MQTTCertificateList"](ParameterArray);
    socket.emit('MQTTCertificateList', Return);
  });
//PIN 死亡次數
socket.on('KillCount', function(ParameterArray) {
  var Return = CommandFunction["KillCount"](ParameterArray);
    socket.emit('KillCount', Return);
  });

//Sim密碼確認
socket.on('InputSimCardPassword', function(ParameterArray) {
  var Return = CommandFunction["InputSimCardPassword"](ParameterArray);
    socket.emit('InputSimCardPassword', Return);
  });

//DataLog　下載
socket.on('ShowSDFilePath', function(ParameterArray) {
  var Return = CommandFunction["ShowSDFilePath"](ParameterArray);
    socket.emit('ShowSDFilePath', Return);
  });

//檔案 上傳下載刪除
  ss(socket).on('UploadFile', function(stream, data) {
	var filename = path.basename(data.name);
	var filepath = path.join('/app/Web/Temporary/', filename);
	var ws = fs.createWriteStream(filepath);
	stream.pipe(ws);
	socket.emit('UploadFile', true);
  });
  ss(socket).on('UploadFirmwareFile', function(stream, data) {
  var filename = path.basename(data.name);
  var filepath = path.join('/mnt/update/', filename);
  var ws = fs.createWriteStream(filepath);
  stream.pipe(ws);
  socket.emit('UploadFirmwareFile', true);
  });
  socket.on('RemoveProjectFile', function(ParameterArray) {
	var Return = CommandFunction["RemoveProjectFile"](ParameterArray);
		socket.emit('RemoveProjectFile', Return);
  });
  socket.on('RemoveOpcuaTrustedCertificate', function(ParameterArray) {
	var Return = CommandFunction["RemoveOpcuaTrustedCertificate"](ParameterArray);
		socket.emit('RemoveOpcuaTrustedCertificate', Return);
  });
  socket.on('RemoveOpcuaServerCertificate', function(ParameterArray) {
  var Return = CommandFunction["RemoveOpcuaServerCertificate"](ParameterArray);
    socket.emit('RemoveOpcuaServerCertificate', Return);
  });
  socket.on('RemoveMqttTrustedCertificate', function(ParameterArray) {
  var Return = CommandFunction["RemoveMqttTrustedCertificate"](ParameterArray);
    socket.emit('RemoveMqttTrustedCertificate', Return);
  });
  socket.on('RemoveMqttCertificate', function(ParameterArray) {
  var Return = CommandFunction["RemoveMqttCertificate"](ParameterArray);
    socket.emit('RemoveMqttCertificate', Return);
  });
  socket.on('RemoveMqttPrivateKey', function(ParameterArray) {
  var Return = CommandFunction["RemoveMqttPrivateKey"](ParameterArray);
    socket.emit('RemoveMqttPrivateKey', Return);
  });
  socket.on('DownloadProjectFile', function(ParameterArray) {
	var Return = CommandFunction["DownloadProjectFile"](ParameterArray);
		socket.emit('DownloadProjectFile', Return);
  });
  socket.on('DownloadOpcuaServerCertificate', function(ParameterArray) {
	var Return = CommandFunction["DownloadOpcuaServerCertificate"](ParameterArray);
		socket.emit('DownloadOpcuaServerCertificate', Return);
  });
  socket.on('UpdateProjectFile', function(ParameterArray) {
	var Return = CommandFunction["UpdateProjectFile"](ParameterArray);
		socket.emit('UpdateProjectFile', Return);
  });
  socket.on('UpdateFirmwareFile', function(ParameterArray) {
  var Return = CommandFunction["UpdateFirmwareFile"](ParameterArray);
    console.log("Hi, app.js_1");
    socket.emit('UpdateFirmwareFile', Return);
  });
  socket.on('UpdateOpcuaTrustedCertificate', function(ParameterArray) {
	var Return = CommandFunction["UpdateOpcuaTrustedCertificate"](ParameterArray);
		socket.emit('UpdateOpcuaTrustedCertificate', Return);
  });
  socket.on('UpdateMqttTrustedCertificate', function(ParameterArray) {
  var Return = CommandFunction["UpdateMqttTrustedCertificate"](ParameterArray);
    socket.emit('UpdateMqttTrustedCertificate', Return);
  });
  socket.on('UpdateMqttCertificate', function(ParameterArray) {
  var Return = CommandFunction["UpdateMqttCertificate"](ParameterArray);
    socket.emit('UpdateMqttCertificate', Return);
  });
  socket.on('UpdateMqttPrivateKey', function(ParameterArray) {
  var Return = CommandFunction["UpdateMqttPrivateKey"](ParameterArray);
    socket.emit('UpdateMqttPrivateKey', Return);
  });
  socket.on('UpdateRtuIcpdasModule', function(ParameterArray) {
  var Return = CommandFunction["UpdateRtuIcpdasModule"](ParameterArray);
    socket.emit('UpdateRtuIcpdasModule', Return);
  });
  socket.on('UpdateTcpIcpdasModule', function(ParameterArray) {
  var Return = CommandFunction["UpdateTcpIcpdasModule"](ParameterArray);
    socket.emit('UpdateTcpIcpdasModule', Return);
  });
  socket.on('UpdateMqttIcpdasModule', function(ParameterArray) {
  var Return = CommandFunction["UpdateMqttIcpdasModule"](ParameterArray);
    socket.emit('UpdateMqttIcpdasModule', Return);
  });
  socket.on('DeleteTarFile', function(ParameterArray) {
	var Return = CommandFunction["DeleteTarFile"](ParameterArray);
		socket.emit('DeleteTarFile', Return);
  });

//REST API
  socket.on('GenerateLoginCredentials', function(ParameterArray) {
	var Return = CommandFunction["GenerateLoginCredentials"](ParameterArray);
		socket.emit('GenerateLoginCredentials', Return);
  });
  socket.on('CheckLoginCredentials', function(ParameterArray) {
	var Return = CommandFunction["CheckLoginCredentials"](ParameterArray);
	  socket.emit('CheckLoginCredentials', Return);
  });
  socket.on('BackgroundExecution', function(ParameterArray) {
	  socket.emit('BackgroundExecution', true);
  });
  socket.on('SaveFinished', function(ParameterArray) {
	  socket.emit('SaveFinished', true);
  });
  socket.on('WebShell', function(ParameterArray) {
  var Return = CommandFunction["WebShell"](ParameterArray);
    socket.emit('WebShell', Return);
  });
  socket.on('GetWebAllLink', function(ParameterArray) {
  var Return = CommandFunction["GetWebAllLink"](ParameterArray);
    socket.emit('GetWebAllLink', Return);
  });


//JSON轉換XML
  socket.on('SaveVariableTableXml', function(ParameterArray) {
	var Return = CommandFunction["SaveVariableTableXml"](ParameterArray);
		socket.emit('SaveVariableTableXml', Return);
  });
  socket.on('SaveModbusDri', function(ParameterArray) {
	var Return = CommandFunction["SaveModbusDri"](ParameterArray);
		socket.emit('SaveModbusDri', Return);
  });
  socket.on('SaveMqttDri', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttDri"](ParameterArray);
    socket.emit('SaveMqttDri', Return);
  });
  socket.on('SaveUAServerDataXml', function(ParameterArray) {
	var Return = CommandFunction["SaveUAServerDataXml"](ParameterArray);
		socket.emit('SaveUAServerDataXml', Return);
  });
  socket.on('SaveMQTTServiceXml', function(ParameterArray) {
	var Return = CommandFunction["SaveMQTTServiceXml"](ParameterArray);
		socket.emit('SaveMQTTServiceXml', Return);
  });
  socket.on('SavePidDriverDri', function(ParameterArray) {
	var Return = CommandFunction["SavePidDriverDri"](ParameterArray);
		socket.emit('SavePidDriverDri', Return);
  });
  socket.on('SaveMethodTableXml', function(ParameterArray) {
	var Return = CommandFunction["SaveMethodTableXml"](ParameterArray);
		socket.emit('SaveMethodTableXml', Return);
  });
  socket.on('SaveDataLogServiceXml', function(ParameterArray) {
  var Return = CommandFunction["SaveDataLogServiceXml"](ParameterArray);
    socket.emit('SaveDataLogServiceXml', Return);
  });
  socket.on('SaveScaleVariableTableXml', function(ParameterArray) {
  var Return = CommandFunction["SaveScaleVariableTableXml"](ParameterArray);
    socket.emit('SaveScaleVariableTableXml', Return);
  });
  socket.on('SaveBitwiseVariableTableXml', function(ParameterArray) {
  var Return = CommandFunction["SaveBitwiseVariableTableXml"](ParameterArray);
    socket.emit('SaveBitwiseVariableTableXml', Return);
  });
//語系翻譯
  socket.on('SaveLanguage', function(ParameterArray) {
	var Return = CommandFunction["SaveLanguage"](ParameterArray);
		socket.emit('SaveLanguage', Return);
  });
//開機
  socket.on('DeviceReboot', function(ParameterArray) {
	var Return = CommandFunction["DeviceReboot"](ParameterArray);
		socket.emit('DeviceReboot', Return);
  });
  socket.on('SaveBootAllData', function(ParameterArray) {
	var Return = CommandFunction["SaveBootAllData"](ParameterArray);
		socket.emit('SaveBootAllData', Return);
  });
  socket.on('ServerStarts', function(ParameterArray) {
		socket.emit('ServerStarts', true);
  });
//帳號設定
  socket.on('SaveAccountSettingAllData', function(ParameterArray) {
	var Return = CommandFunction["SaveAccountSettingAllData"](ParameterArray);
		socket.emit('SaveAccountSettingAllData', Return);
  });
  socket.on('CheckAccountPassword', function(ParameterArray) {
	var Return = CommandFunction["CheckAccountPassword"](ParameterArray);
		socket.emit('CheckAccountPassword', Return);
  });
//功能選單
  socket.on('GetMainSystemStatus', function(ParameterArray) {
	var Return = CommandFunction["GetMainSystemStatus"](ParameterArray);
		socket.emit('GetMainSystemStatus', Return);
  });
  socket.on('StartMainSystem', function(ParameterArray) {
	var Return = CommandFunction["StartMainSystem"](ParameterArray);
		socket.emit('StartMainSystem', Return);
  });
  socket.on('StopMainSystem', function(ParameterArray) {
	var Return = CommandFunction["StopMainSystem"](ParameterArray);
		socket.emit('StopMainSystem', Return);
  });
  socket.on('GetMqttBrokerStatus', function(ParameterArray) {
	var Return = CommandFunction["GetMqttBrokerStatus"](ParameterArray);
		socket.emit('GetMqttBrokerStatus', Return);
  });
  socket.on('StartMqttBroker', function(ParameterArray) {
	var Return = CommandFunction["StartMqttBroker"](ParameterArray);
		socket.emit('StartMqttBroker', Return);
  });
  socket.on('StopMqttBroker', function(ParameterArray) {
	var Return = CommandFunction["StopMqttBroker"](ParameterArray);
		socket.emit('StopMqttBroker', Return);
  });
  socket.on('StartBackstageService', function(ParameterArray) {
  var Return = CommandFunction["StartBackstageService"](ParameterArray);
    socket.emit('StartBackstageService', Return);
  });
//網路設定
  socket.on('GetDdnsStatus', function(ParameterArray) {
	var Return = CommandFunction["GetDdnsStatus"](ParameterArray);
		socket.emit('GetDdnsStatus', Return);
  });
  socket.on('StartDdns', function(ParameterArray) {
	var Return = CommandFunction["StartDdns"](ParameterArray);
		socket.emit('StartDdns', Return);
  });
  socket.on('StopDdns', function(ParameterArray) {
	var Return = CommandFunction["StopDdns"](ParameterArray);
		socket.emit('StopDdns', Return);
  });
  socket.on('Lan1_GetIpv4Ip', function(ParameterArray) {
	var Return = CommandFunction["Lan1_GetIpv4Ip"](ParameterArray);
		socket.emit('Lan1_GetIpv4Ip', Return);
  });
  socket.on('Lan1_SetIpv4StaticIp', function(ParameterArray) {
	var Return = CommandFunction["Lan1_SetIpv4StaticIp"](ParameterArray);
		socket.emit('Lan1_SetIpv4StaticIp', Return);
  });
  socket.on('Lan1_SetDhcp', function(ParameterArray) {
	var Return = CommandFunction["Lan1_SetDhcp"](ParameterArray);
		socket.emit('Lan1_SetDhcp', Return);
  });


  socket.on('Lan2_GetIpv4Ip', function(ParameterArray) {
  var Return = CommandFunction["Lan2_GetIpv4Ip"](ParameterArray);
    socket.emit('Lan2_GetIpv4Ip', Return);
  });
  socket.on('Lan2_SetIpv4StaticIp', function(ParameterArray) {
  var Return = CommandFunction["Lan2_SetIpv4StaticIp"](ParameterArray);
    socket.emit('Lan2_SetIpv4StaticIp', Return);
  });
  socket.on('Lan2_SetDhcp', function(ParameterArray) {
  var Return = CommandFunction["Lan2_SetDhcp"](ParameterArray);
    socket.emit('Lan2_SetDhcp', Return);
  });



  socket.on('GetHostname', function(ParameterArray) {
	var Return = CommandFunction["GetHostname"](ParameterArray);
		socket.emit('GetHostname', Return);
  });
  socket.on('SetHostname', function(ParameterArray) {
	var Return = CommandFunction["SetHostname"](ParameterArray);
		socket.emit('SetHostname', Return);
  });
  socket.on('GetDdns', function(ParameterArray) {
	var Return = CommandFunction["GetDdns"](ParameterArray);
		socket.emit('GetDdns', Return);
  });
  socket.on('SetDdns', function(ParameterArray) {
	var Return = CommandFunction["SetDdns"](ParameterArray);
		socket.emit('SetDdns', Return);
  });

  socket.on('DetectionModel', function(ParameterArray) {
  var Return = CommandFunction["DetectionModel"](ParameterArray);
    socket.emit('DetectionModel', Return);
  });
//無線
  socket.on('IsExistWirelessDevice', function(ParameterArray) {
	var Return = CommandFunction["IsExistWirelessDevice"](ParameterArray);
		socket.emit('IsExistWirelessDevice', Return);
  });
  socket.on('WirelessConnection', function(ParameterArray) {
	var Return = CommandFunction["WirelessConnection"](ParameterArray);
		socket.emit('WirelessConnection', Return);
  });
  socket.on('WirelessDisconnect', function(ParameterArray) {
	var Return = CommandFunction["WirelessDisconnect"](ParameterArray);
		socket.emit('WirelessDisconnect', Return);
  });
  socket.on('GetWirelessData', function(ParameterArray) {
	var Return = CommandFunction["GetWirelessData"](ParameterArray);
		socket.emit('GetWirelessData', Return);
  });
  socket.on('SaveWirelessData', function(ParameterArray) {
	var Return = CommandFunction["SaveWirelessData"](ParameterArray);
		socket.emit('SaveWirelessData', Return);
  });
  socket.on('GetWirelessInformation', function(ParameterArray) {
	var Return = CommandFunction["GetWirelessInformation"](ParameterArray);
		socket.emit('GetWirelessInformation', Return);
  });
//COM Port介面設定
  socket.on('SaveComPortSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveComPortSetting"](ParameterArray);
		socket.emit('SaveComPortSetting', Return);
  });
//RTU模組
  socket.on('SaveRtuIndex', function(ParameterArray) {
	var Return = CommandFunction["SaveRtuIndex"](ParameterArray);
		socket.emit('SaveRtuIndex', Return);
  });
  socket.on('SaveRtuModule', function(ParameterArray) {
	var Return = CommandFunction["SaveRtuModule"](ParameterArray);
		socket.emit('SaveRtuModule', Return);
  });
  socket.on('SaveRtuSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveRtuSetting"](ParameterArray);
		socket.emit('SaveRtuSetting', Return);
  });
  socket.on('SaveRtuVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveRtuVariable"](ParameterArray);
		socket.emit('SaveRtuVariable', Return);
  });
  socket.on('SaveRtuScalingVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRtuScalingVariable"](ParameterArray);
    socket.emit('SaveRtuScalingVariable', Return);
  });
   socket.on('SaveRtuBitwiseVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRtuBitwiseVariable"](ParameterArray);
    socket.emit('SaveRtuBitwiseVariable', Return);
  });
//ASCII模組
  socket.on('SaveAsciiIndex', function(ParameterArray) {
	var Return = CommandFunction["SaveAsciiIndex"](ParameterArray);
		socket.emit('SaveAsciiIndex', Return);
  });
  socket.on('SaveAsciiModule', function(ParameterArray) {
	var Return = CommandFunction["SaveAsciiModule"](ParameterArray);
		socket.emit('SaveAsciiModule', Return);
  });
  socket.on('SaveAsciiSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveAsciiSetting"](ParameterArray);
		socket.emit('SaveAsciiSetting', Return);
  });
  socket.on('SaveAsciiVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveAsciiVariable"](ParameterArray);
		socket.emit('SaveAsciiVariable', Return);
  })
//TCP模組
  socket.on('SaveTcpIndex', function(ParameterArray) {
	var Return = CommandFunction["SaveTcpIndex"](ParameterArray);
		socket.emit('SaveTcpIndex', Return);
  });
  socket.on('SaveTcpModule', function(ParameterArray) {
	var Return = CommandFunction["SaveTcpModule"](ParameterArray);
		socket.emit('SaveTcpModule', Return);
  });
  socket.on('SaveTcpSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveTcpSetting"](ParameterArray);
		socket.emit('SaveTcpSetting', Return);
  });
  socket.on('SaveTcpVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveTcpVariable"](ParameterArray);
		socket.emit('SaveTcpVariable', Return);
  });
  socket.on('SaveTcpScalingVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveTcpScalingVariable"](ParameterArray);
    socket.emit('SaveTcpScalingVariable', Return);
  });
   socket.on('SaveTcpBitwiseVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveTcpBitwiseVariable"](ParameterArray);
    socket.emit('SaveTcpBitwiseVariable', Return);
  });
// Register 模組
  socket.on('SaveRegisterIndex', function(ParameterArray) {
  var Return = CommandFunction["SaveRegisterIndex"](ParameterArray);
    socket.emit('SaveRegisterIndex', Return);
  });
  socket.on('SaveRegisterModule', function(ParameterArray) {
  var Return = CommandFunction["SaveRegisterModule"](ParameterArray);
    socket.emit('SaveRegisterModule', Return);
  });
  socket.on('SaveRegisterSetting', function(ParameterArray) {
  var Return = CommandFunction["SaveRegisterSetting"](ParameterArray);
    socket.emit('SaveRegisterSetting', Return);
  });
  socket.on('SaveRegisterVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRegisterVariable"](ParameterArray);
    socket.emit('SaveRegisterVariable', Return);
  });

// MqttRemoteModules
  socket.on('SaveRemoteMqttGroup', function(ParameterArray) {
  var Return = CommandFunction["SaveRemoteMqttGroup"](ParameterArray);
    socket.emit('SaveRemoteMqttGroup', Return);
  });
  socket.on('SaveMqttGroupCloudIndex', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttGroupCloudIndex"](ParameterArray);
    socket.emit('SaveMqttGroupCloudIndex', Return);
  });

//MQTT模組
  socket.on('SaveMqttIndex', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttIndex"](ParameterArray);
    socket.emit('SaveMqttIndex', Return);
  });
  socket.on('SaveMqttModule', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttModule"](ParameterArray);
    socket.emit('SaveMqttModule', Return);
  });
  socket.on('SaveMqttSetting', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttSetting"](ParameterArray);
    socket.emit('SaveMqttSetting', Return);
  });
  socket.on('SaveMqttVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttVariable"](ParameterArray);
    socket.emit('SaveMqttVariable', Return);
  });
//轉換傳輸 OPC UA
  socket.on('SaveRtuToOpcuaVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveRtuToOpcuaVariable"](ParameterArray);
		socket.emit('SaveRtuToOpcuaVariable', Return);
  });
  socket.on('SaveRtuScalingToOpcuaVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRtuScalingToOpcuaVariable"](ParameterArray);
    socket.emit('SaveRtuScalingToOpcuaVariable', Return);
  });
  socket.on('SaveRtuBitwiseToOpcuaVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRtuBitwiseToOpcuaVariable"](ParameterArray);
    socket.emit('SaveRtuBitwiseToOpcuaVariable', Return);
  });
  socket.on('SaveTcpToOpcuaVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveTcpToOpcuaVariable"](ParameterArray);
		socket.emit('SaveTcpToOpcuaVariable', Return);
  });
  socket.on('SaveTcpScalingToOpcuaVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveTcpScalingToOpcuaVariable"](ParameterArray);
    socket.emit('SaveTcpScalingToOpcuaVariable', Return);
  });
  socket.on('SaveTcpBitwiseToOpcuaVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveTcpBitwiseToOpcuaVariable"](ParameterArray);
    socket.emit('SaveTcpBitwiseToOpcuaVariable', Return);
  });
  socket.on('SaveAsciiToOpcuaVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveAsciiToOpcuaVariable"](ParameterArray);
		socket.emit('SaveAsciiToOpcuaVariable', Return);
  });
  socket.on('SaveMqttToOpcuaVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttToOpcuaVariable"](ParameterArray);
    socket.emit('SaveMqttToOpcuaVariable', Return);
  });
  socket.on('SaveRegisterToOpcuaVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRegisterToOpcuaVariable"](ParameterArray);
    socket.emit('SaveRegisterToOpcuaVariable', Return);
  });
//IoT平台設定
  socket.on('SaveLocalBrokerSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveLocalBrokerSetting"](ParameterArray);
		socket.emit('SaveLocalBrokerSetting', Return);
  });
  socket.on('GetLocalBrokerSetting', function(ParameterArray) {
	var Return = CommandFunction["GetLocalBrokerSetting"](ParameterArray);
		socket.emit('GetLocalBrokerSetting', Return);
  });
  socket.on('SaveRemoteBrokerSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveRemoteBrokerSetting"](ParameterArray);
		socket.emit('SaveRemoteBrokerSetting', Return);
  });
  socket.on('SaveMqttJsonGroup', function(ParameterArray) {
	var Return = CommandFunction["SaveMqttJsonGroup"](ParameterArray);
		socket.emit('SaveMqttJsonGroup', Return);
  });
  socket.on('SaveAzurePlatformSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveAzurePlatformSetting"](ParameterArray);
		socket.emit('SaveAzurePlatformSetting', Return);
  });
  socket.on('SaveOpcUaLocalServer', function(ParameterArray) {
	var Return = CommandFunction["SaveOpcUaLocalServer"](ParameterArray);
		socket.emit('SaveOpcUaLocalServer', Return);
  });
//轉換傳輸 MQTT
  socket.on('SaveRtuToMqttSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveRtuToMqttSetting"](ParameterArray);
		socket.emit('SaveRtuToMqttSetting', Return);
  });
  socket.on('SaveRtuToMqttVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveRtuToMqttVariable"](ParameterArray);
		socket.emit('SaveRtuToMqttVariable', Return);
  });
  socket.on('SaveTcpToMqttSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveTcpToMqttSetting"](ParameterArray);
		socket.emit('SaveTcpToMqttSetting', Return);
  });
  socket.on('SaveTcpToMqttVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveTcpToMqttVariable"](ParameterArray);
		socket.emit('SaveTcpToMqttVariable', Return);
  });
  socket.on('SaveAsciiToMqttSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveAsciiToMqttSetting"](ParameterArray);
		socket.emit('SaveAsciiToMqttSetting', Return);
  });
  socket.on('SaveAsciiToMqttVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveAsciiToMqttVariable"](ParameterArray);
		socket.emit('SaveAsciiToMqttVariable', Return);
  });
//轉換傳輸 MQTT JSON
  socket.on('SaveRtuToMqttJsonVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveRtuToMqttJsonVariable"](ParameterArray);
		socket.emit('SaveRtuToMqttJsonVariable', Return);
  });
  socket.on('SaveTcpToMqttJsonVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveTcpToMqttJsonVariable"](ParameterArray);
		socket.emit('SaveTcpToMqttJsonVariable', Return);
  });
  socket.on('SaveAsciiToMqttJsonVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveAsciiToMqttJsonVariable"](ParameterArray);
		socket.emit('SaveAsciiToMqttJsonVariable', Return);
  });
//時間設定
  socket.on('GetDate', function(ParameterArray) {
	var Return = CommandFunction["GetDate"](ParameterArray);
		socket.emit('GetDate', Return);
  });
  socket.on('GetTime', function(ParameterArray) {
	var Return = CommandFunction["GetTime"](ParameterArray);
		socket.emit('GetTime', Return);
  });
  socket.on('GetDateTime', function(ParameterArray) {
  var Return = CommandFunction["GetDateTime"](ParameterArray);
    socket.emit('GetDateTime', Return);
  });
  socket.on('SaveManuallySetTime', function(ParameterArray) {
	var Return = CommandFunction["SaveManuallySetTime"](ParameterArray);
		socket.emit('SaveManuallySetTime', Return);
  });
  socket.on('SaveNtpSet', function(ParameterArray) {
	var Return = CommandFunction["SaveNtpSet"](ParameterArray);
		socket.emit('SaveNtpSet', Return);
  });
  socket.on('GetNtpData', function(ParameterArray) {
	var Return = CommandFunction["GetNtpData"](ParameterArray);
		socket.emit('GetNtpData', Return);
  });
//系統訊息
  socket.on('GetSystemInformation', function(ParameterArray) {
  var Return = CommandFunction["GetSystemInformation"](ParameterArray);
    socket.emit('GetSystemInformation', Return);
  });
//版本號
  socket.on('GetVersion', function(ParameterArray) {
	var Return = CommandFunction["GetVersion"](ParameterArray);
		socket.emit('GetVersion', Return);
  });
//PID
  socket.on('SavePidOperationVariable', function(ParameterArray) {
	var Return = CommandFunction["SavePidOperationVariable"](ParameterArray);
		socket.emit('SavePidOperationVariable', Return);
  });
// I/O即時顯示
  socket.on('DBusReadVariantArray', function(ParameterArray) {
	var Return = DbusFunction["DBusReadVariantArray"](ParameterArray, BusObject["BusInterface"], socket);
  });
  socket.on('DBusWriteVariantArray', function(ParameterArray) {
	var Return = DbusFunction["DBusWriteVariantArray"](ParameterArray, BusObject["BusInterface"], socket);
  });
//IFTTT通知
  socket.on('SaveIftttConditionTriggerSetting', function(ParameterArray) {
	var Return = CommandFunction["SaveIftttConditionTriggerSetting"](ParameterArray);
		socket.emit('SaveIftttConditionTriggerSetting', Return);
  });
  socket.on('SaveIftttConditionTriggerVariable', function(ParameterArray) {
	var Return = CommandFunction["SaveIftttConditionTriggerVariable"](ParameterArray);
		socket.emit('SaveIftttConditionTriggerVariable', Return);
  });
//遠端資料庫
  socket.on('SaveMsSqlConnection', function(ParameterArray) {
  var Return = CommandFunction["SaveMsSqlConnection"](ParameterArray);
    socket.emit('SaveMsSqlConnection', Return);
  });
  socket.on('SaveTcpToMsSqlVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveTcpToMsSqlVariable"](ParameterArray);
    socket.emit('SaveTcpToMsSqlVariable', Return);
  });
  socket.on('SaveRtuToMsSqlVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRtuToMsSqlVariable"](ParameterArray);
    socket.emit('SaveRtuToMsSqlVariable', Return);
  });
  socket.on('MsSqlConnectionTest', function(ParameterArray) {
  var Return = CommandFunction["MsSqlConnectionTest"](ParameterArray);
    socket.emit('MsSqlConnectionTest', Return);
  });
  socket.on('SaveMqttToMsSqlVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttToMsSqlVariable"](ParameterArray);
    socket.emit('SaveMqttToMsSqlVariable', Return);
  });
//MySQL資料庫
  socket.on('SaveMysqlConnection', function(ParameterArray) {
  var Return = CommandFunction["SaveMysqlConnection"](ParameterArray);
    socket.emit('SaveMysqlConnection', Return);
  });
  socket.on('SaveTcpToMysqlConnectionVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveTcpToMysqlConnectionVariable"](ParameterArray);
    socket.emit('SaveTcpToMysqlConnectionVariable', Return);
  });
  socket.on('SaveRtuToMysqlConnectionVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRtuToMysqlConnectionVariable"](ParameterArray);
    socket.emit('SaveRtuToMysqlConnectionVariable', Return);
  });
  socket.on('MysqlConnectionTest', function(ParameterArray) {
  var Return = CommandFunction["MysqlConnectionTest"](ParameterArray);
    socket.emit('MysqlConnectionTest', Return);
  });
  socket.on('SaveMqttToMysqlVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveMqttToMysqlVariable"](ParameterArray);
    socket.emit('SaveMqttToMysqlVariable', Return);
  });
//本地紀錄
  socket.on('SaveTcpToLocalDataLoggerVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveTcpToLocalDataLoggerVariable"](ParameterArray);
    socket.emit('SaveTcpToLocalDataLoggerVariable', Return);
  });
  socket.on('SaveRtuToLocalDataLoggerVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveRtuToLocalDataLoggerVariable"](ParameterArray);
    socket.emit('SaveRtuToLocalDataLoggerVariable', Return);
  });
  socket.on('SaveLocalDataLogger', function(ParameterArray) {
  var Return = CommandFunction["SaveLocalDataLogger"](ParameterArray);
    socket.emit('SaveLocalDataLogger', Return);
  });

//當main登入後持續改變狀態
  socket.on('MainPageConnection', function(ParameterArray) 
  {
    MainPageConnection = true;
  });


//EIP模組
  socket.on('SaveEipIndex', function(ParameterArray) {
  var Return = CommandFunction["SaveEipIndex"](ParameterArray);
    socket.emit('SaveEipIndex', Return);
  });
  socket.on('SaveEipModule', function(ParameterArray) {
  var Return = CommandFunction["SaveEipModule"](ParameterArray);
    socket.emit('SaveEipModule', Return);
  });
  socket.on('SaveEipSetting', function(ParameterArray) {
  var Return = CommandFunction["SaveEipSetting"](ParameterArray);
    socket.emit('SaveEipSetting', Return);
  });
  socket.on('SaveEipVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveEipVariable"](ParameterArray);
  socket.emit('SaveEipVariable', Return);
  });
  socket.on('SaveEipToOpcuaVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveEipToOpcuaVariable"](ParameterArray);
    socket.emit('SaveEipToOpcuaVariable', Return);
  });
  socket.on('SaveEipDri', function(ParameterArray) {
  var Return = CommandFunction["SaveEipDri"](ParameterArray);
    socket.emit('SaveEipDri', Return);
  });
  socket.on('SaveEipToMqttVariable', function(ParameterArray) {
  var Return = CommandFunction["SaveEipToMqttVariable"](ParameterArray);
    socket.emit('SaveEipToMqttVariable', Return);
  });
  socket.on('SaveEipToMqttSetting', function(ParameterArray) {
  var Return = CommandFunction["SaveEipToMqttSetting"](ParameterArray);
    socket.emit('SaveEipToMqttSetting', Return);
  });
});


//檢查main是否有發信號
var SetInterval_1 = setInterval(function()
{
  MainPageConnection = false;
  var SetInterval_2 = setInterval(function()
  {
    if(MainPageConnection == true)
    {
      CommandFunction["GenerateSignInCredentials"]();
    }
    else if(MainPageConnection == false)
    {
      CommandFunction["DeleteSignInCredentials"]();
    }
    clearInterval(SetInterval_2);
  },1000);
},5000);



app.use(express.static((path.join(__dirname, './public'))));
app.use(compression());

//檔案 下載
app.get('/DownloadFile:file(*)', function(req, res, next){
    var file = req.params.file;
    res.download('/dev/shm/' + file);
});
//Log檔案 下載
app.get('/Datalog:file(*)', function(req, res, next){
    var file = req.params.file;
    res.download('/mnt/' + file);
});

//CGI
app.get('/CGI', function(req, res, next){
    var RestApi = req.query["RestApi"];
    var ParameterArray = req.query["ParameterArray"].split("$");
    var Return = CommandFunction[RestApi](ParameterArray);
    res.send(Return);
});
app.get('/DBus', function(req, res, next){
    var RestApi = req.query["RestApi"];
    var ParameterArray = req.query["ParameterArray"].split("$");
    DbusFunction[RestApi](ParameterArray, BusObject["BusInterface"], res, true);
});
app.get('/LoginStatus', function(req, res, next){
    var Return;
    var Status = req.query["Status"];
    if(Status == "Check")
    {
        Return = CommandFunction["CheckSignInCredentials"]();
    }
    else if(Status == "Generate")
    {
        Return = CommandFunction["GenerateSignInCredentials"]();
    }
    else if(Status == "Delete")
    {
        Return = CommandFunction["DeleteSignInCredentials"]();
    }
    res.send(Return);
});
app.get('/*', function(req, res, next){
  var Return = ModuleData["GetModuleInformation"](new Array(req.path));
  res.send(Return);
});


http.listen(80, function(){
  console.log('listening on *:80');
});