public string AddMessage(AddMessageArgs args)
        {
            Dictionary<string, object> results = new Dictionary<string, object>();
            byte[] imageData = null;
            string imageName = args.message.imageName;
            if (!String.IsNullOrEmpty(args.imageData)) {
                imageData = Convert.FromBase64String(args.imageData);
            }
            try {
                List<PTMessage> messages = new List<PTMessage>();

                // Add text
                if (!String.IsNullOrEmpty(args.message.text)) {
                    args.message.type = "Text";
                    PTMessage message = PTDatabase.AddMessage(args.message, null);
                    message.imageName = null;
                    if (message != null)
                        messages.Add(message);
                }

                // Add image
                if (imageData != null) {
                    PTMessage imageMessage = new PTMessage();
                    imageMessage.senderID = args.message.senderID;
                    imageMessage.protocolID = args.message.protocolID;
                    imageMessage.timestamp = args.message.timestamp;
                    imageMessage.imageName = imageName;
                    imageMessage.exerciseID = args.message.exerciseID;
                    imageMessage.type = "Image";
                    imageMessage.text = null;

                    imageMessage = PTDatabase.AddMessage(imageMessage, imageData);
                    if (imageMessage != null)
                        messages.Add(imageMessage);
                }
                if (imageData != null) {
                    string path = PTDatabase.PathForImageNamed(imageName, false);
                    System.IO.File.WriteAllBytes(path, imageData);
                }
                if (messages.Count == 0) {
                    results["error"] = "Error adding message to the database.";
                } else {
                    results["messages"] = messages;
                }
            } catch (Exception e) {
                results["error"] = e.Message;
            }
            return s.Serialize(results);
        }
Esempio n. 2
0
        public static PTMessage AddMessage(PTMessage message, byte[] imageData)
        {
            try
            {
                // add the message to the db, set message.ID, and return the message
                //TODO: default return values
                //TODO: handle video...
                using (PTLinkDatabaseDataContext db = new PTLinkDatabaseDataContext())
                {
                    Message dbMessage = new Message();
                    dbMessage.ContentTypeId = (from c in db.ContentTypes
                                               where c.Name == message.type
                                               select c.Id).SingleOrDefault();
                    dbMessage.ExerciseId = message.exerciseID;
                    dbMessage.PersonId = message.senderID;
                    dbMessage.ProtocolId = message.protocolID;
                    dbMessage.Timestamp = (long)message.timestamp;

                    if (imageData != null)
                    {
                        //TODO: handle images and video and such
                        dbMessage.Value = message.imageName;
                    }
                    else
                    {
                        dbMessage.Value = message.text;
                    }

                    db.Messages.InsertOnSubmit(dbMessage);
                    db.SubmitChanges();
                    message.ID = dbMessage.Id;
                }
                return message;
            }
            catch
            {
                return null;
            }
        }
Esempio n. 3
0
        public static List<PTMessage> GetMessages(int protocolID, int afterTime)
        {
            try
            {
                //Gets all messages for either a doctor or a patient (see following TODOs)
                //If you pass an afterTime of 0, it will get the last 30 messages
                //Otherwise, it gets however many messages were sent after afterTime
                //TODO: this really doesn't work in the long run, need a protocol id as well, hence first or default
                //TODO: also, this is assuming there's a one-to-one patient-doctor relationship which is totally false
                using (PTLinkDatabaseDataContext db = new PTLinkDatabaseDataContext())
                {
                    List<PTMessage> messages = new List<PTMessage>();
                    PTMessage message;

                    var dbMessages = (from p in db.Protocols
                                      where p.Id == protocolID
                                      select p).FirstOrDefault().Messages.Where(l => l.Timestamp > afterTime);

                    if (dbMessages != null)
                    {
                        //if afterTime is 0, only grab the last 30 messages
                        if (afterTime == 0)
                        {
                            dbMessages = dbMessages.OrderBy(l => l.Timestamp).Take(30);
                        }

                        foreach (Message dbMessage in dbMessages)
                        {
                            message = new PTMessage();
                            if (dbMessage.ExerciseId.HasValue)
                            {
                                message.exerciseID = dbMessage.ExerciseId.Value;
                            }
                            message.ID = dbMessage.Id;
                            message.protocolID = dbMessage.ProtocolId;
                            message.senderID = dbMessage.PersonId;
                            message.timestamp = dbMessage.Timestamp;

                            if (dbMessage.ContentType.Name == "Text")
                            {
                                message.text = dbMessage.Value;
                            }
                            else if (dbMessage.ContentType.Name == "Image")
                            {
                                //TODO: handle images
                                message.imageName = dbMessage.Value;
                            }
                            //TODO: handle video

                            message.type = dbMessage.ContentType.Name;
                            messages.Add(message);
                        }
                    }
                    return messages;
                }
            }
            catch
            {
                return null;
            }
        }
        public string SendMessage(PatientArgs args)
        {
            Dictionary<string, object> results = new Dictionary<string, object>();
            PTDoctor d = PTDatabase.GetDoctor(args.authentication.email, args.authentication.password);
            PTMessage message;

            if (d == null)
            {
                results["error"] = "Invalid username or password";
            }
            else if (args.patientID.HasValue)
            {
                //TODO: this should be reworked
                PTMessage messageArg = new PTMessage();
                PTProtocol tempProtocol = PTDatabase.GetProtocolsForPatient(args.patientID.Value).FirstOrDefault();
                messageArg.protocolID = tempProtocol.ID;
                messageArg.exerciseID = tempProtocol.exercises.First().ID;
                messageArg.senderID = d.ID;
                messageArg.text = args.message;
                messageArg.timestamp = args.lastTime.Value;
                messageArg.type = "Text";
                message = PTDatabase.AddMessage(messageArg, null);
                if (message != null)
                {
                    //TODO: check if is empty what it returns
                    //TODO: should it return message
                    //TODO: need to handle images
                    results["message"] = message;
                }
                else
                {
                    results["error"] = "Error adding message to the database.";
                }
            }
            else
            {
                results["error"] = "No such patient for this doctor.";
            }

            return new JavaScriptSerializer().Serialize(results);
        }