1. /**
    2. * 用于模拟测试数据
    3. * 每3秒生成一次数据
    4. */
    5. const pg = require('pg');
    6. const config = {
    7. user: 'postgres',
    8. host: 'localhost',
    9. database: 'police_rs',
    10. password: '3337',
    11. port: 5432
    12. };
    13. const pool = new pg.Pool(config);
    14. const timer = 3000; // 时间间隔 3s
    15. const device_num = 100; // 初始化数据库模拟设备的数量
    16. const signxMap={};
    17. const signyMap={};
    18. const prexMap={};
    19. const preyMap={};
    20. const typeMap = {};
    21. const bx = [515834, 518373], by = [3688409, 3690194];
    22. const future = new Date('2220/3/28 11:25:50').toISOString(); // 未来的相当长时间
    23. let cnt=0;
    24. initDatabase(device_num);
    25. // loopfn();
    26. async function initDatabase(device_num){ // 根据设备的数量初始化数据库
    27. // 清空历史表和实时表
    28. console.log("重置history_info");
    29. await pool.query(`
    30. DROP TABLE IF EXISTS public.history_info;
    31. CREATE TABLE IF NOT EXISTS public.history_info
    32. (
    33. geometry geometry NOT NULL,
    34. device_id character varying(100) NOT NULL,
    35. id serial,
    36. state character varying(8) DEFAULT 0,
    37. valid_time timestamp(6) with time zone,
    38. unvalid_time timestamp(6) with time zone,
    39. type character varying(8) ,
    40. meta character varying(200),
    41. name character varying(100),
    42. CONSTRAINT history_main_pkey PRIMARY KEY (id)
    43. );
    44. `);
    45. // 插入首条数据
    46. let start = new Date("2022-05-20 12:00:00").getTime();
    47. let cur = new Date().getTime();
    48. for(let id = 0; id < device_num; id++){
    49. typeMap[id] = Math.random() > 0.5? "0":"1";
    50. prexMap[id] = Math.random()*(bx[1]-bx[0]) + bx[0];
    51. preyMap[id] = Math.random()*(by[1]-by[0]) + by[0];
    52. signxMap[id] = Math.random() * 2 - 1;
    53. signyMap[id] = Math.random() * 2 - 1;
    54. let item = {
    55. device_id: '' + id,
    56. name: typeMap[id] + '-' + id,
    57. x:prexMap[id],
    58. y:preyMap[id],
    59. state:"1",
    60. valid_time:new Date(start).toISOString(),
    61. unvalid_time:new Date(start + 30 *60*1000).toISOString(),
    62. meta: "{}",
    63. type: typeMap[id],
    64. };
    65. // 初始化 历史表
    66. await pool.query(`INSERT INTO public.history_info(device_id, geometry, state, valid_time, unvalid_time, type, meta, name) VALUES(
    67. ${item.device_id}, point(${item.x},${item.y})::geometry,'${item.state}', '${item.valid_time}', '${item.unvalid_time}',${item.type},'${item.meta}','${item.name}'
    68. )`);
    69. }
    70. // 快速插入历史模拟数据
    71. for(start += 30 *60*1000; start < cur; start += 30 *60*1000){ // 间隔半小时
    72. for(let id = 0; id < device_num; id++){
    73. prexMap[id] += signxMap[id] * Math.random() * 5;
    74. preyMap[id] += signyMap[id] * Math.random() * 5;
    75. if(prexMap[id] < bx[0]) signxMap[id] = Math.random();
    76. else if(prexMap[id] > bx[1]) signxMap[id] = -Math.random();
    77. if(preyMap[id] < by[0]) signyMap[id] = Math.random();
    78. else if(preyMap[id] > by[1]) signyMap[id] = -Math.random();
    79. let item = {
    80. device_id: '' + id,
    81. name: typeMap[id] + '-' + id,
    82. x : prexMap[id],
    83. y : preyMap[id],
    84. state:"1",
    85. valid_time:new Date(start).toISOString(),
    86. unvalid_time:new Date(start + 30 *60*1000).toISOString(),
    87. meta: "{}",
    88. type: typeMap[id],
    89. };
    90. // 初始化 历史表
    91. await pool.query(`INSERT INTO public.history_info(device_id, geometry, state, valid_time, unvalid_time, type, meta, name) VALUES(
    92. ${item.device_id}, point(${item.x},${item.y})::geometry,'${item.state}', '${item.valid_time}', '${item.unvalid_time}',${item.type},'${item.meta}','${item.name}'
    93. )`);
    94. }
    95. }
    96. }