如何检查车辆是否进入地理围栏或未使用 mongodb 和猫鼬在 NodeJS 中使用地理空间数据?



我一直在使用Node.JS,mongoDB和mongoose框架创建地理围栏和警报系统。

我有一辆带有传感器的车辆,它会不断发送它经过的坐标。 现在,我需要检查该车辆是否进入了我之前创建的地理围栏。

如果车辆进入围栏,我需要向系统管理员发送警报。

但是我对如何使用车辆本身提供的坐标和围栏的坐标来检查车辆是否进入围栏感到困惑。

以下是我到目前为止使用的代码。

地理围栏.js模型

const mongoose = require('mongoose');
const FenceSchema = mongoose.Schema({
fenceId      : {type: String, unique: true, required: false},
fenceName    : {type: String, required: true},
geoData      : {type: Object, required: false},
description  : {type: String, required: false},
status       : {type: Boolean, required: false, default: true}
},{
versionKey: false,
timestamps: true
});
FenceSchema.index({ "geoData": "2dsphere" });
module.exports = mongoose.model("geofences", FenceSchema);

围栏警报的示例数据.js

{
"_id" : ObjectId("5ef9b89334b4f12f74dd60a1"),
"fenceName" : "Ram Nagar",
"description" : "Should alert the admin when anyone enters or leaves through Ram Nagar",
"geoData" : {
"coordinates" : [ 
[ 
[ 
78.4521996974945, 
17.4258916749477
], 
[ 
78.4533584117889, 
17.4262294770614
], 
[ 
78.4534442424774, 
17.4267617700325
], 
[ 
78.4533584117889, 
17.4273657159469
], 
[ 
78.4528541564941, 
17.4279389508985
], 
[ 
78.4519529342651, 
17.4280822593551
], 
[ 
78.4514486789703, 
17.4277854059987
], 
[ 
78.4511590003967, 
17.4269357885517
], 
[ 
78.451384305954, 
17.4262806591452
], 
[ 
78.4521996974945, 
17.4258916749477
]
]
],
"type" : "Polygon"
}
}

围栏警报.js模型

const mongoose = require('mongoose');
const AlertSchema = mongoose.Schema({
deviceId   : {type: mongoose.Schema.Types.ObjectId, ref: 'templates.devices', required: true},
fenceId    : {type: mongoose.Schema.Types.ObjectId, ref: 'geofences', required: true},
status     : {type: Boolean, required: true, default: true},
alertRule  : {type: String, required: true},
notifyType : {type: String, enum: ['WARNING','DANGER','EMERGENCY'], defualt: "WARNING"}
},{
versionKey: false,
timestamps: true
});
module.exports = mongoose.model("fenceAlerts", AlertSchema);

围栏警报的示例数据.js

{
"_id" : ObjectId("5efb0082260fca32e8b08bfe"),
"deviceId" : ObjectId("5ef9f8ffc667822e84ecec9e"), //DeviceName: "VEH-1"
"fenceId" : ObjectId("5ef9b89334b4f12f74dd60a1"),
"alertRule" : "Incoming",
"status" : true,
"notifyType" : "EMERGENCY"
}

车辆发送的坐标

{
"_id" : ObjectId("5efb3bcd829b9310c83091de"),
"latitude" : 40.25,
"longitude" : 20.69,
"deviceId" : "VEH-1",
"entryDayTime" : ISODate("2020-06-30T13:19:09.989Z")
},
{
"_id" : ObjectId("5efb3bf5fc418a182c76d520"),
"latitude" : 42.25,
"longitude" : 21.69,
"deviceId" : "VEH-1",
"entryDayTime" : ISODate("2020-06-30T13:19:49.045Z")
},
{
"_id" : ObjectId("5efb3dd028b6e533a071707e"),
"latitude" : 52.25,
"longitude" : 31.69,
"deviceId" : "VEH-1",
"entryDayTime" : ISODate("2020-06-30T13:27:44.200Z")
},
{
"_id" : ObjectId("5efb3e06a78a9538b4c5b8a8"),
"latitude" : 22.25,
"longitude" : 15.69,
"deviceId" : "VEH-1",
"entryDayTime" : ISODate("2020-06-30T13:28:38.431Z")
},
{
"_id" : ObjectId("5efb3e4b3e598f386cf88357"),
"latitude" : 20.25,
"longitude" : 18.69,
"deviceId" : "VEH-1",
"entryDayTime" : ISODate("2020-06-30T13:29:47.391Z")
},
{
"_id" : ObjectId("5efb46267a00ce41fc466bbe"),
"latitude" : 80.25,
"longitude" : 18.69,
"deviceId" : "VEH-1",
"entryDayTime" : ISODate("2020-06-30T14:03:18.579Z")
},
{
"_id" : ObjectId("5efb4670ceeab33c48973d86"),
"latitude" : 90.25,
"longitude" : 48.69,
"deviceId" : "VEH-1",
"entryDayTime" : ISODate("2020-06-30T14:04:32.951Z")
}

我一直在使用 RabbitMQ 和 MQTT 进行连续数据传输。因此,随着车辆发送的每个有效载荷,我需要检查车辆是否在围栏内。

但实际问题是,如何将设备位置与围栏坐标进行比较?

我见过$geoWithin但不明白如何在我的情况下实施。有没有办法比较它们并返回字符串,例如,车辆进入围栏车辆离开围栏

在研究类似要求的解决方案时遇到了这篇文章。所以,不是完整的答案,而是一系列调查。

MongoDB支持Change Streams,这是一种在集合,数据库或集群上设置监视的机制。聚合查询配置为仅报告满足特定条件的更改。它避免了必须接收要执行条件检查的连续数据流。

如前所述,有一个查询$geoWithin。因此,当车辆在地理围栏内时,条件将为真。

从真到假,显然从假到真的变化,可以用来跟踪退出或进入。需要开发查询语法。

最新更新