Skip to main content

MqttServer

一、设备扩展属性​

属性说明默认值/备注
IPServerIP,为空时指任意IP
端口连接端口1883
允许连接的ID前缀过滤ID,例如Client端ID必须是ThingsGatewayId123456ThingsGatewayId
允许Rpc写入是否允许写入变量True
线程循环间隔上传线程的循环间隔,一般不需要更改(ms)1000
列表分割大小发布的实体列表按大小分割1000
设备Topic设备信息订阅TopicThingsGateway/Device
变量Topic变量信息订阅TopicThingsGateway/Variable
Rpc返回Topic客户端写入变量的订阅Topic,返回写入结果信息ThingsGateway/RpcSub
Rpc写入Topic客户端写入变量的发布TopicThingsGateway/RpcWrite
tip

登录网关的MqttServer还需要配置第三方授权User,并且在客户端填写对应的UserName与Password

二、变量配置​

启用​

启用变量后才能进行上传

允许写入​

单独配置变量是否允许写入

三、实体脚本​

说明​

脚本功能由cs-script实现

 //提供这个例子

//定义返回List
List<dynamic> newModelList=new List<dynamic>();
//input为固定传入值,在变量脚本中为变量实体类List,在设备脚本中为设备实体类List,查看上面的json说明
foreach (var item in input)
{
//添加自定义值
newModelList.Add(new
{
customName=item.Name,//变量名称
customValue=item.Value,//变量值
customCollectTime=item.CollectTime.ToString("yyyy-MM-dd HH:mm:ss fffffff"), //采集时间,这里格式化为自定义时间格式
});

}
return newModelList;

可以看到mqtt上传内容已经改为以上脚本返回值

DTO定义​

#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
// QQ群:605534569
//------------------------------------------------------------------------------
#endregion


using ThingsGateway.Core;
namespace ThingsGateway.Web.Foundation;
/// <summary>
/// 上传DTO
/// </summary>
public class VariableData
{
/// <inheritdoc cref="PrimaryIdEntity.Id"/>
public long Id { get; set; }
/// <inheritdoc cref="MemoryVariable.Name"/>
public string Name { get; set; }
/// <inheritdoc cref="MemoryVariable.Description"/>
public string Description { get; set; }
/// <inheritdoc cref="DeviceVariableRunTime.DeviceName"/>
public string DeviceName { get; set; }
/// <inheritdoc cref="DeviceVariableRunTime.RawValue"/>
public object RawValue { get; set; }
/// <inheritdoc cref="DeviceVariableRunTime.Value"/>
public object Value { get; set; }
/// <inheritdoc cref="DeviceVariableRunTime.ChangeTime"/>
public DateTime ChangeTime { get; set; }
/// <inheritdoc cref="DeviceVariableRunTime.CollectTime"/>
public DateTime CollectTime { get; set; }
/// <inheritdoc cref="DeviceVariableRunTime.IsOnline"/>
public bool IsOnline { get; set; }
/// <inheritdoc cref="MemoryVariable.ReadExpressions"/>
public string ReadExpressions { get; set; }
/// <inheritdoc cref="DeviceVariable.WriteExpressions"/>
public string WriteExpressions { get; set; }
/// <inheritdoc cref="DeviceVariable.IntervalTime"/>
public int IntervalTime { get; set; }
/// <inheritdoc cref="DeviceVariable.OtherMethod"/>
public string OtherMethod { get; set; }
/// <inheritdoc cref="DeviceVariable.VariableAddress"/>
public string VariableAddress { get; set; }

/// <inheritdoc cref="MemoryVariable.ProtectTypeEnum"/>
public int ProtectTypeEnum { get; set; }
/// <inheritdoc cref="MemoryVariable.DataTypeEnum"/>
public int DataTypeEnum { get; set; }
}

#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
// QQ群:605534569
//------------------------------------------------------------------------------
#endregion


using ThingsGateway.Core;
namespace ThingsGateway.Web.Foundation;
/// <summary>
/// 设备上传DTO
/// </summary>
public class DeviceData
{
/// <inheritdoc cref="PrimaryIdEntity.Id"/>
public long Id { get; set; }
/// <inheritdoc cref="CollectDeviceRunTime.PluginName"/>
public string PluginName { get; set; }
/// <inheritdoc cref="CollectDeviceRunTime.DeviceVariableCount"/>
public int DeviceVariablesCount { get; set; }
/// <inheritdoc cref="CollectDeviceRunTime.ActiveTime"/>
public DateTime ActiveTime { get; set; }
/// <inheritdoc cref="CollectDeviceRunTime.DeviceStatus"/>
public int DeviceStatus { get; set; }
/// <inheritdoc cref="CollectDeviceRunTime.LastErrorMessage"/>
public string LastErrorMessage { get; set; }
/// <inheritdoc cref="UploadDevice.Name"/>
public string Name { get; set; }
/// <inheritdoc cref="UploadDevice.Description"/>
public string Description { get; set; }
/// <inheritdoc cref="CollectDeviceRunTime.KeepRun"/>
public bool KeepRun { get; set; }

}


四、RPC写入格式​

    public class MqttRpcNameVaueWithId
{
public string RpcId { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
public class MqttRpcResult
{
public string RpcId { get; set; }
public string Message { get; set; }
public bool Success { get; set; }
}