Trying to do something very similar, but in my own twisted way.
Here's the basic steps.
- Client requests the available sounds from the gateway (as a list)
- Client uses this list, and for each sound that is available:
a. Get the sound file in bytes from the gateway
b. Write the sound file to the client
I've got all scripts in my project library Sound:
import os
import errno
alarmSoundPath = 'C:\SCADA\Alarm Sounds'
alarmSoundOptionsTagPath = '[client]Alarms/AlarmSoundOptions'
def alarmSoundPathExists():
'''
Checks to see if the alarmSoundPath folder exists. Creates it if it does not.
Args:
n/a
Returns
folder (bool) : true if file path exists, or was successfully created
'''
path = alarmSoundPath
#check to see that file path exists
folder = False
if not os.path.exists(path):
#create the folder if it doesn't exist
try:
os.makedirs(path)
folder = True
except OSError as e:
if errno.EEXIST != e.errno:
raise
#folder exists, continue
else:
folder = True
return folder
def getAlarmSoundList():
'''
Get a list of alarm sounds that are available at the alarmSoundPath
Args:
n/a
Returns:
fileList (string list) : list of WAV file names in the directory
'''
path = alarmSoundPath
#check to see that file path exists
folder = alarmSoundPathExists()
#create the file list for return
if folder:
fileList = [ name.split('.')[0] for name in os.listdir(path) if name.upper().endswith('.WAV') ]
else:
fileList = []
return fileList
def getAlarmSoundDataset():
'''
Get a dataset of alarm sounds that are available at the alarmSoundPath
Args:
n/a
Returns:
dataset (dataset) : file names as dataset for use with a dropdown
'''
path = alarmSoundPath
headers = ['Value','Label']
#get file list
fileList = getAlarmSoundList()
#need a list of lists to match the headers
data = [[i, name] for i, name in enumerate(fileList)]
#return dataset
return system.dataset.toDataSet(headers, data)
##################################################################################
############ functions for clients requesting sounds from the gateway ############
##################################################################################
def requestAvailableAlarmSounds():
'''
Request the list of available sounds from the gateway via message handler
Args:
n/a
Returns:
fileList (string list) : list of WAV file names in the directory
'''
return system.util.sendRequest('Project', 'SendAlarmSoundList')
def retrieveSoundBytes(fileName):
'''
Get a sound file from the gateway via message handler
Args:
fileName (string) : alarm sound name to retrieve
Returns:
soundBytes (byte array) : sound file in byte format
'''
return system.util.sendRequest('Project', 'SendAlarmSound', {'fileName':fileName})
def writeSoundBytes(filePath, soundBytes):
'''
Write sound file to the local client at the alarmSoundPath
Args:
filePath (string) : file to create
soundBytes (byte array) : sound file in byte format
Returns:
n/a
'''
#check to see that file path exists
folder = alarmSoundPathExists()
if folder:
system.file.writeFile(filePath, soundBytes)
def updateClientAlarmSounds():
'''
Update alarm sound files on the local client
'''
path = alarmSoundPath
tagPath = alarmSoundOptionsTagPath
fileList = requestAvailableAlarmSounds()
for fileName in fileList:
filePath = path + '\\' + fileName + '.wav'
soundBytes = retrieveSoundBytes(fileName)
writeSoundBytes(filePath, soundBytes)
#update dataset for options dropdown
dataset = getAlarmSoundDataset()
system.tag.writeBlocking([tagPath], [dataset])
##################################################################################
####### functions for the gateway responding to client requests for sounds #######
##################################################################################
def retrieveAlarmSoundList_MH():
'''
Message handler to retrieve the list of available sound files on the gateway
Args:
n/a
Returns:
fileList (string list) : list of WAV file names in the directory
'''
return getAlarmSoundList()
def retrieveAlarmSound_MH(payload):
'''
Message handler to retrieve sound file from the gateway
Args:
payload (dict) : payload 'fileName' for alarm sound file
Returns:
soundBytes (byte array) : sound file in byte format
'''
path = alarmSoundPath
if payload['fileName']:
filePath = path + '\\' + payload['fileName'] + '.wav'
if system.file.fileExists(filePath):
return system.file.readFileAsBytes(filePath)
Gateway Message Handlers:
SendAlarmSound
return Sound.retrieveAlarmSound_MH(payload)
SendAlarmSoundList
return Sound.retrieveAlarmSoundList_MH()
Client Tag Change Script (so I can trigger it at-will):
Sound.updateClientAlarmSounds()
It's not populating. Where might I see some error messages for these?