Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "jaus/extras/mcu/setmicrocontrollerstate.h"
00041 #include "jaus/core/scaledinteger.h"
00042
00043 using namespace JAUS;
00044
00045
00057 int SetMicrocontrollerState::WriteMessageBody(Packet& packet) const
00058 {
00059 int total = 0;
00060 int expected = 0;
00061
00062 Microcontroller::DigitalStates::const_iterator digital;
00063 Microcontroller::AnalogStates::const_iterator analog;
00064
00065 expected += UINT_SIZE;
00066 total += packet.Write((UInt)mDigitalStates.size());
00067
00068 for(digital = mDigitalStates.begin();
00069 digital != mDigitalStates.end();
00070 digital++)
00071 {
00072 expected += UINT_SIZE + (UInt)digital->first.size() + BYTE_SIZE;
00073 total += packet.Write((UInt)digital->first.size());
00074 total += packet.Write((const unsigned char *)digital->first.c_str(), (UInt)digital->first.size());
00075 total += packet.WriteByte(digital->second);
00076 }
00077
00078 expected += UINT_SIZE;
00079 total += packet.Write((UInt)mAnalogStates.size());
00080
00081 for(analog = mAnalogStates.begin();
00082 analog != mAnalogStates.end();
00083 analog++)
00084 {
00085 expected += UINT_SIZE + (UInt)analog->first.size() + INT_SIZE;
00086 total += packet.Write((UInt)analog->first.size());
00087 total += packet.Write((const unsigned char *)analog->first.c_str(), (UInt)analog->first.size());
00088 total += ScaledInteger::Write(packet, analog->second, 100.0, -100.0, ScaledInteger::Int);
00089 }
00090
00091 return total == expected ? total : -1;
00092 }
00093
00094
00106 int SetMicrocontrollerState::ReadMessageBody(const Packet& packet)
00107 {
00108 int total = 0;
00109 int expected = UINT_SIZE*2;
00110 UInt size = 0;
00111
00112 total += packet.Read(size);
00113
00114 for(UInt i = 0; i < size; i++)
00115 {
00116 UInt len = 0;
00117 Byte val = 0;
00118 expected += UINT_SIZE + BYTE_SIZE;
00119 total += packet.Read(len);
00120 expected += len;
00121 std::string str;
00122 total += packet.Read(str, len);
00123 total += packet.Read(val);
00124 mDigitalStates[str] = val > 0 ? true : false;
00125 }
00126
00127 size = 0;
00128 total += packet.Read(size);
00129
00130 for(UInt i = 0; i < size; i++)
00131 {
00132 UInt len = 0;
00133 double val = 0.0;
00134 expected += UINT_SIZE + INT_SIZE;
00135 total += packet.Read(len);
00136 expected += len;
00137 std::string str;
00138 total += packet.Read(str, len);
00139 total += ScaledInteger::Read(packet, val, 100.0, -100.0, ScaledInteger::Int);
00140 mAnalogStates[str] = val;
00141 }
00142
00143 return total == expected ? total : -1;
00144 }
00145
00146
00153 bool SetMicrocontrollerState::IsLargeDataSet(const unsigned int maxPayloadSize) const
00154 {
00155 unsigned int expected = UINT_SIZE*2;
00156
00157 Microcontroller::DigitalStates::const_iterator digital;
00158 Microcontroller::AnalogStates::const_iterator analog;
00159
00160 for(digital = mDigitalStates.begin();
00161 digital != mDigitalStates.end();
00162 digital++)
00163 {
00164 expected += UINT_SIZE + (UInt)digital->first.size() + BYTE_SIZE;
00165 }
00166
00167 for(analog = mAnalogStates.begin();
00168 analog != mAnalogStates.end();
00169 analog++)
00170 {
00171 expected += UINT_SIZE + (UInt)analog->first.size() + INT_SIZE;
00172
00173 }
00174
00175 return expected > maxPayloadSize ? true : false;
00176 }
00177
00178