Ejemplo n.º 1
0
        /// <summary>
        /// Main service porling task
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task ServiceProc(CancellationToken cancellationToken)
        {
            while (cancellationToken.IsCancellationRequested == false)
            {
                var now = DateTime.UtcNow;
                try
                {
                    await Task.Delay(ResolutionMilliSeconds, cancellationToken); // upload 10s each to reduce request count to Azure.

                    var http = WebRequest.Create($"{SEC.Url}/snapshot.jpg");
                    http.Method      = "GET";
                    http.ContentType = "image/jpeg";
                    http.Credentials = new NetworkCredential(SEC.UserName, SEC.Password);

                    now = DateTime.UtcNow;
                    var res = await http.GetResponseAsync();

                    using (var bin = new BinaryReader(res.GetResponseStream()))
                    {
                        var dat = new CircumstancesKeeperModel
                        {
                            Key       = MakeKey(now),
                            EventTime = now,
                            ImageData = bin.ReadBytes((int)res.ContentLength),
                            ImageType = ".jpg",
                        };
                        lock (UploadQueue)
                        {
                            UploadQueue.Enqueue(dat);
                        }
                    }
                }
                catch (WebException ex)
                {
                    var dat = new CircumstancesKeeperModel
                    {
                        Key          = MakeKey(now),
                        EventTime    = now,
                        ImageData    = new byte[] { },
                        ImageType    = ".ERR",
                        ErrorMessage = $"{ex.Message}",
                    };
                    lock (UploadQueue)
                    {
                        UploadQueue.Enqueue(dat);
                    }
                }
                catch (Exception ex)
                {
                    eventlog($"{ex.Message} {ex.StackTrace}");
                    CancelHandler.Cancel();
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Upload to Azure
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private void UploadProc(object prm)
        {
            var cancellationToken = (CancellationToken)prm;
            var item   = new CircumstancesKeeperModel();
            var waitms = 1000;

            try
            {
                while (cancellationToken.IsCancellationRequested == false)
                {
                    Task.Delay(waitms, cancellationToken).Wait();

                    lock (UploadQueue)
                    {
                        if (UploadQueue.Count > 0)
                        {
                            item = UploadQueue.Dequeue();
                        }
                        else
                        {
                            waitms = 1000;
                            continue;
                        }
                    }

                    var blobcontainer = BlobClient.GetContainerReference($"cam-{item.EventTime.ToString("yyyyMMdd")}-{SEC.Location}");
                    blobcontainer.CreateIfNotExists();
                    var fname = $"{item.Key}{item.ImageType}";
                    var file  = blobcontainer.GetBlockBlobReference(fname);
                    file.Metadata["EventTime"] = item.EventTime.ToString(TimeUtil.FormatYMDHMSms);

                    if (item.ImageType != ".ERR")
                    {
                        file.Metadata["Length"] = item.ImageData.Length.ToString();
                        file.UploadFromByteArray(item.ImageData, 0, item.ImageData.Length);
                    }
                    else
                    {
                        file.Metadata["Length"] = "0";
                    }
                    waitms = 10;
#if DEBUG
                    eventlog($"{item.EventTime.ToString(TimeUtil.FormatYMDHMSms)} -- uploaded : {fname}");
#endif
                }
            }
            catch (Exception ex)
            {
                eventlog($"{ex.Message}  {ex.StackTrace}");
                CancelHandler.Cancel();
            }
        }