Example #1
0
        public IHttpActionResult Delete(int lookupID, string feedid, string feedvalue)
        {
            try
            {
                throw new NotImplementedException("This endpoint is not currently fleshed out");
            }
            catch (Exception Ex)
            {
                DEUtilities.LogMessage(string.Format("[{0}] {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, Ex.Message), DEUtilities.LogLevel.Error, Ex);
                return(Content(HttpStatusCode.NotImplemented, ""));
            }

            /*
             * // This code will simply delete all of the feeds, not a specific one
             * try
             * {
             *    if (!dbDal.DeletedRuleFeed(lookupID))
             *    {
             *      return this.StatusCode(HttpStatusCode.InternalServerError);
             *    }
             * }
             * catch (NpgsqlException e)
             * {
             *    return Content(HttpStatusCode.InternalServerError, "Database error occurred");
             * }
             * catch (Exception e)
             * {
             *    return Content(HttpStatusCode.InternalServerError, "Failed to delete the source value rule");
             * }
             * return Ok();*/
        }
Example #2
0
        /// <summary>
        /// Simple constructor
        /// </summary>
        /// <param name="evt">NIEM EM_LC Event</param>
        public EMLCContent(Event evt, string deHash)
        {
            myEvent  = evt;
            myDeHash = deHash;
            ProcessEventDetails();

            try
            {
                xe = XElement.Parse(Properties.Resources.EventTypeCodeList);
            }
            catch (Exception e)
            {
                DEUtilities.LogMessage("Error in creating EMLCContent Content Helper", DEUtilities.LogLevel.Error, e);
                xe = null;
            }
        }
Example #3
0
        public HttpResponseMessage Validate([FromBody] DEv1_0 value)
        {
            List <string> errorList = null;

            try
            {
                Log.Debug("Checking if DE Message is valid");
                string xml = value.ToString();   // Validates DE portion of message and writes to xml

                bool isValid = Fresh.Global.DEUtilities.ValidateNiemSchema(xml, out errorList);

                if (isValid)
                {
                    DEUtilities.LogMessage("The message is valid", DEUtilities.LogLevel.Info);
                    return(Request.CreateResponse(HttpStatusCode.OK, "Message is valid"));
                }
                else
                {
                    DEUtilities.LogMessage("The message was not valid", DEUtilities.LogLevel.Info);
                    string schemaErrorString = "";

                    foreach (string er in errorList)
                    {
                        schemaErrorString = schemaErrorString + er + "\n";
                    }

                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The DE was invalid: " + schemaErrorString));
                }
            }
            catch (IOException Ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The schema files could not be read"));
            }
            catch (FormatException Ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The schema files could not be parsed"));
            }
            catch (Exception Ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The message could not be validated"));
            }
        }
Example #4
0
        public HttpResponseMessage Put(int lookupID, [FromBody] DEv1_0 value)
        {
            string errMsg; // Holds error message

            //--- Validating the DE
            if (DEUtilities.ValidateDE(value, out errMsg) == false)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errMsg));
            }

            //--- Checking if Update or Add

            // If false we are creating a DE. If true, this is an update.
            bool deExists;

            try
            {
                deExists = dbDal.DEExists(value);

                if (deExists)
                {
                    DEUtilities.LogMessage(string.Format("The DE with lookupID {0} exists.  This is an update.", lookupID), DEUtilities.LogLevel.Debug);
                }
                else
                {
                    DEUtilities.LogMessage(string.Format("The DE with lookupID {0} does not exist.  We are adding a new DE.", lookupID), DEUtilities.LogLevel.Debug);
                }
            }
            catch (NpgsqlException e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Database error occurred."));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occurred when validating the request."));
            }

            try
            {
                bool wasSuccessful = dbDal.PutDE(value);

                // If the operation was a success
                if (wasSuccessful)
                {
                    if (this.ArchiveEnabled)
                    {
                        string clientAddress = HttpContext.Current.Request.UserHostAddress;
                        archiveDal.ArchiveDE(value, clientAddress);
                    }

                    // If this was an update
                    if (deExists)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, "Success"));
                    }
                    else // If this was an add
                    {
                        HttpResponseMessage msg = Request.CreateResponse(HttpStatusCode.Created, value);

                        // Getting location for new DE
                        string location = "" + HttpContext.Current.Request.Url;

                        if (!(location.Contains("" + lookupID)))
                        {
                            location = location.TrimEnd('/') + "/" + lookupID;
                        }

                        msg.Headers.Location = new Uri(location);

                        return(msg);
                    }
                }
                else // If the operation failed
                {
                    // If this was an update
                    if (deExists)
                    {
                        errMsg = "Error occurred when updating the DE";
                    }
                    else // If this was an add
                    {
                        errMsg = "Error occurred when adding the DE";
                    }

                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, errMsg));
                }
            }
            catch (NpgsqlException e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Database error occurred"));
            }
            catch (InvalidOperationException e)
            {
                if (deExists)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Conflict, "This was not a valid update.  Make sure the updated DE message is valid and the most recent update."));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed to add the DE"));
                }
            }
            catch (FederationException e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occurred when federating the DE"));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed to add the DE"));
            }
        }
