| 1 | // SPDX-FileCopyrightText: 2011 Torsten Dreyer |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef HAVE_CONFIG_H |
| 5 | # include <config.h> |
| 6 | #endif |
| 7 | |
| 8 | #ifdef HAVE_WINDOWS_H |
| 9 | #include <windows.h> |
| 10 | #endif |
| 11 | |
| 12 | #ifdef WIN32 |
| 13 | #define strtof strtod |
| 14 | #endif |
| 15 | |
| 16 | #include <simgear/io/sg_socket.hxx> |
| 17 | #include <simgear/misc/strutils.hxx> |
| 18 | |
| 19 | #include "ApplicationProperties.hxx" |
| 20 | #include "FGPanelProtocol.hxx" |
| 21 | |
| 22 | #include <string> |
| 23 | using std::cerr; |
| 24 | using std::endl; |
| 25 | using std::string; |
| 26 | using std::vector; |
| 27 | |
| 28 | class PropertySetter { |
| 29 | public: |
| 30 | PropertySetter (SGPropertyNode_ptr node) : _node (node) {} |
| 31 | virtual void setValue (const char *value) = 0; |
| 32 | virtual ~PropertySetter () {}; |
| 33 | protected: |
| 34 | SGPropertyNode_ptr _node; |
| 35 | }; |
| 36 | |
| 37 | class BoolPropertySetter : public PropertySetter { |
| 38 | public: |
| 39 | BoolPropertySetter (SGPropertyNode_ptr node) : PropertySetter (node) {} |
| 40 | virtual void setValue (const char *value) { |
| 41 | _node->setBoolValue (atoi (value) != 0 ); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | class IntPropertySetter : public PropertySetter { |
| 46 | public: |
| 47 | IntPropertySetter (SGPropertyNode_ptr node) : PropertySetter (node) {} |
| 48 | virtual void setValue (const char *value) { |
| 49 | _node->setIntValue (atol (value)); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | class FloatPropertySetter : public PropertySetter { |
| 54 | public: |
| 55 | FloatPropertySetter (SGPropertyNode_ptr node) : PropertySetter (node) {} |
| 56 | virtual void setValue (const char *value) { |
| 57 | _node->setFloatValue (strtof (value, NULL)); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class DoublePropertySetter : public PropertySetter { |
| 62 | public: |
| 63 | DoublePropertySetter (SGPropertyNode_ptr node) : PropertySetter (node) {} |
| 64 | virtual void setValue (const char *value) { |
| 65 | _node->setDoubleValue (strtod (value, NULL)); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | class StringPropertySetter : public PropertySetter { |
| 70 | public: |
| 71 | StringPropertySetter (SGPropertyNode_ptr node) : PropertySetter (node) {} |
| 72 | virtual void setValue (const char *value) { |
| 73 | _node->setStringValue (value); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | FGPanelProtocol::FGPanelProtocol (SGPropertyNode_ptr a_Root) : |
| 78 | SGSubsystem (), |
| 79 | root (a_Root), |
| 80 | io (NULL) { |
| 81 | const SGPropertyNode_ptr outputNode (root->getNode ("protocol/generic/output")); |
| 82 | if (outputNode) { |
| 83 | const vector<SGPropertyNode_ptr> chunks (outputNode->getChildren ("chunk")); |
| 84 | for (vector<SGPropertyNode_ptr>::size_type i = 0; i < chunks.size (); i++) { |
| 85 | const SGPropertyNode_ptr chunk (chunks[i]); |
| 86 | |
| 87 | const SGPropertyNode_ptr nodeNode (chunk->getNode ("node", false)); |
| 88 | if (nodeNode == NULL) { |
| 89 | continue; |
| 90 | } |
| 91 | const SGPropertyNode_ptr node (ApplicationProperties::Properties->getNode (nodeNode->getStringValue (), true)); |
| 92 | |
| 93 | string type; |
| 94 | const SGPropertyNode_ptr typeNode (chunk->getNode ("type", false)); |
| 95 | if (typeNode != NULL) { |
| 96 | type = typeNode->getStringValue (); |
| 97 | } |
| 98 | if (type == "float") { |
| 99 | propertySetterVector.push_back (new FloatPropertySetter (node)); |
| 100 | } else if (type == "double" || type == "fixed") { |
| 101 | propertySetterVector.push_back (new DoublePropertySetter (node)); |
| 102 | } else if (type == "bool" || type == "boolean") { |
| 103 | propertySetterVector.push_back (new BoolPropertySetter (node)); |
| 104 | } else if (type == "string") { |
| 105 | propertySetterVector.push_back (new StringPropertySetter (node)); |
| 106 | } else { |
| 107 | propertySetterVector.push_back (new IntPropertySetter (node)); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | FGPanelProtocol::~FGPanelProtocol() |
| 114 | { |
| 115 | for (PropertySetterVector::size_type i = 0; i < propertySetterVector.size(); i++) { |
| 116 | delete propertySetterVector[i]; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | FGPanelProtocol::update (double dt) { |
| 122 | char buf[2][8192]; |
| 123 | |
| 124 | if (io == NULL) { |
| 125 | return; |
| 126 | } |
| 127 | // read all available lines, keep last one |
| 128 | int Page (0); |
| 129 | bool HaveData (false); |
| 130 | while (io->readline (buf[Page], sizeof (buf[Page]) - 1) > 0) { |
| 131 | HaveData = true; |
| 132 | Page ^= 1; |
| 133 | } |
| 134 | |
| 135 | if (HaveData) { |
| 136 | // process most recent line of data |
| 137 | Page ^= 1; |
| 138 | buf[Page][sizeof (buf[Page]) - 1] = 0; |
| 139 | const vector<string> tokens (simgear::strutils::split (buf[Page], ",")); |
| 140 | for (vector<string>::size_type i = 0; i < tokens.size (); i++) { |
| 141 | if (i < propertySetterVector.size ()) { |
| 142 | propertySetterVector[i]->setValue (tokens[i].c_str ()); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | FGPanelProtocol::init () { |
| 150 | const SGPropertyNode_ptr listenNode (root->getNode ("listen")); |
| 151 | if (listenNode == NULL) { |
| 152 | return; |
| 153 | } |
| 154 | const string hostname (listenNode->getNode ("host", true)->getStringValue ()); |
| 155 | const string port (listenNode->getNode ("port", true)->getStringValue ()); |
| 156 | const string style (listenNode->getNode ("style", true)->getStringValue ()); |
| 157 | |
| 158 | if (io != NULL) { |
| 159 | delete io; |
| 160 | } |
| 161 | io = new SGSocket (hostname, port, style); |
| 162 | |
| 163 | if (!io->open (SG_IO_IN)) { |
| 164 | cerr << "can't open socket " << style << ":" << hostname << ":" << port << endl; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | FGPanelProtocol::reinit () { |
| 170 | init (); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | // Register the subsystem. |
| 175 | #if 0 |
| 176 | SGSubsystemMgr::Registrant<FGPanelProtocol> registrantFGPanelProtocol; |
| 177 | #endif |