public ActionResult <RFSwitchDevice> getSwitch(Enums.ControlledAreas area, string deviceName) { _logger.LogInformation($"getSwitch() Entered. area: {area} device: {deviceName}"); RFSwitchDevice sw = rfSwitches.Where(s => s.area == area && s.deviceName.ToLower() == deviceName.ToLower()).FirstOrDefault(); if (sw != null) { return(sw); } return(NotFound()); }
//[HttpGet("[action]/{id}")] public ActionResult <RFSwitchDevice> Get(int id) { _logger.LogInformation($"GetById() Entered. deviceId: {id}"); RFSwitchDevice sw = rfSwitches.Where(s => s.localDeviceId == id).FirstOrDefault(); if (sw != null) { return(sw); } return(NotFound()); }
public async Task <ActionResult <RFSwitchDevice> > setSwitch(Enums.ControlledAreas area, string deviceName, int swState) { RFSwitchDevice sw = rfSwitches.Where(s => s.area == area && s.deviceName.ToLower() == deviceName.ToLower()).FirstOrDefault(); if (sw == null) { return(NotFound()); } else { return(await setSwitch(sw.localDeviceId, swState)); } }
public async Task <ActionResult <RFSwitchDevice> > setSwitch(int localId, int swState) { //var ipAddr = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress; var clientIP = HttpContext.Features.Get <IHttpConnectionFeature>()?.RemoteIpAddress.ToString(); if (clientIP == "::1") { string[] parts = Environment.GetEnvironmentVariable("HOSTPORT").Split(':'); clientIP = parts[0]; } _logger.LogInformation($"SetSwitch() Entered. deviceId: {localId} State {swState} from {clientIP}"); RFSwitchDevice sw = rfSwitches.Where(s => s.localDeviceId == localId).FirstOrDefault(); if (sw == null) { return(NotFound()); } else //if (sw.swState != swState) { sw.setState(swState, true); //sw.lastOnState = DateTime.Now.Ticks; //AreaEventType eat = (swState > 0) ? AreaEventType.SwitchOn : AreaEventType.SwitchOff; //AreaEvent evt = new AreaEvent(sw.area, sw.deviceName, eat, clientIP); //sw.Events.Add(evt); //EventLogger.logEvent(evt); //sw.swState = swState; //sw.lastOnState = DateTime.Now.Ticks; await sw.mcu?.connection?.SendMessageAsync($"switchchanged:{sw.mcu.remoteDeviceId}:{sw.swState}"); //foreach (var mcu in sw.slaves.Where(s => s.connection != null && s.deviceType == Enums.EnumDeviceType.RFSwitch)) //{ // //await mcu.connection.SendMessageAsync($"switchchanged:{(int)sw.area}:{sw.deviceName}:{sw.swState}"); // await mcu.connection.SendMessageAsync($"switchchanged:{mcu.remoteDeviceId}:{sw.swState}"); //} //switchCollection.save(); return(sw); } }
public ActionResult <RFSwitchDevice> switchChanged(int areaId, string deviceName, int swState) { var clientIP = HttpContext.Features.Get <IHttpConnectionFeature>()?.RemoteIpAddress.ToString(); lock (clientIP) { _logger.LogInformation($"switchChanged() Entered. areaId: {areaId} deviceName: {deviceName} state: {swState} from {clientIP}"); RFSwitchDevice sw = rfSwitches.Where(s => s.area == (Enums.ControlledAreas)areaId && s.deviceName == deviceName).FirstOrDefault(); if (sw == null) { return(NotFound()); } if (sw.swState != swState) { sw.setState(swState, true); //sw.swState = swState; //if (swState > 0) // sw.lastOnState = DateTime.Now.Ticks; //else // sw.lastOffState = DateTime.Now.Ticks; //AreaEventType aet = swState > 0 ? AreaEventType.SwitchOn : AreaEventType.SwitchOff; //AreaEvent evt = new AreaEvent(sw.area, deviceName, aet, sw.mcu.ipAddress, AreaEventStatus.Complete); //sw.Events.Add(evt); //EventLogger.logEvent(evt); } else { _logger.LogInformation($"switchChanged() Done. State was unchanged."); } return(sw); } }