Browse Source

同步安心云萤石摄像头sql

release_1.3.0
巴林闲侠 2 years ago
parent
commit
3c646eda7b
  1. 17
      code/VideoAccess-VCMP/script/1.3.0/data/1_sync_camera_data/.vscode/launch.json
  2. 151
      code/VideoAccess-VCMP/script/1.3.0/data/1_sync_camera_data/index.js
  3. 18
      code/VideoAccess-VCMP/script/1.3.0/data/1_sync_camera_data/package.json

17
code/VideoAccess-VCMP/script/1.3.0/data/1_sync_camera_data/.vscode/launch.json

@ -0,0 +1,17 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "启动程序",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\index.js"
}
]
}

151
code/VideoAccess-VCMP/script/1.3.0/data/1_sync_camera_data/index.js

@ -0,0 +1,151 @@
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: 'postgres',
host: '10.8.30.32',
database: 'video_access',
password: '123',
port: 5432,
})
const anxinPool = new Pool({
user: 'FashionAdmin',
host: '10.8.30.156',
database: 'Anxinyun0726',
password: '123456',
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) {
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}`
}
}
// 检测设备所属
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)
}

18
code/VideoAccess-VCMP/script/1.3.0/data/1_sync_camera_data/package.json

@ -0,0 +1,18 @@
{
"name": "appkey-generator",
"version": "1.0.0",
"description": "tool",
"main": "index.js",
"scripts": {
"test": "mocha",
"start": "set NODE_ENV=development&&node index"
},
"author": "liu",
"license": "ISC",
"dependencies": {
"moment": "^2.29.4",
"pg": "^7.18.2",
"superagent": "3.5.2",
"xlsx": "^0.17.1"
}
}
Loading…
Cancel
Save