Beispiel #1
0
        public async Task DeviceUpdated(DeviceMaster deviceInfo, DeviceValue value)
        {
            var device = _deviceMap.FirstOrDefault(x => x.Id == value.MasterDeviceId);

            if (device == null)
            {
                return;
            }

            _logger.LogInformation($"Device update {IdPrefix}{device.Id}");

            await _client.PublishAsync(new MqttApplicationMessage
            {
                Topic       = $"{_mqttConfig.BaseTopic}/{IdPrefix}{device.Id}/state",
                ContentType = "text/plain",
                Retain      = true,
                Payload     = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(deviceInfo.State, SerializerSettings))
            });

            await _client.PublishAsync(deviceInfo.Devices.Where(x => x.Value.CurrentValue != null).Select(x => new MqttApplicationMessage
            {
                Topic       = $"{_mqttConfig.BaseTopic}/{IdPrefix}{device.Id}/{x.Key}",
                ContentType = "text/plain",
                Retain      = true,
                Payload     = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x.Value.CurrentValue, SerializerSettings))
            }));
        }
Beispiel #2
0
        public long AddDeviceId(DeviceMasterViewModel model)
        {
            var getDeviceId = _deviceMasterRepository.Table.FirstOrDefault(i => i.DeviceId == model.DeviceId && i.UserId == model.UserId && !i.IsDeleted);

            if (getDeviceId != null)
            {
                return(getDeviceId.Id);
            }

            var currentDeviceId = new DeviceMaster
            {
                UserId      = model.UserId,
                DeviceId    = model.DeviceId,
                IsEnable    = true,
                IsDeleted   = false,
                CreatedDate = DateTime.UtcNow,
                CreatedBy   = model.UserId,
                Status      = 0,
            };

            _deviceMasterRepository.Insert(currentDeviceId);
            //_dbContext.SaveChanges();

            return(currentDeviceId.Id);
        }
Beispiel #3
0
        public void Init(string UserId)
        {
            DeviceId = "";
            var          obj = _unitOfWork.GetRepository <DeviceMaster>().GetList(predicate: x => x.DeviceId == DeviceCode);
            DeviceMaster dm  = null;

            if (obj != null)
            {
                dm = obj.Items.FirstOrDefault();
            }

            if (dm == null)
            {
                var RequestToAuthorise = new DeviceMaster()
                {
                    DeviceName = "UnKnown", DeviceId = DeviceCode
                };

                _unitOfWork.GetRepository <DeviceMaster>().Add(RequestToAuthorise);
                _unitOfWork.SaveChanges();
                return;
            }


            DeviceId = dm.OtherId.ToString();
        }
        public bool Update([FromBody] DeviceMaster _oDeviceMaster)
        {
            try
            {
                int output;
                using (var context = new DB001Core())
                {
                    var input = context.DeviceMasters
                                .Where(s => s.DeviceID == _oDeviceMaster.DeviceID)
                                .FirstOrDefault();

                    input.DeviceName = _oDeviceMaster.DeviceName;
                    input.Stock      = _oDeviceMaster.Stock;

                    output = context.SaveChanges();
                }
                return(Convert.ToBoolean(output));
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                context = null;
            }
        }
Beispiel #5
0
 private void SetupComm()
 {
     master = new DeviceMaster()
     {
         Timeout  = 500,
         PortName = "COM1",
         BaudRate = 38400
     };
 }
        public HttpResponseMessage AddDeviceMasterData(DeviceMaster objDeviceMaster, string Token, string AppKey)
        {
            string strJson  = string.Empty;
            var    response = this.Request.CreateResponse(HttpStatusCode.OK);

            try
            {
                Accountmeg objaccountmegment = new Accountmeg();
                string     result            = objaccountmegment.Getresult(AppKey, Token);
                if (result == "true")
                {
                    using (TTPAPIDataContext DB = new TTPAPIDataContext())
                    {
                        var deviceDublicate = (from dm in DB.DeviceMasters
                                               where dm.DeviceUniqueId == objDeviceMaster.DeviceUniqueId
                                               select dm).FirstOrDefault();
                        if (deviceDublicate == null)
                        {
                            DeviceMaster objDeviceMasters = new DeviceMaster();
                            objDeviceMasters.DeviceId       = objDeviceMaster.DeviceId;
                            objDeviceMasters.DeviceModel    = objDeviceMaster.DeviceModel;
                            objDeviceMasters.DeviceTypeId   = objDeviceMaster.DeviceTypeId;
                            objDeviceMasters.DeviceUniqueId = objDeviceMaster.DeviceUniqueId;
                            objDeviceMasters.AccountID      = objDeviceMaster.AccountID;
                            DB.DeviceMasters.InsertOnSubmit(objDeviceMasters);
                            DB.SubmitChanges();
                            strJson          = "{\"Result\":\"204\"}";
                            response.Content = new StringContent(strJson, Encoding.UTF8, "application/json");
                            return(response);
                        }
                        else
                        {
                            strJson          = "{\"Result\":\"Device UniqId Is Already Use\"}";
                            response.Content = new StringContent(strJson, Encoding.UTF8, "application/json");
                            return(response);
                        }
                    }
                }
                else
                {
                    strJson          = "{\"Result\":\"Invalide AppKey\"}";
                    response.Content = new StringContent(strJson, Encoding.UTF8, "application/json");
                    return(response);
                }
            }
            catch (Exception ex)
            {
                strJson          = "{\"Result\":\"" + ex.Message + "\"}";
                response.Content = new StringContent(strJson, Encoding.UTF8, "application/json");
                return(response);
            }
        }
