File indexing completed on 2024-04-14 04:37:59

0001 /*
0002  * Copyright 2021 Aditya Mehra <aix.m@outlook.com>
0003  * Copyright 2018 by Marco Martin <mart@kde.org>
0004  * Copyright 2018 David Edmundson <davidedmundson@kde.org>
0005  *
0006  * Licensed under the Apache License, Version 2.0 (the "License");
0007  * you may not use this file except in compliance with the License.
0008  * You may obtain a copy of the License at
0009  *
0010  *    http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
0017  *
0018  */
0019 
0020 #pragma once
0021 
0022 #include <QObject>
0023 #include <QWebSocket>
0024 #include <QPointer>
0025 #include <QQuickItem>
0026 #include <QTimer>
0027 #include <QSettings>
0028 #include "skillmanager.h"
0029 
0030 class Controller : public QObject
0031 {
0032     Q_OBJECT
0033     Q_PROPERTY(Status status READ status NOTIFY socketStatusChanged)
0034 
0035     Q_PROPERTY(bool speaking READ isSpeaking NOTIFY isSpeakingChanged)
0036     Q_PROPERTY(bool listening READ isListening NOTIFY isListeningChanged)
0037 
0038     Q_PROPERTY(QString currentSkill READ currentSkill NOTIFY currentSkillChanged)
0039     Q_PROPERTY(QString currentIntent READ currentIntent NOTIFY currentIntentChanged)
0040 
0041     Q_PROPERTY(bool serverReady READ serverReady NOTIFY serverReadyChanged)
0042     
0043     Q_PROPERTY(QString webSocketAddress READ webSocketAddress WRITE setWebSocketAddress NOTIFY webSocketChanged)
0044     Q_PROPERTY(bool useHivemind READ useHivemind WRITE setUseHivemind NOTIFY useHivemindChanged)
0045 
0046     Q_ENUMS(Status)
0047 
0048 public:
0049     enum Status {
0050         Connecting,
0051         Open,
0052         Closing,
0053         Closed,
0054         Error
0055     };
0056 
0057     static Controller* instance();
0058     bool isSpeaking() const;
0059     bool isListening() const;
0060     bool serverReady() const;
0061     Status status() const;
0062     QString currentSkill() const;
0063     QString currentIntent() const;
0064     QString webSocketAddress() const;
0065     bool useHivemind() const;
0066 
0067 Q_SIGNALS:
0068     void socketStatusChanged();
0069     void closed();
0070 
0071     void isSpeakingChanged();
0072     void isListeningChanged();
0073     void stopped();
0074     void notUnderstood();
0075     void currentSkillChanged();
0076     void currentIntentChanged();
0077     void serverReadyChanged();
0078     void speechRequestedChanged(bool expectingResponse);
0079 
0080     void intentRecevied(const QString &type, const QVariantMap &data);
0081 
0082     void fallbackTextRecieved(const QString &skill, const QVariantMap &data);
0083 
0084     void utteranceManagedBySkill(const QString &skill);
0085     void skillTimeoutReceived(const QString &skillidleid);
0086     void messageReceived(const QString &message);
0087     
0088     void webSocketChanged();
0089     void useHivemindChanged();
0090 
0091 public Q_SLOTS:
0092     void start();
0093     void disconnectSocket();
0094     void reconnect();
0095     void sendRequest(const QString &type, const QVariantMap &data);
0096     void sendBinary(const QString &type, const QJsonObject &data);
0097     void sendText(const QString &message);
0098     void setWebSocketAddress(QString webSocketAddress);
0099     void setUseHivemind(bool useHivemind);
0100 
0101 private:
0102     explicit Controller(QObject *parent = nullptr);
0103     void onMainSocketMessageReceived(const QString &message);
0104 
0105     QWebSocket m_mainWebSocket;
0106 
0107     QTimer m_reconnectTimer;
0108 
0109     QString m_currentSkill;
0110     QString m_currentIntent;
0111 
0112     bool m_isSpeaking = false;
0113     bool m_isListening = false;
0114     bool m_mycroftLaunched = false;
0115     bool m_serverReady = false;
0116     
0117     QSettings m_settings;
0118 };