public IHttpActionResult Something() { var obj = new { email = FakeO.Internet.Email() }; var context = NotificationHub.GetHubContext(); context.Clients.All.ping(obj); return(Ok(new ApiResponse(200, obj))); }
public async Task <IHttpActionResult> Light(LightApiModel model) { if (ModelState.IsValid && model != null) { collection = mongoDb.GetCollection <object>("light"); var userId = (await db.Devices.FirstAsync(x => x.UniqueId.Equals(model.DeviceId))).UserId; var mapped = Mapper.Map <LightModel>(model); mapped.Created = DateTime.Now; mapped.UserId = userId; var bson = BsonSerializer.Deserialize <ExpandoObject>(BsonDocument.Parse(JsonConvert.SerializeObject(mapped))); await collection.InsertOneAsync(bson); var user = await userManager.FindByIdAsync(userId); var channel = user.Channels.FirstOrDefault(x => x.ChannelId.Equals(5) && x.UserId.Equals(userId)); if (channel != null) { List <LightModelExtended> data = null; if (channel.Price > 0) { var result = (await collection.FindAsync <LightModelExtended>(new BsonDocument(), new FindOptions <object, LightModelExtended> { //Limit = (int)channel.MaxPayedRows })); data = await result.ToListAsync(); } else { data = await(await collection.FindAsync <LightModelExtended>(new BsonDocument("UserId", userId))).ToListAsync(); } NotificationHub.GetHubContext().Clients.All.light(data.ToArray()); return(Ok(new ApiResponse(200, mapped))); } } return(this.BadRequest(new ApiResponse(400))); }
public async Task <IHttpActionResult> Location(LocationApiModel model) { if (ModelState.IsValid && model != null) { collection = mongoDb.GetCollection <object>("location"); var userId = (await db.Devices.FirstAsync(x => x.UniqueId.Equals(model.DeviceId))).UserId; var mapped = Mapper.Map <LocationModel>(model); mapped.Created = DateTime.Now; mapped.UserId = userId; var bson = BsonSerializer.Deserialize <ExpandoObject>(BsonDocument.Parse(JsonConvert.SerializeObject(mapped))); await collection.InsertOneAsync(bson); NotificationHub.GetHubContext().Clients.All.location(mapped); return(Ok(new ApiResponse(200, mapped))); } return(this.BadRequest(new ApiResponse(400))); }
public async Task <IHttpActionResult> Movement(MovementApiModel model) { if (ModelState.IsValid && model != null) { collection = mongoDb.GetCollection <object>("movement"); var userId = (await db.Devices.FirstAsync(x => x.UniqueId.Equals(model.DeviceId))).UserId; var mapped = Mapper.Map <MovementModel>(model); mapped.Created = DateTime.Now; mapped.UserId = userId; var bson = BsonSerializer.Deserialize <ExpandoObject>(BsonDocument.Parse(JsonConvert.SerializeObject(mapped))); await collection.InsertOneAsync(bson); List <object> response = new List <object>(); var channel = await db.Channels.FirstAsync(x => x.ChannelId.Equals(3) && x.UserId.Equals(userId)); List <MovementModelExtended> data = null; if (channel.Price > 0) { var result = (await collection.FindAsync <MovementModelExtended>(new BsonDocument(), new FindOptions <object, MovementModelExtended> { //Limit = (int)channel.MaxPayedRows })); data = await result.ToListAsync(); } else { data = await(await collection.FindAsync <MovementModelExtended>(new BsonDocument("UserId", userId))).ToListAsync(); } for (int i = 1; i <= 24; i++) { int count = 0; var obj = new ExpandoObject(); foreach (var date in data) { if (i.Equals(date.Created.Hour)) { count += 1; if (((IDictionary <string, object>)obj).ContainsKey("date")) { ((IDictionary <string, object>)obj)["date"] = date.Created; } else { obj.AddProp("date", date.Created); } } } var dict = ((IDictionary <string, object>)obj); if (dict.ContainsKey("date")) { obj.AddProp("hour", ((DateTime)dict["date"]).Hour); obj.AddProp("count", count); response.Add(obj); } } NotificationHub.GetHubContext().Clients.All.movement(new ApiResponse(200, response.ToArray())); return(Ok(new ApiResponse(200, mapped))); } return(this.BadRequest(new ApiResponse(400))); }
public async Task <IHttpActionResult> Fridge(FridgeApiModel model) { if (ModelState.IsValid && model != null) { collection = mongoDb.GetCollection <object>("fridge"); var userId = (await db.Devices.FirstAsync(x => x.UniqueId.Equals(model.DeviceId))).UserId; var mapped = Mapper.Map <FridgeModel>(model); mapped.Created = DateTime.Now; mapped.UserId = userId; var bson = BsonSerializer.Deserialize <ExpandoObject>(BsonDocument.Parse(JsonConvert.SerializeObject(mapped))); await collection.InsertOneAsync(bson); List <object> response = new List <object>(); var channel = await db.Channels.FirstAsync(x => x.ChannelId.Equals(2) && x.UserId.Equals(userId)); List <FridgeModelExtended> data = null; if (channel.Price > 0) { var result = (await collection.FindAsync <FridgeModelExtended>(new BsonDocument(), new FindOptions <object, FridgeModelExtended> { //Limit = (int)channel.MaxPayedRows })); data = await result.ToListAsync(); } else { try { data = await(await collection.FindAsync <FridgeModelExtended>(new BsonDocument("UserId", userId))).ToListAsync(); } catch (Exception e) { Debug.WriteLine(e.Message); } } if (data != null) { var distinct = data.Select(x => x.Created.ToShortDateString()).Distinct(); foreach (var date in distinct) { int count = 0; var obj = new ExpandoObject(); foreach (var item in data) { if (item.Created.ToShortDateString().Equals(date)) { count += 1; } } obj.AddProp("count", count); obj.AddProp("date", DateTime.Parse(date)); response.Add(obj); } NotificationHub.GetHubContext().Clients.All.fridge(new ApiResponse(200, response.ToArray())); return(Ok(new ApiResponse(200, mapped))); } else { return(this.NotFound(new ApiResponse(404))); } } return(this.BadRequest(new ApiResponse(400))); }