Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (!Array.prototype.last) { Array.prototype.last = function() { return this[this.length - 1]; }
- var historicMotion = { "x": [], "y": [], "z": [] }
- var historicOrientation = { "x": [], "y": [], "z": [] }
- function setStatus(status) {
- if(ga) { ga('set', 'dimension1', status); } //a se schimba dimensiunea cu cea necesara
- }
- function updateStatus() {
- let movement = mostRecentMovementOverall(75)
- if (mostRecentMovementOverall(4000) > 40) { // TODO: haven't tested this, 1,000 so it's a longer time
- setStatus("conduce")
- } else if (historicOrientation["z"].last() > 70 || historicOrientation["z"].last() < -70) {
- setStatus("Tolanit in pat")
- } else if (historicOrientation["y"].last() > 160 || historicOrientation["y"].last() < -160) {
- setStatus("Tolanit in pat")
- } else if (historicOrientation["y"].last() >= 30 && historicOrientation["y"].last() < 70) {
- if (movement > 18) {
- setStatus("In miscare")
- } else {
- setStatus("Sta pe loc in picioare")
- }
- } else if (historicOrientation["y"].last() >= 70 && historicOrientation["y"].last() < 95) {
- if (movement > 18) {
- setStatus("In Miscare")
- } else {
- setStatus("Pozitie de poza")
- }
- } else if (historicOrientation["y"].last() >= 95 && historicOrientation["y"].last() < 120) {
- setStatus("Pozitie de poza")
- } else if (Math.round(historicOrientation["z"].last()) == 0 && Math.round(historicOrientation["y"].last()) == 0) {
- setStatus("Telefon pe masa")
- } else {
- if (movement > 18) {
- setStatus("In miscare")
- } else {
- setStatus("Sta pe loc in picioare")
- }
- }
- }
- function mostRecentMovementOverall(numberOfHistoricPoints) {
- return (mostRecentMovement(historicMotion["x"], numberOfHistoricPoints, true) +
- mostRecentMovement(historicMotion["y"], numberOfHistoricPoints, true) +
- mostRecentMovement(historicMotion["z"], numberOfHistoricPoints, true)) / 3.0
- }
- // numberOfHistoricPoints: 100 is about 3 seconds
- function mostRecentMovement(array, numberOfHistoricPoints, removeNegatives) {
- if (array.length > numberOfHistoricPoints) {
- totalSum = 0
- for (var toCount = 0; toCount < numberOfHistoricPoints; toCount++) {
- currentElement = array[array.length - toCount - 1]
- currentElement *= (1 - toCount / numberOfHistoricPoints) // weight the most recent data more
- if (currentElement < 0 && removeNegatives) currentElement = currentElement * -1
- if (currentElement > 0.1 || currentElement < -0.1) totalSum += currentElement
- }
- return totalSum * 100 / numberOfHistoricPoints
- }
- return 0 // not enough data yet
- }
- var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android)/);
- if (isMobile != null) {
- window.addEventListener("devicemotion", motion, false);
- function motion(event) {
- historicMotion["x"].push(event.acceleration.x)
- historicMotion["y"].push(event.acceleration.y)
- historicMotion["z"].push(event.acceleration.z)
- }
- window.addEventListener("deviceorientation", orientation, false);
- function orientation(event) {
- historicOrientation["x"].push(event.alpha)
- historicOrientation["y"].push(event.beta)
- historicOrientation["z"].push(event.gamma)
- }
- setTimeout(updateStatus, 500)
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement