Fix jwt-token
This commit is contained in:
@@ -16,22 +16,51 @@ module.exports = (sequelize) => {
|
||||
},
|
||||
comment: 'ID of the device sending heartbeat'
|
||||
},
|
||||
tenant_id: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true,
|
||||
references: {
|
||||
model: 'tenants',
|
||||
key: 'id'
|
||||
},
|
||||
comment: 'Tenant ID for multi-tenancy support'
|
||||
},
|
||||
device_key: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true, // Allow null for testing
|
||||
defaultValue: 'test-device-key',
|
||||
comment: 'Unique key of the sensor from heartbeat message'
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
comment: 'Device status (online, offline, error, etc.)'
|
||||
},
|
||||
timestamp: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
comment: 'Timestamp from device'
|
||||
},
|
||||
uptime: {
|
||||
type: DataTypes.BIGINT,
|
||||
allowNull: true,
|
||||
comment: 'Device uptime in seconds'
|
||||
},
|
||||
memory_usage: {
|
||||
type: DataTypes.INTEGER,
|
||||
type: DataTypes.FLOAT,
|
||||
allowNull: true,
|
||||
comment: 'Memory usage percentage'
|
||||
},
|
||||
cpu_usage: {
|
||||
type: DataTypes.FLOAT,
|
||||
allowNull: true,
|
||||
comment: 'CPU usage percentage'
|
||||
},
|
||||
disk_usage: {
|
||||
type: DataTypes.FLOAT,
|
||||
allowNull: true,
|
||||
comment: 'Disk usage percentage'
|
||||
},
|
||||
firmware_version: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
|
||||
@@ -54,25 +54,26 @@ router.post('/devices/:id/heartbeat', async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
// Validate heartbeat data format
|
||||
const { status, cpu_usage, memory_usage, disk_usage } = req.body;
|
||||
// Extract heartbeat data - handle both simple device heartbeats and detailed health reports
|
||||
const { type, key, status, cpu_usage, memory_usage, disk_usage, uptime, firmware_version } = req.body;
|
||||
|
||||
if (!status || typeof status !== 'string') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Invalid heartbeat data format - status is required'
|
||||
});
|
||||
}
|
||||
// For simple device heartbeats: {type:"heartbeat", key:"unique device ID"}
|
||||
// For detailed health reports: {status:"online", cpu_usage:25.5, memory_usage:60.2, etc.}
|
||||
|
||||
// Create heartbeat record
|
||||
// Create heartbeat record - handle both formats
|
||||
await Heartbeat.create({
|
||||
device_id: deviceId,
|
||||
tenant_id: device.tenant_id,
|
||||
device_key: key || ('device-' + deviceId),
|
||||
status: status || (type === 'heartbeat' ? 'online' : 'unknown'),
|
||||
timestamp: new Date(),
|
||||
status: status,
|
||||
cpu_usage: cpu_usage || null,
|
||||
uptime: uptime || null,
|
||||
memory_usage: memory_usage || null,
|
||||
disk_usage: disk_usage || null
|
||||
cpu_usage: cpu_usage || null,
|
||||
disk_usage: disk_usage || null,
|
||||
firmware_version: firmware_version || null,
|
||||
received_at: new Date(),
|
||||
raw_payload: req.body
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
|
||||
Reference in New Issue
Block a user