Ignition v8.1.35
I have a project for Zebra mobile scanners. Each scanner has it's own respective UDT that it reads/writes to using the scannerNumber
. The scannerNumber
is set by an onActionPerformed
event on a Begin
button that is on the starting page of the project. This starting page is used to either get a :scannerNumber
URL parameter if it were present, if not an entry field appears allowing the user to manually enter a number.
I was asked if I could get/display the scanners gps coords on a map, and I figured out how to do that.
I have a timer script that sends a message to all sessions, causing them to grab their current gps coords, and write them to the respective tag.
I would like to instead save the gps coords in a class instance and persist the scanners
dictionary across script restarts.
Reasons being:
-
Having multiple sessions call
system.tag.writeBlocking()
once they receive the message from the gateway, does not sound efficient. There could be at least 20 scanners online, and possibly more in the future. -
Allowing a new session to then create it's own
Scanner
instance automatically, essentially removes the need to have a UDT for each scanner and not have to create/delete tags manually or via scripting. Although it can be done.
Now, I made two classes that I create an instance of in Perspective Project Scripts.
The newScanner()
function is called from the same onActionPerformed
event on the Begin
button.
I also have a removeScanner(scannerNumber)
function that will delete the scanner from the scanners
dictionary when a session is closed.
How could I (or should I) persist the scanners
dictionary across script restarts?
class Gps:
def __init__(self, lat, lng, tstamp):
self.lat = lat
self.lng = lng
self.tstamp = tstamp
class Scanner:
def __init__(self, session):
self.session = session
self.name = 'Scanner #{0}'.format(str(session.custom.scannerNumber))
self.gps = Gps(session.props.geolocation.data.latitude, session.props.geolocation.data.longitude, session.props.geolocation.data.timestamp)
def __str__(self):
return 'Scanner# {0}, Session ID:{1}, IP: {2}'.format(self.session.custom.scannerNumber, self.session.props.id, self.session.props.address)
def __eq__(self, scannerToCompare):
if isinstance(scannerToCompare, Scanner):
return self.session.custom.scannerNumber == scannerToCompare.session.custom.scannerNumber
return False
def getName(self):
return self.name
def getSession(self):
return self.session
def getScannerNumber(self):
return self.session.custom.scannerNumber
scanners = {}
def newScanner(session):
if session:
new_scanner = Scanner(session)
scanners[new_scanner.getScannerNumber()] = new_scanner
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()
def getScannerGps():
scannersData = []
for scanner in scanners.values():
scannerData = {
"name": scanner.getName(),
"properties": {},
"enabled": True,
"lat": scanner.gps.lat,
"lng": scanner.gps.lng,
"opacity": 1,
"icon": {
"path": "material/location_on",
"color": "#4190F7",
"rotate": 0,
"size": {
"width": 30,
"height": 30
},
"style": {
"classes": ""
}
},
"event": {
"stopPropagation": False
},
"tooltip": {
"content": {
"text": scanner.getScannerNumber(),
"view": {
"path": "",
"params": {}
}
},
"direction": "top",
"permanent": False,
"sticky": False,
"opacity": 0.7
},
"popup": {
"enabled": False,
"content": {
"text": scanner.getName(),
"view": {
"path": "",
"params": {}
}
},
"width": {
"max": 300,
"min": 50
},
"height": {
"max": None
},
"pan": {
"auto": True
},
"closeButton": True,
"autoClose": True,
"closeOnEscapeKey": True,
"closeOnClick": None
}
}
scannersData.append(scannerData)
return scannersData
def refreshScannerGps():
local.util.sendMessageToSessions('refreshScannerGps', payload = None)
def getScannersTable():
scannersData = []
for scanner in scanners.values():
data = {
"scannerNumber": scanner.getScannerNumber(),
"sessionId": scanner.getSession().props.id,
}
scannersData.append(data)
return scannersData
def refreshScannerTable():
local.util.sendMessageToSessions('refreshScannerTable', payload = None)