-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevicestatesgeter.cpp
117 lines (99 loc) · 3.91 KB
/
devicestatesgeter.cpp
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
#include "devicestatesgeter.h"
#include "utils/commonutils.h"
DeviceStatesGeter::DeviceStatesGeter(QObject *parent) : QObject(parent)
{
}
DeviceStatesGeter::motorState DeviceStatesGeter::getMotorState(QString motor_name)
{
QMutexLocker locker(&mutex);
QJsonObject temp_json;
temp_json["cmd"] = "inquiryMotorPos";
temp_json["motorName"] = motor_name;
QString result = emit sendGetDeviceState(getStringFromJsonObject(temp_json));
QJsonObject result_json = getJsonObjectFromString(result);
motorState state;
if((!result_json.contains("motorName"))||result_json["motorName"].toString() != motor_name)
{
qInfo("has no motorName key");
return state;
}
if(!result_json.contains("motorPosition"))
{
qInfo("has no motorPosition key");
return state;
}
state.current_position = result_json["motorPosition"].toDouble();
if(!result_json.contains("motorTargetPosition"))
{
qInfo("has no motorTargetPosition key");
return state;
}
state.isEnabled = result_json["motorEnableState"].toBool(false);
state.isHome = result_json["motorHomeState"].toBool(false);
state.target_position = result_json["motorTargetPosition"].toDouble();
if((!result_json.contains("error"))||result_json["error"].toString() != "")
{
qInfo("has no error key or has error");
return state;
}
state.result = true;
return state;
}
DeviceStatesGeter::IoState DeviceStatesGeter::getInputIoState(QString input_io_name)
{
QMutexLocker locker(&mutex);
QJsonObject temp_json;
temp_json["cmd"] = "inquiryInputIoState";
temp_json["inputIoName"] = input_io_name;
QString result = emit sendGetDeviceState(getStringFromJsonObject(temp_json));
QJsonObject result_json = getJsonObjectFromString(result);
IoState state;
if((!result_json.contains("inputIoName"))||result_json["inputIoName"].toString() != input_io_name)
return state;
if(!result_json.contains("IoValue"))
return state;
state.current_state = result_json["IoValue"].toBool();
if((!result_json.contains("error"))||result_json["error"].toString() != "")
return state;
state.result = true;
return state;
}
DeviceStatesGeter::IoState DeviceStatesGeter::getOutputIoState(QString output_io_name)
{
QMutexLocker locker(&mutex);
QJsonObject temp_json;
temp_json["cmd"] = "inquiryOutputIoState";
temp_json["outputIoName"] = output_io_name;
QString result = emit sendGetDeviceState(getStringFromJsonObject(temp_json));
QJsonObject result_json = getJsonObjectFromString(result);
IoState state;
if((!result_json.contains("outputIoName"))||result_json["outputIoName"].toString() != output_io_name)
return state;
if(!result_json.contains("IoValue"))
return state;
state.current_state = result_json["IoValue"].toBool();
if((!result_json.contains("error"))||result_json["error"].toString() != "")
return state;
state.result = true;
return state;
}
void DeviceStatesGeter::toggleOutputIoState(QString output_io_name, int input_state)
{
QMutexLocker locker(&mutex);
QJsonObject temp_json;
temp_json["cmd"] = "toggleOutputIoState";
temp_json["outputIoName"] = output_io_name;
temp_json["inputState"] = input_state;
QString result = emit sendGetDeviceState(getStringFromJsonObject(temp_json));
QJsonObject result_json = getJsonObjectFromString(result);
IoState state;
if((!result_json.contains("outputIoName"))||result_json["outputIoName"].toString() != output_io_name)
return;
if(!result_json.contains("IoValue"))
return;
state.current_state = result_json["IoValue"].toBool();
if((!result_json.contains("error"))||result_json["error"].toString() != "")
return;
state.result = true;
return;
}