1 #pragma once
2 
3 #include "../DomoticzHardware.h"
4 #include "VehicleApi.h"
5 #include "../../main/concurrent_queue.h"
6 
7 class CeVehicle : public CDomoticzHardwareBase
8 {
9 public:
10 	enum eVehicleType {
11 		Tesla
12 	};
13 
14 	CeVehicle(const int ID, const eVehicleType vehicletype, const std::string& username, const std::string& password, int defaultinterval, int activeinterval, bool allowwakeup, const std::string& carid);
15 	~CeVehicle(void);
16 	bool WriteToHardware(const char* pdata, const unsigned char length) override;
17 private:
18 	enum eApiCommandType {
19 		Send_Climate_Off,
20 		Send_Climate_On,
21 		Send_Climate_Defrost,
22 		Send_Climate_Defrost_Off,
23 		Send_Charge_Start,
24 		Send_Charge_Stop,
25 		Get_All_States,
26 		Get_Location_State,
27 		Get_Charge_State,
28 		Get_Climate_State,
29 		Get_Awake_State,
30 		Wake_Up
31 	};
32 
33 	enum eAlertType {
34 		NotHome,
35 		Charging,
36 		NotCharging,
37 		Idling,
38 		Sleeping
39 	};
40 
41 	enum eWakeState {
42 		Asleep,
43 		WakingUp,
44 		Awake,
45 		SelfAwake
46 	};
47 
48 	struct tVehicle {
49 		bool connected;
50 		bool charging;
51 		bool climate_on;
52 		bool defrost;
53 		bool is_home;
54 		eWakeState wake_state;
55 		std::string charge_state;
56 	};
57 
58 	void Init();
59 	bool ConditionalReturn(bool commandOK, eApiCommandType command);
60 
61 	void Login();
62 	bool WakeUp();
63 	bool IsAwake();
64 	bool GetAllStates();
65 	bool GetLocationState();
66 	void UpdateLocationData(CVehicleApi::tLocationData &data);
67 	bool GetChargeState();
68 	void UpdateChargeData(CVehicleApi::tChargeData& data);
69 	bool GetClimateState();
70 	void UpdateClimateData(CVehicleApi::tClimateData& data);
71 	bool DoNextCommand();
72 	bool DoSetCommand(eApiCommandType command);
73 	std::string GetCommandString(const eApiCommandType command);
74 	void SendAlert();
75 
76 	bool StartHardware() override;
77 	bool StopHardware() override;
78 	void Do_Work();
79 
80 	std::shared_ptr<std::thread> m_thread;
81 
82 	bool m_loggedin;
83 	int m_defaultinterval;
84 	int m_activeinterval;
85 	bool m_allowwakeup;
86 
87 	tVehicle m_car;
88 	CVehicleApi *m_api;
89 	concurrent_queue<eApiCommandType> m_commands;
90 	bool m_setcommand_scheduled;
91 	int m_command_nr_tries;
92 
93 	eAlertType m_currentalert;
94 	std::string m_currentalerttext;
95 };
96 
97