Exemple #1
0
        public static bool SetConfiguration(string key, string identifier, string value)
        {
            Configuration retConfig = null;

            if (!string.IsNullOrEmpty(identifier))
            {
                retConfig = ORM.Select <Configuration> ("Configuration", string.Format("Key = '{0}' AND Identifier = '{1}'", key, identifier)).FirstOrDefault();
            }
            else
            {
                retConfig = ORM.Select <Configuration> ("Configuration", string.Format("Key = '{0}' AND Identifier = NULL", key)).FirstOrDefault();
            }

            if (retConfig == null)
            {
                retConfig = new Configuration()
                {
                    Key = key, Identifier = identifier, Created = DateTime.Now
                };
            }

            retConfig.Value = value;

            return(ORM.Update <Configuration> ("Configuration", retConfig));
        }
        public override EditMapJSONResponse GetResponse(Dictionary <string, string[]> requestAttributes)
        {
            string mapIDstr = string.Empty;

            try
            {
                if (requestAttributes.ContainsKey("ID"))
                {
                    mapIDstr = requestAttributes ["ID"] [0].Trim().ToUpper();

                    var map = ORM.Select <DeviceMap>("DeviceMap", string.Format("ID = '{0}'", mapIDstr)).FirstOrDefault();

                    if (map == null)
                    {
                        throw new KeyNotFoundException(string.Format("The map with an ID of {0} does not exist in the database.", mapIDstr));
                    }


                    foreach (string key in requestAttributes.Keys)
                    {
                        switch (key.Trim().ToUpper())
                        {
                        case "NAME":
                            map.Name = requestAttributes[key][0];
                            break;

                        case "DEVICEKEY":
                            map.DeviceKey = requestAttributes[key][0];
                            break;

                        case "DEVICETYPE":
                            map.DeviceTypeEnum = (DeviceType)Enum.Parse(typeof(DeviceType), requestAttributes[key][0]);
                            break;

                        case "CREATED":
                            map.Created = new DateTimeStamp(DateTime.Parse(requestAttributes[key][0]));
                            break;

                        case "PARENTMAPID":
                            map.ParentMapID = (int)int.Parse(requestAttributes[key][0]);
                            break;
                        }
                    }

                    bool success = ORM.Update <DeviceMap>("DeviceMap", map);
                    return(new EditMapJSONResponse(map, success));
                }
            }
            catch (Exception ex) {
                if (string.IsNullOrEmpty(mapIDstr))
                {
                    mapIDstr = "NULL";
                }

                LogWriter.WriteLog(string.Format("An error was encountered while attempting to update the map. ID:{0}", mapIDstr), ex);
            }

            return(new EditMapJSONResponse(null, false));
        }
Exemple #3
0
        public bool LogValue(IConvertible Value, DateTime logTimeStamp)
        {
            DeviceLog log = new DeviceLog();

            log.DeviceMapID = this.ID;
            log.TimeStamp   = new DateTimeStamp(logTimeStamp);
            log.Value       = Value.ToString();

            return(ORM.Update <DeviceLog> ("DeviceLog", log));
        }
Exemple #4
0
        public static Configuration GetConfiguration(string key, string identifier)
        {
            Configuration retConfig = null;

            if (!string.IsNullOrEmpty(identifier))
            {
                retConfig = ORM.Select <Configuration> ("Configuration", string.Format("Key = '{0}' AND Identifier = '{1}'", key, identifier)).FirstOrDefault();
            }
            else
            {
                retConfig = ORM.Select <Configuration> ("Configuration", string.Format("Key = '{0}' AND Identifier = NULL", key)).FirstOrDefault();
            }

            return(retConfig);
        }
Exemple #5
0
        public static DeviceMap Create(string deviceKey, DeviceType type)
        {
            var match = ORM.Select <DeviceMap> ("DeviceMap", string.Format("DeviceKey = '{0}' and DeviceType = ", deviceKey, (int)type)).FirstOrDefault();

            if (match != null)
            {
                return(match);
            }
            else
            {
                match                = new DeviceMap();
                match.Created        = DateTimeStamp.Now;
                match.DeviceKey      = deviceKey;
                match.DeviceTypeEnum = type;
                match.Name           = deviceKey;

                ORM.Update <DeviceMap> ("DeviceMap", match);
                match = ORM.Select <DeviceMap> ("DeviceMap", string.Format("DeviceKey = '{0}' and DeviceType = ", deviceKey, (int)type)).FirstOrDefault();

                return(match);
            }
        }
