summaryrefslogtreecommitdiffstats
path: root/extras/UI_Handeler.py
blob: 2bfcfc41851e123c3006ba75ba067d96fa48b4aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""
   Copyright 2023 Suchinton Chakravarty

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

from main import *
from PyQt5 import QtCore
from PyQt5.QtCore import QPropertyAnimation
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import QEasingCurve
from PyQt5.QtWidgets import QGraphicsOpacityEffect
from PyQt5.QtWidgets import QDesktopWidget
import logging
import json

from . import Kuksa_Instance as kuksa_instance

# Global variables
subscribed = False
should_execute_callback = True

class UI_Handeler(MainWindow):
    """
    This class handles the UI of the AGL Demo Control Panel application.
    """

    def Hide_Navbar(self, bool_arg):
        """
        This method hides the navigation bar of the UI.

        Args:
        - bool_arg: A boolean value indicating whether to hide the navigation bar or not.
        """
        height = self.BottomMenuSubContainer.height()
        heightExtended = 75 if bool_arg else 0

        self.animation = QPropertyAnimation(self.BottomMenuSubContainer, b"minimumHeight")
        self.animation.setDuration(400)
        self.animation.setStartValue(height)
        self.animation.setEndValue(heightExtended)
        self.animation.setEasingCurve(QtCore.QEasingCurve.InOutQuart)
        self.animation.start()

    def animateSwitch(self, index):
        """
        This method animates the switching of pages for QstackedWidget with the animation being a fade in and out.

        Args:
        - index: The index of the page to switch to.
        """
        self.fader_widget = FaderWidget(self.stackedWidget.currentWidget(), self.stackedWidget.widget(index))
        self.stackedWidget.setCurrentIndex(index)

    def toggleMaximized(self):
        """
        This method toggles the maximized state of the window.
        """
        if self.isMaximized():
            self.showNormal()
        else:
            self.showMaximized()

    def moveWindow(self, event):
        """
        This method moves the window by dragging the header.

        Args:
        - event: The event object containing information about the mouse event.
        """
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(self.pos() + event.globalPos() - self.clickPosition)
            self.clickPosition = event.globalPos()
            event.accept()
    
    def mousePressEvent(self, event):
        self.clickPosition = event.globalPos()
        event.accept()
    
    def mouseReleaseEvent(self, event):
        self.clickPosition = None
        event.accept()
        
    def set_instance(self):
        """
        This method sets the instance of the Kuksa client.

        Returns:
        - True if the client is connected to Kuksa, False otherwise.
        """
        self.kuksa = kuksa_instance.KuksaClientSingleton.instance()
        self.client = self.kuksa.get_client()
        if self.client is not None and self.client.checkConnection():
            return True
        else:
            print("No connection to Kuksa")
            return False

    def subscribe_VSS_Signals(self):
        """
        This method subscribes to the VSS signals from Kuksa.
        """
        global subscribed
        if not subscribed:
            self.kuksa = kuksa_instance.KuksaClientSingleton.instance()
            self.client = self.kuksa.get_client()
            if self.client is not None and self.client.checkConnection():
                signals = [
                    "Vehicle.Speed",
                    "Vehicle.Powertrain.CombustionEngine.Speed",
                    "Vehicle.Body.Lights.DirectionIndicator.Left.IsSignaling",
                    "Vehicle.Body.Lights.DirectionIndicator.Right.IsSignaling",
                    "Vehicle.Body.Lights.Hazard.IsSignaling",
                    "Vehicle.Powertrain.FuelSystem.Level",
                    "Vehicle.Powertrain.CombustionEngine.ECT",
                    "Vehicle.Powertrain.Transmission.SelectedGear",
                    "Vehicle.Cabin.HVAC.Station.Row1.Left.Temperature",
                    "Vehicle.Cabin.HVAC.Station.Row1.Left.FanSpeed",
                    "Vehicle.Cabin.HVAC.Station.Row1.Right.Temperature",
                    "Vehicle.Cabin.HVAC.Station.Row1.Right.FanSpeed"]

                for signal in signals:
                    self.client.subscribe(signal, lambda data: UI_Handeler.VSS_callback(self,data), 'value')
                subscribed = True
            else:
                subscribed = False
                print("No connection to Kuksa")

    def VSS_callback(self,data):
        """
        This method is the callback function for the VSS signals from Kuksa.

        Args:
        - data: The data received from the signal.
        """
        global should_execute_callback
        if should_execute_callback is False:
            return
        IC_Page = self.stackedWidget.widget(1)
        HVAC_Page = self.stackedWidget.widget(2)

        info = json.loads(data)
        path = info.get('data', {}).get('path')
        value = info.get('data', {}).get('dp', {}).get('value')

        print(f"Received subscription event: {path} {value}")

        if path == "Vehicle.Speed":
            IC_Page.Speed_monitor.display(int(IC_Page.Speed_slider.value()))
            IC_Page.Speed_slider.setValue(int(value))

        if path == "Vehicle.Powertrain.CombustionEngine.Speed":
            IC_Page.RPM_slider.setValue(int(value))
            IC_Page.RPM_monitor.display(int(IC_Page.RPM_slider.value()))

        if path == "Vehicle.Body.Lights.DirectionIndicator.Left.IsSignaling":
            IC_Page.leftIndicatorBtn.setChecked(bool(value))

        if path == "Vehicle.Body.Lights.DirectionIndicator.Right.IsSignaling":
            IC_Page.rightIndicatorBtn.setChecked(bool(value))

        if path == "Vehicle.Body.Lights.Hazard.IsSignaling":
            IC_Page.hazardBtn.setChecked(bool(value))

        if path == "Vehicle.Powertrain.FuelSystem.Level":
            IC_Page.fuelLevel_slider.setValue(int(value))

        if path == "Vehicle.Powertrain.CombustionEngine.ECT":
            IC_Page.coolantTemp_slider.setValue(int(value))

        if path == "Vehicle.Powertrain.Transmission.SelectedGear":
            if int(value) == 127:
                IC_Page.driveBtn.setChecked(True)
            elif int(value) == 126:
                IC_Page.parkBtn.setChecked(True)
            elif int(value) == -1:
                IC_Page.reverseBtn.setChecked(True)
            elif int(value) == 0:
                IC_Page.neutralBtn.setChecked(True)

        if path == "Vehicle.Cabin.HVAC.Station.Row1.Left.Temperature":
            HVAC_Page.left_temp.setValue(int(value))

        if path == "Vehicle.Cabin.HVAC.Station.Row1.Left.FanSpeed":
            HVAC_Page.left_fan.setValue(int(value))

        if path == "Vehicle.Cabin.HVAC.Station.Row1.Right.Temperature":
            HVAC_Page.right_temp.setValue(int(value))

        if path == "Vehicle.Cabin.HVAC.Station.Row1.Right.FanSpeed":
            HVAC_Page.right_fan.setValue(int(value))


class FaderWidget(QWidget):
    def __init__(self, old_widget, new_widget):
        super().__init__(new_widget)
        
        self.old_widget = old_widget
        self.new_widget = new_widget

        self.effect = QGraphicsOpacityEffect()
        self.new_widget.setGraphicsEffect(self.effect)

        self.animation = QPropertyAnimation(self.effect, b"opacity")
        self.animation.setDuration(300)
        self.animation.setStartValue(0)
        self.animation.setEndValue(1)
        self.animation.setEasingCurve(QEasingCurve.OutCubic)
        self.animation.finished.connect(self.close)

        self.animate()

    def animate(self):
        self.animation.start()

    def close(self):
        self.old_widget.close()
        self.new_widget.show()
        super().close()