public RFSwitches(ILoggerFactory loggerFactory, IControlledAreas controlledAreas, ISlaveCollection slaveCollection) { _logger = loggerFactory.CreateLogger <RFSwitches>(); _switches = new List <RFSwitchDevice>(); _controlledAreas = controlledAreas; //_piCollection = piCollection as PiCollection; _slaveCollection = slaveCollection as SlaveColection; // bool startFresh = !System.IO.File.Exists(_xmlPath); //load(startFresh, loggerFactory); foreach (var rpi in _slaveCollection.zeros) { foreach (var rfDevice in rpi.RFSwitches) { RFSwitchDevice sw = new RFSwitchDevice((Enums.ControlledAreas)rfDevice.area, rfDevice.deviceName, rfDevice.bitLength); sw.mcu.ipAddress = rpi.ipAddress; sw.mcu.port = rpi.port; rfDevice.onCodes.Skip(1).ToList().ForEach(c => sw.addCode(true, (Convert.ToInt64(c)))); rfDevice.offCodes.Skip(1).ToList().ForEach(c => sw.addCode(false, (Convert.ToInt32(c)))); _switches.Add(sw); } } foreach (var nodeMcu in _slaveCollection.nodeMCUs) { foreach (var rfDevice in nodeMcu.RFSwitches) { RFSwitchDevice sw = new RFSwitchDevice((Enums.ControlledAreas)rfDevice.area, rfDevice.deviceName, rfDevice.bitLength); sw.mcu.ipAddress = nodeMcu.ipAddress; sw.mcu.port = nodeMcu.port; rfDevice.onCodes.Skip(1).ToList().ForEach(c => sw.addCode(true, (Convert.ToInt64(c)))); rfDevice.offCodes.Skip(1).ToList().ForEach(c => sw.addCode(false, (Convert.ToInt32(c)))); _switches.Add(sw); } } _switches.ForEach(d => d.createLogger(loggerFactory)); _switches.ForEach(d => d.controlledAreas = _controlledAreas.controlledAreas); return; }
//public string NickName { get; set; } public override async Task ReceiveAsync(string message) { _logger.LogInformation($"ReceiveAsync({NickName}): {message}"); //RFSwitchResponce responce = new RFSwitchResponce() { success = false}; string responce = ""; try { Enums.ControlledAreas area; HomeControlSocketHandler hdr = this.webSocketHandler as HomeControlSocketHandler; var rcv = JsonConvert.DeserializeObject <RFSwitchMessage>(message); string[] parts = rcv.Message.Split(':'); int areaId, deviceId, swState; string deviceName; uint epoch; RFSwitchDevice sw = null;; switch (rcv.MessageType) { case "startup": //MCU has powered up. get the timestamp of the switch config //If the the MCU's timestamp matches the WebAPI's timestamp then { epoch = Convert.ToUInt32(rcv.Message); if (UInt32.TryParse(rcv.Message, out epoch)) { //does the MCU timestamp match the server's RFSwitch config timestamp? configIdentifier = epoch; DateTime dateTime = epoch.fromUnixTimestamp(); //_startupConfigIds[NickName] = new Tuple<uint, DateTime>(epochTimeStamp, dateTime); _logger.LogInformation($"ReceiveAsync({NickName}): Startup Timestamp ({epoch}) converts to {dateTime.ToString()}"); //TODO: Activate RFSwitches //await hdr.rfCollection.activateSwitches(this, epoch); } else { responce = $"Invalid startup Message: {rcv.Message}"; } } break; case "switchActivated": //switchActivated:areaId:deviceName:deviceId //areaId:deviceName:deviceId if (string.IsNullOrEmpty(rcv.Message)) { _logger.LogError($"ReceiveAsync({NickName}): Empty Message!!"); break; } if (parts.Length < 3) { _logger.LogError($"ReceiveAsync({NickName}): Invalid switchActivated Message!!"); break; } areaId = Convert.ToInt32(parts[0]); deviceName = parts[1]; deviceId = Convert.ToInt32(parts[2]); area = (Enums.ControlledAreas)areaId; sw = hdr.rfSwitches.Where(s => (s.area == area) && s.deviceName == deviceName && s.mcu.ipAddress == this.NickName).FirstOrDefault(); if (sw != null) // && sw.swState != swState) { sw.setWebSocketConnection(deviceId, this); //var mcu = sw.slaves.Where(s => s.ipAddress == NickName).FirstOrDefault(); //if (mcu != null) //{ // mcu.remoteDeviceId = deviceId; //} } break; case "configIdChanged": break; case "switchchanged": if (string.IsNullOrEmpty(rcv.Message)) { _logger.LogError($"ReceiveAsync({NickName}): Empty Message!!"); break; } ///switchchanged:areaId:deviceName:swState:lastOnState:lastOffState //areaId:deviceName:swState if (parts.Length < 3) { _logger.LogError($"ReceiveAsync({NickName}): Invalid switchchanged Message!!"); break; } areaId = Convert.ToInt32(parts[0]); deviceName = parts[1]; swState = Convert.ToInt32(parts[2]); area = (Enums.ControlledAreas)areaId; sw = hdr.rfSwitches.Where(s => (s.area == area) && s.deviceName == deviceName && s.mcu.ipAddress == this.NickName).FirstOrDefault(); if (sw != null) { if (sw.swState != swState) { sw.setState(swState, true); //sw.swState = swState; //AreaEventType aet = swState > 0 ? AreaEventType.SwitchOn : AreaEventType.SwitchOff; //AreaEvent evt = new AreaEvent(sw.area, deviceName, aet, NickName, AreaEventStatus.Complete); //sw.Events.Add(evt); //EventLogger.logEvent(evt); } else { _logger.LogInformation($"switchChanged() Done. State was unchanged."); } } break; case "ping": { responce = $"ping:received from RFController: {NickName}"; } break; } } catch (Exception ex) { _logger.LogError($"ReceiveAsync() Unexpected error: {ex.Message}"); } bool sendResponce = true; if (sendResponce && !string.IsNullOrEmpty(responce)) { await SendMessageAsync(responce); } /* * var receiver = Handler.Connections.FirstOrDefault(m => ((RFSwitchConnection)m).NickName == receiveMessage.Receiver); * * if (receiver != null) * { * var sendMessage = JsonConvert.SerializeObject(new SendMessage * { * Sender = NickName, * Message = receiveMessage.Message * }); * * await receiver.SendMessageAsync(sendMessage); * } * else * { * var sendMessage = JsonConvert.SerializeObject(new SendMessage * { * Sender = NickName, * Message = "Can not seed to " + receiveMessage.Receiver * }); * * await SendMessageAsync(sendMessage); * }*/ }
public static string getActivateMsg(RFSwitchDevice sw) { //setactive:1:Living Room Lamp:1 return($"setactive:{(int)sw.area}:{sw.deviceName}:{sw.swState}"); }