summaryrefslogtreecommitdiff
path: root/plugins/obd2plugin/obd2source.h
blob: 9f789fa4916722e46086971c7cb57955cfa205aa (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

/*
Copyright (C) 2012 Intel Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#ifndef OBD2SOURCE_H
#define OBD2SOURCE_H



#include <abstractsource.h>
#include <string>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include "obdlib.h"
#include <glib.h>


class ObdRequest
{
public:
  VehicleProperty::Property property;
  std::string req;
  std::string arg;
};

class ObdReply
{
public:
  VehicleProperty::Property property;
  std::string req;
  std::string reply;
};


class Obd2Amb
{
public:

	typedef function<std::string (std::string)> ConversionFunction;

	Obd2Amb()
	{
		propertyPidMap[VehicleProperty::VehicleSpeed] = "010D1\r";
		propertyPidMap[VehicleProperty::EngineSpeed] = "010C1\r";
		propertyPidMap[VehicleProperty::MassAirFlow] = "01101\r";
		propertyPidMap[VehicleProperty::AirIntakeTemperature] = "010F1\r";
		propertyPidMap[VehicleProperty::ThrottlePosition] = "01111\r";
		propertyPidMap[VehicleProperty::BatteryVoltage] = "ATRV\r";
		propertyPidMap[VehicleProperty::EngineCoolantTemperature]  = "0105a\r";
		propertyPidMap[VehicleProperty::EngineLoad] = "01041/r";
		propertyPidMap[VehicleProperty::VIN] = "0902/r";
		propertyPidMap[VehicleProperty::WMI] = "0902/r";
		propertyPidMap[VehicleProperty::EngineOilTemperature] = "015C1\r";
		propertyPidMap[VehicleProperty::InteriorTemperature] = "01461\r";
		propertyPidMap[VehicleProperty::FuelConsumption] = "01101\r";



		propertyConversionMap[VehicleProperty::VehicleSpeed] = [](std::string input)
		{
			///velocity is used in other equations.  We'll save it off in a static variable:
			stringstream vssConvert(input);

			vssConvert>>velocity;

			return input;
		};

		propertyConversionMap[VehicleProperty::WMI] = [](std::string input)
		{
			return input.substr(0,3);
		};

		propertyConversionMap[VehicleProperty::FuelConsumption] = [](std::string input)
		{
			double maf;
			stringstream mafConvert(input);

			mafConvert>>maf;

			mafConvert<<1 / (14.75 * 6.26) * maf * 0/60;

			return mafConvert.str();
		};



	}



	map<VehicleProperty::Property, std::string> propertyPidMap;
	map<VehicleProperty::Property, ConversionFunction> propertyConversionMap;

private:

	static uint16_t velocity;
	static double fuelConsumptionOldTime;
};

class OBD2Source : public AbstractSource
{

public:
	OBD2Source(AbstractRoutingEngine* re, map<string, string> config);
	string uuid();
	int portHandle;
	void getPropertyAsync(AsyncPropertyReply *reply);
	void getRangePropertyAsync(AsyncRangePropertyReply *reply){}
	AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request);
	void subscribeToPropertyChanges(VehicleProperty::Property property);
	void unsubscribeToPropertyChanges(VehicleProperty::Property property);
	PropertyList supported();
	PropertyList queuedRequests;
	bool clientConnected;
	PropertyList activeRequests;
	void engineSpeed(double speed);
	void vehicleSpeed(int speed);
	void mafValue(double maf);
	void engineCoolantTemp(int temp);
	PropertyList removeRequests;
	void setSupported(PropertyList list);
	void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, string uuid) {}
	void supportedChanged(PropertyList) {}
	GAsyncQueue* commandQueue;
	GAsyncQueue* subscriptionAddQueue;
	GAsyncQueue* subscriptionRemoveQueue;
	GAsyncQueue* singleShotQueue;
	GAsyncQueue* responseQueue;
	void setConfiguration(map<string, string> config);
	//void randomizeProperties();
	std::string m_port;
	map<VehicleProperty::Property,AsyncPropertyReply*> propertyReplyMap;
	list<VehicleProperty::Property> propertySubscriptionList;
	void updateProperty(VehicleProperty::Property property,AbstractPropertyType *value);
private:
	PropertyList m_supportedProperties;
	GMutex *threadQueueMutex;
	

};

#endif // OBD2SOURCE_H