public async Task <ActionResult <ApiResponse> > PostHeartbeat(RmsConfigModel model) { if (model == null) { return(new ApiResponse { Status = Status.Error, Message = "model was empty" }); } try { await FlushDatabase(); await CreateDbFromModel(model); return(new ApiResponse { Status = Status.Success, Message = "Device db recreated sucessfully" }); } catch (Exception ex) { return(StatusCode(500, new ApiResponse { Status = Status.Error, Message = ex.Message })); } }
private async Task CreateDbFromModel(RmsConfigModel model) { RmsConfig rms = new RmsConfig { Name = model.RmsDeviceInterface.name, Location = model.RmsDeviceInterface.location, IpAddress = model.RmsDeviceInterface.localIp, MacAddress = model.RmsDeviceInterface.macAddress }; foreach (RmsNode node in model.nodes) { foreach (RmsNodeDevice nodeDevice in node.devices) { DeviceType deviceType = ParseLightType(nodeDevice.type); if (deviceType == DeviceType.LightOnOff) { Light newLight = new Light { Location = node.location, Type = deviceType, MAC = nodeDevice.macAddress, CurrentStatus = nodeDevice.status }; newLight.Hooks.Add(new WebHook { ActivationCommand = RmsCommand.LightsOn, HookUrl = BuildHookUrl(nodeDevice, model.RmsDeviceInterface.localIp), Method = "POST", Body = BuildHookActiveBody(node, newLight) }); newLight.Hooks.Add(new WebHook { ActivationCommand = RmsCommand.LightsOff, HookUrl = BuildHookUrl(nodeDevice, model.RmsDeviceInterface.localIp), Method = "POST", Body = BuildHookInactiveBody(node, newLight) }); rms.Lights.Add(newLight); } if (deviceType == DeviceType.Hvac) { Hvac newHvac = new Hvac { Location = node.location, MAC = nodeDevice.macAddress }; double currentTemp; if (double.TryParse(nodeDevice.currentTemperature, out currentTemp)) { newHvac.CurrentTemperature = currentTemp; } double setpointTemp; if (double.TryParse(nodeDevice.setTemperature, out setpointTemp)) { newHvac.SetpointTemperature = setpointTemp; } newHvac.Hooks.Add(new WebHook { ActivationCommand = RmsCommand.HvacSetTemperature, HookUrl = BuildHookUrl(nodeDevice, model.RmsDeviceInterface.localIp), Method = "POST", Body = BuildHookSetTemperatureBody(node, newHvac) }); rms.Hvacs.Add(newHvac); } } } _context.RmsList.Add(rms); _context.SaveChanges(); }
private void UpdateDevice(RmsConfigModel model) { Light device = _context.Lights.SingleOrDefault(rmsDevice => rmsDevice.MAC == model.RmsDeviceInterface.macAddress); foreach (RmsNode node in model.nodes) { foreach (RmsNodeDevice nodeDevice in node.devices) { //TODO: update hooks here } } _context.SaveChanges(); }