/* This is a script for downloading Spray zooGlider files via YMODEM
 * you need to exit to picoDOS before starting the script*/


dirnamebox 'Select Download Directory'	;Prompt user for the directory to download files to
dataPath = inputstr						;Save the response in a variable

/*****************BUILD LOGFILE NAMES******************/
gettime rptName "%Y-%m-%d-%H%M" 		;Gets the system time and saves it to rptName variable
logName = rptName						;Save the time to logName variable	
timeStamp = rptName						;Save the time to the timeStamp variable

strconcat rptName 'REPORT.txt'			;Add text to rptName

strconcat logName 'LOG.log'				;Add text to logName


/*************PROMPT FOR START AND STOP*************/
inputbox 'Input first file number to download' 'File Download Range'	;Prompt user for first file (with a popup box) user respons saved in "inputstr" vairalbe
str2int startFile inputstr										;Get the first file number as an int

inputbox 'Input last file number to download' 'File Download Range' 	;Prompt the user for last file to download (saved in "inputstr")
str2int endFile inputstr										;Get the last file number as an int

if startFile > endFile then										;Check that input is valid
	messagebox 'Invalid file range.' 'Error'					;If invalid display error message box
	exit														;exit macro
endif

sprintf 'Download files %d-%d?' startFile endFile				;Build a string to prompt user for verification (saved in "inputstr" variable)
yesnobox  inputstr 'DownloadFiles?'								;Prompt user for verification before download

if !result then													;If the user says No 
	messagebox 'Exiting Macro' 'Abort'							;Display abort message box
	exit														;Exit the macro
endif
	
/***********Download Files**************/

;Open a report and a log file of the transfer
setdir dataPath													;Set the working difrectory	for the Macro
changedir dataPath												;Set the working directory for TeraTerm												

fileopen RH rptName 1											;Open a file to save the report (any error messages will be saved here)
logopen logName 0 1 1 1											;Open a logfile

;Print Header to report
filewriteln RH 'Spray DATA Download'							;Write a header line to the file
filewriteln RH timeStamp										;Write the timestamp to the file

;Initialize a few counters to keep track of status
missingFile=0
dlError =0
dlGood =0

;first switch to 57600 baud
	send 'baud 57600'											;Select the fastest baud
	pause 1															
	send #13													;Send a carriage return
	timeout = 10												;Set the timeout for the "wait" function to 60 seconds
	wait 'hit <Enter> when ready ...'							;Wait for the spray to send prompt
	
	if result = 0 then											;If we hit a timeout display a message box
		messagebox 'Reached a Timeout baud switch to 57600' 'TIMEOUT'
	endif
	
	setbaud 57600												;Set the TeraTerm baud rate to 57600
	send #13													;Send a carriage return
;next get to the data directory
	send 'cd c:\data'											;goto data directory
	pause 1															
	send #13													;Send a carriage return

for i startFile endFile											;Iterate through the files given by the user

	;Build the file name
	sprintf2 fileName 'D%03d' i									;Build the file name (result will be saved to "fileName")
	;messagebox fileName 'File Name'
	
	;Start the download
	send 'ys '													;Send the picodos ymodem command
	pause 1
	send fileName
	pause 1
	send #13													;Send a carriage return 
	timeout = 10												;Set the timeout for the "wait" function to 60 seconds
	wait 'Sending:'												;Wait for Spray to initiate the transfer

	if result = 0 then											;If we hit a timeout display a message box
		messagebox 'Reached a Timeout waiting for YMODEM transfer' 'TIMEOUT'
	endif
	
	;At this point Spray is ready to transfer
	pause 1														;Pause for 1 second
	ymodemrecv													;Start the TeraTerm YMODEM recieve
	
	if result = 1 then											;If we had a good transfer
		dlGood=dlGood+1											   ;Increment the good transfer counter
	elseif result = 0 then										;If we did not get a good transfer
		dlError=dlError+1 										   ;Increment the error counter
		sprintf2 tempMsg 'Transfer Error: %s' fileName			   ;Build the error message
		filewriteln RH tempMsg									   ;Write the error message to the report file
	endif
		
	timeout = 60												;Set the timeout for the "wait" function to 60 seconds
	wait 'Complete'												;Wait for Spray to indicate the download is complete
	
	if result = 0 then											;If we hit a timeout display a message box
		messagebox 'Reached a Timeout waiting for Spray end of download confirmation' 'TIMEOUT'
	endif

	:endOfFor
	;wait for next prompt and move on to the next file
	timeout = 2													;Reset the wait timeout to the default value
	wait 'C:\DATA\>'											;Wait for the Spray command prompt

next

;Write a summary of the download to the report file
filewriteln RH 'SUMMARY:'											
sprintf2 tempMsg 'Good Transfers: %d' dlGood
filewriteln RH tempMsg
sprintf2 tempMsg 'Missing Files: %d' missingFile
filewriteln RH tempMsg
sprintf2 tempMsg 'Transfer Errors: %d' dlError
filewriteln RH tempMsg

fileclose RH													;Close the report file
logclose														;Close the log file


sprintf2 tempMsg 'Download Complete: %d Good, %d Missing, %d Error' dlGood missingFile dlError	;Build a message indicating we are done with a summary
messagebox tempMsg 'Finished Downloading'														;Display the summary message
exit																							;Exit the macro