Exemple #6
0
 public virtual IEnumerable <DeviceLog> GetLog(DateTime startTime, DateTime endTime)
 {
     return(ORM.Select <DeviceLog>("DeviceLog", string.Format("DeviceMapID = '{0}' AND TimeStamp >= '{1}' AND TimeStamp <= '{2}'", Map.ID, startTime, endTime)));
 }
Exemple #7
0
        public static Dictionary <DeviceMap, IEnumerable <DeviceLog> > GetLogs(DateTime?startTime, DateTime?endTime, IEnumerable <Guid> mapIDs)
        {
            StringBuilder sbFilter  = new StringBuilder();
            StringBuilder inBuilder = null;

            if (startTime != null && startTime > DateTime.MinValue)
            {
                sbFilter.Append("TimeStamp >= '");
                sbFilter.Append(startTime.ToString());
                sbFilter.Append("' ");
            }

            if (endTime != null && endTime > DateTime.MinValue)
            {
                if (sbFilter.Length > 0)
                {
                    sbFilter.Append("AND ");
                }
                sbFilter.Append("TimeStamp <= '");
                sbFilter.Append(endTime.ToString());
                sbFilter.Append("' ");
            }

            if (mapIDs != null && mapIDs.Count() > 0)
            {
                if (sbFilter.Length > 0)
                {
                    sbFilter.Append("AND ");
                }

                inBuilder = new StringBuilder();
                foreach (var id in mapIDs)
                {
                    if (inBuilder.Length > 0)
                    {
                        inBuilder.Append(", ");
                    }
                    inBuilder.Append("'");
                    inBuilder.Append(id.ToString());
                    inBuilder.Append("'");
                }

                sbFilter.Append("DeviceMapID IN (");
                sbFilter.Append(inBuilder.ToString());
                sbFilter.Append(")");
            }

            var tmpLogs = ORM.Select <DeviceLog> ("DeviceLog", sbFilter.ToString());

            inBuilder = new StringBuilder();
            foreach (int id in tmpLogs.Select(l => l.DeviceMapID).Distinct())
            {
                if (inBuilder.Length > 0)
                {
                    inBuilder.Append(", ");
                }
                inBuilder.Append("'");
                inBuilder.Append(id.ToString());
                inBuilder.Append("'");
            }

            var tmpMaps = ORM.Select <DeviceMap> ("DeviceMap", string.Format("ID in ({0})", inBuilder.ToString()));

            return(MapLogs(tmpMaps, tmpLogs));
        }
