216 lines
11 KiB
GDScript
216 lines
11 KiB
GDScript
extends Node2D
|
||
|
||
var unit = Yau07.YaU07.new('ЯУ07')
|
||
var soc_unicast: Socket
|
||
var soc_brodcast: Socket
|
||
var address: String = '0.0.0.0'
|
||
var ports_wr: Dictionary # Словарь с портами для записи
|
||
var port: int
|
||
var broadcast_packet: PackedByteArray
|
||
var node_select1: Node
|
||
var timeout: float = Yau07.ONLINE_TIMEOUT
|
||
var last_update_time: float = 0.0
|
||
var flag_yau_control: bool
|
||
|
||
## Класс для отправки данных в сокет
|
||
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:
|
||
while sock.get_available_packet_count() > 0:
|
||
broadcast_packet = sock.get_packet()
|
||
var addr_receive = sock.get_packet_ip()
|
||
var port_receive = sock.get_packet_port()
|
||
|
||
if (broadcast_packet) and (address == addr_receive) and (port == port_receive):
|
||
$control_dev/control_dev.get_node2(0, 0).text = 'ЯУ-07Б НА СВЯЗИ'
|
||
$control_dev/control_dev.get_node2(0, 0).modulate = Constants.GREEN
|
||
flag_yau_control = true
|
||
last_update_time = 0.0
|
||
|
||
#unit.parse(broadcast_packet)
|
||
else:
|
||
if sock.get_available_packet_count():
|
||
last_update_time = 0.0
|
||
else:
|
||
$control_dev/control_dev.get_node2(0, 0).text = 'НЕТ СВЯЗИ С ЯУ-07Б'
|
||
$control_dev/control_dev.get_node2(0, 0).modulate = Constants.RED
|
||
flag_yau_control = false
|
||
return false
|
||
|
||
|
||
func _ready() -> void:
|
||
# Таблица 1
|
||
node_select1 = $scroll1/table1
|
||
draw_tabl(node_select1, Constants.ROWS_REGS_DATA, Constants.ROWS_REGS_DATA.size())
|
||
|
||
# Выбор прибора для подключения
|
||
var node_select_device = $select_dev/select_dev
|
||
draw_tabl(node_select_device, Constants.SELECT_DEVIСE, Constants.SELECT_DEVIСE.size())
|
||
var get_select_device = node_select_device.get_node2(1, 0)
|
||
# Заполнение адресов
|
||
for i_addr in Constants.ADDRESSES.size():
|
||
get_select_device.add_item(Constants.ADDRESSES[i_addr][0])
|
||
get_select_device.set_item_metadata(i_addr, Constants.ADDRESSES[i_addr].slice(1,3))
|
||
get_select_device.connect('item_selected', Callable(self, 'on_btn_select'))
|
||
node_select_device.get_node2(0,0).text = 'НАЗВАНИЕ ПРИБОРА'
|
||
node_select_device.set_columns_min_size([200, 100])
|
||
|
||
# Контроль
|
||
var node_control_device = $control_dev/control_dev
|
||
draw_tabl(node_control_device, Constants.CONTROL_DEVICE, Constants.CONTROL_DEVICE.size())
|
||
node_control_device.set_columns_min_size([300])
|
||
|
||
|
||
# Привязка для принятия широковещательного канала
|
||
soc_brodcast = Socket.new()
|
||
var rc = soc_brodcast.bind(Constants.BROADCAST_PORT, '*')
|
||
if rc != OK:
|
||
print('Ошибка: Неудачная привязка широковещательного адреса')
|
||
|
||
soc_unicast = Socket.new()
|
||
rc = soc_unicast.bind(Constants.UNICAST_PORT, Constants.UNICAST_ADDRESS)
|
||
if rc != OK:
|
||
print('Ошибка: неудачная привязка адреса: ', address)
|
||
|
||
unit.connect('line_changed', Callable(self, 'on_line_changed'))
|
||
unit.connect('command_fail', Callable(self, 'on_command_fail'))
|
||
unit.connect('command_done', Callable(self, 'on_command_done'))
|
||
|
||
|
||
## Рисует таблицу
|
||
func draw_tabl(tbl, row: Array, count_row: int):
|
||
for i_row in count_row:
|
||
tbl.add_row(row[i_row])
|
||
|
||
|
||
func _process(delta: float) -> void:
|
||
last_update_time += delta
|
||
poll_receive(soc_brodcast)
|
||
if broadcast_packet and flag_yau_control:
|
||
parse_broadcast(broadcast_packet)
|
||
|
||
match unit.process(delta):
|
||
Error.OK: soc_unicast.send_to(address, port, unit.tx_data.slice(0, unit.tx_len))
|
||
Error.FAILED: print('Ошибка отправки данных')
|
||
|
||
|
||
func parse_broadcast(packet):
|
||
var power: int # Мощность с широковещания
|
||
var temperature: int # Температура с широковещания
|
||
var DKM: int
|
||
var EMS_G: int
|
||
var status_board = packet.decode_u8(8)
|
||
var node_control_device = $control_dev/control_dev
|
||
$Packet.text = 'broadcast: ' + str(packet)
|
||
for i in 4:
|
||
if i == 0:
|
||
node_control_device.get_node2(0, i+2).text = 'ЯЧЕЙКА ЭМС-Г НА СВЯЗИ' if status_board & (1 << i) and flag_yau_control else 'ЯЧЕЙКИ ЭМС-Г НЕТ НА СВЯЗИ'
|
||
node_control_device.get_node2(0, i+2).modulate = Constants.GREEN if status_board & (1 << i) and flag_yau_control else Constants.RED
|
||
elif i == 1:
|
||
node_control_device.get_node2(0, i+2).text = 'ЯЧЕЙКА УГ НА СВЯЗИ' if status_board & (1 << i) and flag_yau_control else 'ЯЧЕЙКИ УГ НЕТ НА СВЯЗИ'
|
||
node_control_device.get_node2(0, i+2).modulate = Constants.GREEN if status_board & (1 << i) and flag_yau_control else Constants.RED
|
||
elif i == 2:
|
||
node_control_device.get_node2(0, i+2).text = 'ЯЧЕЙКА УКП №1 НА СВЯЗИ' if status_board & (1 << i) and flag_yau_control else 'ЯЧЕЙКИ УКП №1 НЕТ НА СВЯЗИ'
|
||
node_control_device.get_node2(0, i+2).modulate = Constants.GREEN if status_board & (1 << i) and flag_yau_control else Constants.RED
|
||
elif i == 3:
|
||
node_control_device.get_node2(0, i+2).text = 'ЯЧЕЙКА УКП №2 НА СВЯЗИ' if status_board & (1 << i) and flag_yau_control else 'ЯЧЕЙКИ УКП №2 НЕТ НА СВЯЗИ'
|
||
node_control_device.get_node2(0, i+2).modulate = Constants.GREEN if status_board & (1 << i) and flag_yau_control else Constants.RED
|
||
|
||
var dry_contact: int = packet.decode_u16(35) # Контроль сухих контактов (ИП УГ)
|
||
node_select1.set_node_text(2, 0, '%d' % dry_contact)
|
||
for i in 9:
|
||
if i == 0:
|
||
node_control_device.get_node2(0, i+6).text = 'ОШИБКА. КОНТРОЛЬ ИП МАА №1' if dry_contact & (1 << i) and flag_yau_control else 'ИП МАА №1'
|
||
node_control_device.get_node2(0, i+6).modulate = Constants.RED if dry_contact & (1 << i) and flag_yau_control else Constants.GREEN
|
||
elif i == 1:
|
||
node_control_device.get_node2(0, i+6).text = 'ОШИБКА. КОНТРОЛЬ ИП МАА №2' if dry_contact & (1 << i) and flag_yau_control else 'ИП МАА №2'
|
||
node_control_device.get_node2(0, i+6).modulate = Constants.RED if dry_contact & (1 << i) and flag_yau_control else Constants.GREEN
|
||
elif i == 2:
|
||
node_control_device.get_node2(0, i+6).text = 'ОШИБКА. КОНТРОЛЬ ИП9-50 №1' if dry_contact & (1 << i) and flag_yau_control else 'ИП9-50 №1'
|
||
node_control_device.get_node2(0, i+6).modulate = Constants.RED if dry_contact & (1 << i) and flag_yau_control else Constants.GREEN
|
||
elif i == 3:
|
||
node_control_device.get_node2(0, i+6).text = 'ОШИБКА. КОНТРОЛЬ ИП9-50 №2' if dry_contact & (1 << i) and flag_yau_control else 'ИП9-50 №2'
|
||
node_control_device.get_node2(0, i+6).modulate = Constants.RED if dry_contact & (1 << i) and flag_yau_control else Constants.GREEN
|
||
#elif i == 4:
|
||
#node_control_device.get_node2(0, i+6).text = 'ОШИБКА. КОНТРОЛЬ ИП9-50 №3' if dry_contact & (1 << i) and flag_yau_control else 'ИП9-50 №3'
|
||
#node_control_device.get_node2(0, i+6).modulate = Constants.RED if dry_contact & (1 << i) and flag_yau_control else Constants.GREEN
|
||
elif i == 4:
|
||
node_control_device.get_node2(0, i+6).text = 'ОШИБКА. КОНТРОЛЬ ИП12-50 №1' if dry_contact & (1 << i) and flag_yau_control else 'ИП12-50 №1'
|
||
node_control_device.get_node2(0, i+6).modulate = Constants.RED if dry_contact & (1 << i) and flag_yau_control else Constants.GREEN
|
||
#elif i == 6:
|
||
#node_control_device.get_node2(0, i+6).text = 'ОШИБКА. КОНТРОЛЬ ИП12-50 №2' if dry_contact & (1 << i) and flag_yau_control else 'ИП12-50 №2'
|
||
#node_control_device.get_node2(0, i+6).modulate = Constants.RED if dry_contact & (1 << i) and flag_yau_control else Constants.GREEN
|
||
elif i == 7:
|
||
node_control_device.get_node2(0, i+6).text = 'ОШИБКА. КОНТРОЛЬ ИП5-25' if dry_contact & (1 << i) and flag_yau_control else 'ИП5-25'
|
||
node_control_device.get_node2(0, i+6).modulate = Constants.RED if dry_contact & (1 << i) and flag_yau_control else Constants.GREEN
|
||
|
||
DKM = packet.decode_u16(37)
|
||
node_select1.set_node_text(3, 0, '%d' % DKM)
|
||
|
||
for i in range(8):
|
||
power = packet.decode_u16(39 + 2 * i)
|
||
node_select1.set_node_text(0, i, '%d' % power)
|
||
|
||
for i in range(8):
|
||
var TMP = packet.decode_u16(55 + 2 * i)
|
||
temperature = Constants.CONST_MIN_TEMP + (Constants.MAXIMUM_CODE_ADC - TMP) * Constants.TEMP
|
||
node_select1.set_node_text(1, i, '%d' % temperature)
|
||
|
||
for i in range(8):
|
||
power = packet.decode_u16(71 + 2 * i)
|
||
node_select1.set_node_text(0, i+8, '%d' % power)
|
||
|
||
for i in range(8):
|
||
if len(packet) > 87 + 2 * i:
|
||
var TMP = packet.decode_u16(87 + 2 * i)
|
||
temperature = Constants.CONST_MIN_TEMP + (Constants.MAXIMUM_CODE_ADC - TMP) * Constants.TEMP
|
||
node_select1.set_node_text(1, i+8, '%d' % temperature)
|
||
|
||
for i in range(15):
|
||
EMS_G = packet.decode_u16(9 + 2 * i)
|
||
node_select1.set_node_text(3, i+1, '%d' % EMS_G)
|
||
|
||
|
||
func on_line_changed(_unit) -> void:
|
||
if unit.online:
|
||
print('Off line')
|
||
else:
|
||
print('On line')
|
||
|
||
|
||
## Ессли отсутствует подключение к ячейке ЯУ-07
|
||
func on_command_fail(_unit) -> void:
|
||
print_debug('Command send fail')
|
||
|
||
|
||
## Индикация чтения портов
|
||
func on_command_done(_unit) -> void:
|
||
pass
|
||
|
||
|
||
func _on_control_button(toggled_on: bool) -> void:
|
||
$control_dev.visible = toggled_on
|
||
$Controlpanel.visible = toggled_on
|
||
|
||
|
||
func on_btn_select(intem_from_sector):
|
||
var node_select_device = $select_dev/select_dev
|
||
var get_select_device = node_select_device.get_node2(1, 0)
|
||
var meta = get_select_device.get_item_metadata(intem_from_sector)
|
||
address = meta[0]
|
||
port = meta[1]
|
||
|
||
|
||
func fill_item(node, i_column: int, i_row: int, arr: Dictionary, select: int = 0):
|
||
var optionbutton = node.get_node2(i_column, i_row) as OptionButton
|
||
for key in arr.keys():
|
||
var item_text = key + " - " + str(arr[key])
|
||
optionbutton.add_item(item_text)
|
||
if select < arr.size():
|
||
optionbutton.select(select)
|