Esempio n. 1
0
            public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
                                                         [SignalR(HubName = "BroadcastClientMessage")] IAsyncCollector <SignalRMessage> signalRMessages,
                                                         [SignalRConnectionInfo(HubName = "BroadcastClientMessage")] SignalRConnectionInfo info,
                                                         ILogger log)
            {
                int nSuppressMessage = await Task.Run(() => { return(99); });

                ClientMessage cm = new ClientMessage();

                cm.Sender      = "MessageTest";
                cm.StationId   = "MyStation";
                cm.Direction   = "MyDirection";
                cm.AccessToken = "MyAccessToken";
                cm.Command     = "MyCommand";
                cm.Parameters  = "MyParameters";

                await SignalRClientComms.PublishMessageToSignalRClients(signalRMessages, cm.Sender, cm.StationId, cm.Direction,
                                                                        cm.AccessToken, cm.Command, cm.Parameters);

                /*
                 *         await signalRMessages.AddAsync(new SignalRMessage()
                 *         {
                 *             Target = "ClientMessage",
                 *             Arguments = new object[] { cm }
                 *         });
                 */
                return(new OkObjectResult("messagetest called ok"));
            }
Esempio n. 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [SignalR(HubName = "broadcast")] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            _logger = log;
            log.LogInformation("Performing UploadStationEvent.");
            int nSuppressMessage = await Task.Run(() => { return(99); });

            string HubDeviceId  = req.Query["HubDeviceId"];
            string HubDeviceKey = req.Query["HubDeviceKey"];
            string Event        = req.Query["Event"];

            // All threee parms must be present
            if (HubDeviceId == null || HubDeviceKey == null || Event == null)
            {
                return(new BadRequestObjectResult(Wrapper <ApiResult> .GetWrappedError("Invalid parameters", 400)));
            }

            // Check access Token
            DataAccess da         = new DataAccess(_logger);
            string     sDeviceKey = da.GetStationHubDeviceKeyFromStationHubId(HubDeviceId);

            if (!String.Equals(sDeviceKey, HubDeviceKey, StringComparison.OrdinalIgnoreCase))
            {
                return(new BadRequestObjectResult(Wrapper <ApiResult> .GetWrappedError("Invalid key", 400)));
            }

            if (String.Equals(Event, "Heartbeat"))
            {
                if (da.UpdateStationHeartbeat(HubDeviceId))
                {
                    return(new OkObjectResult(Wrapper <ApiResult> .GetWrappedSuccess()));
                }
            }

            if (String.Equals(Event, "Command"))
            {
                string Command    = req.Query["Command"];
                string Parameters = req.Query["Parameters"];

                log.LogInformation("Command: " + Command + "Parms: " + Parameters);

                if (Command == null || Parameters == null)
                {
                    return(new BadRequestObjectResult(Wrapper <ApiResult> .GetWrappedError("Invalid parameters", 400)));
                }

                // Get Current AccessToken for Station
                string gameId = da.GetGameIdFromHubDeviceId(HubDeviceId);
                string sToken = da.GetStationToken(gameId);

/*
 *      ClientMessage cm = new ClientMessage();
 *      cm.AccessToken = sToken;
 *      cm.Command = Command;
 *      cm.Parameters = Parameters;
 *
 *      await signalRMessages.AddAsync(new SignalRMessage()
 *      {
 *          Target = "notify",
 *          Arguments = new object[] { cm }
 *      });*/

                log.LogInformation("SignalRClientComms.PublishMessageToSignalRClients");
                await SignalRClientComms.PublishMessageToSignalRClients(signalRMessages,
                                                                        gameId, HubDeviceId, "Station2SPA", sToken, Command, Parameters);

                return(new OkObjectResult(Wrapper <ApiResult> .GetWrappedSuccess()));
            }



            // ... other event types
            // ...

            return(new BadRequestObjectResult(Wrapper <ApiResult> .GetWrappedError("Event not recognised", 400)));
        }
