Exemplo n.º 1
0
        /// <summary>
        /// Upload sensor data to server
        /// </summary>
        /// <param name="req">sensor data request</param>
        /// <returns>sensor data response</returns>
        public UploadSensorDataResponse UploadSensorData(UploadSensorDataRequest req)
        {
            logger.Debug("[BEGIN]");
            logger.Trace(CommonUtility.ToJson(req));

            // Define and Initialize response
            UploadSensorDataResponse res = new UploadSensorDataResponse();

            // Transaction start
            using (var dbTransactionContext = context.Database.BeginTransaction())
            {
                try
                {
                    // Check sensor id exists
                    var sensorInfo = context.sensor_info.Where(w => w.id == req.SensorId).FirstOrDefault();
                    if (sensorInfo == null)
                    {
                        // If sensor id does not exists then attach error codes to response
                        res.ResultCode       = SensorErrors.SensorNotRegistered.eCode;
                        res.ResultMessage    = SensorErrors.SensorNotRegistered.eMsg;
                        res.ExceptionMessage = null;

                        // Write error to log
                        CommonUtility.writeErrorLog("UploadSensorData", res, null, logger);

                        // return response
                        return(res);
                    }

                    // create sensor_data entity to be updated
                    sensor_data sensorData = new sensor_data();
                    sensorData.sensor    = sensorInfo.id;
                    sensorData.data      = req.Data;
                    sensorData.timestamp = DateTime.Now;
                    sensorData.is_delete = (short)ECDeleteType.NOT_DELETE;

                    // add entity to dbContext
                    context.sensor_data.Add(sensorData);

                    // save changes to db
                    context.SaveChanges();

                    // commit db transaction
                    dbTransactionContext.Commit();
                }
                catch (Exception ex)
                {
                    // Rollback the database transaction context
                    dbTransactionContext.Rollback();

                    // Attach error codes to response
                    res.ResultCode       = CommonErrors.DBOperationError.eCode;
                    res.ResultMessage    = CommonErrors.DBOperationError.eMsg;
                    res.ExceptionMessage = ex.InnerException.ToString();

                    // Write exception to log
                    CommonUtility.writeErrorLog("UploadSensorData", res, ex, logger);

                    return(res);
                }
            }

            // attach success result codes to response
            res.ResultCode    = CommonErrors.SUCCESS.eCode;
            res.ResultMessage = CommonErrors.SUCCESS.eMsg;

            logger.Debug("[OK]");

            // return response
            return(res);
        }
Exemplo n.º 2
0
        public object BeforeCall(string operationName, object[] inputs)
        {
            logger.Trace("[Start]");
            logger.Info("Method starting");
            //return null; //Todo Comment this line

            if (null == inputs)
            {
                logger.Info("Input null");
            }

            if (inputs.Length <= 0)
            {
                logger.Info("Input legthError");
                throw new FaultException("0003");
            }

            Request req = (Request)inputs[0];

            WristbandEntities context = new WristbandEntities();

            logger.Info("Get Session object");
            login_session session = context.login_session.Where(w => w.session_id == req.SessionId.ToString() && w.status == (short)ECSessionStatus.OPEN).FirstOrDefault();

            if (session == null)
            {
                logger.Info("-SESSION NOT FOUND-");
                context.Dispose();
                throw new FaultException("0004");
            }

            logger.Info("Get username by session");

            if (session.user != null)
            {
                StaticData.S_LogUser = session.user.username;
            }

            int sessionTimeout = 300;

            if (sessionTimeout == 0)
            {
                logger.Info("Timeout : 0");
                session.end_datetime = DateTime.Now;
                logger.Info("Changers save");
                context.SaveChanges();

                context.Dispose();
                return(null);
            }
            else
            {
                if ((DateTime.Now - (DateTime)session.end_datetime).TotalMinutes <= sessionTimeout)
                {
                    logger.Info("Session set date information");
                    session.end_datetime = DateTime.Now;
                    context.SaveChanges();
                    logger.Info("Save information : With timeout");
                    context.Dispose();
                    return(null);
                }
                else
                {
                    logger.Info("Date Exception");
                    context.Dispose();
                    throw new FaultException("0004");
                }
            }
        }