Example #5
0
        /// <summary>
        /// Base method, writes to stream
        /// </summary>
        /// <param name="type">type of object to write</param>
        /// <param name="value">object to write</param>
        /// <param name="writeStream">stream to write to</param>
        /// <param name="content">stuff</param>
        public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
        {
            DEv1_0 de  = value as DEv1_0;
            Event  evt = null;

            try
            {
                if (de != null)
                {
                    ContentObject co = de.ContentObjects[0];

                    EMLCContent evtHelper = (EMLCContent)DEUtilities.FeedContent(de, co);

                    var settings = new XmlWriterSettings();
                    settings.Indent             = false;
                    settings.OmitXmlDeclaration = false;

                    XmlWriter writer = XmlWriter.Create(writeStream, settings);
                    writer.WriteStartElement("html"); //html
                    writer.WriteStartElement("head"); //head
                    writer.WriteStartElement("meta");
                    writer.WriteAttributeString("charset", "UTF-8");

                    // We're returning some DE-related HTML, so set up the page to refresh every 5 seconds
                    // This is not a good way to do it long term.
                    //writer.WriteAttributeString("http-equiv", "refresh");
                    //string contentValue = $"5; URL={HttpContext.Current.Request.Url}";
                    //writer.WriteAttributeString("content", contentValue);

                    writer.WriteEndElement();         //meta
                    writer.WriteElementString("title", "TEST");
                    writer.WriteEndElement();         //head

                    writer.WriteStartElement("body"); //start body

                    writer.WriteStartElement("div");  //start div 1
                    writer.WriteAttributeString("style", "background-color:black;color:white;padding:5px;");

                    #region table
                    writer.WriteStartElement("table"); //start table
                    writer.WriteAttributeString("style", "color:white;");
                    #region row 1
                    writer.WriteStartElement("tr");  //start row

                    writer.WriteStartElement("td");  //start cell
                    writer.WriteAttributeString("style", "padding:15px;");
                    writer.WriteStartElement("img"); //start image
                    writer.WriteAttributeString("src", evtHelper.IconURL());
                    writer.WriteAttributeString("alt", evtHelper.FriendlyName());
                    writer.WriteEndElement();       //end image
                    writer.WriteEndElement();       //end cell

                    writer.WriteStartElement("td"); //start cell
                    writer.WriteElementString("h1", evtHelper.Title());
                    writer.WriteEndElement();       //end cell
                    writer.WriteEndElement();       //end row
                    #endregion row 1

                    #region row 2
                    writer.WriteStartElement("tr"); //start row
                    writer.WriteStartElement("td"); //start cell

                    writer.WriteAttributeString("colspan", "2");
                    writer.WriteValue(evtHelper.FriendlyName());

                    writer.WriteEndElement(); //end cell
                    writer.WriteEndElement(); //end row
                    #endregion row 2
                    writer.WriteEndElement(); //end table
                    #endregion table


                    writer.WriteEndElement(); //end div 1

                    // Loop through each kind of details and append HTML for each one
                    if (evtHelper.ResourceDetails != null)
                    {
                        foreach (ResourceDetail rd in evtHelper.ResourceDetails)
                        {
                            string sColor = "color:black";

                            writer.WriteStartElement("p"); //start paragraph
                            writer.WriteAttributeString("style", sColor);

                            if (rd.Status.PrimaryStatus == ResourcePrimaryStatusCodeList.Available)
                            {
                                sColor = "color:green";
                            }
                            else if (rd.Status.PrimaryStatus == ResourcePrimaryStatusCodeList.ConditionallyAvailable)
                            {
                                sColor = "color:yellow";
                            }
                            else if (rd.Status.PrimaryStatus == ResourcePrimaryStatusCodeList.NotAvailable)
                            {
                                sColor = "color:red";
                            }

                            writer.WriteElementString("p", "Latitude/Longitude: " + evtHelper.Location().Latitude.ToString() + ", " + evtHelper.Location().Longitude.ToString());
                            //writer.WriteElementString("p", "Lon: " + evtHelper.Location().Latitude.ToString());
                            string addr = DEUtilities.ReverseGeocodeLookup(evtHelper.Location().Latitude.ToString(), evtHelper.Location().Longitude.ToString());

                            if (string.IsNullOrWhiteSpace(addr))
                            {
                                addr = "Not Found";
                            }

                            writer.WriteElementString("p", "Address: " + addr); //+ some reverse lookup;

                            writer.WriteRaw("Primary Status: <span style=\"font-weight:bold;" + sColor + "\"> " + rd.Status.PrimaryStatus.ToString() + "</ span>" + " ");
                            writer.WriteEndElement(); //end paragraph
                        }
                    }

                    if (evtHelper.IncidentDetails != null)
                    {
                        foreach (IncidentDetail id in evtHelper.IncidentDetails)
                        {
                            string sColor = "color:black";

                            writer.WriteStartElement("p"); //start paragraph
                            writer.WriteAttributeString("style", sColor);
                            if (id.Status.PrimaryStatus == IncidentPrimaryStatusCodeList.Active)
                            {
                                sColor = "color:green";
                            }
                            else if (id.Status.PrimaryStatus == IncidentPrimaryStatusCodeList.Pending)
                            {
                                sColor = "color:orange";
                            }

                            string addr = "Not Found";
                            writer.WriteElementString("p", "Latitude/Longitude: " + evtHelper.Location().Latitude.ToString() + ", " + evtHelper.Location().Longitude.ToString());

                            if (id.LocationExtension != null && id.LocationExtension.Address != null)
                            {
                                addr = id.LocationExtension.Address.ToString();
                            }
                            else
                            {
                                //writer.WriteElementString("p", "Lon: " + evtHelper.Location().Latitude.ToString());
                                addr = DEUtilities.ReverseGeocodeLookup(evtHelper.Location().Latitude.ToString(), evtHelper.Location().Longitude.ToString());

                                if (string.IsNullOrWhiteSpace(addr))
                                {
                                    addr = "Not Found";
                                }
                            }
                            writer.WriteElementString("p", "Address: " + addr); //+ some reverse lookup;

                            writer.WriteRaw("Primary Status: <span style=\"font-weight:bold;" + sColor + "\"> " + id.Status.PrimaryStatus.ToString() + "</ span>" + " ");
                            writer.WriteEndElement(); //end paragraph
                        }
                    }

                    if (evtHelper.SensorDetails != null)
                    {
                        foreach (SensorDetail sensor in evtHelper.SensorDetails)
                        {
                            string sColor = "color:black;";

                            writer.WriteStartElement("p"); //start paragraph
                            writer.WriteAttributeString("style", sColor);
                            writer.WriteRaw("Sensor ID: " + sensor.ID + " ");
                            writer.WriteEndElement();      //end paragraph

                            writer.WriteStartElement("p"); //start paragraph
                            writer.WriteAttributeString("style", sColor);

                            if (sensor.Status == SensorStatusCodeList.Normal)
                            {
                                sColor = "color:green";
                            }
                            else if (sensor.Status == SensorStatusCodeList.LowPower)
                            {
                                sColor = "color:orange";
                            }
                            else if (sensor.Status == SensorStatusCodeList.Error)
                            {
                                sColor = "color:red";
                            }
                            else if (sensor.Status == SensorStatusCodeList.Sleeping)
                            {
                                sColor = "color:gray";
                            }
                            writer.WriteRaw("Primary Status: <span style=\"font-weight:bold;" + sColor + "\"> " + sensor.Status.ToString() + "</ span>" + " ");
                            writer.WriteEndElement(); //end paragraph

                            //assumes at least one item of device details is present
                            if (sensor.DeviceDetails != null)
                            {
                                CreateSensorDeviceInfo(writer, sensor.DeviceDetails);
                            }

                            if (sensor.PowerDetails != null)
                            {
                                CreateSensorPowerInfo(writer, sensor.PowerDetails);
                            }

                            if (sensor.PhysiologicalDetails != null)
                            {
                                CreateSensorPhysiologicalInfo(writer, sensor.PhysiologicalDetails);
                            }

                            if (sensor.EnvironmentalDetails != null)
                            {
                                CreateSensorEnvironmentalInfo(writer, sensor.EnvironmentalDetails);
                            }

                            if (sensor.LocationDetails != null)
                            {
                                CreateSensorLocationInfo(writer, sensor.LocationDetails);
                            }
                        }
                    }
                    writer.WriteEndElement(); //end body

                    writer.WriteEndElement(); //end html

                    writer.Flush();

                    writer.Close();
                }
            }
            catch (Exception e)
            {
                DEUtilities.LogMessage("An error occurred when trying to parse the DE", DEUtilities.LogLevel.Error);
                CreateErrorInfo(writeStream);
            }
        }
