예제 #1
0
        /// <summary>
        /// 传入UserID初始化LabDataCollect
        /// </summary>
        /// <param name="userId"></param>
        public void LabDataCollectInit(Func <string> userId)
        {
            if (_isClientInit)
            {
                return;
            }

            #region 初始化本地存储
            _localSaveDataTimeLayout = LabTools.GetConfig <LabDataConfig>().LocalSaveDataTimeLayout;
            _userId       = userId;
            _saveDataPath = Application.dataPath + "/Output";
            LabTools.CreatSaveDataFolder(_saveDataPath);
            var userStr = _userId.Invoke().PadLeft(2, '0');
            _saveDataPath = string.Join("_", _saveDataPath + "/" + DateTime.Now.ToString(_localSaveDataTimeLayout), userStr);
            _saveDataPath = LabTools.CreatSaveDataFolder(_saveDataPath);
            #endregion

            #region 初始化上传服务

            var options = new DataSyncClientOptions()
            {
                EndpointAddress = "http://localhost:4000/api/data",
                ProjectId       = LabTools.GetConfig <LabDataConfig>().ProjectId,
                LogFilePath     = labDataSavePath + "/ log.txt"
            };

            //Docker
            options.EndpointAddress = "http://localhost/api/data";

            //server
            _sendToServer           = LabTools.GetConfig <LabDataConfig>().SendToServer;
            options.EndpointAddress = LabTools.GetConfig <LabDataConfig>().ServerPath;


            if (!Directory.Exists("TestStore"))
            {
                Directory.CreateDirectory("TestStore");
            }
            _applicationLifecycle = new SimpleApplicationLifecycle();


            _client = new DataSyncClient(new UnityApplicationFolderProvider(labDataSavePath + "/TestStore"),
                                         _applicationLifecycle, options, _userId);

            _client.Init();

            _isClientInit = true;

            StartUpload();

            #endregion

            Application.wantsToQuit += () => !IsClientRunning;
            _dataWriterDic           = new Dictionary <Type, LabDataWriter>();
            _dataQueue   = new ConcurrentQueue <LabDataBase>();
            _writeThread = new Thread(Queue2Send);
            _writeThread.Start();
        }
예제 #2
0
        private void DataWriterFunc(LabDataBase data)
        {
            var datatype = data.GetType();

            if (!_dataWriterDic.ContainsKey(datatype))
            {
                string dataPath = string.Join("_", _saveDataPath + "/", _userId.Invoke().PadLeft(2, '0'), data.GetType().Name + ".json");
                LabTools.CreatData(dataPath);
                _dataWriterDic.Add(datatype, new LabDataWriter(dataPath));
            }

            _dataWriterDic[datatype].WriteData(data);
        }
예제 #3
0
        /// <summary>
        /// 数据采集,传入数据,频率,是否循环采集
        /// </summary>
        /// <param name="data"></param>
        /// <param name="loop"></param>
        /// <param name="frequency"></param>
        public void DataCollect(IData data, bool loop = true, int frequency = 200)
        {
            if (!_isClientInit)
            {
                Debug.LogError("LabData未初始化");
                return;
            }


            //StartUpload();
            var basePathStr = Application.dataPath + "/Output";

            LabTools.CreatFolder(basePathStr);
            var userStr = _userId.PadLeft(2, '0');

            basePathStr = string.Join("_", basePathStr + "/" + DateTime.Now.ToString(_localSaveDataTimeLayout), userStr);
            basePathStr = LabTools.CreatFolder(basePathStr);

            if (_saveType == SaveType.Csv)
            {
                string dataPath = string.Join("_", basePathStr + "/" + DateTime.Now.ToString(_localSaveDataTimeLayout), userStr, data.SaveData.DataCodeName, data.SaveData.LabDataBase.Invoke() + "." + _saveType.ToString());
                LabTools.CreatData(dataPath);
                DataWriter dw = new DataWriter(dataPath, data.SaveData.LabDataBase.Invoke, _saveType);
                dw.WriteCsvTitle();
                dw.Dispose();
            }
            if (loop)
            {
                string dataPath = string.Join("_", basePathStr + "/" + DateTime.Now.ToString(_localSaveDataTimeLayout), userStr, data.SaveData.DataCodeName, data.SaveData.LabDataBase.Invoke() + "." + _saveType.ToString());
                LabTools.CreatData(dataPath);
                DataWriter dw = new DataWriter(dataPath, () => data.SaveData.LabDataBase.Invoke(), _saveType);
                dw.WriteDataAsyncFrequency();
                _dataWriters.Add(dw);
            }
            else
            {
                string dataPath = string.Join("_", basePathStr + "/" + DateTime.Now.ToString(_localSaveDataTimeLayout), userStr, data.SaveData.DataCodeName, data.SaveData.LabDataBase.Invoke() + "." + _saveType.ToString());
                LabTools.CreatData(dataPath);
                DataWriter dw = new DataWriter(dataPath, () =>
                {
                    if (_sendToServer)
                    {
                        Scope.Send(data.SaveData.LabDataBase.Invoke());
                    }
                    return(data.SaveData.LabDataBase.Invoke());
                }, _saveType);
                dw.WriteOnce();
                dw.Dispose();
            }
        }
예제 #4
0
        /// <summary>
        /// 传入UserID初始化LabDataCollect
        /// </summary>
        /// <param name="userId"></param>
        public void LabDataCollectInit(string userId)
        {
            _localSaveDataTimeLayout = LabTools.GetConfig <LabDataConfig>().LocalSaveDataTimeLayout;
            if (_isClientInit)
            {
                return;
            }
            _userId = userId;
            //StartAutoDataCollect();
            var options = new DataSyncClientOptions()
            {
                EndpointAddress = "http://localhost:4000/api/data",
                ProjectId       = LabTools.GetConfig <LabDataConfig>().ProjectId,
                LogFilePath     = labDataSavePath + "/ log.txt"
            };

            //Docker
            options.EndpointAddress = "http://localhost/api/data";

            //server

            /*
             * _sendToServer = LabTools.GetConfig<LabDataConfig>().SendToServer;
             * options.EndpointAddress = LabTools.GetConfig<LabDataConfig>().ServerPath;
             * //SendDataToServer(0,0,0);
             */



            if (!Directory.Exists("TestStore"))
            {
                Directory.CreateDirectory("TestStore");
            }
            _applicationLifecycle = new SimpleApplicationLifecycle();


            _client = new DataSyncClient(new UnityApplicationFolderProvider(labDataSavePath + "/TestStore"),
                                         _applicationLifecycle, options, () => _userId);

            _client.Init();

            _isClientInit = true;

            StartUpload();
        }
예제 #5
0
        ///connect to wmlab
        ///

        public void SendDataToServer(string finishMinutes, string finishSeconds, int wrongtimes)
        {
            _dataList?.Add(new ATMResultData(finishMinutes, finishSeconds, wrongtimes)
            {
                Tasktype   = finishMinutes,
                Finishtime = finishSeconds,
                Wrongtimes = wrongtimes
            });

            isFinished = false;
            var o = JsonConvert.SerializeObject(_dataList);
            // Debug.Log(o);
            var request = new HTTPRequest(new Uri(LabTools.GetConfig <LabDataConfig>().ServerPath), HTTPMethods.Post, OnRequestFinished) //POST
            {
                RawData = Encoding.UTF8.GetBytes(o)
            };

            if (!request.HasHeader("Content-Type"))
            {
                request.SetHeader("Content-Type", "application/json");
            }
            request.Send();
            Debug.Log("request.Send()");
        }