meet.blade.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. @extends('layouts.meeting')
  2. @section('content')
  3. <div id="meetComponent">
  4. {{--<h5 class="bg-dark font-weight-bold text-white m-0 py-3 px-4 d-flex">
  5. <span>Meeting</span>
  6. <span class="ml-auto" v-if="!started">
  7. Connecting...
  8. </span>
  9. <span class="ml-auto" v-if="started">
  10. <i class="fa fa-clock mr-1 text-light"></i>
  11. @{{ timeDisplay() }}
  12. </span>
  13. </h5>--}}
  14. @if(!$guest)
  15. <div class="d-flex align-items-center justify-content-center py-3 border-bottom">
  16. <span class="mr-3">
  17. {{ $pro->name_display }} | {{ $pro->is_video_visit_assistant ? 'ASSISTANT' : 'MCP' }}
  18. </span>
  19. <button class="btn btn-sm btn-primary px-4 font-weight-bold"
  20. v-on:click.prevent="nextPatient()"
  21. :disabled="client || checkingForNextPatient || started">Next Patient</button>
  22. <span v-if="patientInQueue && !started" class="patient-in-q-alert text-warning text-sm ml-2 small">
  23. <i class="fa fa-circle"></i>
  24. </span>
  25. <span v-if="!patientInQueue && !started" class="text-success text-sm ml-2 small">
  26. <i class="fa fa-circle"></i>
  27. </span>
  28. </div>
  29. <div v-if="!started && noNextPatient" class="bg-light rounded text-center py-1 font-weight-bold text-sm my-3 mx-3">@{{ noNextPatient }}</div>
  30. @endif
  31. <div class="">
  32. <div class="py-3 text-center" v-if="started">
  33. <h6 class="text-black font-weight-bold m-0">Call in progress: @{{ timeDisplay() }}</h6>
  34. </div>
  35. @if($guest)
  36. <div class="py-3 text-center" v-if="!pro || !started">
  37. <h6 class="text-black font-weight-bold m-0">Please wait. Your doctor will be with you shortly...</h6>
  38. </div>
  39. @endif
  40. <div class="main-view mx-auto" <?= !$guest ? 'v-show="!!client"' : '' ?>>
  41. <div id="self-view" class="full-view" data-type="{{ $guest ? 'CLIENT' : 'PRO' }}"></div>
  42. <div class="thumbs">
  43. <div id="remote-view-1" class="remote-view disconnected-view"
  44. data-stream="" data-from="" data-type=""></div>
  45. <div id="remote-view-2" class="remote-view disconnected-view"
  46. data-stream="" data-from="" data-type=""></div>
  47. </div>
  48. <button class="btn btn-danger rounded-circle hang-up"
  49. v-if="started"
  50. title="Leave Call"
  51. v-on:click.prevent="hangUp()">
  52. <i class="fa fa-phone"></i>
  53. </button>
  54. <button class="btn btn-success rounded-circle call-mcp"
  55. v-if="selfUserType === 'ASSISTANT'"
  56. title="Call MCP Pro"
  57. v-on:click.prevent="callMCPPro()">
  58. <i class="fa fa-user-md"></i>
  59. </button>
  60. </div>
  61. </div>
  62. </div>
  63. <script>
  64. new Vue({
  65. el: '#meetComponent',
  66. delimiters: ['@{{', '}}'],
  67. data: {
  68. time: 0,
  69. startTime: 0,
  70. started: false,
  71. client: false,
  72. pro: false,
  73. selfName: '',
  74. selfToken: '',
  75. @if($guest)
  76. clientUid: '',
  77. checkInToken: '',
  78. @endif
  79. otSessionId: '',
  80. @if(!$guest)
  81. checkingForNextPatient: false,
  82. noNextPatient: false,
  83. @endif
  84. otSession: false,
  85. selfUserType: false,
  86. patientInQueue: false,
  87. },
  88. methods: {
  89. @if(!$guest)
  90. pollForNextPatient: function() {
  91. if(!this.started) {
  92. this.nextPatient(true);
  93. }
  94. },
  95. nextPatient: function(_pollOnly = false) {
  96. var self = this;
  97. if(!_pollOnly) this.checkingForNextPatient = true;
  98. $.post('/api/client/getNextClientForVideoVisit', {}, function(_data) {
  99. if(_pollOnly) {
  100. self.patientInQueue = _data.success;
  101. }
  102. else {
  103. self.checkingForNextPatient = false;
  104. if(!_data.success) {
  105. self.noNextPatient = _data.message;
  106. window.setTimeout(function() {
  107. self.noNextPatient = false;
  108. }, 2000);
  109. }
  110. else {
  111. // get ot session key from client record
  112. self.client = true;
  113. self.clientUid = _data.data;
  114. self.getOpenTokSessionId(function() {
  115. self.selfName = '{{ $pro->name_display }}';
  116. $.post('/api/openTok/getClientToken', {
  117. opentokSessionId: self.otSessionId,
  118. name: self.selfName
  119. }, function (_data) {
  120. self.selfToken = _data.data;
  121. self.initOpenTok();
  122. });
  123. });
  124. }
  125. }
  126. }, 'json');
  127. },
  128. @endif
  129. getInitials: function(_name) {
  130. var parts = _name.split(/\s+/g);
  131. parts = parts.map(_part => _part[0]);
  132. return parts.join('');
  133. },
  134. timeDisplay: function() {
  135. var seconds = this.time / 1000,
  136. minutes = parseInt(seconds / 60, 10);
  137. seconds = parseInt(seconds % 60, 10);
  138. return minutes + " min, " + seconds + " sec";
  139. },
  140. hangUp: function() {
  141. if(this.otSession) {
  142. this.otSession.disconnect();
  143. this.otSession = false;
  144. this.otSessionId = '';
  145. this.started = false;
  146. this.startTime = false;
  147. @if(!$guest)
  148. this.client = false;
  149. @else
  150. window.location = '/join';
  151. @endif
  152. }
  153. },
  154. @if(!$guest)
  155. callMCPPro: function() {
  156. // put client in mcp queue
  157. $.ajax({
  158. type: 'post',
  159. url: '/api/clientVideoVisit/PutVideoVisitInMcpQueue',
  160. headers: {
  161. 'sessionKey': localStorage.sessionKey
  162. },
  163. data: {uid: this.clientUid},
  164. dataType: 'json'
  165. })
  166. .done(function (_data) {
  167. console.log(_data);
  168. if(_data.success) {
  169. new Noty({
  170. theme: 'mint',
  171. type: 'success',
  172. text: 'Client added to MCP call queue',
  173. progressBar: false,
  174. timeout: 1500,
  175. }).show();
  176. }
  177. else {
  178. new Noty({
  179. theme: 'mint',
  180. type: 'alert',
  181. text: _data.message,
  182. progressBar: false,
  183. timeout: 3000,
  184. }).show();
  185. }
  186. })
  187. .fail(function (_data) {
  188. console.log(_data);
  189. // alert(_data.message);
  190. });
  191. },
  192. @endif
  193. // OT methods
  194. initOpenTok: function() {
  195. /* fake video feed (temp) */
  196. const randomColour = () => {
  197. return Math.round(Math.random() * 255);
  198. };
  199. const canvas = document.createElement('canvas');
  200. canvas.width = 640;
  201. canvas.height = 480;
  202. const ctx = canvas.getContext('2d');
  203. var pos = 100;
  204. window.setInterval(function() {
  205. ctx.clearRect(0, 0, canvas.width, canvas.height);
  206. ctx.font = "20px Georgia";
  207. ctx.fillStyle = `rgb(220, 220, 220)`;
  208. var userType = '<?= $guest? "Client" : "Pro" ?>';
  209. ctx.fillText("Video feed from the " + userType, 20, pos);
  210. pos += 5;
  211. if(pos > canvas.height) pos = 100;
  212. }, 1000);
  213. var self = this;
  214. var apiKey = '<?= env('TOKBOX_API_KEY', '46678902') ?>';
  215. var sessionId = this.otSessionId;
  216. var token = this.selfToken;
  217. // destroy if existing
  218. self.hangUp();
  219. self.otSession = OT.initSession(apiKey, sessionId);
  220. // peer connected
  221. self.otSession.on('streamCreated', function streamCreated(event) {
  222. var subscriberOptions = {
  223. insertMode: 'append',
  224. width: '100%',
  225. height: '100%'
  226. };
  227. var remoteViewElem = 'remote-view-1';
  228. if($('#remote-view-1').attr('data-stream')) {
  229. remoteViewElem = 'remote-view-2';
  230. }
  231. self.otSession.subscribe(event.stream, remoteViewElem, subscriberOptions, self.handleOpenTokError);
  232. remoteViewElem = $('#' + remoteViewElem);
  233. remoteViewElem.attr('data-stream', event.stream.id);
  234. remoteViewElem.attr('data-from', event.stream.connection.data.split('|')[0]);
  235. var userType = event.target.connection.data.split('|')[1];
  236. if(userType === 'CLIENT') {
  237. remoteViewElem.attr('data-type', 'CLIENT');
  238. }
  239. else {
  240. remoteViewElem.attr('data-type', 'PRO');
  241. }
  242. @if($guest)
  243. self.pro = true;
  244. @else
  245. self.client = true;
  246. @endif
  247. if(!self.startTime) {
  248. self.startTime = new Date().getTime();
  249. window.setInterval(function() {
  250. self.time = new Date().getTime() - self.startTime;
  251. }, 1000);
  252. self.started = true;
  253. }
  254. self.activateParty(event.stream.id);
  255. });
  256. // peer disconnected
  257. self.otSession.on("streamDestroyed", function(event) {
  258. self.activateParty('self');
  259. var remoteViewElem = $('[data-stream="' + event.stream.id + '"]');
  260. if(remoteViewElem.length) {
  261. remoteViewElem.attr('data-stream', '');
  262. remoteViewElem.attr('data-from', '');
  263. }
  264. @if($guest)
  265. self.pro = false;
  266. @else
  267. self.client = false;
  268. @endif
  269. // if no other parties in call, hang up
  270. if(!$('[data-stream]:not([data-stream=""])').length) {
  271. self.hangUp();
  272. }
  273. });
  274. // self connected
  275. self.otSession.on("sessionConnected", function(event) {
  276. console.log(event);
  277. self.selfUserType = event.target.connection.data.split('|')[1];
  278. @if(!$guest)
  279. self.joinMeetingAsPro(self.selfUserType);
  280. @endif
  281. });
  282. // self disconnected
  283. self.otSession.on('sessionDisconnected', function sessionDisconnected(event) {
  284. console.log('You were disconnected from the session.', event.reason);
  285. });
  286. // initialize the publisher
  287. var publisherOptions = {
  288. videoSource: canvas.captureStream(1).getVideoTracks()[0],
  289. insertMode: 'append',
  290. width: '100%',
  291. height: '100%',
  292. };
  293. var publisher = OT.initPublisher('self-view', publisherOptions, self.handleOpenTokError);
  294. publisher.on('streamCreated', function(event) {
  295. var selfView = $('#self-view');
  296. selfView.attr('data-stream', event.stream.id);
  297. selfView.attr('data-from', event.stream.connection.data.split('|')[0]);
  298. @if($guest)
  299. selfView.attr('data-type', 'CLIENT');
  300. @else
  301. selfView.attr('data-type', 'PRO');
  302. @endif
  303. });
  304. // Connect to the session
  305. self.otSession.connect(token, function callback(error) {
  306. if (error) {
  307. self.handleOpenTokError(error);
  308. } else {
  309. // If the connection is successful, publish the publisher to the session
  310. self.otSession.publish(publisher, self.handleOpenTokError);
  311. }
  312. });
  313. },
  314. handleOpenTokError: function(e) {
  315. },
  316. getClientCheckinToken: function(_done) {
  317. var self = this;
  318. $.get('/get-client-checkin-token/' + this.clientUid, function(_data) {
  319. console.log(_data);
  320. self.checkInToken = _data.data;
  321. _done();
  322. }, 'json');
  323. },
  324. getOpenTokSessionId: function(_done) {
  325. var self = this;
  326. @if($guest)
  327. $.ajax({
  328. type: 'post',
  329. url: '/api/clientVideoVisit/startVideoVisitAsClient',
  330. headers: {
  331. 'sessionKey': localStorage.sessionKey
  332. },
  333. data: {checkInToken: this.checkInToken},
  334. dataType: 'json'
  335. })
  336. .done(function (_data) {
  337. console.log(_data);
  338. if(_data.success) {
  339. self.otSessionId = _data.data;
  340. _done();
  341. }
  342. else {
  343. alert(_data.message);
  344. }
  345. })
  346. .fail(function (_data) {
  347. console.log(_data);
  348. alert(_data.message);
  349. });
  350. @else
  351. $.get('/pro/get-opentok-session-key/' + self.clientUid, function(_data) {
  352. self.otSessionId = _data.data;
  353. _done();
  354. }, 'json');
  355. @endif
  356. },
  357. @if(!$guest)
  358. joinMeetingAsPro: function(_type) {
  359. var self = this, endPoint = '';
  360. if(_type === 'ASSISTANT') {
  361. endPoint = 'joinVideoVisitAsAssistantPro';
  362. }
  363. else {
  364. endPoint = 'joinVideoVisitAsMcpPro';
  365. }
  366. $.ajax({
  367. type: 'post',
  368. url: '/api/clientVideoVisit/' + endPoint,
  369. headers: {
  370. 'sessionKey': localStorage.sessionKey
  371. },
  372. data: {uid: self.clientUid},
  373. dataType: 'json'
  374. })
  375. .done(function (_data) {
  376. console.log(_data);
  377. })
  378. .fail(function (_data) {
  379. console.warn(_data);
  380. alert(_data.message);
  381. });
  382. },
  383. @endif
  384. activateParty: function(_stream = 'self') {
  385. var current = $('.full-view');
  386. if(current.attr('data-stream') === _stream) return;
  387. current.removeClass('full-view').addClass('thumb-view');
  388. if(current.attr('data-type') === 'CLIENT') {
  389. current.prependTo('.thumbs');
  390. }
  391. else {
  392. current.appendTo('.thumbs');
  393. }
  394. if(_stream === 'self') {
  395. $('#self-view')
  396. .removeClass('thumb-view')
  397. .removeClass('disconnected-view')
  398. .addClass('full-view')
  399. .prependTo('.main-view');
  400. }
  401. else {
  402. $('div[data-stream="' + _stream + '"]')
  403. .removeClass('thumb-view')
  404. .removeClass('disconnected-view')
  405. .addClass('full-view')
  406. .prependTo('.main-view');
  407. }
  408. }
  409. },
  410. mounted: function() {
  411. var self = this;
  412. @if($guest)
  413. this.clientUid = localStorage.clientUid;
  414. this.getClientCheckinToken(function() { // get client check-in token
  415. self.getOpenTokSessionId(function() { // get opentok session id
  416. var name = [];
  417. if (localStorage.clientFirstName) name.push(localStorage.clientFirstName);
  418. if (localStorage.clientLastName) name.push(localStorage.clientLastName);
  419. this.selfName = name.join(' ');
  420. $.ajax({
  421. type: 'post',
  422. url: '/api/openTok/getClientToken',
  423. headers: {
  424. 'sessionKey': localStorage.sessionKey
  425. },
  426. data: {
  427. opentokSessionId: self.otSessionId,
  428. name: name.join(' ')
  429. },
  430. dataType: 'json'
  431. })
  432. .done(function (_data) {
  433. console.log(_data);
  434. self.selfToken = _data.data;
  435. self.initOpenTok();
  436. })
  437. .fail(function (_data) {
  438. console.warn(_data);
  439. alert(_data.message);
  440. });
  441. });
  442. });
  443. @else
  444. localStorage.sessionKey = '{{ $session->session_key }}';
  445. @endif
  446. $(document).on('click', '.thumbs>div[data-stream]', function() {
  447. self.activateParty($(this).attr('data-stream'));
  448. return false;
  449. });
  450. @if(!$guest)
  451. // poll for new patients and alert
  452. window.setInterval(function() {
  453. self.pollForNextPatient();
  454. }, 5000);
  455. @endif
  456. }
  457. });
  458. </script>
  459. @endsection