报警联动功能不是一个常规的功能,一般是给客户定制的功能,比如探测器采集到的数据报警了,不仅本地要播放报警声音,存储报警记录,发送报警短信和邮件,还需要触发警号面板灯进行报警,而警号面板指示灯也是modbus协议485通信的,需要软件这边在指定的通信端口上发送数据点亮警号灯,而这个通信端口既可以是原来的设备所使用的端口,也可以是独立的端口,所以在做的报警联动信息表的时候,应该考虑到这个因素,报警联动信息表要存储串口号、波特率、模块地址、联动地址集合。
在绝大部分的报警联动应用场景中,可能都有多对多的需求,比如一个设备报警可以触发多个联动,而多个设备报警可以触发同一个联动,这种应用还是非常广泛的,所以在设计软件的时候,都应该考虑到这个功能,具体多对多的映射关系,都可以用户灵活设定,比如这里的联动信息表的联动地址集合,一旦设定了多个联动地址,会逐个触发。
皮肤开源:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo
文件名称:styledemo
体验地址:https://gitee.com/feiyangqingyun/QWidgetExe https://github.com/feiyangqingyun/QWidgetExe
文件名称:bin_sams.zip
#include "frmconfiglink.h"
#include "ui_frmconfiglink.h"
#include "quiwidget.h"
#include "dbhelper.h"
#include "dbdelegate.h"
#include "excelapi.h"
#include "printapi.h"
#include "api.h"
frmConfigLink::frmConfigLink(QWidget *parent) : QWidget(parent), ui(new Ui::frmConfigLink)
{
ui->setupUi(this);
this->initForm();
this->initData();
this->changeStyle();
}
frmConfigLink::~frmConfigLink()
{
delete ui;
}
void frmConfigLink::showEvent(QShowEvent *)
{
model->select();
}
void frmConfigLink::initForm()
{
API::initTableView(ui->tableView);
ui->widgetTop->setProperty("flag", "navbtn");
if (QUIHelper::deskWidth() < 1440) {
ui->labTip->setText("提示 → 改动后需重启应用");
} else {
ui->labTip->setText("提示 → 必须和现场联动信息一致,改动后需重启应用");
}
}
void frmConfigLink::initData()
{
model = new QSqlTableModel(this);
model->setTable("AlarmLink");
model->setSort(0, Qt::AscendingOrder);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
ui->tableView->setModel(model);
ui->tableView->setProperty("model", true);
columnNames.clear();
columnNames << "位号" << "串口号" << "波特率" << "模块地址" << "联动地址集合" << "是否启用";
columnWidths.clear();
columnWidths << 150 << 100 << 100 << 90 << 160 << 100;
for (int i = 0; i < columnNames.count(); i++) {
model->setHeaderData(i, Qt::Horizontal, columnNames.at(i));
ui->tableView->setColumnWidth(i, columnWidths.at(i));
}
//位号委托
d_cbox_positionID = new DbDelegate(this);
d_cbox_positionID->setDelegateType("QComboBox");
ui->tableView->setItemDelegateForColumn(0, d_cbox_positionID);
positionIDChanged();
//启用禁用委托
QStringList linkEnable;
linkEnable << "启用" << "禁用";
DbDelegate *d_cbox_linkEnable = new DbDelegate(this);
d_cbox_linkEnable->setDelegateType("QComboBox");
d_cbox_linkEnable->setDelegateValue(linkEnable);
ui->tableView->setItemDelegateForColumn(5, d_cbox_linkEnable);
//用来切换样式自动改变颜色
delegates << d_cbox_positionID;
delegates << d_cbox_linkEnable;
}
void frmConfigLink::changeStyle()
{
foreach (DbDelegate *delegate, delegates) {
delegate->setTextColor(QUIConfig::TextColor);
delegate->setSelectBgColor(QUIConfig::NormalColorStart);
}
}
void frmConfigLink::positionIDChanged()
{
d_cbox_positionID->setDelegateValue(DBData::NodeInfo_PositionID);
}
void frmConfigLink::on_btnAdd_clicked()
{
if (DBData::NodeInfo_Count == 0) {
QUIHelper::showMessageBoxError("还未添加探测器,请先添加好探测器!", 3);
return;
}
int count = model->rowCount();
model->insertRow(count);
QString positionID = model->index(count - 1, 0).data().toString();
QString comName = model->index(count - 1, 1).data().toString();
int baudRate = model->index(count - 1, 2).data().toInt();
quint8 modelAddr = model->index(count - 1, 3).data().toInt() + 1;
QString linkAddr = model->index(count - 1, 4).data().toString();
QString linkEnable = model->index(count - 1, 5).data().toString();
if (count == 0) {
positionID = DBData::NodeInfo_PositionID.first();
comName = "COM1";
baudRate = 9600;
modelAddr = 1;
linkAddr = "1";
linkEnable = "启用";
}
//设置新增加的行默认值
model->setData(model->index(count, 0), positionID);
model->setData(model->index(count, 1), comName);
model->setData(model->index(count, 2), baudRate);
model->setData(model->index(count, 3), modelAddr);
model->setData(model->index(count, 4), linkAddr);
model->setData(model->index(count, 5), linkEnable);
ui->tableView->setCurrentIndex(model->index(count, 0));
}
void frmConfigLink::on_btnSave_clicked()
{
model->database().transaction();
if (model->submitAll()) {
model->database().commit();
DBHelper::loadAlarmLink();
} else {
model->database().rollback();
QUIHelper::showMessageBoxError("保存信息失败,信息不能为空,请重新填写!");
}
}
void frmConfigLink::on_btnDelete_clicked()
{
if (ui->tableView->currentIndex().row() < 0) {
QUIHelper::showMessageBoxError("请选择要删除的联动信息!");
return;
}
if (QUIHelper::showMessageBoxQuestion("确定要删除该联动信息吗?") == QMessageBox::Yes) {
int row = ui->tableView->currentIndex().row();
model->removeRow(row);
model->submitAll();
int count = model->rowCount();
ui->tableView->setCurrentIndex(model->index(count - 1, 0));
}
}
void frmConfigLink::on_btnReturn_clicked()
{
model->revertAll();
}
void frmConfigLink::on_btnClear_clicked()
{
int count = model->rowCount();
if (count <= 0) {
return;
}
if (QUIHelper::showMessageBoxQuestion("确定要清空所有联动信息吗?") == QMessageBox::Yes) {
DBHelper::clearAlarmLink();
model->select();
}
}
void frmConfigLink::on_btnExcel_clicked()
{
QString name = "报警联动信息";
QString fileName = QString("%1_%2").arg(name).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"));
QString file = API::getSaveFileNames(fileName, "Excel(*.xls)");
if (file.isEmpty()) {
return;
}
QString columns = "PositionID,ComName,BaudRate,ModelAddr,LinkAddr,LinkEnable";
QString where = "order by PositionID asc";
QStringList content = DBHelper::getContent("AlarmLink", columns, where, "", ";");
int rowCount = content.count();
if (rowCount == 0) {
QUIHelper::showMessageBoxError("没有要处理的数据!");
return;
}
ExcelAPI::Instance()->saveExcel(file, name, name, "", columnNames, columnWidths, content);
QString msg = QString("导出%1到Excel").arg(name);
DBHelper::addUserLog("用户操作", msg);
if (QUIHelper::showMessageBoxQuestion(msg + "成功!确定现在就打开吗?") == QMessageBox::Yes) {
QString url = QString("file:///%1").arg(file);
QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
}
}
void frmConfigLink::on_btnPrint_clicked()
{
QString name = "报警联动信息";
QString columns = "PositionID,ComName,BaudRate,ModelAddr,LinkAddr,LinkEnable";
QString where = "order by PositionID asc";
QStringList content = DBHelper::getContent("AlarmLink", columns, where, "", ";");
int rowCount = content.count();
if (rowCount == 0) {
QUIHelper::showMessageBoxError("没有要处理的数据!");
return;
}
PrintAPI::Instance()->print(name, "", columnNames, columnWidths, content);
QString msg = QString("打印%1").arg(name);
DBHelper::addUserLog("用户操作", msg);
}
void frmConfigLink::on_btnInput_clicked()
{
QString fileName;
bool ok = DBHelper::inputData(columnNames.count(), App::FileFilter, "AlarmLink", fileName, QUIHelper::appPath() + "/db", true);
if (!fileName.isEmpty()) {
DBHelper::addUserLog("用户操作", "导入报警联动信息");
if (ok) {
QUIHelper::showMessageBoxInfo("导入报警联动信息成功!", 3);
model->select();
} else {
QUIHelper::showMessageBoxError("导入报警联动信息失败!", 3);
}
}
}
void frmConfigLink::on_btnOutput_clicked()
{
QString columns = "*";
QString where = "order by PositionID asc";
QString title = columnNames.join(App::FileSpliter);
QStringList content = DBHelper::getContent("AlarmLink", columns, where, title);
QString fileName;
bool ok = DBHelper::outputData("报警联动信息", App::FileFilter, content, fileName, QUIHelper::appPath() + "/db");
if (!fileName.isEmpty()) {
DBHelper::addUserLog("用户操作", "导出报警联动信息");
if (ok) {
QUIHelper::showMessageBoxInfo("导出报警联动信息成功!", 3);
} else {
QUIHelper::showMessageBoxError("导出报警联动信息失败!", 3);
}
}
}