try { const { Pool, Client } = require('pg') const XLSX = require('xlsx') const path = require('path') const request = require('superagent') const moment = require('moment') // 连接数据库 const vcmpPool = new Pool({ user: 'FashionAdmin', host: '10.8.40.223', database: 'video-access', password: 'Fas123_', port: 5432, }) const anxinPool = new Pool({ user: 'FashionAdmin', host: '10.8.40.223', database: 'AnxinCloud', password: 'Fas123_', port: 5432, }) const fun = async () => { // note: we don't try/catch this because if connecting throws an exception // we don't need to dispose of the client (it will be undefined) const vcmpClient = await vcmpPool.connect() const anxinClient = await anxinPool.connect() try { await vcmpClient.query('BEGIN') const yingshiHost = 'https://open.ys7.com/api/' // 获取全部萤石摄像头信息 const anxinYSRes = await anxinClient.query(`SELECT * FROM t_video_ipc WHERE type=$1`, ['yingshi']); // console.log(anxinYSRes); // 获取所有萤石账号信息 const secretYSRes = await vcmpClient.query(`SELECT * FROM secret_yingshi`) const secretRes = secretYSRes.rows // console.log('secretRes', secretRes); let addSuccessCount = 0 let addRepeatCount = 0 let addUnlegalCount = 0 let addNoAuthCount = 0 for (let c of anxinYSRes.rows) { console.log(`处理序列号:${c.serial_no}`) if (!c.serial_no) { addUnlegalCount += 1 continue } let vcmpExistRes = await vcmpClient.query( `SELECT * FROM "gbCamera" WHERE streamid=$1`, [c.serial_no] ) let vcmpExistRows = vcmpExistRes.rows let cameraExistRes = await vcmpClient.query(`SELECT * FROM camera WHERE serial_no=$1`, [c.serial_no]) let cameraRes = cameraExistRes.rows || [] let repeatCamera = cameraRes.find(cr => { return ( !cr.channel_no && c.channel_no == 1 ) || (cr.channel_no == c.channel_no) }) if (repeatCamera) { console.log(`当前设备序列号 ${c.serial_no} 通道号 ${c.channel_no} 已存在`); console.log(`已存在 id=${repeatCamera.id} 通道号 ${repeatCamera.channel_no}`); addRepeatCount++ continue } if (vcmpExistRows.length) { let beloneSecretId = null let serialUnlegal = false for (let s of secretRes) { let start = (new Date()).getTime(); console.log(`当前查询账号key ${s.key}`); if (!s.newToken) { const tokenRes = await request.post(`${yingshiHost}lapp/token/get`).query({ appKey: s.key, appSecret: s.secret }) console.log(`查询 token 结果`, tokenRes.body); if (tokenRes.body.code == 200 && tokenRes.body.data) { const { accessToken, expireTime } = tokenRes.body.data s.newToken = accessToken } else { throw `未能获取萤石token ${s.key} ${tokenRes.body.code}` } } while (((new Date()).getTime() - start) < 3000) {//限制频率 continue; } // 检测设备所属 const cameraState = await request.post(`${yingshiHost}lapp/device/info`).query({ accessToken: s.newToken, deviceSerial: c.serial_no }) console.log(`查询设备所属结果`, cameraState.body.code, cameraState.body.msg); if (cameraState.body.code == 200) { console.log(`所属萤石账号 ${s.key}`); beloneSecretId = s.id break } else if (cameraState.body.code == 20018) { } else if (cameraState.body.code == 20014) { serialUnlegal = true break } } if (serialUnlegal) { addUnlegalCount++ continue } if (!beloneSecretId) { console.error('没有查询到所属账号') console.log(vcmpExistRows); addNoAuthCount++ continue } const cameraInQuery = `INSERT INTO "camera" (type, name, serial_no, cloud_control,longitude,latitude,create_time,create_user_id,yingshi_secret_id,gb_id,channel_no) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING id;` await vcmpClient.query(cameraInQuery, ['yingshi', c.name, c.serial_no, c.has_ptz, c.longitude, c.latitude, moment().format(), 1, beloneSecretId, vcmpExistRows[0].id, c.channel_no]) addSuccessCount++ } else { console.error('当前序列号不存在,应在 vcmp 添加相应萤石账号'); addNoAuthCount++ } } console.log(` 共 ${anxinYSRes.rows.length} 添加 ${addSuccessCount} 已有 ${addRepeatCount} 序列号不合法 ${addUnlegalCount} 无所属 ${addNoAuthCount} `); // await client.query('ROLLBACK') await vcmpClient.query('COMMIT') console.log('执行完毕~') } catch (e) { await vcmpClient.query('ROLLBACK') console.log('执行错误~') throw e } finally { vcmpClient.release(); } } fun() } catch (error) { console.error(error) }