public async Task <bool> UpdateRecord(DeviceRecord record)
        {
            if (record == null)
            {
                return(false);
            }

            return(_sensors.Update(record));
        }
        public async Task <long> AddRecord(DeviceRecord record)
        {
            if (record == null)
            {
                return(-1);
            }

            return(_sensors.Insert(record));
        }
        public async Task <long> AddRecord(DeviceRecord record)
        {
            if (record == null)
            {
                return(-1);
            }

            var result = DeviceRecords.Add(record).DeviceRecordId;

            await SaveChangesAsync();

            return(result);
        }
        public async Task <bool> UpdateRecord(DeviceRecord record)
        {
            if (record == null)
            {
                return(false);
            }

            var rec = DeviceRecords.First(n => n.DeviceRecordId == record.DeviceRecordId);

            if (rec != null)
            {
                rec.DeviceMac       = record.DeviceMac;
                rec.DeviceName      = record.DeviceName;
                rec.Time            = record.Time;
                rec.SensorValueList = record.SensorValueList;
                await SaveChangesAsync();

                return(true);
            }

            return(false);
        }
Esempio n. 5
0
        private static void Mqtt_DataReceived(byte[] payload, string topic)
        {
            if (!_verboseLog && !_dbStarted)
            {
                return;
            }

            // parse strings to key/value set
            var tmpStr = Encoding.UTF8.GetString(payload);

            Dictionary <string, string> stringsSet;
            var jss = new JavaScriptSerializer();

            try
            {
                stringsSet = jss.Deserialize <Dictionary <string, string> >(tmpStr);
            }
            catch (Exception e)
            {
                if (_verboseLog)
                {
                    LogToScreen("Deserialize error: " + e);
                }
                //throw;
                return;
            }

            // save to DB if exists
            if (_dbStarted)
            {
                var nameFound = stringsSet.TryGetValue(DeviceName, out var devName);
                var macFound  = stringsSet.TryGetValue(DeviceMac, out var devMac);

                var currentResult = new DeviceRecord();
                if (nameFound || macFound)
                {
                    stringsSet.Remove(DeviceName);
                    stringsSet.Remove(DeviceMac);

                    currentResult.SensorValueList = new List <SensorRecord>();
                    if (nameFound)
                    {
                        currentResult.DeviceName = devName ?? "";
                    }
                    if (macFound)
                    {
                        currentResult.DeviceMac = devMac;
                    }
                    currentResult.Time = DateTime.Now;
                }
                else
                {
                    if (_verboseLog)
                    {
                        LogToScreen("Message parse error: No device name found");
                    }
                    return;
                }

                if (stringsSet.TryGetValue(FwVersion, out var fwVer))
                {
                    currentResult.FwVersion = fwVer;
                    stringsSet.Remove(FwVersion);
                }

                var floatSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
                foreach (var item in stringsSet)
                {
                    var newValue = new SensorRecord();
                    var v        = "";
                    v = floatSeparator == "," ? item.Value.Replace(".", floatSeparator) : v.Replace(",", floatSeparator);

                    if (!float.TryParse(v, out var value))
                    {
                        continue;
                    }

                    //add result to log
                    newValue.Value      = value;
                    newValue.SensorName = item.Key;
                    currentResult.SensorValueList.Add(newValue);
                }

                RecordsDb.AddRecord(currentResult);
            }

            // save to log if needed
            if (_verboseLog)
            {
                var resultStr = new StringBuilder();

                foreach (var s in stringsSet)
                {
                    resultStr.AppendLine("\t[" + topic + "]<< " + s.Key + "=" + s.Value);
                }
                LogToScreen(resultStr + Environment.NewLine);
            }
        }