COMPILE FUNCTION Comm_Vision_Comm( Instruction, Parm1 )

/*
	Commuter Module for Vision Communications Window
	
	J.C. Henry, Inc. - John C. Henry
	
 */

DECLARE SUBROUTINE Center_Window,StatusLine,Set_Property,End_Dialog, Send_Event, Set_Status, Adios, obj_Vision, obj_Notes, obj_Post_Log
DECLARE SUBROUTINE ErrMsg, Send_Message, obj_Tables, Post_Event,  Send_INfo, ShowWindow, obj_Appwindow, Send_Dyn,ErrLog, obj_WO_Mat_Log
DECLARE SUBROUTINE obj_Calib_List
DECLARE FUNCTION Get_Property, Get_Status,Dialog_Box, FindWindow, Utility

$INSERT QUOTE_SIGS_EQU
$INSERT NOTIFICATION_EQU

EQU CRLF$ TO \0D0A\

EQU TRUE$ TO 1
EQU FALSE$ TO 2

$INSERT ENVIRON_CONSTANTS

ErrTitle = 'Error in Comm_Vision_Comm routine'
ErrorMsg = ''


Instructions  = 'Create'
Instructions := @FM:'Timer'
Instructions := @FM:'StartStop'
Instructions := @FM:'CommStartStop'
Instructions := @FM:'Close'

RetVal = ''


BEGIN CASE
	CASE Instruction = 'Create'				; GOSUB Create
	CASE Instruction = 'Timer'				; GOSUB Timer
	CASE Instruction = 'StartStop'			; GOSUB StartStop
	CASE Instruction = 'VisionStartStop'	; GOSUB VisionStartStop
	CASE Instruction = 'Close'				; GOSUB Close
	CASE 1
		ErrorMsg = 'Unknown Instruction ':QUOTE(Instruction):' passed to routine'
		
END CASE


IF ErrorMsg NE '' THEN
	ErrMsg(ErrTitle:@SVM:ErrorMsg)
END


RETURN RetVal


* * * * * * *
Create:
* * * * * * *

ErrMsg('This is deprecated... Bye')

GOTO Close


obj_Appwindow('Create',@WINDOW)

otParms = 'SYSOBJ':@RM:'VISION_COMM'
obj_Tables('LockRec',otParms)	;* Places lock on fictional record

IF Get_Status(errCode) THEN
	ErrorMsg = ErrTitle:@SVM:'Another Vision Communications Server is already running.'
	ErrMsg(ErrorMsg)
	Post_Event(@WINDOW,'CLOSE')
	RETURN
END

Set_Property(@WINDOW,'@LOCKED',1)				;* Set ad-hoc property '@LOCKED' to true
Set_Property(@WINDOW,'TIMER','1000':@FM:'0')			;* Set timer event to 5000 msecs (5 seconds) to check Inbound data from Vision


* Show engine window when running on a runtime JCH

*OpenEngineHandle = FindWindow("AREV":\00\,0)
*ShowWindow(OpenEngineHandle,1)


* No return on create -> automatically starts communications server


RETURN

* * * * * * *
StartStop:
* * * * * * *

StartStop = Get_Property(@WINDOW:'.START_STOP_BUTTON','TEXT')

IF StartStop[1,4] = 'Stop' THEN

	* Server is running
	
	Set_Property(@WINDOW:'.START_STOP_BUTTON','TEXT', 'Start Monitor')
	Set_Property(@WINDOW:'.CLOSE_BUTTON','ENABLED',1)
	
	Set_Property(@WINDOW:'.COMM_START_STOP_BUTTON', 'TEXT', 'Start Vision Communications')
	Set_Property(@WINDOW:'.COMM_START_STOP_BUTTON','ENABLED',0)
	
END ELSE

	Set_Property(@WINDOW:'.START_STOP_BUTTON','TEXT', 'Stop Monitor')
	Set_Property(@WINDOW:'.CLOSE_BUTTON','ENABLED',0)
	
	Set_Property(@WINDOW:'.COMM_START_STOP_BUTTON','TEXT', 'Stop Vision Communications')
	Set_Property(@WINDOW:'.COMM_START_STOP_BUTTON','ENABLED',1)
	
END

RETURN

* * * * * * *
VisionStartStop:
* * * * * * *

CommStartStop = Get_Property(@WINDOW:'.COMM_START_STOP_BUTTON','TEXT')

IF CommStartStop[1,4] = 'Stop' THEN
	Set_Property(@WINDOW:'.COMM_START_STOP_BUTTON', 'TEXT', 'Start Vision Communications')
