Perspective Project Script Persistance

Thanks again, Phil.
Updated code:

scanners = {}

def newScanner(session):
    if session:
    	newScanner = {
    		'session': session,
    		'scannerNumber': session.custom.scannerNumber,
    		'gps': {
    			'lat': session.props.geolocation.data.latitude,
    			'lng': session.props.geolocation.data.longitude,
    			'tstamp': session.props.geolocation.data.timestamp
    		}
    		
    	}
        
        scanners.setDefault(session.custom.scannerNumber, newScanner)
        refreshScannerTable()
        refreshScannerGps()

def removeScanner(scannerNumber):
    if scannerNumber in scanners:
        del scanners[scannerNumber]
        refreshScannerTable()
        refreshScannerGps()

def getScanner(scannerNumber = None):
    if scannerNumber is None:
        return list(scanners.values())
    else:
        return scanners.get(scannerNumber, None)

def updateScannerGps(scannerNumber, lat, lng, tstamp):
    if scannerNumber in scanners:
        scanner = scanners[scannerNumber]
        scanner['gps']['lat'] = lat
        scanner['gps']['lng'] = lng
        scanner['gps']['tstamp'] = tstamp
        
        refreshScannerGps()

The .setDefault() function is "thread safe" for adding a new scanner to the dict. What would be a "thread safe" method for removing scanners as their sessions shutdown/expire? As in my function removeScanner().

What about a session needing to update it's respective scanner's data?
Does the below hold true?

From: Python objects and thread safe - Ignition - Inductive Automation Forum

1 Like