internal async void Start()
        {
            ShokpodSettings settings = await ShokpodSettings.getSettings();

            int secondsPeriod = settings.LiveTileUpdatePeriod;

            dispatcherTimer          = new Windows.UI.Xaml.DispatcherTimer();
            dispatcherTimer.Tick    += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, secondsPeriod);
            dispatcherTimer.Start();
        }
Exemplo n.º 2
0
        public static async void saveToFile(ShokpodSettings _settings)
        {
            try
            {
                Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile settingsFile = await localFolder.CreateFileAsync(FILE_NAME, CreationCollisionOption.ReplaceExisting);

                string json = JsonConvert.SerializeObject(_settings);
                App.Debug(json);
                await FileIO.WriteTextAsync(settingsFile, json);
            } catch (Exception e)
            {
                App.Debug("Error saving " + FILE_NAME + "." + e.Message);
            }
        }
Exemplo n.º 3
0
        public static async Task <ShokpodSettings> getSettings()
        {
            try
            {
                if (_settings == null)
                {
                    _settings = new ShokpodSettings();
                    Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                    StorageFile settingsFile = await localFolder.CreateFileAsync(FILE_NAME, CreationCollisionOption.OpenIfExists);

                    String settingJson = await FileIO.ReadTextAsync(settingsFile);

                    App.Debug(settingJson);

                    JsonObject settingsObject;
                    if (String.IsNullOrEmpty(settingJson))
                    {
                        settingsObject = new JsonObject();
                    }
                    else
                    {
                        settingsObject = JsonObject.Parse(settingJson);
                    }
                    try
                    {
                        _settings.ShokpodApiLocation     = settingsObject.GetNamedString("ShokpodApiLocation");
                        _settings.LiveTileUpdatePeriod   = (int)settingsObject.GetNamedNumber("LiveTileUpdatePeriod");
                        _settings.ServerImpactThreshhold = settingsObject.GetNamedNumber("ServerImpactThreshhold");
                        _settings.DisplayAcceleration    = settingsObject.GetNamedBoolean("DisplayAcceleration");
                    } catch (Exception e)
                    {
                        App.Debug("Error reading " + FILE_NAME + "." + e.Message);
                        //_settings.ShokpodApiLocation = @"http://shokpod.australiaeast.cloudapp.azure.com:8080";
                        _settings.ShokpodApiLocation = @"http://10.10.47.13:8080";

                        _settings.LiveTileUpdatePeriod   = 5;
                        _settings.ServerImpactThreshhold = 10;
                        _settings.DisplayAcceleration    = false;
                        saveToFile(_settings);
                    }
                }
            }
            catch (Exception e)
            {
                App.Debug("Error reading " + FILE_NAME + "." + e.Message);
            }
            return(_settings);
        }
 private void sendBufferedServerRecords()
 {
     ShokpodSettings settings = ShokpodSettings.getSettings().Result;
     MovementRecord recordItem;
     App.Debug("Found " + remoteServerQueue.Count + " records queued for remote server.");
     while (!remoteServerQueue.IsEmpty && remoteServerQueue.TryPeek(out recordItem))
     {
         var itemAsJson = JsonConvert.SerializeObject(recordItem);
         var content = new StringContent(itemAsJson);
         content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
         using (var httpClient = new HttpClient())
         {
             httpClient.Timeout.Add(TimeSpan.FromSeconds(5));
             HttpResponseMessage response = httpClient.PostAsync(settings.ShokpodApiLocation + "/records", content).Result;
             if (response.IsSuccessStatusCode)
             {
                 String json = response.Content.ReadAsStringAsync().Result;
                 var pushServerResponse = JsonConvert.DeserializeAnonymousType(json, new
                 {
                     type = true,
                     data = ""
                 });
                 App.Debug(pushServerResponse.data);
                 OnRecordingEvent(this, new RecordingQueueEventArgs
                 {
                     Record = recordItem.Recording[0]
                 });
                 remoteServerQueue.TryDequeue(out recordItem);
             }
             else
             {
                 OnRecordingEvent(this, new RecordingQueueEventArgs
                 {
                     Response = response
                 });
                 break;
             }
         }
     }
 }
        private async Task<MovementRecord> maximumImpact(MovementRecord record)
        {
            MovementRecord result = null;
            SingleRecord maximumImpact = new SingleRecord();
            foreach (SingleRecord singleRecord in record.Recording)
            {
                double _acceleration = singleRecord.Value.Acceleration;
                if (singleRecord.Value.Acceleration > maximumImpact.Value.Acceleration)
                {
                    maximumImpact = singleRecord;
                }
            }
            ShokpodSettings settings = await ShokpodSettings.getSettings();
            double serverReportingThreshold = settings.ServerImpactThreshhold;

            if (maximumImpact.Value.Acceleration > serverReportingThreshold)
            {
                result = new MovementRecord();
                result.AssignedName = record.AssignedName;
                result.DeviceAddress = record.DeviceAddress;
                result.Recording.Add(maximumImpact);
                OnRecordingEvent(this, new RecordingQueueEventArgs
                {
                    MaximumImpact = maximumImpact
                });
            } else
            {
                App.Debug(maximumImpact.Value.Acceleration + "G is below threshold of " + serverReportingThreshold + "G, not recording it.");
            }
            if(result != null)
            {
                MaximumImpact = maximumImpact.Value.Acceleration;
            } else
            {
                MaximumImpact = 0;
            }
            return result;
        }
        public static async void ToastAsync(string msg)
        {
            ShokpodSettings settings = await ShokpodSettings.getSettings();

            if (settings.DisplayAcceleration)
            {
                string xml = $@"
                <toast activationType='foreground' launch='args'>
                    <visual>
                        <binding template='ToastGeneric'>
                            <text>This is a toast notification</text>
                            <text>{msg}</text>
                        </binding>
                    </visual>
                </toast>";

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);

                ToastNotification notification = new ToastNotification(doc);
                ToastNotifier     notifier     = ToastNotificationManager.CreateToastNotifier();
                notifier.Show(notification);
            }
        }
        private void sendDeviceRecords(List<MovementRecord> recordsToSend)
        {
            ShokpodSettings settings = ShokpodSettings.getSettings().Result;
            Dictionary<string, MovementRecord> groupedByDeviceAddress = new Dictionary<string, MovementRecord>();
            foreach (MovementRecord record in recordsToSend)
            {
                MovementRecord currentCollatingRecord;
                if (!groupedByDeviceAddress.TryGetValue(record.DeviceAddress, out currentCollatingRecord))
                {
                    currentCollatingRecord = new MovementRecord();
                    currentCollatingRecord.AssignedName = record.AssignedName;
                    currentCollatingRecord.DeviceAddress = record.DeviceAddress;
                    groupedByDeviceAddress[record.DeviceAddress] = currentCollatingRecord;
                }
                currentCollatingRecord.Recording.AddRange(record.Recording);
            }

            foreach (MovementRecord recordItem in groupedByDeviceAddress.Values)
            {
                MovementRecord record = maximumImpact(recordItem).Result;
                if (record != null)
                {
                    var itemAsJson = JsonConvert.SerializeObject(record);
                    var content = new StringContent(itemAsJson);
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    try
                    {
                        using (var httpClient = new HttpClient())
                        {
                            httpClient.Timeout.Add(TimeSpan.FromSeconds(5));
                            HttpResponseMessage response = httpClient.PostAsync(settings.ShokpodApiLocation + "/records", content).Result;
                            if (response.IsSuccessStatusCode)
                            {
                                String json = response.Content.ReadAsStringAsync().Result;
                                var pushServerResponse = JsonConvert.DeserializeAnonymousType(json, new
                                {
                                    type = true,
                                    data = ""
                                });
                                App.Debug(pushServerResponse.data);
                                OnRecordingEvent(this, new RecordingQueueEventArgs {
                                    Record = record.Recording[0]
                                });
                            }
                            else
                            {
                                OnRecordingEvent(this, new RecordingQueueEventArgs
                                {
                                    Response = response
                                });
                                AddToRemoteServerQueue(record);
                                App.Debug("Added failed record to the remote server queue: '" + record.Recording[0].Value.Acceleration + "G.'");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        OnRecordingEvent(this, new RecordingQueueEventArgs
                        {
                            Exception = e
                        });
                    }
                }
            }
        }