Example #1
0
        public async Task <ActionResult <iot_sample> > Postiot_sample(iot_sample iot_sample)
        {
            // if timestamp is not provided, default it to Now
            if (iot_sample.timestamp == DateTime.MinValue)
            {
                iot_sample.timestamp = DateTime.Now;
            }

            // calculate or validate calendarday
            String lTimestampDay = iot_sample.timestamp.ToString("yyyy-MM-dd");

            // start of hack (as I am not able to call another controller
            //var iot_calendarday = await iot_calendardayController.Getiot_calendarday_getorcreatebydate(lTimestampDay);
            var iot_calendarday = await _context.iot_calendarday.Where(cd => cd.date == lTimestampDay).FirstOrDefaultAsync();

            if (iot_calendarday == null)
            {
                iot_calendarday = new iot_calendarday {
                    date = lTimestampDay
                };
                _context.iot_calendarday.Add(iot_calendarday);
                await _context.SaveChangesAsync();

                var lMsg2  = new ServerUpdateHubMsg("iot_calendarday", ServerUpdateHubMsg.TOperation.INSERT, iot_calendarday.id);
                var lJson2 = JsonConvert.SerializeObject(lMsg2);
                await _hubContext.Clients.All.SendAsync(lMsg2.entity, lJson2);
            }
            // end of the hack

            if (iot_calendarday == null)
            {
                return(NotFound());
            }
            if (iot_sample.calendardayid == null)
            {
                iot_sample.calendardayid = iot_calendarday.id;
            }
            else
            if (iot_sample.calendardayid != iot_calendarday.id)
            {
                return(BadRequest());
            }

            _context.iot_sample.Add(iot_sample);
            await _context.SaveChangesAsync();

            iot_sample.device      = null;
            iot_sample.calendarday = null;

            var lMsg  = new ServerUpdateHubMsg(_entity, ServerUpdateHubMsg.TOperation.INSERT, iot_sample.id);
            var lJson = JsonConvert.SerializeObject(lMsg);
            await _hubContext.Clients.All.SendAsync(lMsg.entity, lJson);

            return(CreatedAtAction("Getiot_sample", new { id = iot_sample.id }, iot_sample));
        }
Example #2
0
        public async Task <IActionResult> Putiot_sample(long id, iot_sample iot_sample)
        {
            if (id != iot_sample.id)
            {
                return(BadRequest());
            }

            if (iot_sample.importance == null)
            {
                iot_sample.importance = 0;
            }

            _context.Entry(iot_sample).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!iot_sampleExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            var lMsg  = new ServerUpdateHubMsg(_entity, ServerUpdateHubMsg.TOperation.UPDATE, id);
            var lJson = JsonConvert.SerializeObject(lMsg);
            await _hubContext.Clients.All.SendAsync(lMsg.entity, lJson);

            return(NoContent());
        }
Example #3
0
        public async Task <ActionResult <iot_sample> > Postiot_sample_devicecode(string pDeviceCode, iot_sample iot_sample)
        {
            // find device
            if (pDeviceCode == null)
            {
                return(NotFound());
            }

            var iot_device = await _context.iot_device.Where(d => d.code == pDeviceCode).FirstOrDefaultAsync();

            if (iot_device == null)
            {
                return(NotFound());
            }

            if (iot_sample.deviceid == null)
            {
                iot_sample.deviceid = iot_device.id;
            }
            else
            if (iot_sample.deviceid != iot_device.id)
            {
                return(BadRequest());
            }

            iot_sample.device = null;

            return(await Postiot_sample(iot_sample));
        }