/*
ReferentialMessageCore : Class to store referential to decode message
Copyright (C) 2019 AleaJactaEst
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include "modules/debug/debug.h"
#include "modules/impulse/impulsebase.h"
#include "referentialmessagecore.h"
ReferentialMessageCore *ReferentialMessageCore::singleton = nullptr;
/*
* ElementReferential
*/
void ElementReferential::show(int level, int pos)
{
String a;
for(int i=0;iprint("[%s:%d] level:%d %s pos:%3d ------ power:%d\n", __FILE__, __LINE__, level, a.ascii().get_data(), pos, _power2);
DBG_PRINT("level:" + itos(level) + " " + a + " id:" + ImpulseBase::get_command_name(_id) + " pos:" + itos(pos) + " ------ power:" + itos(_power2));
}
else
{
DBG_PRINT("level:" + itos(level) + " " + a + " pos:" + itos(pos) + " id:" + ImpulseBase::get_command_name(_id));
}
level ++;
for(int i=0;i<_children.size();++i)
{
Ref child = _children[i];
child->show(level, i);
}
}
int ElementReferential::read_command(Ref msgin)
{
uint32_t i = msgin->get_serial(_power2);
Ref child = _children[i];
if ( child->_id != ImpulseBase::Impulse::__LAST_ELEMENT )
return child->_id;
return child->read_command(msgin);
}
/*
* ReferentialMessageCore
*/
void ReferentialMessageCore::_bind_methods()
{
}
ReferentialMessageCore *ReferentialMessageCore::get_singleton()
{
if ( ReferentialMessageCore::singleton == nullptr )
ReferentialMessageCore::singleton = new ReferentialMessageCore();
return ReferentialMessageCore::singleton;
}
void ReferentialMessageCore::read_referential_step(Dictionary step, Vector head, Ref root)
{
uint32_t i;
Array keys = step.keys();
String name;
String sep = ":";
if ( keys.size() == 0 )
{
Ref ele;
String a;
int ii;
ele.instance();
for(ii=0;iiput_serial(head.get(ii).get_pos(), getPowerOf2(_size));
}
a += head.get(ii).get_name();
uint32_t _size = head.get(ii).get_size()+1;
ele->put_serial(head.get(ii).get_pos(), getPowerOf2(_size));
int id = ImpulseBase::get_command(a);
if (id != ImpulseBase::Impulse::__LAST_ELEMENT)
{
_encoder[id].copy_ref_bitstream(ele);
root->set_id(id);
}
ele.unref();
return;
}
root->set_size(keys.size());
uint32_t nb = keys.size() - 1;
String a;
for(i=0; i <= nb; ++i)
{
name = keys[i];
Vector info = name.split(sep, false, 2);
if(info.size() == 2 )
{
uint32_t id = info[0].to_int();
Ref root_child = root->add_child(ImpulseBase::Impulse::__LAST_ELEMENT, i);
Variant child ;
child = step[name];
ElementHead tmp;
tmp.create(id, nb, info[1].ascii().get_data());
head.push_back(tmp);
read_referential_step(child, head, root_child);
head.remove(head.size()-1);
}
}
}
void ReferentialMessageCore::read_referential(Dictionary dictionary_message)
{
Vector head;
DBG_PRINT("Read Referential Message");
_decoder->set_id(ImpulseBase::Impulse::__LAST_ELEMENT);
read_referential_step(dictionary_message, head, _decoder);
}
void ReferentialMessageCore::show()
{
// For fun, list powerOf2 - TODO check is same with server side, and check size is same value for msg.xml
/*
{
for(int i=0;i<67;++i)
DBG_PRINT(itos(i) + "=> powerOf2 = " + itos(getPowerOf2(i)));
}
*/
// show Element to push data
{
for(int i = 0; i < ImpulseBase::Impulse::__LAST_ELEMENT; ++i)
{
DBG_PRINT(itos(i) + ") " + ImpulseBase::get_command_name(i) + " " + this->_encoder[i].show().ascii().get_data() + " [size:" + itos(this->_encoder[i].size_data()) + "]");
}
}
// show element to read data
_decoder->show();
}
void ReferentialMessageCore::clear_session()
{
if ( ReferentialMessageCore::singleton == nullptr )
return;
DBG_PRINT("Clear session for Referential Message.");
delete ReferentialMessageCore::singleton;
ReferentialMessageCore::singleton = nullptr;
}
int ReferentialMessageCore::read_command(Ref msgin)
{
return _decoder->read_command(msgin);
}