I have a script containing several functions that perform database updates and then call another function (custom method on root container) to set the State of all the components on the screen (mainly property bindings to update the user that the changes were committed and in fact updated on the backend). These updates need to be performed synchronously since there are intermediary calculations from the 1st database update that are used in the next database update and so on. So I have been using system.util.invokeLater with a 5 second delay to all enough time for the database to update before running the setState function and continuing with subsequent update queries.
So my question is the delay time in reference to the client session? On my PC, internet/vpn connection, the components update correctly. But for some other clients, they don’t see the updates. It’s like the 5 second delay isn’t long enough to update the the database and the components for that specific client. So is this a function of each client’s PC and connection? I don’t want to keep increasing the delay just to accommodate certain ‘slow’ clients.
Is their another way where I can ensure a promise (think Javascript) is resolved before setting the State of all the components to reflect the updates (i.e. eliminating the need for an arbitrary delay value)?
def setState():
#set state of all components
event.source.parent.parent.parent.parent.setState()
return
def updateRM():
#------UPDATE BSRawMaterial table----------------------------------
SolidMass=event.source.parent.getComponent('ContainerGeneral').getComponent('ContainerSolids').getComponent('SolidKg').floatValue
BatchSizeKg=event.source.parent.getComponent('ContainerGeneral').getComponent('ContainerSolids').getComponent('BatchSizeKg').floatValue
RawMaterialTable = event.source.parent.getComponent('ContainerRawMaterial').getComponent('PTRawMaterial')
rowcount = RawMaterialTable.data.rowCount
if BatchSizeKg <= 600:
precision = 4
else:
precision = 2
if rowcount > 0:
for row in range(rowcount):
BSRawMaterialID = RawMaterialTable.data.getValueAt(row,'BSRawMaterialID')
RawMaterialTypeID=RawMaterialTable.data.getValueAt(row,'RawMaterialTypeID')
BSRawMaterialPorcent=RawMaterialTable.data.getValueAt(row,'BSRawMaterialPorcent')
BSRawMaterialMoisture=RawMaterialTable.data.getValueAt(row,'BSRawMaterialMoisture')
BSRawMaterialSSD=RawMaterialTable.data.getValueAt(row,'BSRawMaterialSSD')
BSRawMaterialDensity=RawMaterialTable.data.getValueAt(row,'BSRawMaterialDensity')
BSRawMaterialKg=RawMaterialTable.data.getValueAt(row,'BSRawMaterialKg')
#Calculated Fields
BSRawMaterialKg = round(((BSRawMaterialPorcent/100)*SolidMass) / \
(1-(BSRawMaterialMoisture-BSRawMaterialSSD)/100),precision)
#if RawMaterialTypeID == 0:
#event.source.parent.CementKg = BSRawMaterialKg
BSRawMaterialLiquidKg = round(BSRawMaterialMoisture*BSRawMaterialKg/100,4)
if BSRawMaterialDensity > 0:
BSRawMaterialVolume = round(BSRawMaterialKg/BSRawMaterialDensity,4)
BSRawMaterialWaterContent = round(BSRawMaterialMoisture*BSRawMaterialVolume/100,4)
#Update Query
querylist=[BSRawMaterialKg,BSRawMaterialLiquidKg,BSRawMaterialVolume,BSRawMaterialID]
query='''UPDATE BSRawMaterial SET
BSRawMaterialKg=?,BSRawMaterialLiquidKg=?,BSRawMaterialVolume=?
WHERE BSRawMaterialID=?'''
print querylist
system.db.runPrepUpdate(query, querylist)
system.db.refresh(event.source.parent.getComponent('ContainerRawMaterial').getComponent('PTRawMaterial'), 'data')
print 'Raw Materials Updated'
system.util.invokeLater(setState, 5000) #set state of components in 5 seconds
system.util.invokeLater(updateBinder, 5000) #update binder table in 5 seconds
system.util.invokeLater(updateAdmix, 5000) #update admix table in 5 seconds
#system.util.invokeAsynchronous(updateBinder)
#system.util.invokeAsynchronous(updateAdmix)
return
def updateBinder():
CementKg = event.source.parent.CementKg
BatchSizeKg=event.source.parent.getComponent('ContainerGeneral').getComponent('ContainerSolids').getComponent('BatchSizeKg').floatValue
BinderTable = event.source.parent.getComponent('ContainerBinder').getComponent('PTBinder')
rowcount = BinderTable.data.rowCount
if BatchSizeKg <= 600:
precision = 4
else:
precision = 2
if rowcount > 0:
for row in range(rowcount):
BSBinderID=BinderTable.data.getValueAt(row,'BSBinderID')
BSBinderMassPorcent=BinderTable.data.getValueAt(row,'BSBinderMassPorcent')
#Calculation
BSBinderMassKg =round(CementKg * BSBinderMassPorcent/100,precision)
#Update query
querylist=[BSBinderMassKg,BSBinderID]
query='UPDATE BSBinder SET BSBinderMassKg=? WHERE BSBinderID=?'
print querylist
system.db.runPrepUpdate(query, querylist)
system.db.refresh(event.source.parent.getComponent('ContainerBinder').getComponent('PTBinder'), 'data')
print 'Binders Updated'
return
def updateAdmix():
CementKg = event.source.parent.CementKg
AdmixTable = event.source.parent.getComponent('ContainerAdmixture').getComponent('PTAdmixture')
rowcount = AdmixTable.data.rowCount
if rowcount > 0:
for row in range(rowcount):
BSAdmixtureID=AdmixTable.data.getValueAt(row,'BSAdmixtureID')
BSAdmixtureSG=AdmixTable.data.getValueAt(row,'BSAdmixtureSG')
BSAdmixtureDosage=AdmixTable.data.getValueAt(row,'BSAdmixtureDosage')
#Calculation
BSAdmixtureMassG=round(CementKg * BSAdmixtureSG * BSAdmixtureDosage,2)
#Update query
querylist=[BSAdmixtureMassG,BSAdmixtureID]
query='UPDATE BSAdmixture SET BSAdmixtureMassG=? WHERE BSAdmixtureID=?'
print querylist
system.db.runPrepUpdate(query, querylist)
system.db.refresh(event.source.parent.getComponent('ContainerAdmixture').getComponent('PTAdmixture'), 'data')
print 'Admixtures Updated'
#Enable Buttons
event.source.parent.cursorCode = 0 #reset cursor to default
event.source.parent.getComponent('Button 4').componentEnabled = 1
event.source.componentEnabled = 1
return
def updateWater():
BatchSheetID=event.source.parent.getComponent('ContainerGeneral').getComponent('DropdownBatchSheet').selectedValue
CementKg = event.source.parent.CementKg
LiquidKg = event.source.parent.LiquidKg
TotalWaterContent = event.source.parent.TotalWaterContent
print TotalWaterContent
#Calculation
BatchAdjustedWater = round(LiquidKg - TotalWaterContent, 4)
BatchWC = round(LiquidKg / CementKg, 4)
#Update query
querylist=[BatchAdjustedWater,BatchWC,BatchSheetID]
query='UPDATE BatchSheetGeneral SET BatchAdjustedWater=?,BatchWC=? WHERE BatchSheetID=?'
print querylist
system.db.runPrepUpdate(query, querylist)
#event.source.parent.getComponent('ContainerGeneral').getComponent('ContainerLiquid').getComponent('AjustWater').floatValue=BatchAdjustedWater
#event.source.parent.getComponent('ContainerGeneral').getComponent('wc').floatValue=BatchWC
system.util.invokeLater(setState, 10000) #set the state in 10 seconds
print 'Water Updated'
return
#Disable Buttons
event.source.parent.cursorCode = 3 #show updating cursor
event.source.parent.getComponent('Button 4').componentEnabled = 0
event.source.componentEnabled = 0
#------UPDATE BatchSheetGeneral table----------------------------------
BatchSheetDate=event.source.parent.getComponent('ContainerGeneral').getComponent('Popup Calendar').date
MoldID=event.source.parent.getComponent('ContainerGeneral').getComponent('MoldType').selectedValue
ChildFaceMixID=event.source.parent.getComponent('ContainerGeneral').NewFaceMixID
SampleBoard=event.source.parent.getComponent('ContainerGeneral').getComponent('SampleBoard').intValue
SampleVolume=round(event.source.parent.getComponent('ContainerGeneral').getComponent('SampleVolume').floatValue,8)
CreatedBy=event.source.parent.getComponent('ContainerGeneral').getComponent('CreatedBy').text
ExcecutedBy=event.source.parent.getComponent('ContainerGeneral').getComponent('ExcecutedBy').text
BatchObjective=event.source.parent.getComponent('ContainerGeneral').getComponent('Objective').text
BatchSheetSpecialNote=event.source.parent.getComponent('ContainerGeneral').getComponent('BSSpecialNotes').text
VBUFID=event.source.parent.getComponent('ContainerGeneral').getComponent('VBUF').selectedValue
ProductTypeID=event.source.parent.getComponent('ContainerGeneral').getComponent('ProductType').selectedValue
UseTypeID=event.source.parent.getComponent('ContainerGeneral').getComponent('UseType').selectedValue
ProductColorID=event.source.parent.getComponent('ContainerGeneral').getComponent('Color').selectedValue
BatchSizeKg=round(event.source.parent.getComponent('ContainerGeneral').getComponent('ContainerSolids').getComponent('BatchSizeKg').floatValue,2)
BatchSolidPorcent=round(event.source.parent.getComponent('ContainerGeneral').getComponent('ContainerSolids').getComponent('SolidPorcent').floatValue,2)
BatchSolidMassKg=round(BatchSizeKg * BatchSolidPorcent/100,2)
BatchLiquidKg=round(BatchSizeKg - BatchSolidMassKg,2)
BatchLiquidPorcent=round(100 - BatchSolidPorcent,2)
WaterDensity=round(event.source.parent.getComponent('ContainerGeneral').getComponent('ContainerLiquid').getComponent('WaterDensity').floatValue,2)
WaterVolume=round(BatchLiquidKg / WaterDensity,4)
BatchWC=event.source.parent.getComponent('ContainerGeneral').getComponent('wc').floatValue
BatchSheetID=event.source.parent.getComponent('ContainerGeneral').getComponent('DropdownBatchSheet').selectedValue
if UseTypeID == 2 or UseTypeID != 2 and MoldID > 0: #ensure mold type is selected for throughbody and base mixes
querylist = [BatchSheetDate,MoldID,ChildFaceMixID,
SampleBoard,SampleVolume,CreatedBy,
ExcecutedBy,BatchObjective,BatchSheetSpecialNote,
VBUFID,ProductTypeID,UseTypeID,ProductColorID,
BatchSizeKg,BatchSolidPorcent,BatchSolidMassKg,
BatchLiquidKg,BatchLiquidPorcent,WaterDensity,
WaterVolume,BatchWC,
BatchSheetID]
query = """UPDATE BatchSheetGeneral SET BatchSheetDate = ?,MoldID=?,ChildFaceMixID=?,
SampleBoard=?,SampleVolume=?,CreatedBy=?,
ExcecutedBy=?,BatchObjective=?,BatchSheetSpecialNote=?,
VBUFID=?,ProductTypeID=?,UseTypeID=?,ProductColorID=?,
BatchSizeKg=?,BatchSolidPorcent=?,BatchSolidMassKg=?,
BatchLiquidKg=?,BatchLiquidPorcent=?,WaterDensity=?,
WaterVolume=?,BatchWC=?
WHERE BatchSheetID=?"""
system.db.runPrepUpdate(query, querylist)
#Refresh Components
comp_list = ['BSGeneralState','CementKg','LiquidKg',
'TheorDensity','TotalMass','TotalMassPercent',
'TotalMoisture','TotalRawMaterialVolume',
'TotalWaterContent']
[system.db.refresh(event.source.parent, c) for c in comp_list]
system.util.invokeLater(setState, 5000) #set state of all components on screen in 5 seconds
system.util.invokeLater(updateWater, 5000) #update water fields in batchsheetgeneral table in 5 seconds
system.util.invokeLater(updateRM, 5000) #update raw material table in 5 seconds
else:
system.gui.messageBox('Mold not selected. Cannot save changes')