예제 #1
0
        public PIRDevices(ILoggerFactory loggerFactory, IControlledAreas controlledAreas, ISlaveCollection slaveCollection)
        {
            _logger          = loggerFactory.CreateLogger <PIRDevices>();
            _detects         = new List <PIRDevice>();
            _controlledAreas = controlledAreas;
            _slaveCollection = slaveCollection as SlaveColection;

            //         bool startFresh = !System.IO.File.Exists(_xmlPath);
            //load(startFresh, loggerFactory);
            foreach (var rpi in _slaveCollection.zeros)
            {
                foreach (var rpiDevice in rpi.PirDevices)
                {
                    PIRDevice pir = new PIRDevice((Enums.ControlledAreas)rpiDevice.area, rpiDevice.deviceName, rpi.ipAddress);
                    pir.mcu.port = rpi.port;
                    _detects.Add(pir);
                }
            }
            foreach (var nodeMcu in _slaveCollection.nodeMCUs)
            {
                foreach (var nodeMcuDevice in nodeMcu.PirDevices)
                {
                    PIRDevice pir = new PIRDevice((Enums.ControlledAreas)nodeMcuDevice.area, nodeMcuDevice.deviceName, nodeMcu.ipAddress);
                    pir.mcu.port = nodeMcu.port;
                    _detects.Add(pir);
                }
            }
            _detects.ForEach(d => d.createLogger(loggerFactory));
            _detects.ForEach(d => d.controlledAreas = _controlledAreas.controlledAreas);
            return;
        }
예제 #2
0
        /// <summary>
        /// 2:Kitchen:1:Rec Room N:1:14:13878:53888:0:0|2:Kitchen:2:Rec Room E:0:12:0:0:0:0|2:Kitchen:3:Rec Room W:1:0:13879:53888:0:0|2:Kitchen:4:Rec Room S:0:15:0:0:0:0"
        /// </summary>
        /// <param name="cfgLine"></param>
        /// <returns></returns>
        private static PIRDevice fromStartUp(string mcuIPAddress, string line)
        {
            //areaId:areaName:deviceId:deviceName:inProgress:motionPin:startMillis:runToMillis:endMillis:duration
            //2:Kitchen:1:Rec Room N:1:14:13878:53888:0:0

            PIRDevice retVal = new PIRDevice();

            string[] parts    = line.Split(':');
            int      numParts = parts.Length;

            if (numParts != 4 && numParts != 10)
            {
                Console.WriteLine($"INVALID StartUp LINE: {line}");
                return(null);
            }

            try
            {
                int    p1             = 0;
                int    p2             = numParts == 4 ? 1 : 2;
                int    p3             = p2 + 1;
                int    areaId         = Convert.ToInt32(parts[p1]);
                int    remoteDeviceId = Convert.ToInt32(parts[p2]);
                string deviceName     = parts[p3];

                retVal = new PIRDevice((Enums.ControlledAreas)areaId, deviceName, mcuIPAddress);
                retVal.mcu.remoteDeviceId = remoteDeviceId;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"fromStartUp(): Unexpected error: {ex.Message}");
            }
            return(retVal);
        }
예제 #3
0
        //2:Kitchen:1:Rec Room N:1:14:13878:53888:0:0|2:Kitchen:2:Rec Room E:0:12:0:0:0:0|2:Kitchen:3:Rec Room W:1:0:13879:53888:0:0|2:Kitchen:4:Rec Room S:0:15:0:0:0:0
        internal static List <PIRDevice> buildListFromStartUp(string startUp, string mcuIPAddress)
        {
            List <PIRDevice> list = new List <PIRDevice>();

            //string startUp = "2:Kitchen:1:Rec Room N:1:14:13878:53888:0:0|2:Kitchen:2:Rec Room E:0:12:0:0:0:0|2:Kitchen:3:Rec Room W:1:0:13879:53888:0:0|2:Kitchen:4:Rec Room S:0:15:0:0:0:0";
            string[] lines = startUp.Split('|');

            foreach (string line in lines)
            {
                PIRDevice pir = fromStartUp(mcuIPAddress, line);
                list.Add(pir);
            }

            return(list);
        }
