Guest User

Untitled

a guest
Dec 18th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. /* this is a partial gist */
  2. const Promise = require('bluebird')
  3. const axios = require('axios')
  4. const Camera = require('onvif').Cam
  5.  
  6. class OnvifManager {
  7. //...
  8. this.cameras = {}
  9. //...
  10.  
  11. readCamerasFromDatabase () {
  12. //...
  13. let onvif = // code to read from database
  14.  
  15. let options = {}
  16. options.hostname = onvif.address
  17. options.username = onvif.login
  18. options.password = onvif.password
  19. if (onvif.port && onvif.port !== 80) {
  20. options.port = onvif.port
  21. }
  22.  
  23. let camera = new Camera(options, error => {
  24. camera.onvif = onvif
  25.  
  26. // assign to list of cameras
  27. this.cameras[onvif.id] = camera
  28.  
  29. if (error === null) {
  30. camera.getSnapshotUri({}, (error, data, xml) => {
  31. if (error) {
  32. console.error(`Cannot get snapshot URI for ${camera.onvif.name}/${camera.onvif.address}`)
  33. }
  34. else {
  35. camera.snapshotUri = data
  36. }
  37. })
  38. }
  39. }
  40.  
  41. // call this function whenever a snapshot from the onvif camera is desired
  42. commandFetchSnapshot (id) {
  43. return new Promise((resolve, reject) => {
  44. let camera = this.cameras[id]
  45. if (!camera) {
  46. let results = {
  47. message: 'Not Found',
  48. status: 404
  49. }
  50. reject(new Error(results))
  51. }
  52. else {
  53. // check that we have a snapshotUri attached to this camera
  54. if ('snapshotUri' in camera) {
  55. let snapshotUri = camera.snapshotUri.uri
  56.  
  57. // using axios.get with options { responseType: 'arraybuffer' }
  58. // did not work at all. The data returned was transformed somehow.
  59. // This way works!
  60. axios.request({
  61. responseType: 'arraybuffer',
  62. url: snapshotUri,
  63. method: 'get',
  64. headers: {
  65. 'Content-Type': 'image/*'
  66. },
  67. auth: {
  68. username: camera.username,
  69. password: camera.password
  70. }
  71. })
  72. .then((results) => {
  73. let mimeType = results.headers['content-type']
  74. // using nodejs Buffer to encode the inary to base64
  75. let b64encoded = Buffer.from(results.data, 'binary').toString('base64')
  76. let image = 'data:' + mimeType + ';base64,' + b64encoded
  77. let data = {
  78. mimeType: mimeType,
  79. image: image
  80. }
  81. // uncomment to write image to disk for testing
  82. // const outputFilename = '/tmp/testfile.jpg'
  83. // fs.writeFileSync(outputFilename, results.data)
  84. resolve(data)
  85. })
  86. .catch(error => {
  87. console.error('commandFetchSnapshot error', error)
  88. let results = {
  89. message: 'fetchSnapshot error',
  90. status: 250,
  91. error: error
  92. }
  93. reject(new Error(results))
  94. })
  95. }
  96. else {
  97. let results = {
  98. message: 'Unknown in commandFetchSnapshot',
  99. status: 250
  100. }
  101. reject(new Error(results))
  102. }
  103. }
  104. })
  105. }
  106. }
Add Comment
Please, Sign In to add comment