File indexing completed on 2024-11-10 05:11:10

0001 /*
0002  * Copyright 2018 by Marco Martin <mart@kde.org>
0003  * Copyright 2018 David Edmundson <davidedmundson@kde.org>
0004  *
0005  * Licensed under the Apache License, Version 2.0 (the "License");
0006  * you may not use this file except in compliance with the License.
0007  * You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  *
0017  */
0018 
0019 #pragma once
0020 
0021 #include <QObject>
0022 #include <QWebSocket>
0023 #include <QPointer>
0024 #include <QQuickItem>
0025 
0026 #ifdef Q_OS_ANDROID
0027 #include <QTextToSpeech>
0028 #include <QQueue>
0029 #endif
0030 
0031 #include <QTimer>
0032 
0033 class GlobalSettings;
0034 class QQmlPropertyMap;
0035 class ActiveSkillsModel;
0036 class AbstractSkillView;
0037 
0038 class MycroftController : public QObject
0039 {
0040     Q_OBJECT
0041     Q_PROPERTY(Status status READ status NOTIFY socketStatusChanged)
0042     //FIXME: make those two enums?
0043     Q_PROPERTY(bool speaking READ isSpeaking NOTIFY isSpeakingChanged)
0044     Q_PROPERTY(bool listening READ isListening NOTIFY isListeningChanged)
0045 
0046     Q_PROPERTY(QString currentSkill READ currentSkill NOTIFY currentSkillChanged)
0047     Q_PROPERTY(QString currentIntent READ currentIntent NOTIFY currentIntentChanged)
0048 
0049     Q_PROPERTY(bool serverReady READ serverReady NOTIFY serverReadyChanged)
0050 
0051     Q_ENUMS(Status)
0052 public:
0053     enum Status {
0054         Connecting,
0055         Open,
0056         Closing,
0057         Closed,
0058         Error
0059     };
0060     static MycroftController* instance();
0061 
0062     bool isSpeaking() const;
0063     bool isListening() const;
0064     bool serverReady() const;
0065     Status status() const;
0066     QString currentSkill() const;
0067     QString currentIntent() const;
0068 
0069     //Public API NOT to be used with QML
0070     void registerView(AbstractSkillView *view);
0071 
0072 Q_SIGNALS:
0073     //socket stuff
0074     void socketStatusChanged();
0075     void closed();
0076 
0077     //mycroft
0078     void isSpeakingChanged();
0079     void isListeningChanged();
0080     void stopped();
0081     void notUnderstood();
0082     void currentSkillChanged();
0083     void currentIntentChanged();
0084     void serverReadyChanged();
0085     void speechRequestedChanged(bool expectingResponse);
0086 
0087     //signal with nearly all data
0088     //TODO: remove?
0089     void intentRecevied(const QString &type, const QVariantMap &data);
0090 
0091     //type utterances, type is the current skill
0092     //TODO: remove?
0093     void fallbackTextRecieved(const QString &skill, const QVariantMap &data);
0094 
0095     void utteranceManagedBySkill(const QString &skill);
0096     void skillTimeoutReceived(const QString &skillidleid);
0097 
0098 public Q_SLOTS:
0099     void start();
0100     void disconnectSocket();
0101     void reconnect();
0102     void sendRequest(const QString &type, const QVariantMap &data, const QVariantMap &context = QVariantMap({}));
0103     void sendBinary(const QString &type, const QJsonObject &data, const QVariantMap &context = QVariantMap({}));
0104     void sendText(const QString &message);
0105     void startPTTClient();
0106 
0107 private:
0108     explicit MycroftController(QObject *parent = nullptr);
0109     void onMainSocketMessageReceived(const QString &message);
0110 
0111     QWebSocket m_mainWebSocket;
0112 
0113     QTimer m_reconnectTimer;
0114     QTimer m_reannounceGuiTimer;
0115 
0116     GlobalSettings *m_appSettingObj;
0117 
0118     //TODO: remove
0119     QString m_currentSkill;
0120     QString m_currentIntent;
0121 
0122     QHash<QString, AbstractSkillView *> m_views;
0123 
0124     QHash<QString, QQmlPropertyMap*> m_skillData;
0125 
0126 #ifdef Q_OS_ANDROID
0127     QTextToSpeech *m_speech;
0128     bool m_isExpectingSpeechResponse = false;
0129     QQueue<QString> ttsqueue;
0130 #endif
0131 
0132     QString m_qt_version_context;
0133     bool m_isSpeaking = false;
0134     bool m_isListening = false;
0135     bool m_mycroftLaunched = false;
0136     bool m_serverReady = false;
0137 };
0138