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(); }
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 }); } } } }