Example #1
0
        private void IotServer_NewRequestReceived(IotSession session, IotRequest req)
        {
            logger.Debug($"IoT-Rece: {session.RemoteEndPoint} {req.Device.Mac}-{req.SessionId} {req.Command}");

            IotRequest request = null;

            //var hasErrorRequest = false;
            //var hasErrorResponse = false;
            if (!req.CheckCrc16())
            {
                if (!req.Command.IsResponse)
                {
                    request = req.ErrorCrc();
                }
                else
                {
                    //hasErrorRequest = true;
                }
            }
            else
            {
                switch (req.Command.Name)
                {
                case "00F1":
                {
                    if (!req.CheckSession())
                    {
                        request = CreateErrorSession(req);
                        break;
                    }
                    if (!req.CheckValid() /* && !CheckDevice(req.Device.Mac)*/)
                    {
                        request = req.ErrorDevice();
                        break;
                    }
                    request                  = req.Ok(interval.ToHex(2));
                    request.SessionId        = RandomHelper.GetRandomHex(4);
                    session.SessionId        = request.SessionId;
                    session.Device           = req.Device;
                    sessions[req.Device.Mac] = session;
                    Task.Run(() => DoLogin(req));
                    break;
                }

                case "0001":
                {
                    if (!req.CheckSession(sessions, session))
                    {
                        request = CreateErrorSession(req);
                        break;
                    }
                    Task.Run(() => DoUpload(req));
                    request = req.Ok(GetWeaHex(req.Device.Mac));
                    break;
                }

                case "0002":
                {
                    if (!req.CheckSession(sessions, session))
                    {
                        request = CreateErrorSession(req);
                        break;
                    }
                    Task.Run(() => DoOnline(req));
                    request = req.Ok(DatetimeHelper.GetNowHex());
                    break;
                }

                default:
                {
                    if (req.Command.IsResponse)
                    {
                        if (!req.CheckSession(sessions, session))
                        {
                            //hasErrorResponse = true;
                        }
                        else
                        {
                            api.Dispatch(Name, req.Device.Mac, $"BACK-{req.Command.Name}", $"{req.Command.DataHex}");
                        }
                    }
                    else if (!req.CheckSession(sessions, session))
                    {
                        request = CreateErrorSession(req);
                    }
                    else
                    {
                        Task.Run(() => DoUpData(req));
                        request = req.Ok();
                    }
                    break;
                }
                }
            }

            if (request != null)
            {
                Send(session, request);
            }
        }