From 9558234969c459846cc695d30008d77d51d9dfde Mon Sep 17 00:00:00 2001 From: Emmanuele Sassi Date: Fri, 15 May 2026 16:48:56 +0200 Subject: [PATCH] - aggiunto mode e submode alle risposte - sviluppo calc production per travi - corretto funzionamento Estimate in WindowREST --- BeamPipe.lua | 6 + BeamRESTPipe.lua | 416 ++++++++---- BeamWallPipeLib.lua | 8 +- WindowPipe.lua | 14 + WindowPipeLib.lua | 488 +++++++++++++ WindowRESTPipe.lua | 1550 +++++++++++++++++++++++++++++++++++------- WindowVerifyPipe.lua | 153 +++++ 7 files changed, 2249 insertions(+), 386 deletions(-) create mode 100644 WindowPipeLib.lua create mode 100644 WindowVerifyPipe.lua diff --git a/BeamPipe.lua b/BeamPipe.lua index 27b64d5..ee34288 100644 --- a/BeamPipe.lua +++ b/BeamPipe.lua @@ -246,8 +246,14 @@ while bRun do local Result = {} if nMode == QUESTION_MODES.PREVIEW then nResult, AnswerArgs = Calc_Preview( QuestionArgs) + if AnswerArgs then + AnswerArgs.Mode = nMode + end elseif nMode == QUESTION_MODES.BOM then nResult, AnswerArgs = Calc_BOM( QuestionArgs) + if AnswerArgs then + AnswerArgs.Mode = nMode + end end AnswerArgs.UID = sUid AnswerArgs.RUID = sRUID or 0 diff --git a/BeamRESTPipe.lua b/BeamRESTPipe.lua index 6b64e9e..8a7ee5b 100644 --- a/BeamRESTPipe.lua +++ b/BeamRESTPipe.lua @@ -99,31 +99,31 @@ local function Create_Order( QuestionArgs) -- scrivo testo su file local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) -- creo cartella ordine - local sBtlFilePath = sDataDir .. '\\Beam\\' .. sOrderUID - if not EgtExistsDirectory( sBtlFilePath) then - if not EgtCreateDirectory( sBtlFilePath) then - local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sBtlFilePath .. ' per il progetto ' .. sOrderUID .. '!' + local sOrderDirPath = sDataDir .. '\\Beam\\' .. sOrderUID + if not EgtExistsDirectory( sOrderDirPath) then + if not EgtCreateDirectory( sOrderDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sOrderDirPath .. ' per il progetto ' .. sOrderUID .. '!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end end -- creo cartella riga d'ordine - sBtlFilePath = sDataDir .. '\\Beam\\' .. sOrderUID .. '\\' .. sUID - if EgtExistsDirectory( sBtlFilePath) then - if not EgtEmptyDirectory( sBtlFilePath) then - local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sBtlFilePath .. ' per il progetto ' .. sOrderUID .. '!' + local sOrderLineDirPath = sDataDir .. '\\Beam\\' .. sOrderUID .. '\\' .. sUID + if EgtExistsDirectory( sOrderLineDirPath) then + if not EgtEmptyDirectory( sOrderLineDirPath) then + local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sOrderLineDirPath .. ' per il progetto ' .. sOrderUID .. '!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end else - if not EgtCreateDirectory( sBtlFilePath) then - local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sBtlFilePath .. ' per il progetto ' .. sOrderUID .. '!' + if not EgtCreateDirectory( sOrderLineDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sOrderLineDirPath .. ' per il progetto ' .. sOrderUID .. '!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end end _, _, sExt = EgtSplitPath( sFile) - sBtlFilePath = sBtlFilePath .. '\\' .. sUID .. sExt + local sBtlFilePath = sOrderLineDirPath .. '\\' .. sUID .. sExt -- Apro file Input in scrittura local fhBtl = io.open( sBtlFilePath, 'w') if not fhBtl then @@ -167,7 +167,7 @@ local function Create_Order( QuestionArgs) EgtSetInfo( nPartId, 'LuxTagList', sPartTagList) nPartId = EgtGetNextPart( nPartId) end - local sNgeFilePath = sDataDir .. '\\Beam\\' .. sOrderUID .. '\\' .. sUID .. '\\' .. sUID .. '.nge' + local sNgeFilePath = sOrderLineDirPath .. '\\' .. sUID .. '.nge' EgtSaveFile( sNgeFilePath, GDB_NT.CMPTXT) -- do risultato return 1, {} @@ -376,6 +376,12 @@ local function IsTagInPart( nPartId, TagList) return nQuantity > 0, nQuantity, FoundTagList end +local function GetTagQty( nPartId) + local sPartTagList = EgtGetInfo( nPartId, 'LuxTagList') + local PartTagList = EgtSplitString( sPartTagList, ',') + return #PartTagList +end + -- Calcola la deviazione rispetto alle percentuali desiderate local function computeDeviation(machineLoads, percentages) local total = 0 @@ -805,8 +811,6 @@ local function Calc_Balance( QuestionArgs) if assignment[nAssignmentIndex] == 0 then -- faccio verifica su pezzi singoli per avere un tempo??????????? EgtOutLog( 'Errore! Barra non assegnata a nessuna macchina!!') - - else local nMachGroupId = NestList[nAssignmentIndex].MachGroupList[assignment[nAssignmentIndex]].PartId local sMachineName = NestList[nAssignmentIndex].MachGroupList[assignment[nAssignmentIndex]].Machine @@ -846,37 +850,18 @@ local function Calc_Balance( QuestionArgs) end local function Create_Production( QuestionArgs) - -- leggo argomenti passati - --local sOrderUID = QuestionArgs["ProductionOrderUID"] - --if not sOrderUID or #sOrderUID <= 0 then - -- local sErrorMsg = 'Errore! ProductionOrderUID vuoto!' - -- EgtOutLog(sErrorMsg) - -- return 0, { Error = sErrorMsg} - --end local sUID = QuestionArgs["UID"] if not sUID or #sUID <= 0 then local sErrorMsg = 'Errore! UID vuoto!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end - --local dBarLenght = tonumber( QuestionArgs["BarLenght"]) - --if not dBarLenght or dBarLenght <= 0 then - -- local sErrorMsg = 'Errore! BarLenght vuoto o non valido! (' .. QuestionArgs["BarLenght"] .. ')' - -- EgtOutLog(sErrorMsg) - -- return 0, { Error = sErrorMsg} - --end local sProdCreate = QuestionArgs["ProdCreate"] if not sProdCreate or #sProdCreate <= 0 then local sErrorMsg = 'Errore! ProdCreate vuoto!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end - --local sReqBalance = QuestionArgs["ReqBalance"] - --if not sReqBalance or #sReqBalance <= 0 then - -- local sErrorMsg = 'Errore! ReqBalance vuoto!' - -- EgtOutLog(sErrorMsg) - -- return 0, { Error = sErrorMsg} - --end -- leggo dati per creare ordine local ProdCreate = JSON:decode( sProdCreate) if not ProdCreate or not next( ProdCreate) then @@ -1003,50 +988,126 @@ local function Calc_Production( QuestionArgs) EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end - -- recupero cartella progetto local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) - local sOrderDirPath = sDataDir .. '\\Beam\\' .. sOrderUID .. '\\' .. sUID - if not EgtExistsDirectory( sOrderDirPath) then - local sErrorMsg = 'Errore! Cartella della riga d\'ordine non trovata!' + local sProductionDirPath = sDataDir .. '\\Beam\\' .. ProdCalc.ProdName + if not EgtExistsDirectory( sProductionDirPath) then + local sErrorMsg = 'Errore! Cartella dell\'ordine di produzione non trovata!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end - -- verifico esistenza cartella per progetti di stima - local sEstimateDirPath = sOrderDirPath .. '\\Estimate' - if EgtExistsDirectory( sEstimateDirPath) then - if not EgtEmptyDirectory( sEstimateDirPath) then - local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sEstimateDirPath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - else - if not EgtCreateDirectory( sEstimateDirPath) then - local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sEstimateDirPath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - end - -- creo cartella per progetto di stima corrente - local nCurrTime = os.time() - local sCurrEstimateDirPath = sEstimateDirPath .. '\\' .. nCurrTime - if not EgtCreateDirectory( sCurrEstimateDirPath) then - local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sCurrEstimateDirPath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - local sNgeFilePath = sOrderDirPath .. '\\' .. sUID .. '.nge' - local sEstimateNgeFilePath = sCurrEstimateDirPath .. '\\' .. sUID .. '_' .. nCurrTime .. '.nge' - if not EgtCopyFile( sNgeFilePath, sEstimateNgeFilePath) then - local sErrorMsg = 'Errore! Fallita copia del file!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - if not EgtOpenFile( sEstimateNgeFilePath) then + local sProductionNgeFilePath = sProductionDirPath .. '\\' .. sUID .. '.nge' + if not EgtOpenFile( sProductionNgeFilePath) then local sErrorMsg = 'Errore! Fallita apertura del file!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end + -- elimino tutti i MachGroup gia' presenti + local nMachGroupId = EgtGetFirstMachGroup() + while nMachGroupId do + EgtSetCurrMachGroup( nMachGroupId) + local nRawPartId = EgtGetFirstRawPart( nMachGroupId) + while nRawPartId do + local nPartId = EgtGetFirstPartInRawPart( nRawPartId) + while nPartId do + local sLuxTag = EgtGetInfo( nPartId, 'LuxTag') + EgtResetCurrMachGroup() + local nOrigPartId = EgtDuploGetOriginal( nPartId) + EgtSetCurrMachGroup( nMachGroupId) + local sTagList = EgtGetInfo( nOrigPartId, 'LuxTagList') + -- recupero liste etichette usate e non + local TagTypeList = EgtSplitString( sTagList, ';') + local UnusedTagList = {} + local UsedTagList = {} + if #TagTypeList == 1 then + UnusedTagList = EgtSplitString( sTagList, ',') + else + if TagTypeList[1] and #TagTypeList[1] > 0 then + UnusedTagList = EgtSplitString( TagTypeList[1], ',') + end + if TagTypeList[2] and #TagTypeList[2] > 0 then + UsedTagList = EgtSplitString( TagTypeList[2], ',') + end + end + local nAssignedTagIndex = 0 + for nUsedTagIndex = 1, #UsedTagList do + if UsedTagList[nUsedTagIndex] == sLuxTag then + nAssignedTagIndex = nUsedTagIndex + break + end + end + if nAssignedTagIndex == 0 then + EgtOutLog( 'Warning! Etichetta per pezzo non trovata nell\'eliminazione prima del nesting!') + else + local sAssignedTag = UnusedTagList[nAssignedTagIndex] + -- creo stringa per info etichette aggiornata + local sNewTagList = '' + for nUnusedTagIndex = 1, #UnusedTagList do + sNewTagList = sNewTagList .. UnusedTagList[nUnusedTagIndex] .. ',' + end + sNewTagList = sNewTagList .. UsedTagList[nAssignedTagIndex] .. ';' + for nUsedTagIndex = 1, #UsedTagList do + if nUsedTagIndex ~= nAssignedTagIndex then + sNewTagList = sNewTagList .. UsedTagList[nUsedTagIndex] .. EgtIf( nUsedTagIndex == #UsedTagList or ( nUsedTagIndex == #UsedTagList - 1 and nAssignedTagIndex == #UsedTagList), '', ',') + end + end + -- aggiorno etichetta su pezzo originale + EgtSetInfo( nOrigPartId, 'LuxTagList', sNewTagList) + end + EgtRemovePartFromRawPart( nPartId) + EgtErase( nPartId) + nPartId = EgtGetNextPartInRawPart( nPartId) + end + nRawPartId = EgtGetNextRawPart( nRawPartId) + end + EgtRemoveMachGroup( nMachGroupId) + nMachGroupId = EgtGetFirstMachGroup() + end + EgtSaveFile() + -- recupero Id dei pezzi da nestare + local TagList = EgtSplitString( sTagList) + local nPartId = EgtGetFirstPart() + local PartIdList = {} + local SectionList = {} + local nPartCount = 0 + while nPartId do + local nQuantity = GetTagQty( nPartId) + PartIdList[nPartId] = nQuantity + -- recupero la sezione + local dH = EgtGetInfo( nPartId, 'H', 'd') + local dW = EgtGetInfo( nPartId, 'W', 'd') + local bInSectionList = false + for nSectionIndex = 1, #SectionList do + local Section = SectionList[nSectionIndex] + if Section.H == dH and Section.W == dW then + Section.PartIdList[nPartId] = nQuantity + bInSectionList = true + break + end + end + if not bInSectionList then + table.insert( SectionList, { H = dH, W = dW, PartIdList = { [nPartId] = nQuantity}}) + end + nPartCount = nPartCount + nQuantity + nPartId = EgtGetNextPart( nPartId) + end + local RawList = { } + for sSectXMat, LenList in pairs(ProdCalc.ItemAvaiList) do + local SectXMatSplit = EgtSplitString( sSectXMat, '-') + local SectSplit = EgtSplitString( SectXMatSplit[2], 'x') + local dW = tonumber( SectSplit[1]) + local dH = tonumber( SectSplit[2]) + for nSectIndex = 1, #SectionList do + local CurrSection = SectionList[nSectIndex] + if CurrSection.W == dW and CurrSection.H == dH then + CurrSection.RawList = {} + for nLenIndex = 1, #LenList do + local CurrLen = LenList[nLenIndex] + table.insert( CurrSection.RawList, { ItemID = CurrLen.ItemID, Len = CurrLen.Lenght, Qty = CurrLen.Qty}) + end + end + end + end -- recupero cartella macchine local MachineDirPath = EgtGetStringFromIni( 'Mach', 'MachinesDir', '', sIniFilePath) if not MachineDirPath or #MachineDirPath <=0 then @@ -1058,61 +1119,73 @@ local function Calc_Production( QuestionArgs) --local MachinesDir = { MachineDirPath .. '\\Saomad-Kairos', MachineDirPath .. '\\Essetre-FAST', MachineDirPath .. '\\Essetre-PF1250MAX' } local MachinesDir = { MachineDirPath .. '\\Saomad-Kairos', MachineDirPath .. '\\Essetre-FAST'} --local MachinesDir = { MachineDirPath .. '\\Essetre-FAST'} - local BeamMachineList = {} - local AnswerChannelList = {} + local bMachineFound = false for nMachineIndex = 1, #MachinesDir do local _, sMachineName, _ = EgtSplitPath( MachinesDir[nMachineIndex]) - local sMaterial = EgtGetStringFromIni( 'General', 'Material', '', MachineDirPath .. '\\' .. sMachineName .. '\\' .. sMachineName .. '.ini') - if sMaterial == 'Beam' then - local sMachineOrderDirPath = sCurrEstimateDirPath .. '\\' .. sMachineName - -- creo cartella per macchina - if EgtExistsDirectory( sMachineOrderDirPath) then - if not EgtEmptyDirectory( sMachineOrderDirPath) then - local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sMachineOrderDirPath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - else - if not EgtCreateDirectory( sMachineOrderDirPath) then - local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sMachineOrderDirPath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - end - local sMachineNgeFilePath = sMachineOrderDirPath .. '\\' .. sUID .. '.nge' - -- e copio file nge - if not EgtCopyFile( sEstimateNgeFilePath, sMachineNgeFilePath) then - local sErrorMsg = 'Errore! Copia del file nge per macchina ' .. sMachineName .. ' e progetto ' .. sUID .. ' fallita!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - if not EgtOpenFile( sMachineNgeFilePath) then - local sErrorMsg = 'Errore! Fallita apertura del file per macchina ' .. sMachineName .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - -- creo i singoli bwe e li mando da eseguire via redis - local nPartId = EgtGetFirstPart() - local AnswerArgs = {} - local TagTimeList = {} - local dTotTime = 0 - local nNotMachinedPart = 0 - while nPartId do - local BarPath = sMachineOrderDirPath - local ProjType = BWTYPES.BEAM - local GlobState = CALCSTATES.NOTCALCULATED - local bResult, AnswerChannel = BeamWallPipeLib.AsyncVerifyPartCalc( nPartId, BarPath, ProjType, sMachineName, GlobState) - if bResult then - table.insert( AnswerChannelList, { Channel = AnswerChannel, PartId = nPartId, Machine = sMachineName}) - else - local sErrorMsg = 'Errore! Tentativo di verifica del pezzo ' .. nPartId .. ' fallito!' - EgtOutLog(sErrorMsg) - end - nPartId = EgtGetNextPart( nPartId) - end - table.insert( BeamMachineList, { Name = sMachineName, PartList = { }}) + if sMachineName == ProdCalc.MachineName then + bMachineFound = true + break end end + if not bMachineFound then + local sErrorMsg = 'Errore! Macchina ' .. ProdCalc.MachineName .. ' con cui calcolare ordine di produzione ' .. ProdCalc.ProdName .. ' non trovata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local StartOffset = 20 + local Offset = 6 + -- per ogni sezione + for nSectionIndex = 1, #SectionList do + local Section = SectionList[nSectionIndex] + -- faccio nesting dei pezzi + local sNestingLogFilePath = sProductionDirPath .. '\\' .. ProdCalc.ProdName .. 'NestLog.txt' + local bOk, sErrorMsg = BeamWallPipeLib.NestBeam( Section.PartIdList, Section.RawList, { ProdCalc.MachineName}, StartOffset, Offset, sNestingLogFilePath) + if not bOk then + return 0, { Error = sErrorMsg} + end + end + EgtSaveFile() + -- eseguo verifica dei gruppi di lavorazione creati + -- creo i singoli bwe e li mando da eseguire via redis + local AnswerChannelList = {} + nMachGroupId = EgtGetFirstMachGroup() + local AnswerArgs = {} + local TagTimeList = {} + local dTotTime = 0 + local nNotMachinedPart = 0 + local MachGroupList = {} + local NestList = {} + while nMachGroupId do + local BarPath = sProductionDirPath + local ProjType = BWTYPES.BEAM + local sMachineName = EgtGetMachGroupMachineName( nMachGroupId) + local GlobState = CALCSTATES.NOTCALCULATED + local bResult, AnswerChannel = BeamWallPipeLib.AsyncVerifyPartCalc( nMachGroupId, BarPath, ProjType, sMachineName, GlobState, true) + if bResult then + table.insert( AnswerChannelList, { Channel = AnswerChannel, PartId = nMachGroupId, Machine = sMachineName}) + else + local sErrorMsg = 'Errore! Tentativo di verifica della barra ' .. nMachGroupId .. ' fallito!' + EgtOutLog(sErrorMsg) + end + local nNestIndex = EgtGetInfo( nMachGroupId, "NestIndex", 'i') + local bFound = false + for nMachGroupIndex = 1, #NestList do + local CurrNest = NestList[nMachGroupIndex] + if nNestIndex == CurrNest.NestIndex then + bFound = true + table.insert( CurrNest.MachGroupList, { PartId = nMachGroupId, Machine = sMachineName}) + end + end + if not bFound then + table.insert( NestList, { NestIndex = nNestIndex, MachGroupList = { { PartId = nMachGroupId, Machine = sMachineName}}}) + end + -- recupero info grezzo + local dW = EgtGetInfo( nMachGroupId, 'BARWIDTH', 'd') + local dH = EgtGetInfo( nMachGroupId, 'BARHEIGHT', 'd') + local dL = EgtGetInfo( nMachGroupId, 'BARLEN', 'd') + table.insert( MachGroupList, { PartId = nMachGroupId, NestIndex = nNestIndex, Machine = sMachineName, W = dW, H = dH, L = dL}) + nMachGroupId = EgtGetNextMachGroup( nMachGroupId) + end local nTimeCount = 0 local nTimeClock = 100 while nTimeCount < 1200 and #AnswerChannelList > 0 do @@ -1124,18 +1197,36 @@ local function Calc_Production( QuestionArgs) if bOk then if bAnswerReceived then if PartAnswerArgs then - local sTagList = EgtGetInfo( AnswerChannel.PartId, 'LuxTagList') - local TagList = EgtSplitString( sTagList, ',') - local CalcResult - CalcResult = EgtIf( tonumber( PartAnswerArgs.MachiningOk) == 1, PART_VERIFICATION_RESULTS.MACHINABLE, PART_VERIFICATION_RESULTS.NOTMACHINABLE) - local BeamMachine = '' - for nMachineIndex = 1, #BeamMachineList do - if BeamMachineList[nMachineIndex].Name == AnswerChannel.Machine then - BeamMachine = BeamMachineList[nMachineIndex] + if PartAnswerArgs.Error then + local sErrorMsg = 'Errore! La verifica del pezzo ' .. AnswerChannel.PartId .. ' ha restituito un errore! (' .. PartAnswerArgs.Error .. ')' + EgtOutLog(sErrorMsg) + else + --local sTagList = EgtGetInfo( AnswerChannel.PartId, 'LuxTagList') + --local TagList = EgtSplitString( sTagList, ',') + -- + local CalcResult + CalcResult = EgtIf( tonumber( PartAnswerArgs.MachiningOk) == 1, PART_VERIFICATION_RESULTS.MACHINABLE, PART_VERIFICATION_RESULTS.NOTMACHINABLE) + for nMachGroupIndex = 1, #MachGroupList do + local CurrMachGroup = MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == tonumber( PartAnswerArgs.BarId) then + CurrMachGroup.CalcResult = CalcResult + CurrMachGroup.Time = ( tonumber( PartAnswerArgs.Time) or 0) + end + end + local bNestFound = false + for nNestIndex = 1, #NestList do + if not bNestFound then + for nMachGroupIndex = 1, #NestList[nNestIndex].MachGroupList do + local CurrMachGroup = NestList[nNestIndex].MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == tonumber( PartAnswerArgs.BarId) then + CurrMachGroup.CalcResult = CalcResult + CurrMachGroup.Time = ( tonumber( PartAnswerArgs.Time) or 0) + bNestFound = true + break + end + end + end end - end - for nTagIndex = 1, #TagList do - table.insert( BeamMachine.PartList, { Tag = TagList[nTagIndex], CalcResult = CalcResult, Time = ( PartAnswerArgs.Time or 0)}) end table.insert( RemoveAnswerList, nAnswerIndex) else @@ -1159,22 +1250,69 @@ local function Calc_Production( QuestionArgs) local sTagList = EgtGetInfo( AnswerChannelList[nAnswerCount].PartId, 'LuxTagList') local TagList = EgtSplitString( sTagList, ',') CalcResult = PART_VERIFICATION_RESULTS.CALCULATIONFAILED - local sErrorMsg = 'Errore! Verifica sul pezzo ' .. AnswerChannelList[nAnswerCount].PartId .. ' fallita! (' .. AnswerChannelList[nAnswerCount].Channel .. ')' + local sErrorMsg = 'Errore! Verifica sul pezzo ' .. AnswerChannelList[nAnswerCount].PartId .. ' fallita!' EgtOutLog(sErrorMsg) - local BeamMachine - for nMachineIndex = 1, #BeamMachineList do - if BeamMachineList[nMachineIndex].Name == AnswerChannelList[nAnswerCount].Machine then - BeamMachine = BeamMachineList[nMachineIndex] + for nMachGroupIndex = 1, #MachGroupList do + local CurrMachGroup = MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == AnswerChannelList.PartId then + CurrMachGroup.CalcResult = 0 + CurrMachGroup.Time = 0 end end - for nTagIndex = 1, #TagList do - table.insert( BeamMachine.PartList, { Tag = TagList[nTagIndex], CalcResult = CalcResult, Time = 0}) + local bNestFound = false + for nNestIndex = 1, #NestList do + if not bNestFound then + for nMachGroupIndex = 1, #NestList[nNestIndex].MachGroupList do + local CurrMachGroup = NestList[nNestIndex].MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == AnswerChannelList.PartId then + CurrMachGroup.CalcResult = 0 + CurrMachGroup.Time = 0 + bNestFound = true + break + end + end + end end end end + -- creo lista grezzi utilizzati e calcolo tempo totale + local dTotTime = 0 + local RawList = { } + for nMachGroupIndex = 1, #MachGroupList do + local MachGroup = MachGroupList[nMachGroupIndex] + dTotTime = dTotTime + MachGroup.Time + local bRawFound = false + for nRawIndex = 1, #RawList do + local Raw = RawList[nRawIndex] + if MachGroup.W == Raw.W and MachGroup.H == Raw.H and MachGroup.L == Raw.L then + bRawFound = true + Raw.Count = Raw.Count + 1 + break + end + end + if not bRawFound then + for nSectIndex = 1, #SectionList do + local bFound = false + local CurrSection = SectionList[nSectIndex] + if CurrSection.W == MachGroup.W and CurrSection.H == MachGroup.H then + for nRawIndex = 1, #CurrSection.RawList do + if CurrSection.RawList[nRawIndex].Len == MachGroup.L then + table.insert( RawList, { ItemID = CurrSection.RawList[nRawIndex].ItemID, W = MachGroup.W, H = MachGroup.H, L = MachGroup.L, Count = 1, Material = 'Pine', Used = MachGroup.L}) + bFound = true + break + end + end + end + if bFound then + break + end + end + end + end + -- do risultato - local JsonBeamMachineList = JSON:encode( BeamMachineList) - return 1, { Estimate = JsonBeamMachineList} + local JsonRawList = JSON:encode( RawList) + return 1, { TotTime = dTotTime, RawList = JsonRawList} end diff --git a/BeamWallPipeLib.lua b/BeamWallPipeLib.lua index bef3047..7eb5b0b 100644 --- a/BeamWallPipeLib.lua +++ b/BeamWallPipeLib.lua @@ -423,10 +423,10 @@ function BeamWallPipeLib.VerifyBeam( QuestionArgs) local CmdType = tonumber( QuestionArgs["CmdType"]) --local ProgramPage = tonumber( QuestionArgs["ProgramPage"]) _G.BEAM = { BASEDIR = sBaseDir, - FILE = BarPath, - MACHINE = MachineName, - FLAG = CmdType, - BW = true } + FILE = BarPath, + MACHINE = MachineName, + FLAG = CmdType, + BW = true } local bOk, sErr = pcall( dofile, BEAM.BASEDIR .. "\\BatchProcessNew.lua") if not bOk then local sErrorMsg = 'Errore! Chiamata alla funzione BatchProcessNew.lua fallita! (' .. ( sErr or '') .. ')' diff --git a/WindowPipe.lua b/WindowPipe.lua index 5b08bbd..23acd40 100644 --- a/WindowPipe.lua +++ b/WindowPipe.lua @@ -534,8 +534,14 @@ while bRun do local Result = {} if nMode == QUESTION_MODES.PREVIEW then nResult, AnswerArgs = Calc_Preview( QuestionArgs) + if AnswerArgs then + AnswerArgs.Mode = nMode + end elseif nMode == QUESTION_MODES.BOM then nResult, AnswerArgs = Calc_BOM( QuestionArgs) + if AnswerArgs then + AnswerArgs.Mode = nMode + end elseif nMode == QUESTION_MODES.HARDWARE then if QuestionArgs.SubMode then local nSubMode = tonumber( QuestionArgs["SubMode"]) @@ -554,6 +560,10 @@ while bRun do AnswerArgs = { Error = sErrorMsg} EgtOutLog( sErrorMsg) end + if AnswerArgs then + AnswerArgs.Mode = nMode + AnswerArgs.SubMode = nSubMode + end else nResult = 0 local sErrorMsg = 'Errore! Domanda senza SubMode che é obbligatorio!' @@ -573,6 +583,10 @@ while bRun do AnswerArgs = { Error = sErrorMsg} EgtOutLog( sErrorMsg) end + if AnswerArgs then + AnswerArgs.Mode = nMode + AnswerArgs.SubMode = nSubMode + end else nResult = 0 local sErrorMsg = 'Errore! Domanda senza SubMode che é obbligatorio!' diff --git a/WindowPipeLib.lua b/WindowPipeLib.lua new file mode 100644 index 0000000..2e84e49 --- /dev/null +++ b/WindowPipeLib.lua @@ -0,0 +1,488 @@ +-- +-- EEEEEEEEEE GGGGGG TTTTTTTTTTTTTT +-- EEEEEEEEEE GGGGGGGGGG TTTTTTTTTTTTTT +-- EEEE GGGG GGGG TTTT +-- EEEE GGGG TTTT +-- EEEEEEE GGGG GGGGGGG TTTT +-- EEEEEEE GGGG GGGGGGG TTTT +-- EEEE GGGG GGGG TTTT +-- EEEE GGGG GGGG TTTT +-- EEEEEEEEEE GGGGGGGGGG TTTT +-- EEEEEEEEEE GGGGGG TTTT +-- +-- WindowPipeLib by Egalware s.r.l. 2023/05/09 +-- Questo script fa da libreria delle funzioni di calcolo e verifica delle finestre + +-- Tabella per definizione modulo +local WindowPipeLib = {} + +require( 'EgtBase') +--_ENV = EgtProtectGlobal() +--EgtEnableDebug( true) + +local sIniFilePath = EgtGetIniFile() +local sBaseDir = EgtGetStringFromIni( 'Window', 'BaseDir', '', sIniFilePath) +EgtAddToPackagePath( sBaseDir .. '\\Designing\\?.lua') + +-- modalita' di chiamata +QUESTION_MODES = { + NULL = 0, + PREVIEW = 1, + BOM = 2, + HARDWARE = 3, + CONFIG = 4, + ORDER = 5, + VERIFY = 6, +} + +-- Tipo di progetto +PROJECT_TYPES = { + NULL = 0, + PROJ = 1, + PROD = 2, +} + +-- Tipo di progetto (Beam o Wall) +BWTYPES = { + NULL = 0, + BEAM = 1, + WALL = 2, +} + +CALCSTATES = { + NOTCALCULATED = -1, + OK = 0, + INFO = 1, + WARNING = 2, + ERROR_ = 3, + COLLISION = 4, +} + +CMDTYPES = { + GENERATE = 0, + MODIFY = 1, + SIMULATE = 2, + CHECK = 3, + CHECKGEN = 4, + POINT_CLOUD = 5, + RAWPART = 6, + EDIT = 7, + CHECKNOSIM = 8, + TOPOLOGY = 9, + OPENTOOLDB = 11, -- Non utilizzato + OPENMACHINIGDB = 12, -- Non utilizzato +} + +local CUTID = 'CUTID' +local BTL_PRT_PDN = 'PDN' +local OUTLINE = 'Outline' +local BTL_FTR_GRP = 'GRP' +local BTL_FTR_MAINID = 'MAINID' +local BTL_FTR_AUXID = 'AUXID' +local PROCESSINGS = 'Processings' +local TASKID = 'TASKID' + +_G.package.loaded.JSON = nil +local JSON = require( 'JSON') + +-- Canali +local QuestionChannel = "EgwVerifyQuestion" +local AnswerChannel = "EgwVerifyAnswer" + +local nAnswerChannel_Count = 0 +local nAnswerChannel_LastSecond = 0 + +local nRedisConnectionId = 0 + +--------------------------------------------------------------------- +-- funzione che verifica la connessione a Redis +local function VerifyRedisConnection( sConnectionString) + local bRedisConnect = false + if LUX and LUX.REDISID and LUX.REDISID > 0 then + -- connessione già creata dall'esterno + nRedisConnectionId = LUX.REDISID + elseif nRedisConnectionId == 0 then + -- se nessuna connessione la creo + bRedisConnect, nRedisConnectionId = EgtRedisAsyncConnect( sConnectionString) + if not bRedisConnect then + EgtOutLog( 'Errore! Impossibile connettersi con il server Redis dal Lua!') + end + end + return bRedisConnect +end + +--------------------------------------------------------------------- +-- funzione che aggiorna CutId e TaskId +local function UpdateCutIdAndTaskId( nPartId) + -- Assegno Id a CutId per calcoli + EgtSetInfo( nPartId, CUTID, nPartId, true) + + -- Assegno TASKID agli outline per calcoli + local nOutLayerId = EgtGetFirstNameInGroup( nPartId, OUTLINE) + local nOutlineId = EgtGetFirstInGroup( nOutLayerId) + while nOutlineId do + -- verifico che sia una feature + local nGRP = EgtGetInfo( nOutlineId, BTL_FTR_GRP, 'i') + if nGRP then + -- verifico se ha un principale + local nMainOffsId = EgtGetInfo( nOutlineId, BTL_FTR_MAINID, 'i') + local nTaskId = nOutlineId + if nMainOffsId then + nTaskId = nOutlineId + nMainOffsId + end + -- assegno TASKID alla feature per calcoli + EgtSetInfo( nOutlineId, TASKID, nTaskId) + -- recupero eventuali AuxId + local sAuxIds = EgtGetInfo( nOutlineId, BTL_FTR_AUXID) + if sAuxIds then + local AuxIdArray = EgtSplitString( sAuxIds, ",") + if AuxIdArray.Count > 0 then + for AuxIdIndex = 1, #AuxIdArray do + local AuxId = AuxIdArray[AuxIdIndex] + AuxId = EgtTrim( AuxId) + AuxId = AuxId:gsub("^%+", "") + local nAuxId = tonumber( AuxId) + if nAuxId then + EgtSetInfo( nOutlineId + nAuxId, TASKID, nTaskId) + end + end + end + end + end + nOutlineId = EgtGetNext( nOutlineId) + end + + -- Assegno TASKID alle feature per calcoli + local nProcessingId = EgtGetFirstNameInGroup( nPartId, PROCESSINGS) + local nFeatureId = EgtGetFirstInGroup( nProcessingId) + while nFeatureId do + -- verifico che sia una feature + local nGRP = EgtGetInfo( nFeatureId, BTL_FTR_GRP, 'i') + if nGRP then + -- verifico se ha un principale + local nMainOffsId = EgtGetInfo( nFeatureId, BTL_FTR_MAINID, 'i') + local nTaskId = nFeatureId + if nMainOffsId then + nTaskId = nFeatureId + nMainOffsId + end + -- assegno TASKID alla feature per calcoli + EgtSetInfo( nFeatureId, TASKID, nTaskId) + -- recupero eventuali AuxId + local sAuxIds = EgtGetInfo( nFeatureId, BTL_FTR_AUXID) + if sAuxIds then + local AuxIdArray = EgtSplitString( sAuxIds, ",") + for AuxIdIndex = 1, #AuxIdArray do + local AuxId = AuxIdArray[AuxIdIndex] + AuxId = EgtTrim( AuxId) + AuxId = AuxId:gsub("^%+", "") + local nAuxId = tonumber( AuxId) + if nAuxId then + EgtSetInfo( nFeatureId + nAuxId, TASKID, nTaskId) + end + end + end + end + nFeatureId = EgtGetNext( nFeatureId) + end + return true +end + +--------------------------------------------------------------------- +-- funzione che crea un Bwe da un pezzo Btl +local function CreateBWEFromPart( nPartId, sProjDirPath) + -- Aggiorno identificativi per segnalazione errori + UpdateCutIdAndTaskId( nPartId) + + -- Disabilito segnalazione modificato + local bOldEnMod = EgtGetEnableModified() + if bOldEnMod then + EgtDisableModified() + end + + -- Recupero visibilita' pezzo e lo imposto a visibile + local nOldPartMode = 0 + EgtGetMode( nPartId, nOldPartMode) + EgtSetMode( nPartId, GDB_MD.STD) + + -- Salvo pezzo nel suo progetto + local bOk = false + local nPDN = EgtGetInfo( nPartId, BTL_PRT_PDN, 'i') + --EgtSaveFile( 'c:\\Temp\\TestPDN.nge') + + local sPartFilePath = sProjDirPath .. '\\' .. tostring(nPDN) .. '.bwe' + bOk = EgtSaveObjToFile({ nPartId, EgtGetFirstNameInGroup( GDB_ID.ROOT, BTLINFO), EgtGetFirstNameInGroup( GDB_ID.ROOT, PROJECTINFO)}, sPartFilePath, GDB_NT.CMPTXT) + + -- Ripristino visibilita' pezzo + EgtSetMode( nPartId, nOldPartMode) + + -- Ripristino stato segnalazione modifica + if bOldEnMod then + EgtEnableModified() + end + + return EgtIf( bOk, sPartFilePath, '') +end + +--------------------------------------------------------------------- +-- funzione che crea un Bwe da un MachGroup +local function CreateBWEFromMachGroup( nMachGroupId, sProjDirPath) + -- Aggiorno identificativi per segnalazione errori + EgtSetCurrMachGroup( nMachGroupId) + --local nRawId = EgtGetFirstRawPart() + --while nRawId do + -- if EgtVerifyRawPartPhase( nRawId, 1) then + -- local nPartId = EgtGetFirstPartInRawPart( nRawId) + -- while nPartId do + -- UpdateCutIdAndTaskId(nPartId) + -- nPartId = EgtGetNextPartInRawPart( nPartId) + -- end + -- end + -- nRawId = EgtGetNextRawPart( nRawId) + --end + + -- Salvo gruppo di lavorazione nel suo progetto + local sMachGroupName = EgtGetMachGroupName( nMachGroupId) + local sMachGroupFilePath = sProjDirPath .. '\\' .. sMachGroupName .. '.bwe' + if EgtSaveMachGroupToFile( nMachGroupId, { }, sMachGroupFilePath, GDB_NT.CMPTEXT) then + return sMachGroupFilePath + end + return '' +end + +--------------------------------------------------------------------- +-- funzione che verifica un pezzo Btl +function WindowPipeLib.VerifyBtlPart( nPartId, BarPath) + + -- verifico connessione a Redis + local bRedisConnect = VerifyRedisConnection( 'redis.ufficio:26379, serviceName=devel, DefaultDatabase=6, keepAlive=180, connectTimeout=15000, syncTimeout=15000, asyncTimeout=15000, abortConnect=false, ssl=false, allowAdmin=true') + + BarPath = CreateBWEFromPart( nPartId, BarPath) + local CurrAnswerChannel = AnswerChannel .. os.time() + + local Args = { BarId = nPartId, + BarPath = BarPath, + --GlobState = CALCSTATES.NOTCALCULATED, + CmdType = CMDTYPES.CHECKNOSIM, + OutputChannel = CurrAnswerChannel} + + local Question = { nThreadIndex = 0, + nId = 1, + ExecEnvironment = 2, + Args = Args} + local JsonQuestion = JSON:encode( Question) + + local bOk = EgtRedisAsyncPublish( nRedisConnectionId, QuestionChannel, JsonQuestion) + local AnswerArgs + if bOk then + EgtOutLog('Messaggio Redis pubblicato:' .. QuestionChannel .. ' ' .. JsonQuestion) + local bOk, Answer = EgtRedisAsyncSubscribeOneMessage( nRedisConnectionId, CurrAnswerChannel, 60000) + -- se la connessione è stata creata in questa funzione la chiudo + if bRedisConnect then + EgtRedisAsyncDisconnect( nRedisConnectionId) + nRedisConnectionId = 0 + end + if bOk then + EgtOutLog('Risposta Redis ricevuta:' .. Answer) + local JsonAnswer = JSON:decode( Answer) + AnswerArgs = JsonAnswer['Args'] + AnswerArgs.OutputChannel = nil + return 1, AnswerArgs + else + local sErrorMsg = 'Errore! Risposta dal canale di verifica non arrivata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + else + local sErrorMsg = 'Errore! Pubblicazione sul canale di verifica fallita!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end +end + +local function GetAnswerChannelCode() + local nCurrTime = os.time() + if nCurrTime == nAnswerChannel_LastSecond then + nAnswerChannel_Count = nAnswerChannel_Count + 1 + return nCurrTime .. nAnswerChannel_Count + else + nAnswerChannel_LastSecond = nCurrTime + nAnswerChannel_Count = 0 + return nCurrTime + end +end + +function WindowPipeLib.AsyncVerifyPartCalc( nPartId, BarPath, sMachineName) + + -- verifico connessione a Redis + local bRedisConnect = VerifyRedisConnection( 'redis.ufficio:26379, serviceName=devel, DefaultDatabase=6, keepAlive=180, connectTimeout=15000, syncTimeout=15000, asyncTimeout=15000, abortConnect=false, ssl=false, allowAdmin=true') + + EgtResetCurrMachGroup() + + BarPath = CreateBWEFromMachGroup( nPartId, BarPath) + + local CurrAnswerChannel = AnswerChannel .. GetAnswerChannelCode() + + local Args = { BarId = nPartId, + --ProgramPage = PROJECT_TYPES.PROJ, + BarPath = BarPath, + --ProjType = BWTYPES.BEAM, + MachineName = sMachineName, + --GlobState = CALCSTATES.NOTCALCULATED, + CmdType = 2, + OutputChannel = CurrAnswerChannel, + Mode = QUESTION_MODES.VERIFY, + UID = 1} + + local Question = { nThreadIndex = 0, + nId = 1, + ExecEnvironment = 1, + Args = Args} + local JsonQuestion = JSON:encode( Question) + + EgtRedisAsyncSubscribe( nRedisConnectionId, CurrAnswerChannel) + local bOk = EgtRedisAsyncPublish( nRedisConnectionId, QuestionChannel, JsonQuestion) + if not bOk then + EgtOutLog( 'Error! Verify Redis publish failed on PartId ' .. nPartId .. ' and channel ' .. CurrAnswerChannel .. '!') + EgtRedisAsyncUnsubscribe( nRedisConnectionId, CurrAnswerChannel) + EgtOutLog( 'EgtRedisAsyncUnsubscribe ' .. nRedisConnectionId .. ' ' .. CurrAnswerChannel) + end + return bOk, CurrAnswerChannel +end + +function WindowPipeLib.AsyncVerifyPartResult( CurrAnswerChannel) + + -- verifico connessione a Redis + local bRedisConnect = VerifyRedisConnection( 'redis.ufficio:26379, serviceName=devel, DefaultDatabase=6, keepAlive=180, connectTimeout=15000, syncTimeout=15000, asyncTimeout=15000, abortConnect=false, ssl=false, allowAdmin=true') + + local bOk, nCount, Answer = EgtRedisAsyncGetMessage( nRedisConnectionId, CurrAnswerChannel) + + if bRedisConnect then + EgtRedisAsyncDisconnect( nRedisConnectionId) + nRedisConnectionId = 0 + end + + if bOk then + if nCount > 0 then + EgtOutLog('Risposta Redis ricevuta:' .. Answer) + EgtRedisAsyncUnsubscribe( nRedisConnectionId, CurrAnswerChannel) + EgtOutLog( 'EgtRedisAsyncUnsubscribe ' .. nRedisConnectionId .. ' ' .. CurrAnswerChannel) + local JsonAnswer = JSON:decode( Answer) + if JsonAnswer then + AnswerArgs = JsonAnswer['Args'] + AnswerArgs.OutputChannel = nil + return true, true, AnswerArgs + else + return true, true + end + else + return true, false + end + else + return false + end + +end + +--------------------------------------------------------------------- +-- funzione che analizza i risultati della verifica +local function AnalyzeResult( RESULT) + local bMachiningOk = true + local dTime = 0 + for nElementIndex = 1, #RESULT do + local CurrElement = RESULT[nElementIndex] + if CurrElement.sType == 'Part' then + if tonumber( CurrElement.nErr) > 0 then + bMachiningOk = false + end + elseif CurrElement.sType == 'Feature' then + if not CurrElement.ChosenStrategy or not CurrElement.ChosenStrategy.sStatus or ( CurrElement.ChosenStrategy.sStatus ~= 'Completed' and CurrElement.ChosenStrategy.sStatus ~= 'Not-Completed') then + bMachiningOk = false + end + elseif CurrElement.sType == 'Time' then + dTime = CurrElement.dTime + end + end + return bMachiningOk, dTime +end + +--------------------------------------------------------------------- +-- funzione che verifica un MachGroup delle finestre +function WindowPipeLib.VerifyWindow( QuestionArgs) + if QuestionArgs and QuestionArgs.BarPath and QuestionArgs.CmdType then + local BarId = tonumber( QuestionArgs["BarId"]) + local BarPath = QuestionArgs["BarPath"] + local MachineName = QuestionArgs["MachineName"] + local CmdType = tonumber( QuestionArgs["CmdType"]) + --local ProgramPage = tonumber( QuestionArgs["ProgramPage"]) + _G.WIN = { BASEDIR = sBaseDir .. '\\CAMAuto', + FILE = BarPath, + MACHINE = MachineName, + FLAG = CmdType} + local bOk, sErr = pcall( dofile, WIN.BASEDIR .. "\\BatchProcessWin.lua") + if not bOk then + local sErrorMsg = 'Errore! Chiamata alla funzione BatchProcessNew.lua fallita! (' .. ( sErr or '') .. ')' + EgtOutLog( sErrorMsg) + return 0, { Error = sErrorMsg} + end + local bMachiningOk ,dTime = AnalyzeResult( WIN.RESULT) + --local nResult = EgtIf( bMachiningOk, 1, 0) + local AnswerArgs = {} + AnswerArgs.ERR = WIN.ERR + AnswerArgs.BarId = BarId + AnswerArgs.BarPath = BarPath + AnswerArgs.Time = dTime + AnswerArgs.MachiningOk = EgtIf( bMachiningOk, 1, 0) + _G.WIN = {} + return 1, AnswerArgs + else + local sErrorMsg = 'Errore! Domanda senza argomenti che sono obbligatori!' + EgtOutLog( sErrorMsg) + return 0, { Error = sErrorMsg} + end +end + +--------------------------------------------------------------------- +-- funzione che esegue il nesting sui pezzi passati +function WindowPipeLib.NestWindow( MachineNameList) -- PartIdList, RawList, MachineNameList, StartOffset, Offset, LogFile) + --if QuestionArgs and QuestionArgs.BarPath and QuestionArgs.MachineName and QuestionArgs.CmdType then + --local BarId = tonumber( QuestionArgs["BarId"]) + --local BarPath = QuestionArgs["BarPath"] + --local MachineName = QuestionArgs["MachineName"] + --local CmdType = tonumber( QuestionArgs["CmdType"]) + --local ProgramPage = tonumber( QuestionArgs["ProgramPage"]) + --_G.NEST = { BASEDIR = sBaseDir, + -- FILE = LogFile, + -- MACHINELIST = MachineNameList, + -- STARTOFFSET = 20, + -- OFFSET = 7 } + _G.WIN = { MACHINELIST = MachineNameList} + --_G.PARTLIST = PartIdList + --_G.RAWLIST = RawList + --local sFileName = EgtGetStringFromIni( 'Nest', 'NestExec', '', sIniFilePath) + --local bOk, sErr = pcall( dofile, sBaseDir .. '\\' .. sFileName .. 'New.lua') + local bOk, sErr = pcall( dofile, sBaseDir .. '\\CAMAuto\\' .. 'CreateMachGroups.lua') + if not bOk then + local sErrorMsg = 'Errore! Chiamata alla funzione NestProcessNew.lua fallita! (' .. ( sErr or '') .. ')' + EgtOutLog( sErrorMsg) + return false, sErrorMsg + end + --EgtSaveFile() + --local bMachiningOk ,dTime = AnalyzeResult( RESULT) + ----local nResult = EgtIf( bMachiningOk, 1, 0) + --local AnswerArgs = {} + --AnswerArgs.ERR = BEAM.ERR + --AnswerArgs.BarId = BarId + --AnswerArgs.BarPath = BarPath + --AnswerArgs.Time = dTime + --AnswerArgs.MachiningOk = EgtIf( bMachiningOk, 1, 0) + --_G.BEAM = {} + return true + --else + -- local sErrorMsg = 'Errore! Domanda senza argomenti che sono obbligatori!' + -- EgtOutLog( sErrorMsg) + -- return 0, { Error = sErrorMsg} + --end +end + +--------------------------------------------------------------------- +return WindowPipeLib \ No newline at end of file diff --git a/WindowRESTPipe.lua b/WindowRESTPipe.lua index e5293d7..8c76e9f 100644 --- a/WindowRESTPipe.lua +++ b/WindowRESTPipe.lua @@ -32,7 +32,9 @@ QUESTION_ORDER_SUBMODES = { NULL = 0, CREATE = 1, ESTIMATE = 2, - CONFIRM = 3, + BALANCE = 3, + CREATE_PRODUCTION = 4, + CALC_PRODUCTION = 5, } PART_VERIFICATION_RESULTS = { @@ -47,11 +49,14 @@ local JSON = require( 'JSON') local sIniFilePath = EgtGetIniFile() local sScriptDir = EgtGetStringFromIni( 'Lux', 'ScriptDir', '', sIniFilePath) EgtAddToPackagePath( sScriptDir .. '\\?.lua') -local sBaseDir = EgtGetStringFromIni( 'Beam', 'BaseDir', '', sIniFilePath) -EgtAddToPackagePath( sBaseDir .. '\\?.lua') +local sBaseDir = EgtGetStringFromIni( 'Window', 'BaseDir', '', sIniFilePath) +EgtAddToPackagePath( sBaseDir .. '\\Designing\\?.lua') -_G.package.loaded.BeamWallPipeLib = nil -local BeamWallPipeLib = require( 'BeamWallPipeLib') +_G.package.loaded.WinProject = nil +local WinProject = require( 'WinProject') + +_G.package.loaded.WindowPipeLib = nil +local WindowPipeLib = require( 'WindowPipeLib') -- Connessiona a Redis local bRedisConnect, nRedisConnectionId = EgtRedisAsyncConnect( ENG.Param2) @@ -64,276 +69,1320 @@ end local function Create_Order( QuestionArgs) -- leggo argomenti passati - local sFile = QuestionArgs["FileName"] - local sBtl = QuestionArgs["SerializedData"] + local sJwd = QuestionArgs["SerializedData"] + if not sJwd or #sJwd <= 0 then + local sErrorMsg = 'Errore! Btl file vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end local sOrderUID = QuestionArgs["OrderUID"] + if not sOrderUID or #sOrderUID <= 0 then + local sErrorMsg = 'Errore! Nome ordine vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end local sUID = QuestionArgs["UID"] - local sTagList = QuestionArgs["TagsList"] - if sFile and #sFile > 0 then - if sBtl and #sBtl > 0 then - if sOrderUID and #sOrderUID > 0 then - if sUID and #sUID > 0 then - if sTagList and #sTagList > 0 then - -- scrivo testo su file - local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) - -- creo cartella ordine - local sBtlFilePath = sDataDir .. '\\Beam\\' .. sOrderUID - if not EgtExistsDirectory( sBtlFilePath) then - if not EgtCreateDirectory( sBtlFilePath) then - local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sBtlFilePath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - end - -- creo cartella riga d'ordine - sBtlFilePath = sDataDir .. '\\Beam\\' .. sOrderUID .. '\\' .. sUID - if EgtExistsDirectory( sBtlFilePath) then - if not EgtEmptyDirectory( sBtlFilePath) then - local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sBtlFilePath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - else - if not EgtCreateDirectory( sBtlFilePath) then - local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sBtlFilePath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - end - _, _, sExt = EgtSplitPath( sFile) - sBtlFilePath = sBtlFilePath .. '\\' .. sUID .. sExt - -- Apro file Input in scrittura - local fhBtl = io.open( sBtlFilePath, 'w') - if not fhBtl then - local sErrorMsg = 'Errore! Apertura file ' .. sBtlFilePath .. ' per scrittura btl non riuscita!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - -- Scrittura nuova linea - fhBtl:write( sBtl .. '\n') - -- Chiudo file - fhBtl:close() - -- verifico tipo di file - local bOk = false - if string.lower( sExt) == '.btl' then - bOk = EgtImportBtl( sBtlFilePath) - elseif string.lower( sExt) == '.btlx' then - bOk = EgtImportBtlx( sBtlFilePath) - else - local sErrorMsg = 'Errore! Estensione file non riconosciuta!' - EgtOutLog(sErrorMsg) - os.remove(sBtlFilePath) - return 0, { Error = sErrorMsg} - end - if not bOk then - local sErrorMsg = 'Errore! Importazione file non riuscita!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - -- assegno etichette ai pezzi - local TagList = EgtSplitString( sTagList, ',') - local nPartId = EgtGetFirstPart() - local nTagIndex = 1 - while nPartId do - local nCnt = EgtGetInfo( nPartId, "CNT", 'i') - local sPartTagList = '' - for nCntIndex = 1, nCnt do - sPartTagList = sPartTagList .. TagList[nTagIndex] .. EgtIf( nCntIndex < nCnt, ',', '') - nTagIndex = nTagIndex + 1 - end - EgtSetInfo( nPartId, 'LuxTagList', sPartTagList) - nPartId = EgtGetNextPart( nPartId) - end - local sNgeFilePath = sDataDir .. '\\Beam\\' .. sOrderUID .. '\\' .. sUID .. '\\' .. sUID .. '.nge' - EgtSaveFile( sNgeFilePath, GDB_NT.CMPTXT) - -- do risultato - return 1, {} - else - local sErrorMsg = 'Errore! Lista etichette vuota!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - else - local sErrorMsg = 'Errore! UID vuoto!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - else - local sErrorMsg = 'Errore! Nome ordine vuoto!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - else - local sErrorMsg = 'Errore! Btl file vuoto!' + if not sUID or #sUID <= 0 then + local sErrorMsg = 'Errore! UID vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local sTagList = QuestionArgs["TagList"] + if not sTagList or #sTagList <= 0 then + local sErrorMsg = 'Errore! Lista etichette vuota!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- scrivo testo su file + local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) + -- creo cartella ordine + local sOrderDirPath = sDataDir .. '\\Window\\' .. sOrderUID + if not EgtExistsDirectory( sOrderDirPath) then + if not EgtCreateDirectory( sOrderDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sOrderDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + end + -- creo cartella riga d'ordine + local sOrderLineDirPath = sDataDir .. '\\Window\\' .. sOrderUID .. '\\' .. sUID + if EgtExistsDirectory( sOrderLineDirPath) then + if not EgtEmptyDirectory( sOrderLineDirPath) then + local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sOrderLineDirPath .. ' per il progetto ' .. sOrderUID .. '!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end else - local sErrorMsg = 'Errore! Nome file vuoto!' + if not EgtCreateDirectory( sOrderLineDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sOrderLineDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + end + -- imposto cartella profili + local sProfileDir = '' + sProfileDir = EgtGetStringFromIni( 'Window', 'ProfileDir', '', sIniFilePath) + if sProfileDir and #sProfileDir > 0 then + WDG.PROFILEPATH = sProfileDir + SetProfilePath() + else + local sErrorMsg = 'Errore! Cartella dei profili non trovata!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end + -- carico jwd + WDG.JWD = sJwd + WinManage_LoadJwd() + local AreaId = WDG.AREAID + -- imposto opzioni visualizzazione + WDG.VALUE = true + _G.WinCalculate_SetCalcSolid() + WDG.VALUE = false + WinCalculate_SetSimplifiedSolid() + WDG.VALUE = false + WinCalculate_SetCalcPreview() + -- creazione pezzi + WDG.FRAMEID = AreaId + WinCalculate_CreatePartFromArea() + -- calcolo hardware + WDG.FRAMEID = AreaId + WDG.CALC_HARDWARELIST=false + WDG.CALC_POSITIONLIST=false + WDG.CALC_MACHININGLIST=false + WDG.CALC_OPTIONLIST=false + WinCalculate_AddHardware() + -- assegno etichette ai pezzi + local TagList = EgtSplitString( sTagList, ',') + local nPartId = EgtGetFirstPart() + local nTagIndex = 1 + while nPartId do + local nCnt = EgtGetInfo( nPartId, "CNT", 'i') or 1 + EgtSetInfo( nPartId, 'LuxTagList', TagList[nTagIndex]) + nTagIndex = nTagIndex + 1 + nPartId = EgtGetNextPart( nPartId) + end + local sNgeFilePath = sOrderLineDirPath .. '\\' .. sUID .. '.nge' + EgtSaveFile( sNgeFilePath, GDB_NT.CMPTXT) + -- do risultato + return 1, {} end local function Calc_Estimate( QuestionArgs) -- leggo argomenti passati local sOrderUID = QuestionArgs["OrderUID"] + if not sOrderUID or #sOrderUID <= 0 then + local sErrorMsg = 'Errore! OrderUID vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end local sUID = QuestionArgs["UID"] - if sOrderUID and #sOrderUID > 0 then - if sUID and #sUID > 0 then - -- apro progetto - local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) - local sOrderDirPath = sDataDir .. '\\Beam\\' .. sOrderUID .. '\\' .. sUID - if not EgtExistsDirectory( sOrderDirPath) then - local sErrorMsg = 'Errore! Cartella della riga d\'ordine non trovata!' + if not sUID or #sUID <= 0 then + local sErrorMsg = 'Errore! UID vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- recupero cartella progetto + local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) + local sOrderDirPath = sDataDir .. '\\Window\\' .. sOrderUID .. '\\' .. sUID + if not EgtExistsDirectory( sOrderDirPath) then + local sErrorMsg = 'Errore! Cartella della riga d\'ordine non trovata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- verifico esistenza cartella per progetti di stima + local sEstimateDirPath = sOrderDirPath .. '\\Estimate' + if EgtExistsDirectory( sEstimateDirPath) then + if not EgtEmptyDirectory( sEstimateDirPath) then + local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sEstimateDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + else + if not EgtCreateDirectory( sEstimateDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sEstimateDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + end + -- creo cartella per progetto di stima corrente + local nCurrTime = os.time() + local sCurrEstimateDirPath = sEstimateDirPath .. '\\' .. nCurrTime + if not EgtCreateDirectory( sCurrEstimateDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sCurrEstimateDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local sNgeFilePath = sOrderDirPath .. '\\' .. sUID .. '.nge' + local sEstimateNgeFilePath = sCurrEstimateDirPath .. '\\' .. sUID .. '_' .. nCurrTime .. '.nge' + if not EgtCopyFile( sNgeFilePath, sEstimateNgeFilePath) then + local sErrorMsg = 'Errore! Fallita copia del file!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + if not EgtOpenFile( sEstimateNgeFilePath) then + local sErrorMsg = 'Errore! Fallita apertura del file!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- recupero cartella macchine + local MachineDirPath = EgtGetStringFromIni( 'Mach', 'MachinesDir', '', sIniFilePath) + if not MachineDirPath or #MachineDirPath <=0 then + local sErrorMsg = 'Errore! Cartella delle macchine non impostata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + --local MachinesDir = EgtFindAllFiles( MachineDirPath .. '\\*.*') + --local MachinesDir = { MachineDirPath .. '\\Saomad-Kairos', MachineDirPath .. '\\Essetre-FAST', MachineDirPath .. '\\Essetre-PF1250MAX' } + local MachinesDir = { MachineDirPath .. '\\Saomad-Just3500'} + --local MachinesDir = { MachineDirPath .. '\\Essetre-FAST'} + local WindowMachineList = {} + local AnswerChannelList = {} + local MachGroupList = {} + local NestList = {} + for nMachineIndex = 1, #MachinesDir do + local _, sMachineName, _ = EgtSplitPath( MachinesDir[nMachineIndex]) + local sMaterial = EgtGetStringFromIni( 'General', 'Material', '', MachineDirPath .. '\\' .. sMachineName .. '\\' .. sMachineName .. '.ini') + if sMaterial == 'Wood' then + local sMachineOrderDirPath = sCurrEstimateDirPath .. '\\' .. sMachineName + -- creo cartella per macchina + if EgtExistsDirectory( sMachineOrderDirPath) then + if not EgtEmptyDirectory( sMachineOrderDirPath) then + local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sMachineOrderDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + else + if not EgtCreateDirectory( sMachineOrderDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sMachineOrderDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + end + local sMachineNgeFilePath = sMachineOrderDirPath .. '\\' .. sUID .. '.nge' + -- e copio file nge + if not EgtCopyFile( sEstimateNgeFilePath, sMachineNgeFilePath) then + local sErrorMsg = 'Errore! Copia del file nge per macchina ' .. sMachineName .. ' e progetto ' .. sUID .. ' fallita!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end - local sNgeFilePath = sOrderDirPath .. '\\' .. sUID .. '.nge' + if not EgtOpenFile( sMachineNgeFilePath) then + local sErrorMsg = 'Errore! Fallita apertura del file per macchina ' .. sMachineName .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- assegno Id del serramento + local nPartId = EgtGetFirstPart() + while nPartId do + EgtSetInfo( nPartId, 'WIN_ID', sUID) + nPartId = EgtGetNextPart( nPartId) + end + WindowPipeLib.NestWindow( { sMachineName}) + + EgtSaveFile() + + -- eseguo verifica dei gruppi di lavorazione creati + -- creo i singoli bwe e li mando da eseguire via redis + local nMachGroupId = EgtGetFirstMachGroup() + local AnswerArgs = {} + local TagTimeList = {} + local dTotTime = 0 + local nNotMachinedPart = 0 + while nMachGroupId do + local BarPath = sMachineOrderDirPath + local sMachineName = EgtGetMachGroupMachineName( nMachGroupId) + local bResult, AnswerChannel = WindowPipeLib.AsyncVerifyPartCalc( nMachGroupId, BarPath, sMachineName) + if bResult then + table.insert( AnswerChannelList, { Channel = AnswerChannel, PartId = nMachGroupId, Machine = sMachineName}) + else + local sErrorMsg = 'Errore! Tentativo di verifica della barra ' .. nMachGroupId .. ' fallito!' + EgtOutLog(sErrorMsg) + end + local nNestIndex = EgtGetInfo( nMachGroupId, "NestIndex", 'i') + local bFound = false + for nMachGroupIndex = 1, #NestList do + local CurrNest = NestList[nMachGroupIndex] + if nNestIndex == CurrNest.NestIndex then + bFound = true + table.insert( CurrNest.MachGroupList, { PartId = nMachGroupId, Machine = sMachineName}) + end + end + if not bFound then + table.insert( NestList, { NestIndex = nNestIndex, MachGroupList = { { PartId = nMachGroupId, Machine = sMachineName}}}) + end + table.insert( MachGroupList, { PartId = nMachGroupId, NestIndex = nNestIndex, Machine = sMachineName}) + nMachGroupId = EgtGetNextMachGroup( nMachGroupId) + end + table.insert( WindowMachineList, { Name = sMachineName, PartList = { }}) + end + end + local nTimeCount = 0 + local nTimeClock = 100 + while nTimeCount < 1200 and #AnswerChannelList > 0 do + local RemoveAnswerList = {} + for nAnswerIndex = 1, #AnswerChannelList do + local AnswerChannel = AnswerChannelList[nAnswerIndex] + -- verifico se ci sono i risultati e aspetto che arrivino tutti + local bOk, bAnswerReceived, PartAnswerArgs = WindowPipeLib.AsyncVerifyPartResult( AnswerChannel.Channel) + if bOk then + if bAnswerReceived then + if PartAnswerArgs then + if PartAnswerArgs.Error then + local sErrorMsg = 'Errore! La verifica del pezzo ' .. AnswerChannel.PartId .. ' ha restituito un errore! (' .. PartAnswerArgs.Error .. ')' + EgtOutLog(sErrorMsg) + else + local TagList = { } + EgtSetCurrMachGroup( AnswerChannel.PartId) + local nCurrRawPartId = EgtGetFirstRawPart() + while nCurrRawPartId do + local nCurrPartId = EgtGetFirstPartInRawPart( nCurrRawPartId) + while nCurrPartId do + local sCurrTag = EgtGetInfo( nCurrPartId, 'LuxTagList') + if sCurrTag then + table.insert( TagList, sCurrTag) + end + nCurrPartId = EgtGetNextPartInRawPart( nCurrPartId) + end + nCurrRawPartId = EgtGetNextRawPart( nCurrRawPartId) + end + local CalcResult = EgtIf( tonumber( PartAnswerArgs.MachiningOk) == 1, PART_VERIFICATION_RESULTS.MACHINABLE, PART_VERIFICATION_RESULTS.NOTMACHINABLE) + local WindowMachine = '' + for nMachineIndex = 1, #WindowMachineList do + if WindowMachineList[nMachineIndex].Name == AnswerChannel.Machine then + WindowMachine = WindowMachineList[nMachineIndex] + end + end + for nTagIndex = 1, #TagList do + table.insert( WindowMachine.PartList, { Tag = TagList[nTagIndex], CalcResult = CalcResult, Time = 0}) + end + if not WindowMachine.Time then + WindowMachine.Time = tonumber( PartAnswerArgs.Time) or 0 + else + WindowMachine.Time = WindowMachine.Time + ( tonumber( PartAnswerArgs.Time) or 0) + end + --local CalcResult + --CalcResult = EgtIf( tonumber( PartAnswerArgs.MachiningOk) == 1, PART_VERIFICATION_RESULTS.MACHINABLE, PART_VERIFICATION_RESULTS.NOTMACHINABLE) + --for nMachGroupIndex = 1, #MachGroupList do + -- local CurrMachGroup = MachGroupList[nMachGroupIndex] + -- if CurrMachGroup.PartId == tonumber( PartAnswerArgs.BarId) then + -- CurrMachGroup.CalcResult = CalcResult + -- CurrMachGroup.Time = ( tonumber( PartAnswerArgs.Time) or 0) + -- end + --end + --local bNestFound = false + --for nNestIndex = 1, #NestList do + -- if not bNestFound then + -- for nMachGroupIndex = 1, #NestList[nNestIndex].MachGroupList do + -- local CurrMachGroup = NestList[nNestIndex].MachGroupList[nMachGroupIndex] + -- if CurrMachGroup.PartId == tonumber( PartAnswerArgs.BarId) then + -- CurrMachGroup.CalcResult = CalcResult + -- CurrMachGroup.Time = ( tonumber( PartAnswerArgs.Time) or 0) + -- bNestFound = true + -- break + -- end + -- end + -- end + --end + end + table.insert( RemoveAnswerList, nAnswerIndex) + else + local sErrorMsg = 'Errore! Tentativo di verifica del pezzo sul canale' .. AnswerChannel.Channel .. ' fallito!' + EgtOutLog(sErrorMsg) + end + end + elseif bOk then + local sErrorMsg = 'Errore! Funzione di lettura risultato sul canale ' .. AnswerChannel.Channel .. ' fallita!' + EgtOutLog(sErrorMsg) + end + end + for nRemoveAnswerCount = #RemoveAnswerList, 1, -1 do + table.remove( AnswerChannelList, RemoveAnswerList[nRemoveAnswerCount]) + end + EgtPause( nTimeClock, true) + nTimeCount = nTimeCount + 1 + end + if #AnswerChannelList > 0 then + for nAnswerCount = 1, #AnswerChannelList do + local sTagList = EgtGetInfo( AnswerChannelList[nAnswerCount].PartId, 'LuxTagList') + local TagList = EgtSplitString( sTagList, ',') + CalcResult = PART_VERIFICATION_RESULTS.CALCULATIONFAILED + local sErrorMsg = 'Errore! Verifica sul pezzo ' .. AnswerChannelList[nAnswerCount].PartId .. ' fallita!' + EgtOutLog(sErrorMsg) + local WindowMachine = '' + for nMachineIndex = 1, #WindowMachineList do + if WindowMachineList[nMachineIndex].Name == AnswerChannelList[nAnswerCount].Machine then + WindowMachine = WindowMachineList[nMachineIndex] + end + end + for nTagIndex = 1, #TagList do + table.insert( WindowMachine.PartList, { Tag = TagList[nTagIndex], CalcResult = CalcResult, Time = 0}) + end + if not WindowMachine.Time then + WindowMachine.Time = 0 + end + end + end + -- do risultato + local JsonWindowMachineList = JSON:encode( WindowMachineList) + return 1, { Estimate = JsonWindowMachineList} +end + +local function IsTagInPart( nPartId, TagList) + local sPartTagList = EgtGetInfo( nPartId, 'LuxTagList') + local PartTagList = EgtSplitString( sPartTagList, ',') + local nQuantity = 0 + local FoundTagList = { } + for nPartTagListIndex = 1, #PartTagList do + for nTagListIndex = 1, #TagList do + if PartTagList[nPartTagListIndex] == TagList[nTagListIndex] then + nQuantity = nQuantity + 1 + table.insert( FoundTagList, PartTagList[nPartTagListIndex]) + end + end + end + return nQuantity > 0, nQuantity, FoundTagList +end + +local function GetTagQty( nPartId) + local sPartTagList = EgtGetInfo( nPartId, 'LuxTagList') + local PartTagList = EgtSplitString( sPartTagList, ',') + return #PartTagList +end + +-- Calcola la deviazione rispetto alle percentuali desiderate +local function computeDeviation(machineLoads, percentages) + local total = 0 + for _, load in ipairs(machineLoads) do + total = total + load + end + if total == 0 then + return 0 + end + + local dev = 0 + for j, load in ipairs(machineLoads) do + local q = load / total + local p = percentages[j] + dev = dev + (q - p) * (q - p) + end + return dev +end + +-- Algoritmo greedy per assegnare i pezzi alle macchine +local function assignGreedy(items, percentages) + local N = #items + local M = #items[1].MachGroupList + + -- carico iniziale per macchina + local machineLoads = {} + for j = 1, M do + machineLoads[j] = 0 + end + + -- assegnazione: per ogni pezzo, indice della macchina scelta (1..M) + local assignment = {} + + -- ORDINAMENTO INTELLIGENTE: + -- 1) prima i pezzi con meno macchine valide + -- 2) a parità, quelli più pesanti + table.sort(items, function(a, b) + local validA, validB = 0, 0 + for _, m in ipairs(a.MachGroupList) do + if m.CalcResult and m.CalcResult > 0 then + validA = validA + 1 + end + end + for _, m in ipairs(b.MachGroupList) do + if m.CalcResult and m.CalcResult > 0 then + validB = validB + 1 + end + end + if validA ~= validB then + return validA < validB + end + local maxA, maxB = 0, 0 + for _, m in ipairs(a.MachGroupList) do + if m.Time > maxA then + maxA = m.Time + end + end + for _, m in ipairs(b.MachGroupList) do + if m.Time > maxB then + maxB = m.Time + end + end + return maxA > maxB + end) + + for i, item in ipairs(items) do + local bestMachine = nil + local bestDev = nil + + -- prova ad assegnare il pezzo a ciascuna macchina + for j = 1, M do local mInfo = item.MachGroupList[j] + -- VINCOLO: CalcResult deve essere > 0 + if mInfo.CalcResult and mInfo.CalcResult > 0 then + local time = mInfo.Time + -- prova temporaneamente + machineLoads[j] = machineLoads[j] + time + local dev = computeDeviation(machineLoads, percentages) + machineLoads[j] = machineLoads[j] - time + if bestDev == nil or dev < bestDev then + bestDev = dev + bestMachine = j + end + end + end + -- Se nessuna macchina è valida → errore + -- DA MIGLIORARE! se nessuna macchina riesce a farlo, devo rivalutare i pezzi singoli su ogni macchina e farne la somma + if not bestMachine then + local sErrorMsg = 'Errore! Nessuna macchina valida per il MachGroup ' .. tostring(item.Index) .. '!' + EgtOutLog(sErrorMsg) + bestMachine = 0 + end + + -- assegna definitivamente + assignment[i] = bestMachine + local chosenTime = 0 + if bestMachine ~= 0 then + chosenTime = item.MachGroupList[bestMachine].Time + machineLoads[bestMachine] = machineLoads[bestMachine] + chosenTime + end + end + + return assignment, machineLoads +end + +local function Calc_Balance( QuestionArgs) + -- leggo argomenti passati + local sOrderUID = QuestionArgs["OrderUID"] + if not sOrderUID or #sOrderUID <= 0 then + local sErrorMsg = 'Errore! OrderUID vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local sUID = QuestionArgs["UID"] + if not sUID or #sUID <= 0 then + local sErrorMsg = 'Errore! UID vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local dBarLenght = tonumber( QuestionArgs["BarLenght"]) + if not dBarLenght or dBarLenght <= 0 then + local sErrorMsg = 'Errore! BarLenght vuoto o non valido! (' .. QuestionArgs["BarLenght"] .. ')' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local sTagList = QuestionArgs["TagList"] + if not sTagList or #sTagList <= 0 then + local sErrorMsg = 'Errore! TagList vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local sReqBalance = QuestionArgs["ReqBalance"] + if not sReqBalance or #sReqBalance <= 0 then + local sErrorMsg = 'Errore! ReqBalance vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- recupero cartella progetto + local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) + local sOrderDirPath = sDataDir .. '\\Beam\\' .. sOrderUID .. '\\' .. sUID + if not EgtExistsDirectory( sOrderDirPath) then + local sErrorMsg = 'Errore! Cartella della riga d\'ordine non trovata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- verifico esistenza cartella per progetti di bilanciamento + local sBalanceDirPath = sOrderDirPath .. '\\Balance' + if EgtExistsDirectory( sBalanceDirPath) then + if not EgtEmptyDirectory( sBalanceDirPath) then + local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sBalanceDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + else + if not EgtCreateDirectory( sBalanceDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sBalanceDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + end + -- creo cartella per progetto di bilanciamento corrente + local nCurrTime = os.time() + local sCurrBalanceDirPath = sBalanceDirPath .. '\\' .. nCurrTime + if not EgtCreateDirectory( sCurrBalanceDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sCurrBalanceDirPath .. ' per il progetto ' .. sOrderUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local sNgeFilePath = sOrderDirPath .. '\\' .. sUID .. '.nge' + local sBalanceNgeFilePath = sCurrBalanceDirPath .. '\\' .. sUID .. '_' .. nCurrTime .. '.nge' + if not EgtCopyFile( sNgeFilePath, sBalanceNgeFilePath) then + local sErrorMsg = 'Errore! Fallita copia del file!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + if not EgtOpenFile( sBalanceNgeFilePath) then + local sErrorMsg = 'Errore! Fallita apertura del file!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- recupero Id dei pezzi da nestare + local TagList = EgtSplitString( sTagList) + local nPartId = EgtGetFirstPart() + local PartIdList = {} + local SectionList = {} + local nPartCount = 0 + while nPartId do + local bFound, nQuantity, _ = IsTagInPart( nPartId, TagList) + if bFound then + PartIdList[nPartId] = nQuantity + -- recupero la sezione + --local dL = EgtGetInfo( nPartId, 'L', 'd') + local dH = EgtGetInfo( nPartId, 'H', 'd') + local dW = EgtGetInfo( nPartId, 'W', 'd') + local bInSectionList = false + for nSectionIndex = 1, #SectionList do + local Section = SectionList[nSectionIndex] + if Section.H == dH and Section.W == dW then + Section.PartIdList[nPartId] = nQuantity + bInSectionList = true + break + end + end + if not bInSectionList then + table.insert( SectionList, { H = dH, W = dW, PartIdList = { [nPartId] = nQuantity}}) + end + nPartCount = nPartCount + nQuantity + end + nPartId = EgtGetNextPart( nPartId) + end + local RawList = { { Len = dBarLenght, Qty = nPartCount}} + -- recupero cartella macchine + local MachineDirPath = EgtGetStringFromIni( 'Mach', 'MachinesDir', '', sIniFilePath) + if not MachineDirPath or #MachineDirPath <=0 then + local sErrorMsg = 'Errore! Cartella delle macchine non impostata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + --local MachinesDir = EgtFindAllFiles( MachineDirPath .. '\\*.*') + --local MachinesDir = { MachineDirPath .. '\\Saomad-Kairos', MachineDirPath .. '\\Essetre-FAST', MachineDirPath .. '\\Essetre-PF1250MAX' } + local MachinesDir = { MachineDirPath .. '\\Saomad-Kairos', MachineDirPath .. '\\Essetre-FAST'} + --local MachinesDir = { MachineDirPath .. '\\Essetre-FAST'} + local ReqBalance = JSON:decode( sReqBalance) + if not ReqBalance or not next( ReqBalance) then + local sErrorMsg = 'Errore! Richiesto bilanciamento senza macchine!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local MachineNameList = {} + local PercentageList = {} + local nPairCount = 0 + local sManualMachineName = 'Manual' + local bManualParts = false + for sMachineName, dPercentage in pairs(ReqBalance) do + if sMachineName and #sMachineName > 0 then + if sMachineName == sManualMachineName then + bManualParts = true + elseif EgtExistsDirectory( MachineDirPath .. '\\' .. sMachineName) then + table.insert( MachineNameList, sMachineName) + table.insert( PercentageList, dPercentage) + end + end + nPairCount = nPairCount + 1 + end + if bManualParts and (#MachineNameList > 0 or #PercentageList > 0) then + local sErrorMsg = 'Errore! Richiesto bilanciamento tra macchine e pezzi da fare a mano!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + if not bManualParts and ( #MachineNameList == 0 or #PercentageList == 0 or #MachineNameList ~= #PercentageList or #MachineNameList ~= nPairCount) then + local sErrorMsg = 'Errore! Richiesto bilanciamento con macchine o percentuali non valide!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + if bManualParts then + local sFakeManualMachineName = 'Essetre-FAST' -- da gestire da ini file o in altro modo? + table.insert( MachineNameList, sFakeManualMachineName) + end + local StartOffset = 20 + local Offset = 6 + -- per ogni sezione + for nSectionIndex = 1, #SectionList do + local Section = SectionList[nSectionIndex] + -- faccio nesting dei pezzi + local sNestingLogFilePath = sCurrBalanceDirPath .. '\\' .. sUID .. '_' .. nCurrTime .. 'NestLog.txt' + local bOk, sErrorMsg = BeamWallPipeLib.NestBeam( Section.PartIdList, RawList, MachineNameList, StartOffset, Offset, sNestingLogFilePath) + if not bOk then + return 0, { Error = sErrorMsg} + end + end + local MachineTagList = { } + if bManualParts then + -- recupero etichette divise per risposta + MachineTagList = { [sManualMachineName] = { TagList = { }, BarQty = 0, Time = 0}} + local nMachGroupId = EgtGetFirstMachGroup() + while nMachGroupId do + EgtSetCurrMachGroup( nMachGroupId) + local nRawPartId = EgtGetFirstRawPart() + while nRawPartId do + local nPartId = EgtGetFirstPartInRawPart( nRawPartId) + while nPartId do + local sTag = EgtGetInfo( nPartId, 'LuxTag') + --if not MachineTagList[sManualMachineName] then + -- MachineTagList[sMachineName] = { } + -- MachineTagList[sMachineName].TagList = { } + -- MachineTagList[sMachineName].BarQty = 0 + -- MachineTagList[sMachineName].Time = 0 + --end + table.insert( MachineTagList[sManualMachineName].TagList, sTag) + nPartId = EgtGetNextPartInRawPart( nPartId) + end + nRawPartId = EgtGetNextRawPart( nRawPartId) + end + MachineTagList['Manual'].BarQty = MachineTagList[sManualMachineName].BarQty + 1 + nMachGroupId = EgtGetNextMachGroup( nMachGroupId) + end + else + -- eseguo verifica dei gruppi di lavorazione creati + -- creo i singoli bwe e li mando da eseguire via redis + local AnswerChannelList = {} + local nMachGroupId = EgtGetFirstMachGroup() + local AnswerArgs = {} + local TagTimeList = {} + local dTotTime = 0 + local nNotMachinedPart = 0 + local MachGroupList = {} + local NestList = {} + while nMachGroupId do + local BarPath = sCurrBalanceDirPath + local ProjType = BWTYPES.BEAM + local sMachineName = EgtGetMachGroupMachineName( nMachGroupId) + local GlobState = CALCSTATES.NOTCALCULATED + local bResult, AnswerChannel = BeamWallPipeLib.AsyncVerifyPartCalc( nMachGroupId, BarPath, ProjType, sMachineName, GlobState, true) + if bResult then + table.insert( AnswerChannelList, { Channel = AnswerChannel, PartId = nMachGroupId, Machine = sMachineName}) + else + local sErrorMsg = 'Errore! Tentativo di verifica della barra ' .. nMachGroupId .. ' fallito!' + EgtOutLog(sErrorMsg) + end + local nNestIndex = EgtGetInfo( nMachGroupId, "NestIndex", 'i') + local bFound = false + for nMachGroupIndex = 1, #NestList do + local CurrNest = NestList[nMachGroupIndex] + if nNestIndex == CurrNest.NestIndex then + bFound = true + table.insert( CurrNest.MachGroupList, { PartId = nMachGroupId, Machine = sMachineName}) + end + end + if not bFound then + table.insert( NestList, { NestIndex = nNestIndex, MachGroupList = { { PartId = nMachGroupId, Machine = sMachineName}}}) + end + table.insert( MachGroupList, { PartId = nMachGroupId, NestIndex = nNestIndex, Machine = sMachineName}) + nMachGroupId = EgtGetNextMachGroup( nMachGroupId) + end + local nTimeCount = 0 + local nTimeClock = 100 + while nTimeCount < 1200 and #AnswerChannelList > 0 do + local RemoveAnswerList = {} + for nAnswerIndex = 1, #AnswerChannelList do + local AnswerChannel = AnswerChannelList[nAnswerIndex] + -- verifico se ci sono i risultati e aspetto che arrivino tutti + local bOk, bAnswerReceived, PartAnswerArgs = BeamWallPipeLib.AsyncVerifyPartResult( AnswerChannel.Channel) + if bOk then + if bAnswerReceived then + if PartAnswerArgs then + if PartAnswerArgs.Error then + local sErrorMsg = 'Errore! La verifica del pezzo ' .. AnswerChannel.PartId .. ' ha restituito un errore! (' .. PartAnswerArgs.Error .. ')' + EgtOutLog(sErrorMsg) + else + --local sTagList = EgtGetInfo( AnswerChannel.PartId, 'LuxTagList') + --local TagList = EgtSplitString( sTagList, ',') + -- + local CalcResult + CalcResult = EgtIf( tonumber( PartAnswerArgs.MachiningOk) == 1, PART_VERIFICATION_RESULTS.MACHINABLE, PART_VERIFICATION_RESULTS.NOTMACHINABLE) + for nMachGroupIndex = 1, #MachGroupList do + local CurrMachGroup = MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == tonumber( PartAnswerArgs.BarId) then + CurrMachGroup.CalcResult = CalcResult + CurrMachGroup.Time = ( tonumber( PartAnswerArgs.Time) or 0) + end + end + local bNestFound = false + for nNestIndex = 1, #NestList do + if not bNestFound then + for nMachGroupIndex = 1, #NestList[nNestIndex].MachGroupList do + local CurrMachGroup = NestList[nNestIndex].MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == tonumber( PartAnswerArgs.BarId) then + CurrMachGroup.CalcResult = CalcResult + CurrMachGroup.Time = ( tonumber( PartAnswerArgs.Time) or 0) + bNestFound = true + break + end + end + end + end + end + table.insert( RemoveAnswerList, nAnswerIndex) + else + local sErrorMsg = 'Errore! Tentativo di verifica del pezzo sul canale' .. AnswerChannel.Channel .. ' fallito!' + EgtOutLog(sErrorMsg) + end + end + elseif bOk then + local sErrorMsg = 'Errore! Funzione di lettura risultato sul canale ' .. AnswerChannel.Channel .. ' fallita!' + EgtOutLog(sErrorMsg) + end + end + for nRemoveAnswerCount = #RemoveAnswerList, 1, -1 do + table.remove( AnswerChannelList, RemoveAnswerList[nRemoveAnswerCount]) + end + EgtPause( nTimeClock, true) + nTimeCount = nTimeCount + 1 + end + if #AnswerChannelList > 0 then + for nAnswerCount = 1, #AnswerChannelList do + local sTagList = EgtGetInfo( AnswerChannelList[nAnswerCount].PartId, 'LuxTagList') + local TagList = EgtSplitString( sTagList, ',') + CalcResult = PART_VERIFICATION_RESULTS.CALCULATIONFAILED + local sErrorMsg = 'Errore! Verifica sul pezzo ' .. AnswerChannelList[nAnswerCount].PartId .. ' fallita!' + EgtOutLog(sErrorMsg) + for nMachGroupIndex = 1, #MachGroupList do + local CurrMachGroup = MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == AnswerChannelList.PartId then + CurrMachGroup.CalcResult = 0 + CurrMachGroup.Time = 0 + end + end + local bNestFound = false + for nNestIndex = 1, #NestList do + if not bNestFound then + for nMachGroupIndex = 1, #NestList[nNestIndex].MachGroupList do + local CurrMachGroup = NestList[nNestIndex].MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == AnswerChannelList.PartId then + CurrMachGroup.CalcResult = 0 + CurrMachGroup.Time = 0 + bNestFound = true + break + end + end + end + end + end + end + -- divido le barre sulle macchine secondo le percentuali dei tempi + local assignment, loads = assignGreedy(NestList, PercentageList) + -- recupero etichette divise per risposta + for nAssignmentIndex = 1, #assignment do + if assignment[nAssignmentIndex] == 0 then + -- faccio verifica su pezzi singoli per avere un tempo??????????? + EgtOutLog( 'Errore! Barra non assegnata a nessuna macchina!!') + else + local nMachGroupId = NestList[nAssignmentIndex].MachGroupList[assignment[nAssignmentIndex]].PartId + local sMachineName = NestList[nAssignmentIndex].MachGroupList[assignment[nAssignmentIndex]].Machine + EgtSetCurrMachGroup( nMachGroupId) + local nRawPartId = EgtGetFirstRawPart() + while nRawPartId do + local nPartId = EgtGetFirstPartInRawPart( nRawPartId) + while nPartId do + local sTag = EgtGetInfo( nPartId, 'LuxTag') + if not MachineTagList[sMachineName] then + MachineTagList[sMachineName] = { } + MachineTagList[sMachineName].TagList = { } + MachineTagList[sMachineName].BarQty = 0 + MachineTagList[sMachineName].Time = 0 + end + table.insert( MachineTagList[sMachineName].TagList, sTag) + nPartId = EgtGetNextPartInRawPart( nPartId) + end + nRawPartId = EgtGetNextRawPart( nRawPartId) + end + MachineTagList[sMachineName].BarQty = MachineTagList[sMachineName].BarQty + 1 + MachineTagList[sMachineName].Time = MachineTagList[sMachineName].Time + NestList[nAssignmentIndex].MachGroupList[assignment[nAssignmentIndex]].Time + end + end + end + EgtResetCurrMachGroup() + local bDebug = true + if bDebug then + EgtSaveFile() + else + EgtEmptyDirectory( sCurrBalanceDirPath) + EgtEraseDirectory( sCurrBalanceDirPath) + end + -- do risultato + local JsonMachineTagList = JSON:encode( MachineTagList) + return 1, { Balance = JsonMachineTagList} +end + +local function Create_Production( QuestionArgs) + local sUID = QuestionArgs["UID"] + if not sUID or #sUID <= 0 then + local sErrorMsg = 'Errore! UID vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local sProdCreate = QuestionArgs["ProdCreate"] + if not sProdCreate or #sProdCreate <= 0 then + local sErrorMsg = 'Errore! ProdCreate vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- leggo dati per creare ordine + local ProdCreate = JSON:decode( sProdCreate) + if not ProdCreate or not next( ProdCreate) then + local sErrorMsg = 'Errore! Lista pezzi da mettere in produzione vuota o non leggibile!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- creo cartella per ordine di produzione + local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) + local sProductionDirPath = sDataDir .. '\\Beam\\' .. ProdCreate.ProdName + if EgtExistsDirectory( sProductionDirPath) then + if not EgtEmptyDirectory( sProductionDirPath) then + local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sProductionDirPath .. ' per il progetto ' .. sUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + else + if not EgtCreateDirectory( sProductionDirPath) then + local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sProductionDirPath .. ' per il progetto ' .. sUID .. '!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + end + -- creo il contesto secondario + local nDefaultContext = EgtGetContext() + local nTempContext = EgtCreateContext() + -- importo tutti i pezzi richiesti + EgtNewFile() + for sOrderID, Order in pairs(ProdCreate.OrderTagDict) do + for sOrderRowID, TagList in pairs(Order) do + -- imposto il contesto secondario + EgtSetContext( nTempContext) + -- apro il progetto + local sNgeFilePath = sDataDir .. '\\Beam\\' .. sOrderID .. '\\' .. sOrderRowID .. '\\' .. sOrderRowID .. '.nge' if not EgtOpenFile( sNgeFilePath) then local sErrorMsg = 'Errore! Fallita apertura del file!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end - -- recupero cartella macchine - local MachineDirPath = EgtGetStringFromIni( 'Mach', 'MachinesDir', '', sIniFilePath) - if not MachineDirPath or #MachineDirPath <=0 then - local sErrorMsg = 'Errore! Cartella delle macchine non impostata!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - --local MachinesDir = EgtFindAllFiles( MachineDirPath .. '\\*.*') - local MachinesDir = { MachineDirPath .. '\\Saomad-Kairos', MachineDirPath .. '\\Essetre-FAST', MachineDirPath .. '\\Essetre-PF1250MAX' } - --local MachinesDir = { MachineDirPath .. '\\Essetre-FAST'} - local BeamMachineList = {} - local AnswerChannelList = {} - for nMachineIndex = 1, #MachinesDir do - local _, sMachineName, _ = EgtSplitPath( MachinesDir[nMachineIndex]) - local sMaterial = EgtGetStringFromIni( 'General', 'Material', '', MachineDirPath .. '\\' .. sMachineName .. '\\' .. sMachineName .. '.ini') - if sMaterial == 'Beam' then - local sMachineOrderDirPath = sOrderDirPath .. '\\' .. sMachineName - -- creo cartella per macchina - if EgtExistsDirectory( sMachineOrderDirPath) then - if not EgtEmptyDirectory( sMachineOrderDirPath) then - local sErrorMsg = 'Errore! Impossibile svuotare cartella ' .. sMachineOrderDirPath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - else - if not EgtCreateDirectory( sMachineOrderDirPath) then - local sErrorMsg = 'Errore! Impossibile creare cartella ' .. sMachineOrderDirPath .. ' per il progetto ' .. sOrderUID .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end + -- cerco i pezzi + local nPartId = EgtGetFirstPart() + local PartIdList = {} + local SectionList = {} + local nPartCount = 0 + while nPartId do + local bFound, _, FoundTagList = IsTagInPart( nPartId, TagList) + if bFound then + local sNewTagList = '' + for nFoundTagIndex = 1, #FoundTagList do + sNewTagList = sNewTagList .. FoundTagList[nFoundTagIndex] .. EgtIf( nFoundTagIndex == #FoundTagList, '', ',') end - local sMachineNgeFilePath = sMachineOrderDirPath .. '\\' .. sUID .. '.nge' - -- e copio file nge - if not EgtCopyFile( sNgeFilePath, sMachineNgeFilePath) then - local sErrorMsg = 'Errore! Copia del file nge per macchina ' .. sMachineName .. ' e progetto ' .. sUID .. ' fallita!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - if not EgtOpenFile( sMachineNgeFilePath) then - local sErrorMsg = 'Errore! Fallita apertura del file per macchina ' .. sMachineName .. '!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} - end - -- creo i singoli bwe e li mando da eseguire via redis - local nPartId = EgtGetFirstPart() - local AnswerArgs = {} - local TagTimeList = {} - local dTotTime = 0 - local nNotMachinedPart = 0 - while nPartId do - local BarPath = sMachineOrderDirPath - local ProjType = BWTYPES.BEAM - local GlobState = CALCSTATES.NOTCALCULATED - local bResult, AnswerChannel = BeamWallPipeLib.AsyncVerifyBtlPartCalc( nPartId, BarPath, ProjType, sMachineName, GlobState) - if bResult then - table.insert( AnswerChannelList, { Channel = AnswerChannel, PartId = nPartId, Machine = sMachineName}) - else - local sErrorMsg = 'Errore! Tentativo di verifica del pezzo ' .. nPartId .. ' fallito!' - EgtOutLog(sErrorMsg) - end - nPartId = EgtGetNextPart( nPartId) - end - table.insert( BeamMachineList, { Name = sMachineName, PartList = { }}) + EgtSetInfo( nPartId, 'LuxTagList', sNewTagList) + EgtCopyEx( nTempContext, nPartId, nDefaultContext, GDB_ID.ROOT) end + nPartId = EgtGetNextPart( nPartId) end - local nTimeCount = 0 - local nTimeClock = 100 - while nTimeCount < 1200 and #AnswerChannelList > 0 do - local RemoveAnswerList = {} - for nAnswerIndex = 1, #AnswerChannelList do - local AnswerChannel = AnswerChannelList[nAnswerIndex] - -- verifico se ci sono i risultati e aspetto che arrivino tutti - local bOk, bAnswerReceived, PartAnswerArgs = BeamWallPipeLib.AsyncVerifyBtlPartResult( AnswerChannel.Channel) - if bOk then - if bAnswerReceived then - if PartAnswerArgs then - local sTagList = EgtGetInfo( AnswerChannel.PartId, 'LuxTagList') - local TagList = EgtSplitString( sTagList, ',') - local CalcResult - CalcResult = EgtIf( tonumber( PartAnswerArgs.MachiningOk) == 1, PART_VERIFICATION_RESULTS.MACHINABLE, PART_VERIFICATION_RESULTS.NOTMACHINABLE) - local BeamMachine = '' - for nMachineIndex = 1, #BeamMachineList do - if BeamMachineList[nMachineIndex].Name == AnswerChannel.Machine then - BeamMachine = BeamMachineList[nMachineIndex] - end - end - for nTagIndex = 1, #TagList do - table.insert( BeamMachine.PartList, { Tag = TagList[nTagIndex], CalcResult = CalcResult, Time = ( PartAnswerArgs.Time or 0)}) - end - table.insert( RemoveAnswerList, nAnswerIndex) - else - local sErrorMsg = 'Errore! Tentativo di verifica del pezzo sul canale' .. AnswerChannel.Channel .. ' fallito!' - EgtOutLog(sErrorMsg) - end - end - elseif bOk then - local sErrorMsg = 'Errore! Funzione di lettura risultato sul canale ' .. AnswerChannel.Channel .. ' fallita!' - EgtOutLog(sErrorMsg) - end - end - for nRemoveAnswerCount = #RemoveAnswerList, 1, -1 do - table.remove( AnswerChannelList, RemoveAnswerList[nRemoveAnswerCount]) - end - EgtPause( nTimeClock, true) - nTimeCount = nTimeCount + 1 - end - if #AnswerChannelList > 0 then - for nAnswerCount = 1, #AnswerChannelList do - local sTagList = EgtGetInfo( AnswerChannelList[nAnswerCount].PartId, 'LuxTagList') - local TagList = EgtSplitString( sTagList, ',') - CalcResult = PART_VERIFICATION_RESULTS.CALCULATIONFAILED - local sErrorMsg = 'Errore! Verifica sul pezzo ' .. AnswerChannelList[nAnswerCount].PartId .. ' fallita!' - EgtOutLog(sErrorMsg) - local BeamMachine - for nMachineIndex = 1, #BeamMachineList do - if BeamMachineList[nMachineIndex].Name == AnswerChannelList[nAnswerCount].Machine then - BeamMachine = BeamMachineList[nMachineIndex] - end - end - for nTagIndex = 1, #TagList do - table.insert( BeamMachine.PartList, { Tag = TagList[nTagIndex], CalcResult = CalcResult, Time = 0}) - end - end - end - -- do risultato - local JsonBeamMachineList = JSON:encode( BeamMachineList) - return 1, { Estimate = JsonBeamMachineList} - else - local sErrorMsg = 'Errore! UID vuoto!' - EgtOutLog(sErrorMsg) - return 0, { Error = sErrorMsg} end - else - local sErrorMsg = 'Errore! OrderUID vuoto!' + end + EgtSetContext( nDefaultContext) + local sProductionNgeFilePath = sProductionDirPath .. '\\' .. sUID .. '.nge' + EgtSaveFile( sProductionNgeFilePath, GDB_NT.CMPTXT) + -- calcolo quantita' barre, volume, barre lineari + local SectionList = {} + local TotVol = 0 + local nPartId = EgtGetFirstPart( nRawPartId) + while nPartId do + local dW = EgtGetInfo( nPartId, "W", 'd') + local dH = EgtGetInfo( nPartId, "H", 'd') + local dL = EgtGetInfo( nPartId, "L", 'd') + local sMaterial = EgtGetInfo( nPartId, "MATERIAL") + local sTagList = EgtGetInfo( nPartId, 'LuxTagList') + local TagList = EgtSplitString( sTagList, ',') + local nCnt = #TagList + local Section = nil + for nSectionIndex = 1, #SectionList do + if SectionList[nSectionIndex].W == dW and SectionList[nSectionIndex].H == dH and SectionList[nSectionIndex].Material == sMaterial then + Section = SectionList[nSectionIndex] + end + end + if Section then + Section.Qty = Section.Qty + nCnt + Section.TotLen = Section.TotLen + ( dL * nCnt) + Section.Volume = Section.Volume + ( dW * dH * dL * nCnt) + else + table.insert( SectionList, { W = dW, H = dH, Material = sMaterial, Qty = nCnt, TotLen = ( dL * nCnt), Volume = (dW * dH * dL * nCnt)}) + end + TotVol = TotVol + ((( dW / 1000) * ( dH / 1000) * ( dL / 1000)) * nCnt) + nPartId = EgtGetNextPart( nPartId) + end + local BOM = {} + for nSectionIndex = 1, #SectionList do + local Section = SectionList[nSectionIndex] + table.insert( BOM, { ClassCode = 'BeamTrunk', + ItemCode = 'Pine-' .. Section.W .. 'x' .. Section.H, + ItemQty = Section.Qty, + DescriptionCode = '', + Volume = Section.Volume / 1000000000, + Qty = Section.TotLen / 1000}) + end + -- do risultato + local JsonBOM = JSON:encode( BOM) + -- do risultato + return 1, { BOM = JsonBOM} +end + +local function Calc_Production( QuestionArgs) + -- leggo argomenti passati + local sUID = QuestionArgs["UID"] + if not sUID or #sUID <= 0 then + local sErrorMsg = 'Errore! UID vuoto!' EgtOutLog(sErrorMsg) return 0, { Error = sErrorMsg} end + local sProdCalc = QuestionArgs["ProdCalc"] + if not sProdCalc or #sProdCalc <= 0 then + local sErrorMsg = 'Errore! ProdCalc vuoto!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- leggo dati per creare ordine + local ProdCalc = JSON:decode( sProdCalc) + if not ProdCalc or not next( ProdCalc) then + local sErrorMsg = 'Errore! Lista pezzi da mettere in produzione vuota o non leggibile!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- recupero cartella progetto + local sDataDir = EgtGetStringFromIni( 'Lux', 'DataDir', '', sIniFilePath) + local sProductionDirPath = sDataDir .. '\\Beam\\' .. ProdCalc.ProdName + if not EgtExistsDirectory( sProductionDirPath) then + local sErrorMsg = 'Errore! Cartella dell\'ordine di produzione non trovata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local sProductionNgeFilePath = sProductionDirPath .. '\\' .. sUID .. '.nge' + if not EgtOpenFile( sProductionNgeFilePath) then + local sErrorMsg = 'Errore! Fallita apertura del file!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + -- elimino tutti i MachGroup gia' presenti + local nMachGroupId = EgtGetFirstMachGroup() + while nMachGroupId do + EgtSetCurrMachGroup( nMachGroupId) + local nRawPartId = EgtGetFirstRawPart( nMachGroupId) + while nRawPartId do + local nPartId = EgtGetFirstPartInRawPart( nRawPartId) + while nPartId do + local sLuxTag = EgtGetInfo( nPartId, 'LuxTag') + EgtResetCurrMachGroup() + local nOrigPartId = EgtDuploGetOriginal( nPartId) + EgtSetCurrMachGroup( nMachGroupId) + local sTagList = EgtGetInfo( nOrigPartId, 'LuxTagList') + -- recupero liste etichette usate e non + local TagTypeList = EgtSplitString( sTagList, ';') + local UnusedTagList = {} + local UsedTagList = {} + if #TagTypeList == 1 then + UnusedTagList = EgtSplitString( sTagList, ',') + else + if TagTypeList[1] and #TagTypeList[1] > 0 then + UnusedTagList = EgtSplitString( TagTypeList[1], ',') + end + if TagTypeList[2] and #TagTypeList[2] > 0 then + UsedTagList = EgtSplitString( TagTypeList[2], ',') + end + end + local nAssignedTagIndex = 0 + for nUsedTagIndex = 1, #UsedTagList do + if UsedTagList[nUsedTagIndex] == sLuxTag then + nAssignedTagIndex = nUsedTagIndex + break + end + end + if nAssignedTagIndex == 0 then + EgtOutLog( 'Warning! Etichetta per pezzo non trovata nell\'eliminazione prima del nesting!') + else + local sAssignedTag = UnusedTagList[nAssignedTagIndex] + -- creo stringa per info etichette aggiornata + local sNewTagList = '' + for nUnusedTagIndex = 1, #UnusedTagList do + sNewTagList = sNewTagList .. UnusedTagList[nUnusedTagIndex] .. ',' + end + sNewTagList = sNewTagList .. UsedTagList[nAssignedTagIndex] .. ';' + for nUsedTagIndex = 1, #UsedTagList do + if nUsedTagIndex ~= nAssignedTagIndex then + sNewTagList = sNewTagList .. UsedTagList[nUsedTagIndex] .. EgtIf( nUsedTagIndex == #UsedTagList or ( nUsedTagIndex == #UsedTagList - 1 and nAssignedTagIndex == #UsedTagList), '', ',') + end + end + -- aggiorno etichetta su pezzo originale + EgtSetInfo( nOrigPartId, 'LuxTagList', sNewTagList) + end + EgtRemovePartFromRawPart( nPartId) + EgtErase( nPartId) + nPartId = EgtGetNextPartInRawPart( nPartId) + end + nRawPartId = EgtGetNextRawPart( nRawPartId) + end + EgtRemoveMachGroup( nMachGroupId) + nMachGroupId = EgtGetFirstMachGroup() + end + EgtSaveFile() + -- recupero Id dei pezzi da nestare + local TagList = EgtSplitString( sTagList) + local nPartId = EgtGetFirstPart() + local PartIdList = {} + local SectionList = {} + local nPartCount = 0 + while nPartId do + local nQuantity = GetTagQty( nPartId) + PartIdList[nPartId] = nQuantity + -- recupero la sezione + local dH = EgtGetInfo( nPartId, 'H', 'd') + local dW = EgtGetInfo( nPartId, 'W', 'd') + local bInSectionList = false + for nSectionIndex = 1, #SectionList do + local Section = SectionList[nSectionIndex] + if Section.H == dH and Section.W == dW then + Section.PartIdList[nPartId] = nQuantity + bInSectionList = true + break + end + end + if not bInSectionList then + table.insert( SectionList, { H = dH, W = dW, PartIdList = { [nPartId] = nQuantity}}) + end + nPartCount = nPartCount + nQuantity + nPartId = EgtGetNextPart( nPartId) + end + local RawList = { } + for sSectXMat, LenList in pairs(ProdCalc.ItemAvaiList) do + local SectXMatSplit = EgtSplitString( sSectXMat, '-') + local SectSplit = EgtSplitString( SectXMatSplit[2], 'x') + local dW = tonumber( SectSplit[1]) + local dH = tonumber( SectSplit[2]) + for nSectIndex = 1, #SectionList do + local CurrSection = SectionList[nSectIndex] + if CurrSection.W == dW and CurrSection.H == dH then + CurrSection.RawList = {} + for nLenIndex = 1, #LenList do + local CurrLen = LenList[nLenIndex] + table.insert( CurrSection.RawList, { ItemID = CurrLen.ItemID, Len = CurrLen.Lenght, Qty = CurrLen.Qty}) + end + end + end + end + -- recupero cartella macchine + local MachineDirPath = EgtGetStringFromIni( 'Mach', 'MachinesDir', '', sIniFilePath) + if not MachineDirPath or #MachineDirPath <=0 then + local sErrorMsg = 'Errore! Cartella delle macchine non impostata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + --local MachinesDir = EgtFindAllFiles( MachineDirPath .. '\\*.*') + --local MachinesDir = { MachineDirPath .. '\\Saomad-Kairos', MachineDirPath .. '\\Essetre-FAST', MachineDirPath .. '\\Essetre-PF1250MAX' } + local MachinesDir = { MachineDirPath .. '\\Saomad-Kairos', MachineDirPath .. '\\Essetre-FAST'} + --local MachinesDir = { MachineDirPath .. '\\Essetre-FAST'} + local bMachineFound = false + for nMachineIndex = 1, #MachinesDir do + local _, sMachineName, _ = EgtSplitPath( MachinesDir[nMachineIndex]) + if sMachineName == ProdCalc.MachineName then + bMachineFound = true + break + end + end + if not bMachineFound then + local sErrorMsg = 'Errore! Macchina ' .. ProdCalc.MachineName .. ' con cui calcolare ordine di produzione ' .. ProdCalc.ProdName .. ' non trovata!' + EgtOutLog(sErrorMsg) + return 0, { Error = sErrorMsg} + end + local StartOffset = 20 + local Offset = 6 + -- per ogni sezione + for nSectionIndex = 1, #SectionList do + local Section = SectionList[nSectionIndex] + -- faccio nesting dei pezzi + local sNestingLogFilePath = sProductionDirPath .. '\\' .. ProdCalc.ProdName .. 'NestLog.txt' + local bOk, sErrorMsg = BeamWallPipeLib.NestBeam( Section.PartIdList, Section.RawList, { ProdCalc.MachineName}, StartOffset, Offset, sNestingLogFilePath) + if not bOk then + return 0, { Error = sErrorMsg} + end + end + EgtSaveFile() + -- eseguo verifica dei gruppi di lavorazione creati + -- creo i singoli bwe e li mando da eseguire via redis + local AnswerChannelList = {} + nMachGroupId = EgtGetFirstMachGroup() + local AnswerArgs = {} + local TagTimeList = {} + local dTotTime = 0 + local nNotMachinedPart = 0 + local MachGroupList = {} + local NestList = {} + while nMachGroupId do + local BarPath = sProductionDirPath + local ProjType = BWTYPES.BEAM + local sMachineName = EgtGetMachGroupMachineName( nMachGroupId) + local GlobState = CALCSTATES.NOTCALCULATED + local bResult, AnswerChannel = BeamWallPipeLib.AsyncVerifyPartCalc( nMachGroupId, BarPath, ProjType, sMachineName, GlobState, true) + if bResult then + table.insert( AnswerChannelList, { Channel = AnswerChannel, PartId = nMachGroupId, Machine = sMachineName}) + else + local sErrorMsg = 'Errore! Tentativo di verifica della barra ' .. nMachGroupId .. ' fallito!' + EgtOutLog(sErrorMsg) + end + local nNestIndex = EgtGetInfo( nMachGroupId, "NestIndex", 'i') + local bFound = false + for nMachGroupIndex = 1, #NestList do + local CurrNest = NestList[nMachGroupIndex] + if nNestIndex == CurrNest.NestIndex then + bFound = true + table.insert( CurrNest.MachGroupList, { PartId = nMachGroupId, Machine = sMachineName}) + end + end + if not bFound then + table.insert( NestList, { NestIndex = nNestIndex, MachGroupList = { { PartId = nMachGroupId, Machine = sMachineName}}}) + end + -- recupero info grezzo + local dW = EgtGetInfo( nMachGroupId, 'BARWIDTH', 'd') + local dH = EgtGetInfo( nMachGroupId, 'BARHEIGHT', 'd') + local dL = EgtGetInfo( nMachGroupId, 'BARLEN', 'd') + table.insert( MachGroupList, { PartId = nMachGroupId, NestIndex = nNestIndex, Machine = sMachineName, W = dW, H = dH, L = dL}) + nMachGroupId = EgtGetNextMachGroup( nMachGroupId) + end + local nTimeCount = 0 + local nTimeClock = 100 + while nTimeCount < 1200 and #AnswerChannelList > 0 do + local RemoveAnswerList = {} + for nAnswerIndex = 1, #AnswerChannelList do + local AnswerChannel = AnswerChannelList[nAnswerIndex] + -- verifico se ci sono i risultati e aspetto che arrivino tutti + local bOk, bAnswerReceived, PartAnswerArgs = BeamWallPipeLib.AsyncVerifyPartResult( AnswerChannel.Channel) + if bOk then + if bAnswerReceived then + if PartAnswerArgs then + if PartAnswerArgs.Error then + local sErrorMsg = 'Errore! La verifica del pezzo ' .. AnswerChannel.PartId .. ' ha restituito un errore! (' .. PartAnswerArgs.Error .. ')' + EgtOutLog(sErrorMsg) + else + --local sTagList = EgtGetInfo( AnswerChannel.PartId, 'LuxTagList') + --local TagList = EgtSplitString( sTagList, ',') + -- + local CalcResult + CalcResult = EgtIf( tonumber( PartAnswerArgs.MachiningOk) == 1, PART_VERIFICATION_RESULTS.MACHINABLE, PART_VERIFICATION_RESULTS.NOTMACHINABLE) + for nMachGroupIndex = 1, #MachGroupList do + local CurrMachGroup = MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == tonumber( PartAnswerArgs.BarId) then + CurrMachGroup.CalcResult = CalcResult + CurrMachGroup.Time = ( tonumber( PartAnswerArgs.Time) or 0) + end + end + local bNestFound = false + for nNestIndex = 1, #NestList do + if not bNestFound then + for nMachGroupIndex = 1, #NestList[nNestIndex].MachGroupList do + local CurrMachGroup = NestList[nNestIndex].MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == tonumber( PartAnswerArgs.BarId) then + CurrMachGroup.CalcResult = CalcResult + CurrMachGroup.Time = ( tonumber( PartAnswerArgs.Time) or 0) + bNestFound = true + break + end + end + end + end + end + table.insert( RemoveAnswerList, nAnswerIndex) + else + local sErrorMsg = 'Errore! Tentativo di verifica del pezzo sul canale' .. AnswerChannel.Channel .. ' fallito!' + EgtOutLog(sErrorMsg) + end + end + elseif bOk then + local sErrorMsg = 'Errore! Funzione di lettura risultato sul canale ' .. AnswerChannel.Channel .. ' fallita!' + EgtOutLog(sErrorMsg) + end + end + for nRemoveAnswerCount = #RemoveAnswerList, 1, -1 do + table.remove( AnswerChannelList, RemoveAnswerList[nRemoveAnswerCount]) + end + EgtPause( nTimeClock, true) + nTimeCount = nTimeCount + 1 + end + if #AnswerChannelList > 0 then + for nAnswerCount = 1, #AnswerChannelList do + local sTagList = EgtGetInfo( AnswerChannelList[nAnswerCount].PartId, 'LuxTagList') + local TagList = EgtSplitString( sTagList, ',') + CalcResult = PART_VERIFICATION_RESULTS.CALCULATIONFAILED + local sErrorMsg = 'Errore! Verifica sul pezzo ' .. AnswerChannelList[nAnswerCount].PartId .. ' fallita!' + EgtOutLog(sErrorMsg) + for nMachGroupIndex = 1, #MachGroupList do + local CurrMachGroup = MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == AnswerChannelList.PartId then + CurrMachGroup.CalcResult = 0 + CurrMachGroup.Time = 0 + end + end + local bNestFound = false + for nNestIndex = 1, #NestList do + if not bNestFound then + for nMachGroupIndex = 1, #NestList[nNestIndex].MachGroupList do + local CurrMachGroup = NestList[nNestIndex].MachGroupList[nMachGroupIndex] + if CurrMachGroup.PartId == AnswerChannelList.PartId then + CurrMachGroup.CalcResult = 0 + CurrMachGroup.Time = 0 + bNestFound = true + break + end + end + end + end + end + end + -- creo lista grezzi utilizzati e calcolo tempo totale + local dTotTime = 0 + local RawList = { } + for nMachGroupIndex = 1, #MachGroupList do + local MachGroup = MachGroupList[nMachGroupIndex] + dTotTime = dTotTime + MachGroup.Time + local bRawFound = false + for nRawIndex = 1, #RawList do + local Raw = RawList[nRawIndex] + if MachGroup.W == Raw.W and MachGroup.H == Raw.H and MachGroup.L == Raw.L then + bRawFound = true + Raw.Count = Raw.Count + 1 + break + end + end + if not bRawFound then + for nSectIndex = 1, #SectionList do + local bFound = false + local CurrSection = SectionList[nSectIndex] + if CurrSection.W == MachGroup.W and CurrSection.H == MachGroup.H then + for nRawIndex = 1, #CurrSection.RawList do + if CurrSection.RawList[nRawIndex].Len == MachGroup.L then + table.insert( RawList, { ItemID = CurrSection.RawList[nRawIndex].ItemID, W = MachGroup.W, H = MachGroup.H, L = MachGroup.L, Count = 1, Material = 'Pine', Used = MachGroup.L}) + bFound = true + break + end + end + end + if bFound then + break + end + end + end + end + + -- do risultato + local JsonRawList = JSON:encode( RawList) + return 1, { TotTime = dTotTime, RawList = JsonRawList} end + + local nErr = 999 local nNilCount = 0 local bRun = true @@ -367,6 +1416,8 @@ while bRun do local nMode = tonumber( QuestionArgs["Mode"]) -- esecuzione della corretta modilita' EgtOutLog('Ricevuta richiesta calcolo: nThreadIndex=' .. nThreadIndex .. ', nId=' .. nId .. ', UID=' .. sUid .. ', nMode=' .. nMode) + _G.WDG = {} + WDG.REDISID = nRedisConnectionId local Result = {} if nMode == QUESTION_MODES.ORDER then if QuestionArgs.SubMode then @@ -378,6 +1429,19 @@ while bRun do _G.LUX = {} LUX.REDISID = nRedisConnectionId nResult, AnswerArgs = Calc_Estimate( QuestionArgs) + elseif nSubMode == QUESTION_ORDER_SUBMODES.BALANCE then + _G.LUX = {} + LUX.REDISID = nRedisConnectionId + nResult, AnswerArgs = Calc_Balance( QuestionArgs) + AnswerArgs.Group = QuestionArgs['Group'] + elseif nSubMode == QUESTION_ORDER_SUBMODES.CREATE_PRODUCTION then + --_G.LUX = {} + --LUX.REDISID = nRedisConnectionId + nResult, AnswerArgs = Create_Production( QuestionArgs) + elseif nSubMode == QUESTION_ORDER_SUBMODES.CALC_PRODUCTION then + _G.LUX = {} + LUX.REDISID = nRedisConnectionId + nResult, AnswerArgs = Calc_Production( QuestionArgs) else nResult = 0 local sErrorMsg = 'Errore! Valore di SubMode errato!' diff --git a/WindowVerifyPipe.lua b/WindowVerifyPipe.lua new file mode 100644 index 0000000..ceaeb4f --- /dev/null +++ b/WindowVerifyPipe.lua @@ -0,0 +1,153 @@ +-- +-- EEEEEEEEEE GGGGGG TTTTTTTTTTTTTT +-- EEEEEEEEEE GGGGGGGGGG TTTTTTTTTTTTTT +-- EEEE GGGG GGGG TTTT +-- EEEE GGGG TTTT +-- EEEEEEE GGGG GGGGGGG TTTT +-- EEEEEEE GGGG GGGGGGG TTTT +-- EEEE GGGG GGGG TTTT +-- EEEE GGGG GGGG TTTT +-- EEEEEEEEEE GGGGGGGGGG TTTT +-- EEEEEEEEEE GGGGGG TTTT +-- +-- BeamWallVerifyPipe by Egalware s.r.l. 2025/12/02 +-- Questo script gestisce le richieste di verifica travi che arrivano da LUX + +require( 'EgtBase') +_ENV = EgtProtectGlobal() +EgtEnableDebug( false) + +-- modalita' di chiamata +QUESTION_MODES = { + NULL = 0, + PREVIEW = 1, + BOM = 2, + HARDWARE = 3, + CONFIG = 4, + ORDER = 5, + VERIFY = 6, +} + +QUESTION_ORDER_SUBMODES = { + NULL = 0, + CREATE = 1, + ESTIMATE = 2, + CONFIRM = 3, +} + +_G.package.loaded.JSON = nil +local JSON = require( 'JSON') + +local sIniFilePath = EgtGetIniFile() +local sScriptDir = EgtGetStringFromIni( 'Lux', 'ScriptDir', '', sIniFilePath) +EgtAddToPackagePath( sScriptDir .. '\\?.lua') +local sBaseDir = EgtGetStringFromIni( 'Window', 'BaseDir', '', sIniFilePath) +EgtAddToPackagePath( sBaseDir .. '\\?.lua') +EgtAddToPackagePath( sBaseDir .. '\\Designing\\?.lua') + +_G.package.loaded.WindowPipeLib = nil +local WindowPipeLib = require( 'WindowPipeLib') + +-- Connessiona a Redis +local bRedisConnect, nRedisConnectionId = EgtRedisAsyncConnect( ENG.Param2) +if bRedisConnect then + EgtOutLog('Connessione con il server Redis dal Lua effettuata con successo con Id=' .. nRedisConnectionId .. '!') +else + nRedisConnectionId = 0 + EgtOutLog('Errore! Impossibile connettersi con il server Redis dal Lua!') +end + +-- funzione che verifica un bwe +local function Calc_Verify( QuestionArgs) + local nResult, AnswerArgs = WindowPipeLib.VerifyWindow( QuestionArgs) + return nResult, AnswerArgs +end + +-- funzione che esegue il nesting +local function Calc_Nesting( QuestionArgs) + local nResult, AnswerArgs = BeamWallPipeLib.NestBeam( QuestionArgs) + return nResult, AnswerArgs +end + +local nErr = 999 +local nNilCount = 0 +local bRun = true + +-- ciclo principale che analizza le richieste +while bRun do + local sReadLine = io.stdin:read( 'l') + EgtOutLog( 'Letta riga da stdin') + if sReadLine then + if sReadLine == 'quit' then + EgtOutLog( 'Chiusura processo') + break + end + if sReadLine ~= '' and sReadLine:find( '^#8477271#') and sReadLine:find( '#8477271#$') then + EgtOutLog( 'Lettura istruzione valida') + sReadLine = string.sub( sReadLine, 10, #sReadLine - 9) + local Question = JSON:decode( sReadLine) + local nThreadIndex = -1 + local nId = -1 + local ExecEnvironment = -1 + local nResult = 0 + local AnswerArgs = {} + if Question and Question.nThreadIndex and Question.nId and Question.Args then + nThreadIndex = tonumber( Question["nThreadIndex"]) + nId = tonumber( Question["nId"]) + ExecEnvironment = tonumber( Question["ExecEnvironment"]) + local QuestionArgs = Question["Args"] + if QuestionArgs and QuestionArgs.Mode and QuestionArgs.UID then + local sUid = QuestionArgs["UID"] + local sRUID = QuestionArgs["RUID"] + local nMode = tonumber( QuestionArgs["Mode"]) + -- esecuzione della corretta modilita' + EgtOutLog('Ricevuta richiesta calcolo: nThreadIndex=' .. nThreadIndex .. ', nId=' .. nId .. ', UID=' .. sUid .. ', nMode=' .. nMode) + local Result = {} + if nMode == QUESTION_MODES.VERIFY then + _G.LUX = {} + LUX.REDISID = nRedisConnectionId + nResult, AnswerArgs = Calc_Verify( QuestionArgs) + if AnswerArgs then + AnswerArgs.OutputChannel = QuestionArgs["OutputChannel"] + EgtOutLog('OutputChannel=' .. AnswerArgs.OutputChannel) + end + end + AnswerArgs.UID = sUid + AnswerArgs.RUID = sRUID or 0 + else + nResult = 0 + local sErrorMsg = 'Errore! Domanda senza argomenti o senza UID o Mode che sono obbligatori!' + AnswerArgs = { Error = sErrorMsg} + EgtOutLog( sErrorMsg) + end + else + nResult = 0 + local sErrorMsg = 'Errore! Formato domanda non riconosciuto o senza nThreadIndex, nId o Args!' + AnswerArgs = { Error = sErrorMsg} + EgtOutLog( sErrorMsg) + end + -- do risultato + local Result = { nThreadIndex = nThreadIndex, + nId = nId, + ExecEnvironment = ExecEnvironment, + nResult = nResult, + Args = AnswerArgs} + -- invio risposta + EgtOutLog( 'Invio risposta') + local JsonResult = JSON:encode( Result) + io.stdout:write( "#8376261#" .. JsonResult .. "#8376261#" .. '\n') + io.stdout:flush() + EgtOutLog( 'Risposta inviata') + EgtNewFile() + end + else + if nNilCount >= 20 then + bRun = false + EgtOutLog( 'Errore! Lettura da stdin fallita!') + end + nNilCount = nNilCount + 1 + EgtPause( 100, true) + end +end + +EgtRedisAsyncDisconnect( nRedisConnectionId) \ No newline at end of file