a1700f635c
- aggiunte linee di debug per processi - aggiunta modalita' hardware, config, order, verify - migliorato png delle travi - aggiunto volume in BOM - aggiunto sanitizeUtf8 - spostata connessione Redis a inizio file - gestite chiamate REST per beam - gestita verifica non parallela per travi - aggiunte opzioni hardware finestre - aggiunta calcolo forma anta finestra
355 lines
15 KiB
Lua
355 lines
15 KiB
Lua
--
|
|
-- 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
|
|
--
|
|
-- BeamRESTPipe by Egalware s.r.l. 2025/12/02
|
|
-- Questo script gestisce le richieste REST che arrivano da LUX per le travi
|
|
|
|
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,
|
|
}
|
|
|
|
PART_VERIFICATION_RESULTS = {
|
|
CALCULATIONFAILED = -1,
|
|
NOTMACHINABLE = 0,
|
|
MACHINABLE = 1,
|
|
}
|
|
|
|
_G.package.loaded.JSON = nil
|
|
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')
|
|
|
|
_G.package.loaded.BeamWallPipeLib = nil
|
|
local BeamWallPipeLib = require( 'BeamWallPipeLib')
|
|
|
|
-- 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
|
|
|
|
local function Create_Order( QuestionArgs)
|
|
-- leggo argomenti passati
|
|
local sFile = QuestionArgs["FileName"]
|
|
local sBtl = QuestionArgs["SerializedData"]
|
|
local sOrderUID = QuestionArgs["OrderUID"]
|
|
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!'
|
|
EgtOutLog(sErrorMsg)
|
|
return 0, { Error = sErrorMsg}
|
|
end
|
|
else
|
|
local sErrorMsg = 'Errore! Nome file vuoto!'
|
|
EgtOutLog(sErrorMsg)
|
|
return 0, { Error = sErrorMsg}
|
|
end
|
|
end
|
|
|
|
local function Calc_Estimate( QuestionArgs)
|
|
-- leggo argomenti passati
|
|
local sOrderUID = QuestionArgs["OrderUID"]
|
|
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!'
|
|
EgtOutLog(sErrorMsg)
|
|
return 0, { Error = sErrorMsg}
|
|
end
|
|
local sNgeFilePath = sOrderDirPath .. '\\' .. sUID .. '.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-PF1250' }
|
|
local BeamMachineList = {}
|
|
for nMachineIndex = 1, #MachinesDir do
|
|
local _, sMachineName, _ = EgtSplitPath( MachinesDir[nMachineIndex])
|
|
local sMaterial = EgtGetStringFromIni( 'General', 'Material', '', MachineDirPath .. '\\' .. sMachineName .. '\\' .. sMachineName .. '.ini')
|
|
if sMaterial == 'Beam' then
|
|
local CurrMachine = { Name = sMachineName, PartList = { }}
|
|
-- creo i singoli bwe e li mando da eseguire via redis
|
|
--BeamWallPipeLib.VerifyBtlPart( nPartId, BarPath, ProjType, sMachineName, GlobState)
|
|
local nPartId = EgtGetFirstPart()
|
|
local AnswerArgs = {}
|
|
local TagTimeList = {}
|
|
local dTotTime = 0
|
|
local nNotMachinedPart = 0
|
|
while nPartId do
|
|
local BarPath = sOrderDirPath
|
|
local ProjType = BWTYPES.BEAM
|
|
local GlobState = CALCSTATES.NOTCALCULATED
|
|
local nResult, PartAnswerArgs = BeamWallPipeLib.VerifyBtlPart( nPartId, BarPath, ProjType, sMachineName, GlobState)
|
|
local sTagList = EgtGetInfo( nPartId, 'LuxTagList')
|
|
local TagList = EgtSplitString( sTagList, ',')
|
|
local CalcResult
|
|
if nResult == 1 then
|
|
CalcResult = EgtIf( tonumber( PartAnswerArgs.MachiningOk) == 1, PART_VERIFICATION_RESULTS.MACHINABLE, PART_VERIFICATION_RESULTS.NOTMACHINABLE)
|
|
else
|
|
CalcResult = PART_VERIFICATION_RESULTS.CALCULATIONFAILED
|
|
local sErrorMsg = 'Errore! Verifica sul pezzo ' .. nPartId .. ' fallita!'
|
|
EgtOutLog(sErrorMsg)
|
|
end
|
|
for nTagIndex = 1, #TagList do
|
|
table.insert( CurrMachine.PartList, { Tag = TagList[nTagIndex], CalcResult = CalcResult, Time = ( PartAnswerArgs.Time or 0)})
|
|
end
|
|
nPartId = EgtGetNextPart( nPartId)
|
|
end
|
|
table.insert( BeamMachineList, CurrMachine)
|
|
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!'
|
|
EgtOutLog(sErrorMsg)
|
|
return 0, { Error = sErrorMsg}
|
|
end
|
|
end
|
|
|
|
local nErr = 999
|
|
local nNilCount = 0
|
|
local bRun = true
|
|
|
|
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 sSanitizedReadLine = EgtSanitizeUtf8(sReadLine)
|
|
local Question = JSON:decode( sSanitizedReadLine)
|
|
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 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.ORDER then
|
|
if QuestionArgs.SubMode then
|
|
local nSubMode = tonumber( QuestionArgs["SubMode"])
|
|
EgtOutLog('SubMode=' .. nSubMode)
|
|
if nSubMode == QUESTION_ORDER_SUBMODES.CREATE then
|
|
nResult, AnswerArgs = Create_Order( QuestionArgs)
|
|
elseif nSubMode == QUESTION_ORDER_SUBMODES.ESTIMATE then
|
|
_G.LUX = {}
|
|
LUX.REDISID = nRedisConnectionId
|
|
nResult, AnswerArgs = Calc_Estimate( QuestionArgs)
|
|
else
|
|
nResult = 0
|
|
local sErrorMsg = 'Errore! Valore di SubMode errato!'
|
|
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!'
|
|
AnswerArgs = { Error = sErrorMsg}
|
|
EgtOutLog( sErrorMsg)
|
|
end
|
|
end
|
|
AnswerArgs.UID = sUid
|
|
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) |