Esempio n. 3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [SignalR(HubName = "BroadcastClientMessage")] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            _logger = log;
            log.LogInformation("Checking access code.");
            int nSuppressMessage = await Task.Run(() => { return(99); });

            string GameId    = req.Query["GameId"];
            string inputCode = req.Query["InputCode"];

            if (GameId == null || inputCode == null)
            {
                AccessResult arError = new AccessResult();
                arError.Success = false;
                var wrappedError = new Wrapper <AccessResult>(arError);
                wrappedError.ErrorMessage = "Invalid parameters";
                wrappedError.StatusCode   = 400;
                return(new BadRequestObjectResult(wrappedError));
            }

            DataAccess da = new DataAccess(_logger);


            string currentToken = da.GetStationToken(GameId);

            if (currentToken != "")
            {
                AccessResult arError = new AccessResult();
                arError.Success = false;
                var wrappedError = new Wrapper <AccessResult>(arError);
                wrappedError.ErrorMessage = "Station already in use";
                wrappedError.StatusCode   = 200;
                return(new OkObjectResult(wrappedError));
            }

            string realCode = da.GetStationAccessCode(GameId);

            if (realCode != inputCode)
            {
                AccessResult arError = new AccessResult();
                arError.Success = false;
                var wrappedError = new Wrapper <AccessResult>(arError);
                wrappedError.ErrorMessage = "Incorrect input code";
                wrappedError.StatusCode   = 200;
                return(new OkObjectResult(wrappedError));
            }

            string token = da.GenerateNewStationToken(GameId);

            // Attach client to station
            string hubDeviceId = da.GetStationHubDeviceId(GameId);
            await SignalRClientComms.PublishMessageToSignalRClients(signalRMessages, GameId, hubDeviceId, "SPA2Station", token, "AttachClient", "");

            AccessResult ar = new AccessResult();

            ar.Success = true;
            ar.GameId  = GameId;
            ar.Token   = token;
            var wrapped = new Wrapper <AccessResult>(ar);

            wrapped.StatusCode = 200;
            return(new OkObjectResult(wrapped));
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log,
            [SignalR(HubName = "BroadcastClientMessage")] IAsyncCollector <SignalRMessage> signalRMessages)
        {
            _logger = log;
            log.LogInformation("Performing PassCommandToStation.");
            int nSuppressMessage = await Task.Run(() => { return(99); });

            string GameId      = req.Query["GameId"];
            string AccessToken = req.Query["AccessToken"];
            string cmd         = req.Query["Command"];
            string parms       = req.Query["Parameters"];

            // All threee parms must be present
            if (GameId == null || AccessToken == null || cmd == null)
            {
                AccessResult arError = new AccessResult();
                arError.Success = false;
                var wrappedError = new Wrapper <AccessResult>(arError);
                wrappedError.ErrorMessage = "Invalid parameters";
                wrappedError.StatusCode   = 400;
                return(new BadRequestObjectResult(wrappedError));
            }

            // Check access Token
            DataAccess da     = new DataAccess(_logger);
            string     sToken = da.GetStationToken(GameId);

            if (!String.Equals(sToken, AccessToken, StringComparison.OrdinalIgnoreCase))
            {
                AccessResult arError = new AccessResult();
                arError.Success = false;
                var wrappedError = new Wrapper <AccessResult>(arError);
                wrappedError.ErrorMessage = "Invalid token";
                wrappedError.StatusCode   = 400;
                return(new BadRequestObjectResult(wrappedError));
            }

            string hubDeviceId  = da.GetStationHubDeviceId(GameId);
            string hubDeviceKey = da.GetStationHubDeviceKey(GameId);

            if (string.IsNullOrEmpty(hubDeviceId) || string.IsNullOrEmpty(hubDeviceKey))
            {
                AccessResult arError = new AccessResult();
                arError.Success = false;
                var wrappedError = new Wrapper <AccessResult>(arError);
                wrappedError.ErrorMessage = "No hub device for station";
                wrappedError.StatusCode   = 400;
                return(new BadRequestObjectResult(wrappedError));
            }

            if (string.IsNullOrEmpty(parms))
            {
                parms = "";
            }

            // Cloud2DeviceHelper.SendCommmand(hubDeviceId, hubDeviceKey, cmd, parms);
            await SignalRClientComms.PublishMessageToSignalRClients(signalRMessages, GameId, hubDeviceId, "SPA2Station", AccessToken, cmd, parms);

            string s       = "Command performed OK";
            var    wrapped = new Wrapper <string>(s);

            wrapped.StatusCode = 200;
            return(new OkObjectResult(wrapped));
        }