示例#1
0
        public Task Handle(CheckpointMessage cmd)
        {
            if (string.IsNullOrWhiteSpace(cmd?.Identifier))
            {
                throw new ArgumentException("Identifier not specified.");
            }

            if (cmd.Latitude.Equals(default(double)))
            {
                throw new ArgumentException("Latitude is 0");
            }

            if (cmd.Longitude.Equals(default(double)))
            {
                throw new ArgumentException("Longitude is 0");
            }

            LastSeenAt = cmd.LastSeenAt;
            Longitude  = cmd.Longitude;
            Latitude   = cmd.Latitude;

            var stream = System.StreamOf("default", "checkpoint");

            stream.Push(cmd);

            return(Task.FromResult(0));
        }
        public CheckpointModule()
        {
            Patch["/checkpoint/{identifier}", true] = async(parameters, cancellectionToken) =>
            {
                if (string.IsNullOrWhiteSpace(parameters.identifier))
                {
                    var response = (Response)"Identifier missing";
                    response.StatusCode = HttpStatusCode.BadRequest;
                    return(response);
                }

                var device = Program.Orleankka.ActorOf(typeof(DeviceActor), parameters.identifier);

                Instant now = SystemClock.Instance.GetCurrentInstant();

                var checkpoint = new CheckpointMessage()
                {
                    Identifier = parameters.identifier,
                    Longitude  = parameters.longitude,
                    Latitude   = parameters.latitude,
                    LastSeenAt = SystemClock.Instance.GetCurrentInstant()
                };

                try
                {
                    await device.On(checkpoint);

                    var response = (Response)"Success";
                    response.StatusCode = HttpStatusCode.OK;
                    return(response);
                }
                catch (Exception ex)
                {
                    // warning: this is poc code, returning exceptions to client is a security
                    // risk factor.
                    var response = (Response)ex.Message;
                    response.StatusCode = HttpStatusCode.BadRequest;
                    return(response);
                }
            };
        }