kanzi 3.9.8 DataSource动态预览

本文讲述了如何通过自定义插件改进Kanzi的DataSource功能,实现实时响应xml变更,提高开发效率。涉及内容包括属性设置、定时读取文件、XML解析和数据对象更新。

kanzi的DataSource功能解耦了界面与c++交互,从传统的属性值更新变为xml更新,更加灵活。
遗憾的是在kanzi preview中,每次修改xml需要手动Restart,很繁琐,如果工程很大,需要等待很长时间。
官方提供了socket解决方案,但是需要买。
为此,我实现了一个插件PluginDynamicDataSource,根据xml变化实时更新预览,更加高效灵活。

1. 插件创建

参考使用kanzi开发仪表HMI插件

2. 添加属性

在这里插入图片描述

  • XmlFilenameProperty用于选择xml文件,它和DataSource插件的xml文件一样
  • NodeRefByStringProperty用于包含/可修改Data Context属性的那个节点,一般工程中只有一个根节点满足条件,比如RootPage,其他子节点自动继承Data Context属性
metadata.editor = "BrowseFileTextEditor";
可以把XmlFilenameProperty属性设置为文件夹选择器
//帮助文档
//将编辑器设为 BrowseFileTextEditor,该编辑器含有一个文本框,旁边有一个浏览 (Browse) 按钮。

NodeRefByStringProperty属性值是相对于当前节点,要获取真正的节点,这样处理

string path = getProperty(NodeRefByStringProperty);    
NodeSharedPtr node = lookupNode<Node>(path);

获取节点的Data Context属性

ResourceSharedPtr root = DataContext::getDataContext(*node);
DataContextSharedPtr data = dynamic_pointer_cast<DataContext>(root);
//获取dataobject
m_root = data->getData();

3. 添加定时器

参考 kanzi API案例 5. 定时器

3.1 读取文件

static vector<char> ReadFileContentsByte(std::string filename)
{
    vector<char> str;

    std::fstream fin;
    fin.open(filename, ios::in | ios::binary);

    if (!fin.is_open())
    {
        return str;
    }
    //const int LENGTH = 1000;

    char temp;
    while ((temp = fin.get()) != EOF)
    {
        str.push_back((char)temp);
        //std::cout << (byte)temp;
    }
    fin.close();

    return str;
}

3.2 解析文件

vector<char> filedata = ReadFileContentsByte(filename);
if (filedata.size() > 0) {
    parseFile(filedata);
 }

解析xml时候利用addDataObjectsRecursively递归更新dataobject值

void PluginDynamicDataSource::parseFile(vector<char> fileData)
{
    //kzLogDebug(("[Debug] -- function: {} -- line: {}", __FUNCTION__, __LINE__));
    // Clear the previous data object tree.
    //m_root.reset();
    if (!m_root) {
        return;
    }

    // Parse the XML document from the memory and release the open file.
    tinyxml2::XMLDocument doc;
    tinyxml2::XMLError error = doc.Parse(fileData.data(), fileData.size());

    // If the plugin successfully loads the file set in the XML Data Source File property, create data objects.
    if (error == tinyxml2::XML_SUCCESS)
    {
        // Get the root XML element.
        const tinyxml2::XMLElement* element = doc.RootElement();
        // Create the root data object of the data source.
       // m_root = make_shared<DataObject>(getDomain(), "Root");
        int index = 0;
        do
        {
            // Populate the child data objects of the root data object.
            addDataObjectsRecursively(getDomain(), m_root, element, index);
            // Handle all sibling elements of the root XML element.
            element = element->NextSiblingElement();
            index++;
        } while (element);

        //notifyModified();
    }

}

修改外部xml文件后,用setDataObject更新对应的值

static void addDataObjectsRecursively(Domain* domain, DataObjectSharedPtr parent, const tinyxml2::XMLElement* xml, int index)
{
    // Check whether the current element in the XML file has the type attribute set.
    const tinyxml2::XMLAttribute* typeAttribute = xml->FindAttribute("type");

    // Get value of the type attribute.
    const char* type = 0;
    if (typeAttribute)
    {
        type = typeAttribute->Value();
    }

    DataObjectSharedPtr object = parent->getChild(index);
    // Create the data object based on the value of the type attribute.
    setDataObject(domain, object, type, xml->Name(), xml->GetText());
    // Add the data object as a child to the parent data object.
    //parent->addChild(object);


    // Traverse the tree in the XML file to add data objects for each child element of the current XML element.
    int i = 0;
    for (const tinyxml2::XMLElement* child = xml->FirstChildElement(); child; child = child->NextSiblingElement())
    {
        // Recurse.
        addDataObjectsRecursively(domain, object, child, i);
        i++;
    }
}
void setDataObject(Domain* domain, DataObjectSharedPtr obj, const char* type, const char* name, const char* text)
{

    // Create an integer data object from the int type attributes.
    if (obj->getName() != string(name)) {
        kzLogDebug(("setDataObject error {} != xml->{}", obj->getName(), name));
        return;
    }
    if (type && strcmp(type, "int") == 0)
    {
        int value = 0;
        if (text)
        {
            value = atoi(text);
        }

        shared_ptr<DataObjectInt> v = dynamic_pointer_cast<DataObjectInt>(obj);
        v->setValue(value);
    }
    // Create a float data object from the float and real type attributes.
    else if (type && (strcmp(type, "real") == 0 || strcmp(type, "float") == 0))
    {
        double value = 0;
        if (text)
        {
            value = atof(text);
        }
        shared_ptr<DataObjectReal> v = dynamic_pointer_cast<DataObjectReal>(obj);
        v->setValue(value);
    }
    // Create a Boolean data object from the bool and boolean type attributes.
    else if (type && (strcmp(type, "bool") == 0 || strcmp(type, "boolean") == 0))
    {
        bool value = false;
        if (text)
        {
            value = (strcmp(text, "true") == 0);
        }
        shared_ptr<DataObjectBool> v = dynamic_pointer_cast<DataObjectBool>(obj);
        v->setValue(value);
    }
    // Create a string data object from the string type attributes.
    else if (type && strcmp(type, "string") == 0)
    {
        string value;
        if (text)
        {
            value = text;
        }
        shared_ptr<DataObjectString> v = dynamic_pointer_cast<DataObjectString>(obj);
        v->setValue(value);
    }
    else
    {
        // If the type attribute is not set, create a generic data object.
        // This is used to create the hierarchy in the data source.
       // object = make_shared<DataObject>(domain, name);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值