Exemple #8
0
        public override JSONObject GetResponse(System.Collections.Generic.Dictionary <string, string[]> requestAttributes)
        {
            bool              _parseSuccess    = true;
            DateTimeStamp     _startDate       = DateTimeStamp.MinValue;
            DateTimeStamp     _endDate         = DateTimeStamp.MaxValue;
            List <string>     _tempSensorNames = new List <string> ();
            List <DeviceType> _tempSensorTypes = new List <DeviceType> ();

            StringBuilder sb = new StringBuilder();

            if (requestAttributes.ContainsKey("start"))
            {
                if (!DateTimeStamp.TryParse(requestAttributes["start"].FirstOrDefault(), out _startDate))
                {
                    _parseSuccess = false;
                }
                if (sb.Length > 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append(string.Format("TimeStamp >= '{0}'", _startDate.ToString()));                   //'2015-03-09 09:38:00'
            }

            if (requestAttributes.ContainsKey("end"))
            {
                if (!DateTimeStamp.TryParse(requestAttributes["end"].FirstOrDefault(), out _endDate))
                {
                    _parseSuccess = false;
                }
                if (sb.Length > 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append(string.Format("TimeStamp <= '{0}'", _endDate.ToString()));
            }

            if (requestAttributes.ContainsKey("name"))
            {
                bool firstName = true;
                var  nameList  = requestAttributes ["name"];               //.FirstOrDefault().Split ("|".ToCharArray ());

                if (nameList.Count() > 0)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(" AND ");
                    }
                    sb.Append("DeviceMapID in (select ID from DeviceMap where ");

                    foreach (string name in nameList)
                    {
                        if (firstName)
                        {
                            firstName = false;
                        }
                        else
                        {
                            sb.Append(" OR ");
                        }

                        sb.Append(string.Format("DeviceKey = '{0}'", name));
                    }

                    sb.Append(")");
                }
            }

            if (requestAttributes.ContainsKey("type"))
            {
                bool firstType = true;
                var  typeList  = requestAttributes["type"];

                if (typeList.Count() > 0)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(" AND ");
                    }
                    sb.Append("DeviceMapID in (select ID from DeviceMap where DeviceType in (");


                    foreach (string devType in typeList)                      //typeList) {

                    {
                        DeviceType t = DeviceType.Temperature;
                        try {
                            if (!Enum.TryParse <DeviceType> (devType, out t))
                            {
                                t = (DeviceType)int.Parse(devType);
                            }

                            if (firstType)
                            {
                                firstType = false;
                            }
                            else
                            {
                                sb.Append(", ");
                            }

                            sb.Append(((int)t).ToString());
                        } catch {
                        }
                    }
                    sb.Append("))");
                }
            }


            var        conn = ORM.CreateConnection();
            JSONObject resp = null;

            try
            {
                conn.Open();

                var logs = ORM.Select <DeviceLog> ("DeviceLog", sb.ToString(), ref conn);
                var maps = ORM.Select <DeviceMap> ("DeviceMap", string.Format("ID in (select distinct DeviceMapID from DeviceLog where {0})", sb.ToString()));

                if (requestAttributes.ContainsKey("csv") && requestAttributes["csv"][0].Trim().ToUpper() == "TRUE")
                {
                    resp = new CSVDeviceResponse(logs, maps);
                }
                else
                {
                    resp = new ChartJSGraphDevResponse(logs, maps);
                }
            }
            catch (Exception ex) {
                LogWriter.WriteLog(string.Format("An error was encountered wile attempting to get temperature logs for the filter '{0}'", sb.ToString()), ex);
            }
            conn.Close();

            if (!_parseSuccess)
            {
                return(null);
            }
            else
            {
                return(resp);
            }
        }
Exemple #9
0
        public override JSONObject GetResponse(Dictionary <string, string[]> requestAttributes)
        {
            bool          _parseSuccess    = true;
            List <string> _tempSensorNames = new List <string> ();

            StringBuilder sb = new StringBuilder();

            sb.Append(string.Format("DeviceType = {0}", (int)DeviceType.Temperature));

            if (requestAttributes.ContainsKey("names"))
            {
                bool firstName = true;
                if (sb.Length > 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append("(");

                foreach (string name in requestAttributes["names"].ToString().Split("|".ToCharArray()))
                {
                    if (firstName)
                    {
                        firstName = false;
                    }
                    else
                    {
                        sb.Append(" OR ");
                    }

                    sb.Append(string.Format("Name like '{0}'", name));
                }

                sb.Append(")");
            }

            var conn = ORM.CreateConnection();
            DeviceLogJSONResponse resp = null;

            try
            {
                conn.Open();

                var maps = ORM.Select <DeviceMap> ("DeviceMap", sb.ToString(), ref conn);
                var logs = new List <DeviceLog>();
                foreach (var map in maps)
                {
                    MonitoringManager.ReadDevice(map.DeviceKey);
                }
                resp = new DeviceLogJSONResponse(logs, maps);
            }
            catch (Exception ex) {
                LogWriter.WriteLog(string.Format("An error was encountered while attempting to get temperature logs for the filter '{0}'", sb.ToString()), ex);
            }
            conn.Close();

            if (!_parseSuccess)
            {
                return(null);
            }
            else
            {
                return(resp);
            }
        }
        public override JSONObject GetResponse(System.Collections.Generic.Dictionary <string, string[]> requestAttributes)
        {
            bool          _parseSuccess = false;
            StringBuilder sb            = new StringBuilder();

            if (requestAttributes.ContainsKey("key"))
            {
                bool firstKey = true;
                var  nameList = requestAttributes ["key"];                //.FirstOrDefault().Split ("|".ToCharArray ());

                if (nameList.Count() > 0)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(" AND ");
                    }
                    sb.Append("(");

                    foreach (string name in nameList)
                    {
                        if (firstKey)
                        {
                            firstKey = false;
                        }
                        else
                        {
                            sb.Append(" OR ");
                        }

                        sb.Append(string.Format("Key like '{0}'", name));
                    }

                    sb.Append(")");
                }
            }

            if (requestAttributes.ContainsKey("id"))
            {
                var nameList = requestAttributes ["id"];                 //.FirstOrDefault().Split ("|".ToCharArray ());

                if (nameList.Count() > 0)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(" AND ");
                    }
                    sb.Append("(Identifier = NULL");

                    foreach (string name in nameList)
                    {
                        sb.Append(" OR ");

                        sb.Append(string.Format("Identifier like '{0}'", name));
                    }

                    sb.Append(")");
                }
            }

            var        conn = ORM.CreateConnection();
            JSONObject resp = null;

            try
            {
                conn.Open();

                resp          = new SettingsReturn(ORM.Select <Configuration> ("Configuration", sb.ToString(), ref conn));
                _parseSuccess = true;
            }
            catch (Exception ex) {
                LogWriter.WriteLog(string.Format("An error was encountered wile attempting to get temperature logs for the filter '{0}'", sb.ToString()), ex);
            }
            conn.Close();

            if (!_parseSuccess)
            {
                return(null);
            }
            else
            {
                return(resp);
            }
        }
 public override DeviceListResponse GetResponse(Dictionary <string, string[]> requestAttributes)
 {
     return(new DeviceListResponse(ORM.Select <DeviceMap> ("DeviceMap", "").Select(d => new DeviceInfoJSON(d)).ToList()));
 }
