001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.server;
019
020import java.util.Collections;
021import org.photonvision.common.configuration.ConfigManager;
022import org.photonvision.common.dataflow.DataChangeDestination;
023import org.photonvision.common.dataflow.DataChangeService;
024import org.photonvision.common.dataflow.DataChangeSource;
025import org.photonvision.common.dataflow.DataChangeSubscriber;
026import org.photonvision.common.dataflow.events.DataChangeEvent;
027import org.photonvision.common.dataflow.events.IncomingWebSocketEvent;
028import org.photonvision.common.dataflow.events.OutgoingUIEvent;
029import org.photonvision.common.dataflow.networktables.NetworkTablesManager;
030import org.photonvision.common.dataflow.websocket.UIPhotonConfiguration;
031import org.photonvision.common.logging.Logger;
032
033public class UIInboundSubscriber extends DataChangeSubscriber {
034    public UIInboundSubscriber() {
035        super(
036                Collections.singletonList(DataChangeSource.DCS_WEBSOCKET),
037                Collections.singletonList(DataChangeDestination.DCD_GENSETTINGS));
038    }
039
040    @Override
041    public void onDataChangeEvent(DataChangeEvent<?> event) {
042        if (event instanceof IncomingWebSocketEvent incomingWSEvent) {
043            if (incomingWSEvent.propertyName.equals("userConnected")
044                    || incomingWSEvent.propertyName.equals("sendFullSettings")) {
045                // Send full settings
046                var settings =
047                        UIPhotonConfiguration.programStateToUi(ConfigManager.getInstance().getConfig());
048                var message =
049                        new OutgoingUIEvent<>("fullsettings", settings, incomingWSEvent.originContext);
050                DataChangeService.getInstance().publishEvent(message);
051                Logger.sendConnectedBacklog();
052                NetworkTablesManager.getInstance().broadcastConnectedStatus();
053            }
054        }
055    }
056}