131 lines
4.5 KiB
GDScript
131 lines
4.5 KiB
GDScript
extends Node
|
|
|
|
const INDEX_SIZE = 0xC
|
|
var unit_fs_1 = Fs.FGOS.new()
|
|
var counter: int = 0
|
|
var client_fs_1: StreamPeer
|
|
var ip_fs_1: String = Constants.ADDRESSES[0][4]
|
|
var peerstream_fs_1: PacketPeer
|
|
var state_fs = Constants.STATE.IDLE
|
|
var command_stack: Array
|
|
var counter_fs_1: int = 0
|
|
|
|
|
|
func _ready() -> void:
|
|
pass
|
|
|
|
|
|
# Подключится к модулю ФС
|
|
func connect_fs_1():
|
|
if client_fs_1:
|
|
unit_fs_1.disconnect_fs(client_fs_1)
|
|
state_fs = Constants.STATE.IDLE
|
|
command_stack = []
|
|
|
|
client_fs_1 = StreamPeerTCP.new()
|
|
peerstream_fs_1 = PacketPeerStream.new()
|
|
var rc = client_fs_1.connect_to_host(ip_fs_1, Constants.PORT_FS)
|
|
if rc!= Error.OK:
|
|
emit_signal("port_error", ip_fs_1)
|
|
else:
|
|
peerstream_fs_1.set_stream_peer(client_fs_1)
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if client_fs_1:
|
|
poll_receive_fs(client_fs_1, peerstream_fs_1, ip_fs_1)
|
|
state_machine_fs(client_fs_1)
|
|
|
|
|
|
func state_machine_fs(client_fs):
|
|
## State fs machine
|
|
if client_fs == client_fs_1:
|
|
if (state_fs == Constants.STATE.IDLE) or (state_fs == Constants.STATE.DONE):
|
|
if command_stack:
|
|
var pack: Array = command_stack.pop_front()
|
|
unit_fs_1.send_fs_data(pack, client_fs_1, peerstream_fs_1)
|
|
state_fs = Constants.STATE.SEND
|
|
|
|
|
|
func poll_receive_fs(client: StreamPeerTCP, peerstream: PacketPeer, ip_fs: String):
|
|
client.poll()
|
|
var peer = peerstream.get_stream_peer()
|
|
# Проверяем, существует ли соединение
|
|
if not peer:
|
|
emit_signal("port_error", ip_fs)
|
|
return
|
|
# Проверяем состояние подключения
|
|
if client.get_status() != StreamPeerTCP.STATUS_CONNECTED:
|
|
if client == client_fs_1:
|
|
emit_signal("port_fs_closed_1", ip_fs)
|
|
return
|
|
|
|
if peer.get_available_bytes() > 0:
|
|
var rx_data: PackedByteArray
|
|
rx_data.append_array(peer.get_data(peer.get_available_bytes())[1])
|
|
if client == client_fs_1:
|
|
emit_signal("data_from_fs_1", rx_data)
|
|
emit_signal("port_fs_opened_1", ip_fs)
|
|
if state_fs == Constants.STATE.SEND:
|
|
state_fs = Constants.STATE.DONE
|
|
|
|
|
|
func _enter_tree() -> void:
|
|
pass
|
|
# Подключаем сигналы к методам
|
|
#Network.connect('data_send', _on_data_send)
|
|
#Network.connect('data_from_fs_1', _on_data_received)
|
|
#Network.connect('port_opened', _on_port_changed.bind(Color.GREEN))
|
|
#Network.connect('port_closed', _on_port_changed.bind(Color.GRAY))
|
|
#Network.connect('port_error', _on_port_changed.bind(Color.RED))
|
|
|
|
|
|
#func _on_port_changed(port_name: String, color: Color) -> void:
|
|
#$manage_panel/con/ip_connect.text = port_name
|
|
#$fs_connect.self_modulate = color
|
|
|
|
|
|
func _on_data_send():
|
|
pass
|
|
|
|
#
|
|
#func _on_data_received(data: PackedByteArray):
|
|
#var GRP = data.decode_u8(0x0)
|
|
#var CMD = data.decode_u8(0x1)
|
|
#match CMD:
|
|
#Constants.CMD.READ_CARRIER:
|
|
#if GRP == Constants.GROUP.BASE:
|
|
#$manage_panel/get_freq/freq_ind.text = str(data.decode_u64(0xC)) + ' Гц'
|
|
#Constants.CMD.READ_ATT:
|
|
#if GRP == Constants.GROUP.RF:
|
|
#$manage_panel/get_att/att_ind.text = '-' + str(data.decode_float(0x18)) + ' dB'
|
|
#Constants.CMD.READ_GEN_STATUS:
|
|
#if GRP == Constants.GROUP.GENERATOR:
|
|
#if data.decode_u32(0xC) == 0:
|
|
#$manage_panel/get_gen_status/status_ind.text = 'Включен'
|
|
#$manage_panel/get_gen_status/status_ind.modulate = Color.GREEN
|
|
#else:
|
|
#$manage_panel/get_gen_status/status_ind.text = 'Выключен'
|
|
#$manage_panel/get_gen_status/status_ind.modulate = Color.RED
|
|
#Constants.CMD.BASE_GET_CURRENT_TEMP:
|
|
#if GRP == Constants.GROUP.BASE:
|
|
#$temp_1.text = '%.2f' % data.decode_float(0xC) + ' °C'
|
|
#$temp_2.text = str(data.decode_float(0x10)) + ' °C'
|
|
#$temp_3.text = str(data.decode_float(0x14)) + ' °C'
|
|
#$read.text = str(data.hex_encode())
|
|
|
|
|
|
func _on_start():
|
|
unit_fs_1.start_session(counter, Network.client_fs_1)
|
|
unit_fs_1.get_base_current_temp(counter, Network.client_fs_1)
|
|
|
|
|
|
func _on_get_status(toggled_btn):
|
|
var inverted_state = not toggled_btn
|
|
unit_fs_1.set_gen_state(inverted_state, counter, Network.client_fs_1)
|
|
unit_fs_1.get_gen_state(counter, Network.client_fs_1)
|
|
|
|
|
|
func _on_addres_fs(input_addr: String) -> void:
|
|
ip_fs_1 = input_addr
|