lib.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. let DebugMode = false, Wines = [], ParamMap = {
  2. ListTimeOut: 60,
  3. DetailTimeOut: 120,
  4. PayTimeOut: 180,
  5. WaitCountDown: 5,
  6. WarnLine: 3000,
  7. DangerLine: 500,
  8. VolumeAdv: 5,
  9. VolumeNormal: 15
  10. }, EventBus = {}, SockEventMap = {"pon": c => EventBus["debug"](`time: ${new Date().toLocaleString()}, pin/pon: ${c}`)},
  11. socket = null, socketTimer = null, socketUrl = "";
  12. // wss://wine.ifarmcloud.com/api/seller/socket, ws://192.168.1.6:3080/seller/socket
  13. const ServerPrefix = "wss://wine.ifarmcloud.com/api/seller/socket";
  14. const Android = (func, ...args) => {
  15. let handler = window.android;
  16. if (handler === undefined) return console.warn("no android handler");
  17. if (handler[func] === undefined) return console.warn(`there is no func named: <${func}>`);
  18. handler[func](...args);
  19. },
  20. Register = (name, func) => window[name] = func,
  21. VmAndroid = () => {
  22. window._weight = 0;
  23. window._timer = null;
  24. window.android = {
  25. pin: window.pon,
  26. onWebMounted: () => window.startApp("EMULATOR32X1X14X0", "v2023.11.01"),
  27. openFrontGate: () => window.frontGateResult(true),
  28. openBackGate: () => window.backGateResult(true),
  29. lightOn: index => console.log(`light on: ${index}`),
  30. lightOff: index => console.log(`light off: ${index}`),
  31. isCupProperlyPlaced: index => {
  32. console.log(`is cup properly placed at ${index}`)
  33. window._weight = 34; // 0.2g
  34. setTimeout(() => window.cupProperlyPlaced(window._weight), 1500);
  35. },
  36. openGas: () => console.log("open gas"),
  37. closeGas: () => console.log("close gas"),
  38. openValve: (index, pulse) => {
  39. console.log(`valve[${index}] opened for: ${pulse}`);
  40. let outed = 0;
  41. window._timer = setInterval(() => {
  42. outed += 30;
  43. window._weight += 48;
  44. if (outed >= pulse) {
  45. clearInterval(window._timer);
  46. window._timer = null;
  47. window.normalFinish();
  48. } else window.volumePulse(outed);
  49. }, 200);
  50. },
  51. closeValve: index => {
  52. console.log(`valve[${index}] closed`);
  53. if (window._timer !== null) clearInterval(window._timer);
  54. window._timer = null;
  55. },
  56. readSteadyWeight: index => {
  57. console.log(`steady weight of ${index}`);
  58. window.steadyWeight(window._weight);
  59. },
  60. setVolume: volume => console.log(`volume will be set to: ${volume}`),
  61. hide: () => console.log("gesture will be hide"),
  62. show: () => console.log("gesture will be show")
  63. };
  64. },
  65. SendWss = (data, ttl) => {
  66. if (socket !== null) return socket.send(JSON.stringify(data));
  67. if (ttl === undefined) ttl = 3;
  68. if (ttl > 0) setTimeout(() => SendWss(data, ttl - 1), 1000);
  69. EventBus["debug"](`event: ${data.event}, ttl: ${ttl}`);
  70. },
  71. ConnectSocket = device => {
  72. if (device !== undefined) socketUrl = `${ServerPrefix}/${device}`;
  73. socket = new WebSocket(socketUrl);
  74. socket.onclose = () => {
  75. socket = null;
  76. ConnectSocket();
  77. }
  78. socket.onopen = () => {
  79. let count = 0;
  80. clearInterval(socketTimer);
  81. socketTimer = setInterval(() => SendWss({event: "pin", data: count++}), 50 * 1000);
  82. }
  83. socket.onmessage = event => {
  84. let body = JSON.parse(event.data);
  85. SockEventMap[body.event] && SockEventMap[body.event](body.data);
  86. }
  87. },
  88. Volume2Weight = (v, d) => Math.floor(v * d / 50),
  89. Weight2Volume = (w, d) => (50 * w / d).toFixed(2),
  90. Weight2Pulse = (w, d, ppv) => Math.floor(50 * w / d * ppv),
  91. Pulse2Volume = (p, ppv) => (p / ppv).toFixed(2),
  92. Pulse2Weight = (p, d, ppv) => (p * d / ppv / 50).toFixed(2);
  93. export default {
  94. Wines, SockEventMap, ParamMap, EventBus, DebugMode,
  95. Android, Register, ConnectSocket, SendWss, VmAndroid,
  96. Volume2Weight, Weight2Volume, Pulse2Volume, Weight2Pulse, Pulse2Weight
  97. }