Brain4itPostController.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Brain4itPostController.js
  3. *
  4. * @author realor
  5. */
  6. import { Controller } from './Controller.js'
  7. import { Brain4it } from '../lib/Brain4it.js'
  8. class Brain4itPostController extends Controller {
  9. constructor(object, name) {
  10. super(object, name)
  11. this.url = ''
  12. this.module = 'your_module'
  13. this.accessKey = 'access_key'
  14. this.func = '@function_to_call'
  15. this.input = 0
  16. this.autoStart = false
  17. this._onNodeChanged = this.onNodeChanged.bind(this)
  18. this._value = null
  19. }
  20. onStart() {
  21. this.application.addEventListener('scene', this._onNodeChanged, false)
  22. }
  23. onStop() {
  24. this.application.removeEventListener('scene', this._onNodeChanged, false)
  25. }
  26. onNodeChanged(event) {
  27. if (event.type === 'nodeChanged' && this.hasChanged(event)) {
  28. let value = this.input
  29. if (value !== this._value) {
  30. this.postData(value)
  31. }
  32. }
  33. }
  34. postData(value) {
  35. this._value = value
  36. let url = this.url
  37. if (url.trim().length === 0) return
  38. if (url[0] === '/') {
  39. url = document.location.protocol + '//' + document.location.host + url
  40. }
  41. let module = this.module
  42. let func = this.func
  43. console.info('POST ' + url + ' -> ' + value)
  44. let client = new Brain4it.Client(url, module + '/' + func)
  45. client.send(value)
  46. }
  47. }
  48. Controller.addClass(Brain4itPostController)
  49. export { Brain4itPostController }