﻿AppType=JavaFX
Build1=Default,b4j.example
File1=SSTV_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: 600
	#MainFormHeight: 600 
#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 txtfText1 As TextField
	Private txtfText2 As TextField
	Private txtfText3 As TextField
	Private lblHeader As Label
	
	Private btnCablePwr As Button
	Private btnTvPwr As Button
	Private btnChDn As Button
	Private btnChUp As Button
	Private btnMute As Button
	Private btnVolDn As Button
	Private btnVolUp As Button
	Private btn0 As Button
	Private btn1 As Button
	Private btn2 As Button
	Private btn3 As Button
	Private btn4 As Button
	Private btn5 As Button
	Private btn6 As Button
	Private btn7 As Button
	Private btn8 As Button
	Private btn9 As Button
	
	Private btnExit As Button
	
End Sub

Sub AppStart (Form1 As Form, Args() As String)
	MainForm = Form1
	MainForm.RootPane.LoadLayout("SSTV_IR") 'Load the layout file.
	MainForm.Show
	txtfText1.Text = "192.168.1.94"

	Connect(txtfText1.Text)
	Log(txtfText1.Text)
	txtfText2.RequestFocus
	
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

Private Sub txtfText2_Action
	If txtfText2.Text = "T" Then
		btnTvPwr_Action
		txtfText2.Text = ""
	End If
	If txtfText2.Text = "C" Then
		btnCablePwr_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "U" Then
		btnChUp_Action
		txtfText2.Text = ""
	End If
	If txtfText2.Text = "D" Then
		btnChDn_Action
		txtfText2.Text = ""
	End If
	If txtfText2.Text = "Q" Then
		btnMute_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "P" Then
		btnVolUp_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "M" Then
		btnVolDn_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "X" Then
		btnExit_Action
		txtfText2.Text = ""
	End If
	If txtfText2.Text = "0" Then
		btn0_Action
		txtfText2.Text = ""
	End If
	If txtfText2.Text = "1" Then
		btn1_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "2" Then
		btn2_Action
		txtfText2.Text = ""
	End If
	If txtfText2.Text = "3" Then
		btn3_Action
		txtfText2.Text = ""
	End If
	If txtfText2.Text = "4" Then
		btn4_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "5" Then
		btn5_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "6" Then
		btn6_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "7" Then
		btn7_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "8" Then
		btn8_Action
		txtfText2.Text = ""
	End If
	If	txtfText2.Text = "9" Then
		btn9_Action
		txtfText2.Text = ""
	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 btnTvPwr_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("tvpwr")))
	Log("tvpwr")
End Sub

Sub btnCablePwr_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("cblpwr")))
	Log("cblpwr")
End Sub

Sub btnChUp_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("chup")))
	Log("chup")
End Sub

Sub btnChDn_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("chdn")))
	Log("chdn")
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

Sub btn0_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num0")))
	Log("num0")
End Sub

Sub btn1_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num1")))
	Log("num1")
End Sub

Sub btn2_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num2")))
	Log("num2")
End Sub

Sub btn3_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num3")))
	Log("num3")
End Sub

Sub btn4_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num4")))
	Log("num4")
End Sub

Sub btn5_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num5")))
	Log("num5")
End Sub

Sub btn6_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num6")))
	Log("num6")
End Sub

Sub btn7_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num7")))
	Log("num7")
End Sub

Sub btn8_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num8")))
	Log("num8")
End Sub

Sub btn9_Action
	ser.Initialize
	astream.Write(ser.ConvertArrayToBytes(Array("num9")))
	Log("num9")
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 "tvpwr"
			Log("tv on/off")
			txtfText3.Text = "TV On/Off"
			Log("data0 = " & data(0))
			
		Case "cblpwr"
			Log("cable pn/off")
			txtfText3.Text = "Cable ON/OFF"
			Log("data0 = " & data(0))
			
		Case "chup"
			Log("channel up")
			txtfText3.Text = "Channel Up"
			Log("data0 = " & data(0))
			
		Case "chdn"
			Log("channel down")
			txtfText3.Text = "Channel Down"
			Log("data0 = " & 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))
			
		Case "num0"
			Log("num 0")
			txtfText3.Text = "Number 0"
			Log("data0 = " & data(0))
			
		Case "num1"
			Log("num 1")
			txtfText3.Text = "Number 1"
			Log("data0 = " & data(0))
			
		Case "num2"
			Log("num 2")
			txtfText3.Text = "Number 2"
			Log("data0 = " & data(0))
			
		Case "num3"
			Log("num 3")
			txtfText3.Text = "Number 3"
			Log("data0 = " & data(0))
			
		Case "num4"
			Log("num 4")
			txtfText3.Text = "Number 4"
			Log("data0 = " & data(0))
			
		Case "num5"
			Log("num 5")
			txtfText3.Text = "Number 5"
			Log("data0 = " & data(0))
			
		Case "num6"
			Log("num 6")
			txtfText3.Text = "Number 6"
			Log("data0 = " & data(0))
			
		Case "num7"
			Log("num 7")
			txtfText3.Text = "Number 7"
			Log("data0 = " & data(0))
			
		Case "num8"
			Log("num 8")
			txtfText3.Text = "Number 8"
			Log("data0 = " & data(0))
			
		Case "num9"
			Log("num 9")
			txtfText3.Text = "Number 9"
			Log("data9 = " & data(0))
			
	End Select
	'ExitApplication
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