Example #6
0
        /// <summary>
        /// Parses through the Icon File looking for a match on type and icon filename
        /// </summary>
        /// <param name="groupName">Group of icons to look for</param>
        /// <returns>Icon Fully Qualified Path</returns>
        private string GetIconFilename(string groupName)
        {
            string    iconFilename = "";
            IconSet   niemIcons    = null;
            IconGroup group        = null;

            //sanity check
            if (IconConfig.Icons != null)
            {
                foreach (IconSet iset in IconConfig.Icons.Sets)
                {
                    if (iset.KindofSet.Equals("NIEM", StringComparison.InvariantCultureIgnoreCase))
                    {
                        niemIcons = iset;
                        break;
                    }
                }

                if (niemIcons != null)
                {
                    foreach (IconGroup iGroup in niemIcons.Groups)
                    {
                        if (iGroup.KindofGroup.Equals(groupName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            group = iGroup;
                            break;
                        }
                    }

                    if (group != null)
                    {
                        string   resourceType = myEvent.EventTypeDescriptor.CodeValue.ToString();
                        string[] splitType    = resourceType.Split('_');

                        string temp = "";
                        //TODO:assumes file extension is .png, need to fix later

                        //this loop strips off the right most subtype when looking for a match
                        //on file name
                        //For Exmample: ALS Ambulance type is
                        //ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_EMS_AMBULANCE_ALS
                        //each pass removes the right most subtype until either a match is found
                        //or beginning of the string is hit (which isn't good)
                        //ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_EMS_AMBULANCE_ALS
                        //ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_EMS_AMBULANCE
                        //ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_EMS
                        //ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM
                        //...
                        for (int i = splitType.Length; i >= 0; i--)
                        {
                            temp = String.Join("_", splitType, 0, i) + ".png";

                            DEUtilities.LogMessage("Searching for icon filename: " + temp + " in NIEM group: " + groupName, DEUtilities.LogLevel.Debug);

                            if (group.Filenames.Contains(temp))
                            {
                                iconFilename = group.RootFolder + @"/" + temp;
                                break;
                            }
                        }

                        if (string.IsNullOrWhiteSpace(iconFilename))
                        {
                            DEUtilities.LogMessage("Icon filename not found for: " + resourceType, DEUtilities.LogLevel.Error);
                        }
                    }
                }
            }
            return(iconFilename);
        }