util.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : `0${n}`
  13. }
  14. const promisify = (api) => {
  15. return (options, ...params) => {
  16. return new Promise((resolve, reject) => {
  17. api(Object.assign({}, options, { success: resolve, fail: reject }), ...params);
  18. });
  19. }
  20. }
  21. const BeaconUtils = {
  22. // 计算距离
  23. calculateAccuracy:function(txPower, rssi, n) {
  24. // return (0.89976) * Math.pow(rssi / txPower, 7.7095) + 0.111
  25. return Math.pow(10, Math.abs(rssi - txPower) / (10 * n))
  26. },
  27. //求数组平均值
  28. arrayAverage:function (arr) {
  29. let tmp = arr.reduce((acc, val) => acc + val, 0) / arr.length
  30. return tmp
  31. },
  32. //求一维队列某点的高斯模糊权重 @param(队列长度,目标位置, 平均差)
  33. getOneGuassionArray: function(size, kerR, sigma) {
  34. if (size % 2 > 0) {
  35. size -= 1
  36. }
  37. if (!size) {
  38. return []
  39. }
  40. if (kerR > size-1){
  41. return []
  42. }
  43. let sum = 0;
  44. let arr = new Array(size);
  45. for (let i = 0; i < size; i++) {
  46. arr[i] = Math.exp(-((i - kerR) * (i - kerR)) / (2 * sigma * sigma));
  47. sum += arr[i];
  48. }
  49. return arr.map(e => e / sum);
  50. }
  51. }
  52. module.exports = {
  53. formatTime,
  54. promisify,
  55. BeaconUtils
  56. }