/// <summary> /// keeps the sqlite log DB in sync with the local in-memory object /// </summary> private async Task SyncMessageListAsync() { // allow only one poll of this type at a time if (MessageDBSync == true) { return; } MessageDBSync = true; try { // get channels from MednaNet API Channels = (await Client.Channels.GetChannels()).ToList(); // update visual handler once await has completed MessageDBSync = false; // update channel list foreach (var c in Channels.OrderBy(a => a.discordId).ToList()) { DVH.channels.UpdateChannel(c.id, c.channelName); } // Update the UI DVH.UpdateChannelButtons(); // make sure at least one channel is selected DVH.CheckChannelSelection(); } catch (Exception ex) { APIDisconnected(ex); return; } }
/// <summary> /// retreives an updated channellist from the mednanet API asyncronously /// once the await has completed DiscordVisualHandler will asyncronously update the channels UI /// </summary> private async void GetChannelsAsync() { // allow only one poll of this type at a time if (ChannelsIsPolling == true) { return; } ChannelsIsPolling = true; try { // get channels from MednaNet API Channels = (await Client.Channels.GetChannels()).ToList(); // update visual handler once await has completed ChannelsIsPolling = false; // update channel list foreach (var c in Channels.OrderBy(a => a.discordId).ToList()) { DVH.channels.UpdateChannel(c.id, c.channelName); } // add invisible channel with ID 0 (for initial display) DVH.channels.UpdateChannel(0, "initChannel"); // Update the UI DVH.UpdateChannelButtons(); // make sure at least one channel is selected DVH.CheckChannelSelection(); } catch (Exception ex) { APIDisconnected(ex); return; } }
/// <summary> /// retreives an updated userlist from the mednanet API asyncronously /// once the await has completed DiscordVisualHandler will asyncronously update the users UI /// </summary> private async void GetUsersAsync() { // allow only one poll of this type at a time if (UsersIsPolling == true) { return; } UsersIsPolling = true; try { // get users from MednaNet API Users = await Client.Users.GetAllUsers(); // update visual handler once await has completed UsersIsPolling = false; foreach (var u in Users) { ClientType ct = ClientType.discord; if (u.discordId == null) { ct = ClientType.medlaunch; } DVH.users.UpdateUser(u.id, u.username, ct, true); } DVH.UpdateUsers(); } catch (Exception ex) { APIDisconnected(ex); return; } }
public void Run(User user, Patient patient, Image image, StructureSet structureSet, PlanSetup planSetup, IEnumerable <PlanSetup> planSetups, IEnumerable <PlanSum> planSums, Window window) { // //Dose profiles // var prof = Profile.getBeamDoseProfile(planSetup.Beams.First(), new VVector(1.0, 0, 0), distanceFromIsocenterTowardSourceInmm: 30); Helpers.DumpIntoMatlab(prof.Select(val => double.IsNaN(val.Value) ? 0 : val.Value).ToArray(), new int[] { prof.Count }, "orthogonalDoseProfile"); var dprof = Profile.getBeamDepthDoseProfileAlongBeamAxis(structureSet.Structures.Single(st => st.Id == "BODY"), planSetup.Beams.ToArray()[4]); Helpers.DumpIntoMatlab(dprof.Select(val => double.IsNaN(val.Value) ? 0 : val.Value).ToArray(), new int[] { dprof.Count }, "depthdoseProfile"); var dprof2 = Profile.getBeamDoseProfile(planSetup.Beams.First(), new VVector(0, -1.0, 0)); Helpers.DumpIntoMatlab(dprof2.Select(val => double.IsNaN(val.Value) ? 0 : val.Value).ToArray(), new int[] { dprof2.Count }, "depthdoseProfile2"); // // Dose plane // var plane = Plane.OrthogonalDoseCrossSection(planSetup.Beams.ToArray()[4]); var planeAsArray = new double[plane.Length]; Buffer.BlockCopy(plane, 0, planeAsArray, 0, plane.Length * sizeof(double)); Helpers.DumpIntoMatlab(planeAsArray, new int[] { plane.GetLength(1), plane.GetLength(0) }, "orthogonalPlane"); // // Image profiles // var(xProf, yProf, zProf) = Profile.getImageProfilesThroughIsocenter(planSetup); Helpers.DumpIntoMatlab(xProf.Select(val => double.IsNaN(val.Value) ? 0 : val.Value).ToArray(), new int[] { xProf.Count }, "XProfile"); Helpers.DumpIntoMatlab(xProf.Select(val => double.IsNaN(val.Value) ? 0 : val.Value).ToArray(), new int[] { xProf.Count }, "XProfile"); Helpers.DumpIntoMatlab(zProf.Select(val => double.IsNaN(val.Value) ? 0 : val.Value).ToArray(), new int[] { zProf.Count }, "ZProfile"); // // DVH with dose profiles. // planSetup.DoseValuePresentation = DoseValuePresentation.Absolute; var p2 = patient.Courses.Single(cr => cr.Id == planSetup.Course.Id).PlanSetups.Single(pl => pl.Id == "Plan2"); p2.DoseValuePresentation = DoseValuePresentation.Absolute; var doses = new Dose[] { planSetup.Dose //p2.Dose }; var(cDVH, dDVH, sampleCoverage) = DVH.StructureDVH(doses, structureSet.Structures.Single(st => st.Id == "CTV"), new DVH.NoConversion()); Helpers.DumpIntoMatlab(Helpers.into1DArray(dDVH.Select(val => new double[] { val.doseValue, val.Volume }).ToArray()), new int[] { 2, dDVH.Length }, "dDVH"); Helpers.DumpIntoMatlab(Helpers.into1DArray(cDVH.Select(val => new double[] { val.doseValue, val.Volume }).ToArray()), new int[] { 2, cDVH.Length }, "cDVH"); return; }
public void APIDisconnected(Exception exception) { Timer.Stop(); IsConnected = false; DVH.SetConnectedStatus(false); DVH.PostLocalOnlyMessage("MednaNet has disconnected. Reason: " + exception.Message); DVH.PostLocalOnlyMessage("Please try reconnecting..."); }
public void APIDisconnected(Exception exception) { isConnected = false; LastError = exception.Message; DVH.SetConnectedStatus(false); DVH.PostLocalOnlyMessage("MednaNet has disconnected. Reason: " + LastError); DVH.PostLocalOnlyMessage("Please try reconnecting..."); }
private void LocalPost(Messages msg) { // post return message straight away to the channel LastChannelMessages[msg.channel] = msg.id; // write to textbox DVH.WriteToTextBox(new List <Messages> { msg }); }
/// <summary> /// Message updating logic /// </summary> private async void GetMessagesAsync() { if (MessagesIsPolling == true) { return; } MessagesIsPolling = true; try { // get messages for all channels foreach (var c in Channels) { // get last id from channel message ids dictionary int lastId = 0; if (LastChannelMessages.ContainsKey(c.id)) { lastId = LastChannelMessages[c.id]; } // get messages frm API if (lastId == 0) { // no local messageId - get all messages using the timeframe specified var inc = (await Client.Channels.GetChannelMessagesFrom(c.id, DateTime.Now.AddMinutes(-3000))).ToList(); var t = inc.OrderBy(a => a.id).ToList().LastOrDefault(); if (t != null) { LastChannelMessages[c.id] = t.id; } // write to textbox DVH.WriteToTextBox(inc.OrderBy(a => a.id).ToList()); // save to database //SaveToLoggingDatabase(inc); } else { var inc = (await Client.Channels.GetChannelMessagesAfterMessageId(c.id, lastId)).ToList(); MessagesIsPolling = false; var t = inc.OrderBy(a => a.id).ToList().LastOrDefault(); if (t != null) { LastChannelMessages[c.id] = t.id; } // write to textbox DVH.WriteToTextBox(inc.OrderBy(a => a.id).ToList()); // save to database //SaveToLoggingDatabase(inc); } MessagesIsPolling = false; } } catch (Exception ex) { APIDisconnected(ex); return; } MessagesIsPolling = false; }
public async void DoPoll() { AbortThread = true; isPolling = true; Timer.Stop(); if (isConnected == false) { return; } /* Set Username */ // if the specified local username differs from API, set it if (Username != CurrentInstall.username) { CurrentInstall.username = Username; try { await Client.Install.UpdateUsername(CurrentInstall); } catch (Exception ex) { APIDisconnected(ex); return; } MednaNetSettings.SetUsername(Username); } /* Update Channels */ try { Channels = await Client.Channels.GetChannels(); } catch (Exception ex) { APIDisconnected(ex); return; } // update channel list foreach (var c in Channels.OrderBy(a => a.discordId)) { DVH.channels.UpdateChannel(c.id, c.channelName); } // Update the UI DVH.UpdateChannelButtons(); // make sure at least one channel is selected DVH.CheckChannelSelection(); /* Update Users */ // query API try { Users = await Client.Users.GetAllUsers(); } catch (Exception ex) { APIDisconnected(ex); return; } // update user list foreach (var u in Users) { if (u.discordId == null) // && u.isOnline == false) { //continue; } ClientType ct = ClientType.discord; if (u.discordId == null) { ct = ClientType.medlaunch; } DVH.users.UpdateUser(u.id, u.username, ct, true); } // Update the UI DVH.UpdateUsers(); /* Update messages */ List <Messages> foundMessages = new List <Messages>(); // loop through each channel foreach (var c in Channels) { // get the last received message from this channel //var lookup = DiscordMessages.GetLastMessageId(c.id); int lookup = 0; var lookupA = AllMessages.Where(a => a.channel == c.id).OrderBy(b => b.id).LastOrDefault(); if (lookupA != null) { lookup = lookupA.id; } if (lookup == 0) { // no message found - get all messages using the timeframe specified try { foundMessages.AddRange((await Client.Channels.GetChannelMessagesFrom(c.id, DateTime.Now.AddMinutes(MessageHistoryInMinutes))).ToList()); } catch (Exception ex) { APIDisconnected(ex); return; } } else { // result found - get messages from the API after this message ID try { foundMessages.AddRange((await Client.Channels.GetChannelMessagesAfterMessageId(c.id, lookup)).ToList()); } catch (Exception ex) { APIDisconnected(ex); return; } } // get the last messageId for this channel already posted to the screen int lastWritten = 0; if (LastChannelMessages.ContainsKey(c.id)) { lastWritten = LastChannelMessages[c.id]; } // get the last messageId received on this channel int lastId = lookup; //var look2 = DiscordMessages.GetLastMessageId(c.id); int look2 = 0; var look2a = AllMessages.Where(a => a.channel == c.id).OrderBy(b => b.id).LastOrDefault(); if (look2a != null) { look2 = look2a.id; } if (look2 > lookup) { lastId = look2; } // update the dictionary LastChannelMessages[c.id] = lastId; } // add foundMessages to the database //DiscordMessages.SaveToDatabase(foundMessages); // add foundMessages to AllMessages AllMessages.AddRange(foundMessages); AllMessages.Distinct(); // now post new messages based on the currently selected channel foreach (var c in Channels) { int last = LastChannelMessages[c.id]; //var newMessages = DiscordMessages.GetAllMessages().Where(a => a.MessageId > last).OrderBy(b => b.MessageId).ToList(); var newMessages = AllMessages.Where(a => a.id > last).OrderBy(b => b.id).ToList().OrderBy(d => d.id).ToList(); foreach (var me in newMessages) { // create discordmessage format DiscordMessage d = new DiscordMessage(); d.channel = me.channel; d.code = me.code; d.message = me.message; d.messageId = me.id; //d.name = me.name; d.postedOn = me.postedOn; // write the message to the relevant local channel MednaNetAPI.Instance.DVH.PostMessage(d); //DVH.PostMessage(me, c.id); } } AbortThread = false; isPolling = false; Timer.Start(); /* * foreach (var c in Channels) * { * // get last message ID recorded for this channel * var lookup = MessageLog.GetLastChannelMessage(c.id); * * if (lookup == null) * { * // no messages found locally for this channel, get all messages from the last day * try * { * var messages = await Client.Channels.GetChannelMessagesFrom(c.id, DateTime.Now.AddMinutes(-600)); * * // add to new messages * foreach (var m in messages.OrderBy(a => a.id)) * { * if (m.channel == 1) * { * * } * * // check that is isnt one that we have posted ourself * var allZeros = from a in MednaNetAPI.Instance.MessageArchive * where a.APIMessage.channel == c.id && a.APIMessage.id == 0 && a.APIMessage.message == m.message && a.HasBeenParsed == true * select a; * * if (allZeros.Count() > 0) * { * // message found, do nothing * } * else * { * MessageLog.AddMessage(m); * } * } * } * catch (Exception ex) { APIDisconnected(ex); return; } * * * } * else * { * // query the API for newer messages * try * { * var messages = await Client.Channels.GetChannelMessagesAfterMessageId(c.id, lookup.id); * // add to new messages * foreach (var m in messages) * { * MessageLog.AddMessage(m); * } * } * catch (Exception ex) { APIDisconnected(ex); return; } * } * } * * // process new messages * MessageLog.PostNewMessages(); * AbortThread = false; * isPolling = false; * Timer.Start(); * */ }