Exemple #12
0
        public override JSONObject GetResponse(System.Collections.Generic.Dictionary <string, string[]> requestAttributes)
        {
            bool          _parseSuccess    = true;
            DateTime      _startDate       = DateTime.MinValue;
            DateTime      _endDate         = DateTime.MaxValue;
            List <string> _tempSensorNames = new List <string> ();

            StringBuilder sb = new StringBuilder();

            if (requestAttributes.ContainsKey("start"))
            {
                if (!DateTime.TryParse(requestAttributes ["start"].ToString(), out _startDate))
                {
                    _parseSuccess = false;
                }
                if (sb.Length > 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append(string.Format("TimeStamp >= '{0}'", _startDate.ToString()));
            }

            if (requestAttributes.ContainsKey("end"))
            {
                if (!DateTime.TryParse(requestAttributes ["end"].ToString(), out _endDate))
                {
                    _parseSuccess = false;
                }
                if (sb.Length > 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append(string.Format("TimeStamp <= '{0}'", _endDate.ToString()));
            }

            sb.Append(string.Format("DeviceMapID in (select ID from DeviceMap where DeviceType = {0})", (int)DeviceType.Temperature));

            if (requestAttributes.ContainsKey("names"))
            {
                bool firstName = true;
                if (sb.Length > 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append("DeviceMapID in (select ID from DeviceMap where ");

                foreach (string name in requestAttributes["names"].ToString().Split("|".ToCharArray()))
                {
                    if (firstName)
                    {
                        firstName = false;
                    }
                    else
                    {
                        sb.Append(" OR ");
                    }

                    sb.Append(string.Format("Name like '{0}'", name));
                }

                sb.Append(")");
            }

            var conn = ORM.CreateConnection();
            TemperatureJSONResponse resp = null;

            try
            {
                conn.Open();

                var logs = ORM.Select <DeviceLog> ("DeviceLog", sb.ToString(), ref conn);
                var maps = ORM.Select <DeviceMap> ("DeviceMap", string.Format("ID in (select distinct DeviceMapID from DeviceLog where {0})", sb.ToString()));

                resp = new TemperatureJSONResponse(logs, maps);
            }
            catch (Exception ex) {
                LogWriter.WriteLog(string.Format("An error was encountered wile attempting to get temperature logs for the filter '{0}'", sb.ToString()), ex);
            }
            conn.Close();

            if (!_parseSuccess)
            {
                return(null);
            }
            else
            {
                return(resp);
            }
        }