示例#1
0
        public async Task <HttpResponseMessage> GetFirmware(string deviceName)
        {
            string tag = Request.Headers.IfNoneMatch
                         .Select(eTag => eTag.Tag.Replace("\"", ""))
                         .FirstOrDefault();
            FirmwareUpgrade upgrade = await devicesService.GetNewFirmware(deviceName, tag);

            await devicesService.UpdateLastContactTime(deviceName);

            if (upgrade != null)
            {
                // run HEXMOD on the upgrade file (get flash attributes Start, Length, CRC16-CCITT (iReflected, oReflected))
                HexFileUtility.FlashCRCData flashCRC = HexFileUtility.ProcessHEX(upgrade.FileData);

                var response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new ByteArrayContent(upgrade.FileData.Concat(flashCRC.bytesToAppend).ToArray());
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Headers.ETag = new EntityTagHeaderValue(String.Format("\"{0}\"", upgrade.FileHash), false);
                return(response);
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.NoContent));
            }
        }
        public async Task <ActionResult> Create(FormCollection form)
        {
            FirmwareUpgradeViewModel vm = new FirmwareUpgradeViewModel();

            if (TryUpdateModel(vm))
            {
                byte[] upgradeBytes = null;
                using (BinaryReader r = new BinaryReader(vm.UploadedFile.InputStream))
                {
                    upgradeBytes = r.ReadBytes(vm.UploadedFile.ContentLength);
                }

                // Calculate MD5 of the file.
                string fileMD5 = HexFileUtility.GetMd5Hash(upgradeBytes);

                FirmwareUpgrade upgrade = new FirmwareUpgrade
                {
                    Name     = vm.Name,
                    FileHash = fileMD5,
                    FileData = upgradeBytes,
                };
                upgrade = await upgradesService.CreateFirmwareUpgrade(upgrade);

                if (upgrade == null)
                {
                    ModelState.AddModelError("UpgradeCreationFailed", "Failed to create upgrade. Does an upgrade with this name already exist?");
                    return(View(vm));
                }
            }

            return(RedirectToAction("Index"));
        }