Ejemplo n.º 1
0
        private bool registerSensor(string sensorName)
        {
            ServiceConfiguration conf = ServiceConfiguration.Instance;

            int lastReadIndex = this.dataCache.GetLastReadIndex(sensorName);

            // e.g. http://localhost/sensor/registry/addSensor
            string postAddr = $"http://{conf.registryAddress}:{conf.registryPort}/"
                              + $"{conf.registerSensorPath}";

            SensorDataArg reqArg = new SensorDataArg(sensorName,
                                                     conf.hostIP,
                                                     conf.listeningPort,
                                                     lastReadIndex);
            string stringReqArg = JsonConvert.SerializeObject(reqArg);

            Console.WriteLine($"Trying to register with: {stringReqArg} on: {postAddr} ... ");

            // this method could possibly throw exception
            HttpResponseMessage responseMessage = null;

            try
            {
                responseMessage = httpClient
                                  .PostAsync(postAddr, new StringContent(stringReqArg,
                                                                         Encoding.UTF8,
                                                                         "application/json"))
                                  .Result;               // .Result is going to force blocking execution
            }
            catch (Exception e)
            {
                this.logger.logError($"Exception in sensor registration  on: {postAddr}, "
                                     + $" reason: {e.Message}");
                return(false);
            }

            if (responseMessage != null &&
                responseMessage.IsSuccessStatusCode)
            {
                return(true);
            }
            else
            {
                if (responseMessage == null)
                {
                    // no response ... just failure
                    this.logger.logError("Http request for sensor registration failed ... ");
                }
                else
                {
                    // status code is not success
                    this.logger.logError("Http response for registration failed: "
                                         + responseMessage.ReasonPhrase);
                }

                return(false);
            }
        }
Ejemplo n.º 2
0
        public IActionResult updateSensor([FromBody] SensorDataArg reqArg)
        {
            ServiceConfiguration conf = ServiceConfiguration.Instance;

            RegistryResponse response = sensorRegistry.updateSensorRecord(reqArg.SensorName,
                                                                          reqArg.IpAddress,
                                                                          reqArg.PortNum,
                                                                          reqArg.LastReadIndex);

            if (response.status == RegistryStatus.ok)
            {
                return(new OkObjectResult(response.singleData));
            }

            return(new BadRequestObjectResult("Registry response: " + response.status.ToString()));
        }
Ejemplo n.º 3
0
        public IActionResult postSensor([FromBody] SensorDataArg reqArg)
        {
            Console.WriteLine($"Sensor add trough post request: {reqArg.SensorName}");

            RegistryResponse response = sensorRegistry.addSensorRecord(reqArg.SensorName,
                                                                       reqArg.IpAddress,
                                                                       reqArg.PortNum,
                                                                       reqArg.LastReadIndex);

            if (response.status == RegistryStatus.ok)
            {
                return(new OkObjectResult(response.singleData));
            }

            return(new BadRequestObjectResult("Registry response: " + response.status.ToString()));
        }