﻿AppType=JavaFX
Build1=Default,b4j.example
File1=Denon_IR.bjl
FileGroup1=Default Group
Group=Default Group
Library1=jcore
Library2=jfx
Library3=jrandomaccessfile
Library4=jnetwork
Module1=B4RSerializator
NumberOfFiles=1
NumberOfLibraries=4
NumberOfModules=1
Version=8.31
@EndOfDesignText@
#Region Project Attributes 
	#MainFormWidth: 400
	#MainFormHeight: 250
#End Region

Sub Process_Globals
	Private fx As JFX
	Private MainForm As Form
	Private socket As Socket
	Public connected As Boolean
	Dim data() As Object
	Private astream As AsyncStreams
	Private ser As B4RSerializator
	
	Private btnConnect As Button
	Private lblStatus As Label
	Private btnExit As Button
	
	Private txtfText1 As TextField
	Private txtfText3 As TextField
	Private lblHeader As Label
	
	Private btnMute As Button
	Private btnVolDn As Button
	Private btnVolUp As Button
	
	Private tmrTextWipe As Timer
	
End Sub

Sub AppStart (Form1 As Form, Args() As String)
	MainForm = Form1
	MainForm.RootPane.LoadLayout("Denon_IR") 'Load the layout file.
	MainForm.Show
	Form1.WindowTop = 50
	Form1.WindowLeft = 50
	txtfText1.Text = "192.168.1.93"

	Connect(txtfText1.Text)
	Log(txtfText1.Text)
	
	tmrTextWipe.Initialize("TextWipeTimer", 2000)'2000 = 2 seconds
	'tmrTextWipe.Enabled = True
	
End Sub


Sub btnConnect_Click
	Connect(txtfText1.Text)
	Log(txtfText1.Text)
End Sub

Sub btnExit_Action
	Log("Quit")
	ExitApplication
End Sub


Public Sub Connect(ip As String)
	If astream.IsInitialized Then
		Log("Open")
		astream.Close
		Log("Closed")
	End If
	socket.Initialize("socket")
	socket.Connect(ip, 51042, 30000)
	Log (ip)
	
End Sub

Private Sub Socket_Connected (Successful As Boolean)
	If Successful Then
		connected = True
		astream.Initialize(socket.InputStream, socket.OutputStream, "astream")
		StateChanged
	End If
End Sub



Public Sub StateChanged
	If connected Then
		lblStatus.TextColor=fx.Colors.Green
	Else
		lblStatus.TextColor=fx.Colors.Red
		Log("Not Connected")
	End If
	
End Sub


Sub btnMute_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("mute")))
	Log("mute")
End Sub

Sub btnVolUp_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("vup")))
	Log("vup")
End Sub

Sub btnVolDn_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("vdn")))
	Log("vdn")
End Sub


Private Sub astream_NewData (Buffer() As Byte)
	data = ser.ConvertBytesToArray(Buffer)
	Log("data")
	Log("data0 = " & data(0))
	'Log("data1 = " & data(1))
	Select data(0)
	
		Case "vup"
			Log("vol up")
			txtfText3.Text = "Volume Up"
			Log("data0 = " & data(0))
			
		Case "vdn"
			Log("vol dn")
			txtfText3.Text = "Volume Down"
			Log("data0 = " & data(0))
			
		Case "mute"
			Log("mute")
			txtfText3.Text = "Mute On/Off"
			Log("data0 = " & data(0))
			
							
	End Select
	
	tmrTextWipe.Enabled = True
	
End Sub

Sub TextWipeTimer_Tick
	
	txtfText3.Text = ""
	tmrTextWipe.Enabled = False
	
End Sub

Private Sub Astream_Error
	Log("Disconnected")
	connected = False
	StateChanged
	
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
	Return True
End Sub