/**
* 用于模拟测试数据
* 每3秒生成一次数据
*/
const pg = require('pg');
const config = {
user: 'postgres',
host: 'localhost',
database: 'police_rs',
password: '3337',
port: 5432
};
const pool = new pg.Pool(config);
const timer = 3000; // 时间间隔 3s
const device_num = 100; // 初始化数据库模拟设备的数量
const signxMap={};
const signyMap={};
const prexMap={};
const preyMap={};
const typeMap = {};
const bx = [515834, 518373], by = [3688409, 3690194];
const future = new Date('2220/3/28 11:25:50').toISOString(); // 未来的相当长时间
let cnt=0;
initDatabase(device_num);
// loopfn();
async function initDatabase(device_num){ // 根据设备的数量初始化数据库
// 清空历史表和实时表
console.log("重置history_info");
await pool.query(`
DROP TABLE IF EXISTS public.history_info;
CREATE TABLE IF NOT EXISTS public.history_info
(
geometry geometry NOT NULL,
device_id character varying(100) NOT NULL,
id serial,
state character varying(8) DEFAULT 0,
valid_time timestamp(6) with time zone,
unvalid_time timestamp(6) with time zone,
type character varying(8) ,
meta character varying(200),
name character varying(100),
CONSTRAINT history_main_pkey PRIMARY KEY (id)
);
`);
// 插入首条数据
let start = new Date("2022-05-20 12:00:00").getTime();
let cur = new Date().getTime();
for(let id = 0; id < device_num; id++){
typeMap[id] = Math.random() > 0.5? "0":"1";
prexMap[id] = Math.random()*(bx[1]-bx[0]) + bx[0];
preyMap[id] = Math.random()*(by[1]-by[0]) + by[0];
signxMap[id] = Math.random() * 2 - 1;
signyMap[id] = Math.random() * 2 - 1;
let item = {
device_id: '' + id,
name: typeMap[id] + '-' + id,
x:prexMap[id],
y:preyMap[id],
state:"1",
valid_time:new Date(start).toISOString(),
unvalid_time:new Date(start + 30 *60*1000).toISOString(),
meta: "{}",
type: typeMap[id],
};
// 初始化 历史表
await pool.query(`INSERT INTO public.history_info(device_id, geometry, state, valid_time, unvalid_time, type, meta, name) VALUES(
${item.device_id}, point(${item.x},${item.y})::geometry,'${item.state}', '${item.valid_time}', '${item.unvalid_time}',${item.type},'${item.meta}','${item.name}'
)`);
}
// 快速插入历史模拟数据
for(start += 30 *60*1000; start < cur; start += 30 *60*1000){ // 间隔半小时
for(let id = 0; id < device_num; id++){
prexMap[id] += signxMap[id] * Math.random() * 5;
preyMap[id] += signyMap[id] * Math.random() * 5;
if(prexMap[id] < bx[0]) signxMap[id] = Math.random();
else if(prexMap[id] > bx[1]) signxMap[id] = -Math.random();
if(preyMap[id] < by[0]) signyMap[id] = Math.random();
else if(preyMap[id] > by[1]) signyMap[id] = -Math.random();
let item = {
device_id: '' + id,
name: typeMap[id] + '-' + id,
x : prexMap[id],
y : preyMap[id],
state:"1",
valid_time:new Date(start).toISOString(),
unvalid_time:new Date(start + 30 *60*1000).toISOString(),
meta: "{}",
type: typeMap[id],
};
// 初始化 历史表
await pool.query(`INSERT INTO public.history_info(device_id, geometry, state, valid_time, unvalid_time, type, meta, name) VALUES(
${item.device_id}, point(${item.x},${item.y})::geometry,'${item.state}', '${item.valid_time}', '${item.unvalid_time}',${item.type},'${item.meta}','${item.name}'
)`);
}
}
}