76 lines
3.7 KiB
C++
76 lines
3.7 KiB
C++
#pragma once
|
|
#include <QCoreApplication>
|
|
#include <QCryptographicHash>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QLockFile>
|
|
#include <QSaveFile>
|
|
#include <QSet>
|
|
#include <cmath>
|
|
|
|
namespace UserCoordinateStore {
|
|
inline QString path() { return QCoreApplication::applicationDirPath()+"/data/user_coordinates.json"; }
|
|
inline QString scope(const QString &endpoint,const QString &device) {
|
|
return QString::fromLatin1(QCryptographicHash::hash(endpoint.toUtf8()+QByteArray(1,'\0')+device.toUtf8(),QCryptographicHash::Sha256).toHex());
|
|
}
|
|
inline bool validPose(const QJsonValue &v) {
|
|
if (!v.isArray() || v.toArray().size()!=6) return false;
|
|
for (const auto &x:v.toArray()) if (!x.isDouble() || !std::isfinite(x.toDouble())) return false;
|
|
return true;
|
|
}
|
|
inline bool valid(const QJsonObject &p) {
|
|
const QString name=p.value("name").toString();
|
|
if (p.value("user_reference").toString("base")!="base" ||
|
|
p.value("tcp_reference").toString("flange")!="flange" ||
|
|
p.value("units").toString("m_rad")!="m_rad" ||
|
|
p.value("rotation_convention").toString("RzRyRx")!="RzRyRx") return false;
|
|
return !name.trimmed().isEmpty() && name==name.trimmed() && name.size()<=80 && !name.contains(QChar::Null)
|
|
&& validPose(p.value("user_pose")) && validPose(p.value("tcp_offset"));
|
|
}
|
|
inline bool root(const QString &file,QJsonObject &out,QString &error) {
|
|
QFile f(file);
|
|
if (!f.exists()) { out={{"version",1},{"scopes",QJsonObject{}}}; return true; }
|
|
if (!f.open(QIODevice::ReadOnly)) { error=f.errorString(); return false; }
|
|
QJsonParseError e;
|
|
const auto doc=QJsonDocument::fromJson(f.readAll(),&e);
|
|
if (e.error!=QJsonParseError::NoError || !doc.isObject() || doc.object().value("version").toInt()!=1 || !doc.object().value("scopes").isObject()) {
|
|
error=QStringLiteral("自定义坐标系文件格式错误,未覆盖原文件"); return false;
|
|
}
|
|
out=doc.object(); return true;
|
|
}
|
|
inline bool load(const QString &file,const QString &identity,QJsonArray &profiles,QString &error) {
|
|
QJsonObject r;
|
|
if (!root(file,r,error)) return false;
|
|
const auto value=r.value("scopes").toObject().value(identity);
|
|
if (!value.isUndefined() && !value.isArray()) { error=QStringLiteral("坐标系分组格式错误"); return false; }
|
|
profiles=value.toArray(); QSet<QString> names;
|
|
for (const auto &v:profiles) {
|
|
const auto p=v.toObject();
|
|
if (!valid(p) || names.contains(p.value("name").toString())) { error=QStringLiteral("坐标系记录无效或名称重复"); return false; }
|
|
names.insert(p.value("name").toString());
|
|
}
|
|
return true;
|
|
}
|
|
inline bool save(const QString &file,const QString &identity,const QJsonArray &profiles,QString &error) {
|
|
QSet<QString> names;
|
|
for (const auto &v:profiles) {
|
|
const auto p=v.toObject(); const auto n=p.value("name").toString();
|
|
if (!valid(p) || names.contains(n)) { error=QStringLiteral("坐标系名称或位姿无效"); return false; }
|
|
names.insert(n);
|
|
}
|
|
if (!QDir().mkpath(QFileInfo(file).absolutePath())) { error=QStringLiteral("无法创建坐标系保存目录"); return false; }
|
|
QLockFile lock(file+".lock");
|
|
if (!lock.tryLock(250)) { error=QStringLiteral("坐标系文件正在被其他实例使用"); return false; }
|
|
QJsonObject r;
|
|
if (!root(file,r,error)) return false;
|
|
auto groups=r.value("scopes").toObject(); groups.insert(identity,profiles); r.insert("scopes",groups);
|
|
const auto bytes=QJsonDocument(r).toJson(); QSaveFile f(file);
|
|
if (!f.open(QIODevice::WriteOnly) || f.write(bytes)!=bytes.size() || !f.commit()) { error=f.errorString(); return false; }
|
|
return true;
|
|
}
|
|
}
|