예제 #4
0
        public override async Task ReceiveAsync(string message)
        {
            //_logger.LogInformation($"ReceiveAsync({NickName}): {message}");

            //RFSwitchResponce responce = new RFSwitchResponce() { success = false};
            string responce = "";

            try
            {
                //MessageType
                if (message.Length < 14 || message.Substring(0, 1) != "{" && message.Substring(message.Length - 1, 1) != "}")
                {
                    responce = "Expecting JSON for MessageType Object.";
                }
                else
                {
                    var mcuMessage = JsonConvert.DeserializeObject <MCUMessage>(message);

                    switch (mcuMessage.MessageType)
                    {
                    case "startup":
                        //MCU has powered up. get the timestamp of the switch config
                    {
                        int identified = 0;
                        //areaId:areaName:deviceId:deviceName:inProgress:motionPin:startMillis:runToMillis:endMillis:duration
                        var mcuDevices = PIRDevice.buildListFromStartUp(mcuMessage.Message, this.NickName);
                        for (int i = 0; i < mcuDevices.Count; i++)
                        {
                            PIRDevice pirStartUp  = mcuDevices[i];
                            PIRDevice existingPir = this.webSocketHandler.pirDevices.Where(d => d.area == pirStartUp.area && d.deviceName == pirStartUp.deviceName && pirStartUp.mcu.ipAddress == this.NickName).FirstOrDefault();

                            if (existingPir != null)
                            {
                                existingPir.setWebSocketConnection(pirStartUp.mcu.remoteDeviceId, this);
                                //existingPir.mcu.remoteDeviceId = pirStartUp.mcu.remoteDeviceId;
                                identified++;
                                //existingPir.Connection = this;
                            }

                            /*else
                             * {
                             *      _logger.LogWarning($"Adding motion from StartUp: Area {pirStartUp.areaName} Name: {pirStartUp.deviceName}");
                             *      pirStartUp.mcu.connection = this;
                             *      this.webSocketHandler.areaDetections.Add(pirStartUp);
                             * }*/
                        }

                        if (this.NickName.StartsWith("RpiZero"))
                        {
                            responce = $"Startup complete. {mcuDevices.Count} detections received. {identified} identified";
                        }
                    }
                    break;

                    case "start":
                        //start:1
                    {
                        if (string.IsNullOrEmpty(mcuMessage.Message))
                        {
                            _logger.LogError($"ReceiveAsync({NickName}): Empty Message!!");
                            break;
                        }

                        int       remoteDeviceId = Convert.ToInt32(mcuMessage.Message);
                        PIRDevice pirdevice      = this.webSocketHandler.pirDevices.Where(d => d.mcu.ipAddress == this.NickName && d.mcu.remoteDeviceId == remoteDeviceId).FirstOrDefault();
                        if (pirdevice != null)
                        {
                            await pirdevice.startDetecting();
                        }
                        else
                        {
                            responce = $"Start failed: Unable to find PIRDevice (id={remoteDeviceId}, mcu={this.NickName})";
                        }

                        //await setRelayStates(pirdevice.area, true);
                    }
                    break;

                    case "stop":
                        ///stop:1
                    {
                        if (string.IsNullOrEmpty(mcuMessage.Message))
                        {
                            _logger.LogError($"ReceiveAsync({NickName}): Empty Message!!");
                            break;
                        }

                        int       remoteDeviceId = Convert.ToInt32(mcuMessage.Message);
                        PIRDevice pirdevice      = this.webSocketHandler.pirDevices.Where(d => d.mcu.ipAddress == this.NickName && d.mcu.remoteDeviceId == remoteDeviceId).FirstOrDefault();
                        if (pirdevice != null)
                        {
                            pirdevice?.stopDetecting();
                        }
                        else
                        {
                            responce = $"Stop failed: Unable to find PIRDevice (id={remoteDeviceId}, mcu={this.NickName})";
                        }
                        //await setRelayStates(pirdevice.area, false);
                    }
                    break;

                    case "ping":
                    {
                        responce = $"ping:received from PIRController: {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);
             * }*/
        }