1 //----------------------------------------------------------------------------- 2 // 3 // ValueByte.cpp 4 // 5 // Represents an 8-bit value 6 // 7 // Copyright (c) 2010 Mal Lansell <openzwave@lansell.org> 8 // 9 // SOFTWARE NOTICE AND LICENSE 10 // 11 // This file is part of OpenZWave. 12 // 13 // OpenZWave is free software: you can redistribute it and/or modify 14 // it under the terms of the GNU Lesser General Public License as published 15 // by the Free Software Foundation, either version 3 of the License, 16 // or (at your option) any later version. 17 // 18 // OpenZWave is distributed in the hope that it will be useful, 19 // but WITHOUT ANY WARRANTY; without even the implied warranty of 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 // GNU Lesser General Public License for more details. 22 // 23 // You should have received a copy of the GNU Lesser General Public License 24 // along with OpenZWave. If not, see <http://www.gnu.org/licenses/>. 25 // 26 //----------------------------------------------------------------------------- 27 28 #include <sstream> 29 #include "tinyxml.h" 30 #include "value_classes/ValueByte.h" 31 #include "Msg.h" 32 #include "platform/Log.h" 33 #include "Manager.h" 34 #include <ctime> 35 36 namespace OpenZWave 37 { 38 namespace Internal 39 { 40 namespace VC 41 { 42 43 //----------------------------------------------------------------------------- 44 // <ValueByte::ValueByte> 45 // Constructor 46 //----------------------------------------------------------------------------- ValueByte(uint32 const _homeId,uint8 const _nodeId,ValueID::ValueGenre const _genre,uint8 const _commandClassId,uint8 const _instance,uint16 const _index,string const & _label,string const & _units,bool const _readOnly,bool const _writeOnly,uint8 const _value,uint8 const _pollIntensity)47 ValueByte::ValueByte(uint32 const _homeId, uint8 const _nodeId, ValueID::ValueGenre const _genre, uint8 const _commandClassId, uint8 const _instance, uint16 const _index, string const& _label, string const& _units, bool const _readOnly, bool const _writeOnly, uint8 const _value, uint8 const _pollIntensity) : 48 Value(_homeId, _nodeId, _genre, _commandClassId, _instance, _index, ValueID::ValueType_Byte, _label, _units, _readOnly, _writeOnly, false, _pollIntensity), m_value(_value), m_valueCheck(false) 49 { 50 m_min = 0; 51 m_max = 255; 52 } 53 54 //----------------------------------------------------------------------------- 55 // <ValueByte::ValueByte> 56 // Constructor (from XML) 57 //----------------------------------------------------------------------------- ValueByte()58 ValueByte::ValueByte() 59 { 60 m_min = 0; 61 m_max = 255; 62 } 63 GetAsString() const64 std::string const ValueByte::GetAsString() const 65 { 66 stringstream ss; 67 ss << (uint32) GetValue(); 68 return ss.str(); 69 } 70 SetFromString(string const & _value)71 bool ValueByte::SetFromString(string const& _value) 72 { 73 uint32 val = (uint32) atoi(_value.c_str()); 74 if (val < 256) 75 { 76 return Set((uint8) val); 77 } 78 return false; 79 } 80 81 //----------------------------------------------------------------------------- 82 // <ValueByte::ReadXML> 83 // Apply settings from XML 84 //----------------------------------------------------------------------------- ReadXML(uint32 const _homeId,uint8 const _nodeId,uint8 const _commandClassId,TiXmlElement const * _valueElement)85 void ValueByte::ReadXML(uint32 const _homeId, uint8 const _nodeId, uint8 const _commandClassId, TiXmlElement const* _valueElement) 86 { 87 Value::ReadXML(_homeId, _nodeId, _commandClassId, _valueElement); 88 89 int intVal; 90 if (TIXML_SUCCESS == _valueElement->QueryIntAttribute("value", &intVal)) 91 { 92 m_value = (uint8) intVal; 93 } 94 else 95 { 96 Log::Write(LogLevel_Info, "Missing default byte value from xml configuration: node %d, class 0x%02x, instance %d, index %d", _nodeId, _commandClassId, GetID().GetInstance(), GetID().GetIndex()); 97 } 98 } 99 100 //----------------------------------------------------------------------------- 101 // <ValueByte::WriteXML> 102 // Write ourselves to an XML document 103 //----------------------------------------------------------------------------- WriteXML(TiXmlElement * _valueElement)104 void ValueByte::WriteXML(TiXmlElement* _valueElement) 105 { 106 Value::WriteXML(_valueElement); 107 108 char str[8]; 109 snprintf(str, sizeof(str), "%d", m_value); 110 _valueElement->SetAttribute("value", str); 111 } 112 113 //----------------------------------------------------------------------------- 114 // <ValueByte::Set> 115 // Set a new value in the device 116 //----------------------------------------------------------------------------- Set(uint8 const _value)117 bool ValueByte::Set(uint8 const _value) 118 { 119 // create a temporary copy of this value to be submitted to the Set() call and set its value to the function param 120 ValueByte* tempValue = new ValueByte(*this); 121 tempValue->m_value = _value; 122 123 // Set the value in the device. 124 bool ret = ((Value*) tempValue)->Set(); 125 126 // clean up the temporary value 127 delete tempValue; 128 129 return ret; 130 } 131 132 //----------------------------------------------------------------------------- 133 // <ValueByte::OnValueRefreshed> 134 // A value in a device has been refreshed 135 //----------------------------------------------------------------------------- OnValueRefreshed(uint8 const _value)136 void ValueByte::OnValueRefreshed(uint8 const _value) 137 { 138 switch (VerifyRefreshedValue((void*) &m_value, (void*) &m_valueCheck, (void*) &_value, ValueID::ValueType_Byte)) 139 { 140 case 0: // value hasn't changed, nothing to do 141 break; 142 case 1: // value has changed (not confirmed yet), save _value in m_valueCheck 143 m_valueCheck = _value; 144 break; 145 case 2: // value has changed (confirmed), save _value in m_value 146 m_value = _value; 147 break; 148 case 3: // all three values are different, so wait for next refresh to try again 149 break; 150 } 151 } 152 } // namespace VC 153 } // namespace Internal 154 } // namespace OpenZWave 155