Initial commit of akmon project
This commit is contained in:
38
server/gateway-mqtt-node/scripts/simulate_downlink.js
Normal file
38
server/gateway-mqtt-node/scripts/simulate_downlink.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'dotenv/config'
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
const SUPABASE_URL = process.env.SUPABASE_URL
|
||||
const SUPABASE_SERVICE_ROLE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY
|
||||
if (!SUPABASE_URL || !SUPABASE_SERVICE_ROLE_KEY) throw new Error('Missing SUPABASE_* envs')
|
||||
|
||||
const supa = createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, { auth: { autoRefreshToken: false, persistSession: false } })
|
||||
|
||||
const topic = process.env.SIM_DOWNLINK_TOPIC || 'device/demo-001/down'
|
||||
const payload = process.env.SIM_DOWNLINK_PAYLOAD || JSON.stringify({ cmd: 'beep', duration_ms: 500 })
|
||||
const payloadEncoding = process.env.SIM_DOWNLINK_ENCODING || 'json'
|
||||
const qos = parseInt(process.env.SIM_DOWNLINK_QOS || '1', 10)
|
||||
const retain = /^true$/i.test(process.env.SIM_DOWNLINK_RETAIN || 'false')
|
||||
|
||||
const row = {
|
||||
id: uuidv4(),
|
||||
topic,
|
||||
payload,
|
||||
payload_encoding: payloadEncoding,
|
||||
qos,
|
||||
retain,
|
||||
status: 'pending',
|
||||
scheduled_at: new Date().toISOString(),
|
||||
created_by: null
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const { data, error } = await supa.from('mqtt_downlinks').insert(row).select('id, topic, payload_encoding, qos, retain, status').single()
|
||||
if (error) {
|
||||
console.error('insert error:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log('inserted:', data)
|
||||
}
|
||||
|
||||
main().catch((e) => { console.error(e); process.exit(1) })
|
||||
Reference in New Issue
Block a user