END ELSE
	Set_Property(@WINDOW:'.COMM_START_STOP_BUTTON', 'TEXT', 'Stop Vision Communications')
END

RETURN

* * * * * * *
Close:
* * * * * * *

UNLOCK ALL

Set_Property('SYSTEM','IDLEPROC','ADIOS')


RETURN


* * * * * * *
Timer:
* * * * * * *

Set_Status(0)

Set_Property(@WINDOW:'.CURR_TIME','TEXT',OCONV( Time(), 'MTHS' ))		;* Clock on window

LastTime = Get_Property(@WINDOW,'@LAST_TIME')
LastLoginTime = Get_Property(@WINDOW,'@LAST_LOGIN_TIME')

IF Get_Property(@WINDOW:'.COMM_START_STOP_BUTTON','TEXT')[1,' '] = 'Stop' THEN
	VisionCommFlag = 1
END ELSE
	VisionCommFlag = 0
END

IF Get_Property(@WINDOW:'.START_STOP_BUTTON','TEXT')[1,' '] = 'Stop' THEN

	* We're in run mode
	
	IF LastTime > Time() THEN
		Set_Property(@WINDOW,'@LAST_TIME', Time())				;* Midnight -> clock resets to 0
		Set_Property(@WINDOW,'@LAST_LOGIN_TIME',Time())
		LastTime = Time()
		LastLoginTime = Time()
		*obj_Calib_List('SendReminders')							;* Sends calibration notifications to recepients

	END

	IF Get_Property(@WINDOW:'.COMM_START_STOP_BUTTON','TEXT')[1,' '] = 'Stop' THEN
		VisionCommFlag = 1
	END ELSE
		VisionCommFlag = 0
	END
	
	IF Time() >= (LastLoginTime + 3600) AND VisionCommFlag THEN
		
		* Login to the AS400 so we can access the 'Z:' drive which is a folder used for moving stuff
		
		AS400Login = 'C:\PROGRA~1\IBM\CLIENT~1\CWBLOGON.EXE 10.64.110.100 /U EPIDWNLD /P EPIDWNLD'
		
		Set_Property(@WINDOW,'@LAST_LOGIN_TIME',Time())
		
		Result = Utility('RUNWIN',AS400Login,1)							

		IF Result<2> = '' THEN
			ErrLog('No Logon to the AS400!')		;* This doesn't seem to ever happen - leave it for now
		END

	END


	IF Time() >= (LastTime + 5) THEN
			
		IF VisionCommFlag THEN
			
			Send_Info('Checking Inbound...')
		
			obj_Vision('GetInbound')

			IF Get_Status(errCode) THEN
			
				ErrLog(errCode)			;* ErrLog clears status
			
				Recipients		= XLATE('NOTIFICATION','ORDER_ENTRY',NOTIFICATION_USER_ID$,'X')
				SentFrom		= 'Vision Comm Server'
				Subject			= 'Vision Communications Server Errors'
				Message			= "Communications Errors logged during 'Get_Inbound' on the Vision Communications Server"
				AttachWindow	= ''
				AttachKey		= ''
				SendToGroup		= ''

				Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
				obj_Notes('Create',Parms)
				
				IF Get_Status(errCode) THEN ErrLog(errCode)
				
			END
			
			*Send_Event(@WINDOW:'.START_STOP_BUTTON','CLICK')				;* One shot - Comment out to run continuous *************************************
			
			* Check 'VISION_COMM' for pending outbound transaction
			
			Send_Info('Checking Outbound...')
			
			obj_Vision('SendOutbound')
			
			IF Get_Status(errCode) THEN
			
				ErrLog(errCode)			;* ErrLog clears status
				 
				Recipients		= XLATE('NOTIFICATION','ORDER_ENTRY',NOTIFICATION_USER_ID$,'X')
				SentFrom		= 'Vision Comm Server'
				Subject			= 'Vision Communications Server Errors'
				Message			= "Communications Errors logged during 'SendOutbound' on the Vision Communications Server"
				AttachWindow	= ''
				AttachKey		= ''
				SendToGroup		= ''

				Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
				obj_Notes('Create',Parms)
				
				IF Get_Status(errCode) THEN ErrLog(errCode)

			END
		END	;* End of check for VisionCommFlag = 1
		
		Send_Info('Posting Barcode Scans...')
		obj_WO_Mat_Log('Post')
		
		Send_Info('Posting Post Log Transactions...')
		obj_Post_Log('Post')
		
		Send_Info('Forwarding eMail...')
		obj_Notes('ForwardEMail')
		
		Set_Property(@WINDOW,'@LAST_TIME', Time())

	END
END

Send_Info(' ')

RETURN
