123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- const formatTime = date => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
- }
- const formatNumber = n => {
- n = n.toString()
- return n[1] ? n : `0${n}`
- }
- const promisify = (api) => {
- return (options, ...params) => {
- return new Promise((resolve, reject) => {
- api(Object.assign({}, options, { success: resolve, fail: reject }), ...params);
- });
- }
- }
- const BeaconUtils = {
-
- // 计算距离
- calculateAccuracy:function(txPower, rssi, n) {
- // return (0.89976) * Math.pow(rssi / txPower, 7.7095) + 0.111
- return Math.pow(10, Math.abs(rssi - txPower) / (10 * n))
- },
- //求数组平均值
- arrayAverage:function (arr) {
- let tmp = arr.reduce((acc, val) => acc + val, 0) / arr.length
- return tmp
- },
- //求一维队列某点的高斯模糊权重 @param(队列长度,目标位置, 平均差)
- getOneGuassionArray: function(size, kerR, sigma) {
- if (size % 2 > 0) {
- size -= 1
- }
- if (!size) {
- return []
- }
- if (kerR > size-1){
- return []
- }
- let sum = 0;
- let arr = new Array(size);
- for (let i = 0; i < size; i++) {
- arr[i] = Math.exp(-((i - kerR) * (i - kerR)) / (2 * sigma * sigma));
- sum += arr[i];
- }
- return arr.map(e => e / sum);
- }
- }
- module.exports = {
- formatTime,
- promisify,
- BeaconUtils
- }
|