Beispiel #7
0
 public static void Init()
 {
     Mod       = new ModuleMaster();
     Dic       = new DictionMaster();
     Area      = new AreaMaster();
     Track     = new TrackMaster();
     Device    = new DeviceMaster();
     Goods     = new GoodsMaster();
     Warn      = new WarningMaster();
     TileTrack = new TileTrackMaster();
     Role      = new RoleMaster();
     PreStart();
 }
Beispiel #8
0
        public void UpdateDeviceId(DeviceMasterViewModel model)
        {
            var DeviceIddata = _deviceMasterRepository.Table.FirstOrDefault(i => i.DeviceId == model.DeviceId && i.UserId == model.UserId && !i.IsDeleted);

            if (DeviceIddata != null)
            {
                var currentDeviceId = new DeviceMaster
                {
                    Id       = DeviceIddata.Id,
                    UserId   = DeviceIddata.UserId,
                    DeviceId = model.DeviceId,
                    IsEnable = DeviceIddata.IsEnable,
                    //IsDeleted = IpAddress.IsDeleted,
                    UpdatedDate = DateTime.UtcNow,
                    UpdatedBy   = DeviceIddata.UserId
                };

                _deviceMasterRepository.Update(currentDeviceId);
                //_dbContext.SaveChanges();
            }
        }
 public bool Add([FromBody] DeviceMaster _oDeviceMaster)
 {
     try
     {
         int output;
         using (var context = new DB001Core())
         {
             context.DeviceMasters.Add(_oDeviceMaster);
             output = context.SaveChanges();
         }
         return(Convert.ToBoolean(output));
     }
     catch (Exception ex)
     {
         throw;
     }
     finally
     {
         context = null;
     }
 }
Beispiel #10
0
        public async Task RefreshDevices()
        {
            Message message = null;

            try
            {
                message = await _client.SendAndReceiveCommand <Message>(new GetAllNewInfos(_lastUpdate), 5000);

                var result = message.Response;
                _lastUpdate = result?["currentTimestamp"]?.Value <long>() ?? throw new Exception("Server did not respond.");
                if (!_definitions.Any())
                {
                    var definitions = result["newCompatibilityConfiguration"]?["compatibleRadioStandards"]?.ToObject <CompatibleDevices[]>()?.SelectMany(x => x.Definitions);
                    _definitions.AddRange(definitions?.Where(x => x.DeviceType != null) ?? Array.Empty <DeviceDefinition>());
                }
                _rooms.AddRange(result["newRoomInfos"]?.ToObject <RoomInfo[]>()?.Where(x => x.RoomName != "${Service}") ?? Array.Empty <RoomInfo>());

                if (result["newDeviceInfos"].HasValues)
                {
                    Raw = result;
                }
                var deviceInfo = result["newDeviceInfos"]?.ToObject <DeviceInfo[]>() ?? Array.Empty <DeviceInfo>();
                Parallel.ForEach(deviceInfo, x =>
                {
                    x.Definition = _definitions.FirstOrDefault(y => y.DeviceDesignation == x.DeviceDesignation);
                });
                foreach (var masterDevice in deviceInfo.GroupBy(x => x.MasterDeviceId == 0 ? x.DeviceId : x.MasterDeviceId))
                {
                    var masterId = masterDevice.Key;
                    if (DeviceMasters.Any(x => x.Id == masterId))
                    {
                        continue;
                    }

                    var devices = masterDevice.ToArray();
                    var room    = _rooms.FirstOrDefault(x => devices.Any(y => y.RoomId == x.RoomId));
                    if (room == null)
                    {
                        continue;
                    }

                    var master = new DeviceMaster(masterId, room, devices)
                    {
                        GatewayDevice = _client.GatewayDevice
                    };
                    DeviceMasters.Add(master);
                }

                var values = result["newDeviceValues"]?.ToObject <DeviceValue[]>() ?? Array.Empty <DeviceValue>();
                foreach (var value in values)
                {
                    ClientDeviceUpdated(this, value);
                }

                Ready = true;
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Refresh failed\n{JsonConvert.SerializeObject(message)}");
            }
        }