add module fs
This commit is contained in:
@@ -1,26 +1,41 @@
|
||||
extends Node
|
||||
|
||||
# ЯУ-07 Блок
|
||||
var unit = Yau07.YaU07.new('ЯУ07')
|
||||
var soc_unicast: Socket
|
||||
var soc_brodcast: Socket
|
||||
var ip_address: String = Constants.ADDRESSES[0][1]
|
||||
var ip_port: int = Constants.ADDRESSES[0][2]
|
||||
var ip_address_yau: String = Constants.ADDRESSES[0][1]
|
||||
var ip_port_yau: int = Constants.ADDRESSES[0][2]
|
||||
var timeout: float = Yau07.ONLINE_TIMEOUT
|
||||
var last_update_time: float = 0.0
|
||||
var broadcast_packet: PackedByteArray
|
||||
var state = Constants.STATE.WAIT
|
||||
var send_array: Array = []
|
||||
|
||||
# ФС Блок
|
||||
var ip_fs_1: String = Constants.ADDRESSES[0][4]
|
||||
var command_stack: Array
|
||||
var state_fs = Constants.STATE.IDLE
|
||||
var client_fs_1: StreamPeer
|
||||
var peerstream: PacketPeer
|
||||
|
||||
signal yau_status_line(_status) ## Вызывается когда меняется состояние связи с ячейкой
|
||||
signal yau_receive(_data_from_yau) ##
|
||||
signal yau_receive(_data_from_yau)
|
||||
signal yau_read_isa(_unit_isa_ports)
|
||||
|
||||
signal data_send()
|
||||
signal data_received(data:PackedByteArray)
|
||||
signal port_opened(port_name: String)
|
||||
signal port_closed(port_name: String)
|
||||
signal port_error(error_str: String)
|
||||
|
||||
|
||||
## Класс для отправки данных в сокет
|
||||
class Socket extends PacketPeerUDP:
|
||||
func send_to(addr: String, port: int, data: PackedByteArray):
|
||||
self.set_dest_address(addr, port)
|
||||
self.put_packet(data)
|
||||
|
||||
|
||||
|
||||
func poll_receive(sock: Socket) -> bool: ## Приёмник
|
||||
if timeout - last_update_time > 0:
|
||||
@@ -29,7 +44,7 @@ func poll_receive(sock: Socket) -> bool: ## Приёмник
|
||||
var addr_receive = sock.get_packet_ip()
|
||||
var port_receive = sock.get_packet_port()
|
||||
|
||||
if (ip_address == addr_receive) and (ip_port == port_receive):
|
||||
if (ip_address_yau == addr_receive) and (ip_port_yau == port_receive):
|
||||
last_update_time = 0.0
|
||||
unit.parse(broadcast_packet)
|
||||
var data_from_yau_07 = unit.status
|
||||
@@ -40,6 +55,19 @@ func poll_receive(sock: Socket) -> bool: ## Приёмник
|
||||
return false
|
||||
|
||||
|
||||
func work_proc():
|
||||
client_fs_1.poll()
|
||||
var peer = peerstream.get_stream_peer()
|
||||
if not peer:
|
||||
return
|
||||
var peer_len_data = peer.get_available_bytes()
|
||||
if peer_len_data > 0:
|
||||
emit_signal("port_opened", ip_fs_1) # Эмитируем сигнал об открытии порта
|
||||
get_data_fs(peer)
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
# Привязка для принятия широковещательного канала
|
||||
soc_brodcast = Socket.new()
|
||||
@@ -50,23 +78,33 @@ func _ready() -> void:
|
||||
soc_unicast = Socket.new()
|
||||
rc = soc_unicast.bind(Constants.UNICAST_PORT, Constants.UNICAST_ADDRESS)
|
||||
if rc != OK:
|
||||
print_debug('Ошибка: неудачная привязка адреса: ', ip_address)
|
||||
print_debug('Ошибка: неудачная привязка адреса: ', ip_address_yau)
|
||||
|
||||
unit.connect('line_changed', Callable(self, 'on_line_changed'))
|
||||
unit.connect('command_fail', Callable(self, 'on_command_fail'))
|
||||
connect_fs()
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if client_fs_1:
|
||||
## State fs machine
|
||||
if (state_fs == Constants.STATE.IDLE) or (state_fs == Constants.STATE.DONE):
|
||||
if command_stack:
|
||||
var pack = command_stack.pop_front()
|
||||
send_data(pack)
|
||||
state_fs = Constants.STATE.SEND
|
||||
work_proc()
|
||||
|
||||
last_update_time += delta
|
||||
poll_receive(soc_brodcast)
|
||||
poll_receive(soc_unicast)
|
||||
state_machine()
|
||||
state_machine_yau07()
|
||||
match unit.process(delta):
|
||||
Error.OK: soc_unicast.send_to(ip_address, ip_port, unit.tx_data.slice(0, unit.tx_len))
|
||||
Error.OK: soc_unicast.send_to(ip_address_yau, ip_port_yau, unit.tx_data.slice(0, unit.tx_len))
|
||||
Error.FAILED: print_debug('Ошибка отправки данных')
|
||||
|
||||
|
||||
func state_machine():
|
||||
func state_machine_yau07():
|
||||
var fl_done = (unit.cmd_state == unit.CmdState.DONE or unit.cmd_state == unit.CmdState.FAIL)
|
||||
## Режим ожидания
|
||||
match state:
|
||||
@@ -90,22 +128,19 @@ func state_machine():
|
||||
if fl_done:
|
||||
emit_signal('yau_read_isa', unit.isa_ports)
|
||||
state = Constants.STATE.WAIT
|
||||
|
||||
|
||||
|
||||
# Записать команду в ИСА
|
||||
func read_port_isa(read_ports: Array) -> void:
|
||||
send_array.append(['rd', read_ports])
|
||||
|
||||
|
||||
# Прочитать данные из порта от ИСА
|
||||
func write_port_isa(port_isa: int, data_isa: int) -> void:
|
||||
var write_data: Dictionary = {port_isa: data_isa}
|
||||
send_array.append(['wr', write_data])
|
||||
|
||||
|
||||
func on_command_change_device(meta_device):
|
||||
ip_address = meta_device[0]
|
||||
ip_port = meta_device[1]
|
||||
|
||||
|
||||
func on_line_changed(_unit) -> void:
|
||||
emit_signal('yau_status_line', unit.online)
|
||||
|
||||
@@ -113,3 +148,50 @@ func on_line_changed(_unit) -> void:
|
||||
## Ессли отсутствует подключение к ячейке ЯУ-07
|
||||
func on_command_fail(_unit) -> void:
|
||||
print_debug('Command send fail')
|
||||
|
||||
|
||||
func on_command_change_device(meta_device):
|
||||
ip_address_yau = meta_device[0]
|
||||
ip_port_yau = meta_device[1]
|
||||
ip_fs_1 = meta_device[3]
|
||||
#ip_fs_2 = meta_device[4]
|
||||
#ip_fs_3 = meta_device[5]
|
||||
connect_fs()
|
||||
|
||||
|
||||
# Подключится к модулю ФС
|
||||
func connect_fs():
|
||||
client_fs_1 = StreamPeerTCP.new()
|
||||
peerstream = 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.set_stream_peer(client_fs_1)
|
||||
|
||||
|
||||
# Получить данные от ФС
|
||||
func get_data_fs(peer):
|
||||
var rx_data: PackedByteArray
|
||||
var len_data_in_buf = peer.get_available_bytes()
|
||||
if len_data_in_buf > 0:
|
||||
rx_data.append_array(peer.get_data(len_data_in_buf)[1])
|
||||
emit_signal("data_received", rx_data)
|
||||
if state_fs == Constants.STATE.SEND:
|
||||
state_fs = Constants.STATE.DONE
|
||||
|
||||
|
||||
# Отправить команду для ФС
|
||||
func send_data(data_to_sand):
|
||||
if client_fs_1.get_status() == 2:
|
||||
var peer = peerstream.get_stream_peer()
|
||||
if peer:
|
||||
peer.put_data(data_to_sand)
|
||||
emit_signal("data_send")
|
||||
|
||||
|
||||
# Разорвать соединение с ФС
|
||||
#func disconnect_fs():
|
||||
#client_fs_1.disconnect_from_host()
|
||||
#state_fs = Constants.STATE.IDLE
|
||||
#peerstream = PacketPeerStream.new()
|
||||
|
||||
Reference in New Issue
Block a user