var Helper = { "data" : "", "fixedData": new Array(), fetchData: function(testName){ let el = document.getElementById('message'); el.innerHTML = ''; el.style.display = "none"; $.post("http://localhost/iot/api/getData.php", {name: testName},function (data, status) { Helper.fixedData = new Array(); Helper.data = data; Helper.data.splice(0, 1); Helper.data.splice(Helper.data.length - 1, 1) var arrayLength = Helper.data.length; for (var i = 0; i < arrayLength; i++) { let objStr = Helper.data[i]; let obj = objStr.slice(0, -4); obj = obj.slice(3); try{ tranformeredObj = JSON.parse(obj); if (Helper.validateObj(tranformeredObj)){ Helper.fixedData.push(tranformeredObj); } } catch(error){ console.log("Record "+ i + " can't be converted.."); } } if (Helper.fixedData.length > 0){ el.innerHTML = 'Data for test: ' + testName +' loaded!'; el.style.display = "inline-block"; el.style.color = "#20d420"; } else if (testName && testName.length > 0){ el.innerHTML = 'Could not load data for test: ' + testName; el.style.display = "inline-block"; el.style.color = "red"; } }).fail(function (status) { el.innerHTML='Could not load data for test: '+testName; el.style.display = "inline-block"; el.style.color = "red"; }); }, validateObj: function(obj){ if (typeof obj.A.X === 'undefined' || isNaN(obj.A.X)){ return false; } if (typeof obj.A.Y === 'undefined' || isNaN(obj.A.Y)){ return false; } if (typeof obj.A.Z === 'undefined' || isNaN(obj.A.Z)){ return false; } if (typeof obj.M.X === 'undefined' || isNaN(obj.M.X)){ return false; } if (typeof obj.M.Y === 'undefined' || isNaN(obj.M.Y)){ return false; } if (typeof obj.M.Z === 'undefined' || isNaN(obj.M.Z)){ return false; } if (typeof obj.G.X === 'undefined' || isNaN(obj.G.X)){ return false; } if (typeof obj.G.Y === 'undefined' || isNaN(obj.G.Y)){ return false; } if (typeof obj.G.Z === 'undefined' || isNaN(obj.G.Z)){ return false; } return true; }, startSimulation: function(){ if(Helper.fixedData.length > 0){ setup(); start = true; } } }