前言

如果相熟爱智和看过我之前文章的敌人见到这篇文章肯定会有很大疑难,SDDC 作为智能设施发现控制协议,怎么会用在 windows 上?

这所有还是源自于我微小的脑洞,因为这段在搞 Windows 开发,突发奇想能不能把电脑也接入到爱智上,于是就把嵌入式设施应用的 SDDC 协定移植到了 Windows 上,本文就介绍下基于 QT 移植的 libsddc 库,其实我还移植到了 VS2022 上了,这个之后再介绍吧。

软硬件抉择

这里应用 windows 开发,除了电脑也不须要其余额定的硬件了。

软件的话,应用的是 QT 5.9.0 版本,官网下载太慢了,举荐大家下载这个清华大学开源镜像站的资源:https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.9/5.9.0/qt-opensource-windows-x86-5.9.0.exe

代码获取与解析

代码能够从我的 gitee 仓库间接获取:

https://gitee.com/inspiration-desktop/windows-libsddc.git

关上 libsddc 我的项目如下:

其中 SDDC 相干代码都已基于 windows环境进行兼容批改,具体批改内容能够全局搜寻 __WINDOWS__ 宏来查看,其中次要的差别是 windows 和嵌入式零碎的 socket 相干实现上,还有就是多线程,QT自身就反对 pthread ,这个给移植带来了很大的便当,不像 VS 为了反对 pthread 还须要一顿折腾,对于VS的移植之后的文章再介绍吧。

main.cpp 代码解析,次要内容是获取uuid作为设施惟一标识(其实还是我没找到适合的获取 windows MAC 地址的接口...);

#include "mainwindow.h"#include <QApplication>#include "sddc_message_example.h"#include "sddc.h"#include "test_thread.h"#include "cJSON.h"#include <QUuid>#include <QFile>#include <iostream>int main(int argc, char *argv[]){    QApplication a(argc, argv);        // 启动可视化窗口,临时用不到    //MainWindow w;        //w.show();    char * uuid_str;    char buffer[128];    QString uuidstr;    QUuid uuid;    // 获取uuid作为设施惟一标识    QFile file("uuid.txt");    if(file.exists()){        std::cout << "file exist\n";        if(!file.open(QIODevice::ReadWrite)){            std::cout << "open file failed\n";        }else{            //读取文件            //判断文件是否曾经读到开端了             while(!file.atEnd()){                //读取数据                memset(buffer,0,sizeof(buffer));                qint64 length = file.readLine(buffer,128);                if(length != -1){                    uuid_str = (char*)&buffer;                    std::cout << "read success\n";                }            }            file.close();        }    }else{        if(!file.open(QIODevice::ReadWrite)){            std::cout << "open file failed\n";        }else{            uuid = QUuid::createUuid();            uuidstr = uuid.toString();            uuid_str = (char *)uuidstr.remove("{").remove("}").remove("-").toStdString().data();            memset(buffer,0,sizeof(buffer));            memcpy(buffer,uuid_str,strlen(uuid_str));            uuid_str = buffer;            std::cout << uuid_str << std::endl;            qint64 length = -1;            length = file.write(uuid_str);            if(length == -1){                std::cout << "write file failed\n";            }else{                std::cout << "write file success\n";            }            file.close();        }    }    std::cout << uuid_str << std::endl;    // 启动一个新线程进行其余业务解决    //test_thread *thread1 = new test_thread();    //thread1->start();    // 启动 SDDC 协定    sddc_main(uuid_str);    a.exec();    return 0;}

成果

点击左下角的绿色三角运行程序;

能够在爱智设施搜寻中发现对应设施并增加;