Fix jwt-token

This commit is contained in:
2025-09-17 06:02:55 +02:00
parent 83fc1098f6
commit 286c23b350
2 changed files with 43 additions and 13 deletions

View File

@@ -16,22 +16,51 @@ module.exports = (sequelize) => {
}, },
comment: 'ID of the device sending heartbeat' 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: { device_key: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: true, // Allow null for testing allowNull: true, // Allow null for testing
defaultValue: 'test-device-key', defaultValue: 'test-device-key',
comment: 'Unique key of the sensor from heartbeat message' 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: { uptime: {
type: DataTypes.BIGINT, type: DataTypes.BIGINT,
allowNull: true, allowNull: true,
comment: 'Device uptime in seconds' comment: 'Device uptime in seconds'
}, },
memory_usage: { memory_usage: {
type: DataTypes.INTEGER, type: DataTypes.FLOAT,
allowNull: true, allowNull: true,
comment: 'Memory usage percentage' 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: { firmware_version: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: true, allowNull: true,

View File

@@ -54,25 +54,26 @@ router.post('/devices/:id/heartbeat', async (req, res) => {
}); });
} }
// Validate heartbeat data format // Extract heartbeat data - handle both simple device heartbeats and detailed health reports
const { status, cpu_usage, memory_usage, disk_usage } = req.body; const { type, key, status, cpu_usage, memory_usage, disk_usage, uptime, firmware_version } = req.body;
if (!status || typeof status !== 'string') { // For simple device heartbeats: {type:"heartbeat", key:"unique device ID"}
return res.status(400).json({ // For detailed health reports: {status:"online", cpu_usage:25.5, memory_usage:60.2, etc.}
success: false,
message: 'Invalid heartbeat data format - status is required'
});
}
// Create heartbeat record // Create heartbeat record - handle both formats
await Heartbeat.create({ await Heartbeat.create({
device_id: deviceId, device_id: deviceId,
tenant_id: device.tenant_id, tenant_id: device.tenant_id,
device_key: key || ('device-' + deviceId),
status: status || (type === 'heartbeat' ? 'online' : 'unknown'),
timestamp: new Date(), timestamp: new Date(),
status: status, uptime: uptime || null,
cpu_usage: cpu_usage || null,
memory_usage: memory_usage || 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({ res.status(200).json({