void TJsonVal::GetArrNumSpV(TIntFltKdV& NumSpV) const { EAssert(IsArr()); for (int ElN = 0; ElN < GetArrVals(); ElN++) { PJsonVal ArrVal = GetArrVal(ElN); EAssert(ArrVal->IsArr()); EAssert(ArrVal->GetArrVals() == 2); int Idx = ArrVal->GetArrVal(0)->GetInt(); double Val = ArrVal->GetArrVal(1)->GetNum(); NumSpV.Add(TIntFltKd(Idx, Val)); } NumSpV.Sort(); }
// // TBackupProfile // TBackupProfile::TBackupProfile(const PJsonVal& SettingsJson, const TStr& Destination_, const TStr& ProfileName_) { Destination = Destination_; if (Destination.Len() > 0 && (Destination.LastCh() != '\\' || Destination.LastCh() != '/')) Destination += "/"; ProfileName = ProfileName_; if (!TDir::Exists(Destination)) TDir::GenDir(Destination); VersionsToKeep = SettingsJson->GetObjInt("versionsToKeep", 1); PJsonVal FoldersJson = SettingsJson->GetObjKey("folders"); EAssertR(FoldersJson->IsArr(), "Expected to get an array of folders"); for (int N = 0; N < FoldersJson->GetArrVals(); N++) { PJsonVal FolderJson = FoldersJson->GetArrVal(N); TBackupFolderInfo FolderInfo; FolderInfo.Folder = FolderJson->GetObjStr("folder"); if (FolderJson->IsObjKey("extensions")) FolderJson->GetObjStrV("extensions", FolderInfo.Extensions); if (FolderInfo.Extensions.IsIn("*")) FolderInfo.Extensions.Clr(); FolderInfo.IncludeSubfolders = FolderJson->GetObjBool("includeSubfolders"); if (FolderJson->IsObjKey("skipIfContaining")) FolderJson->GetObjStrV("skipIfContaining", FolderInfo.SkipIfContainingV); FolderV.Add(FolderInfo); } // load logs of the previous backups ProfileLogFile = Destination + ProfileName + "/backupInfo.json"; if (TFile::Exists(ProfileLogFile)) { PJsonVal LogJson = TJsonVal::GetValFromStr(TStr::LoadTxt(ProfileLogFile)); if (LogJson->IsArr()) { for (int N = 0; N < LogJson->GetArrVals(); N++) { PJsonVal Log = LogJson->GetArrVal(N); LogV.Add(TBackupLogInfo(Log)); } } } }
void TNodeJsRf24Radio::set(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); TNodeJsRf24Radio* JsRadio = ObjectWrap::Unwrap<TNodeJsRf24Radio>(Args.Holder()); if (Args.Length() == 0) { return; } const PJsonVal ArgVal = TNodeJsUtil::GetArgJson(Args, 0); bool Success = true; if (ArgVal->IsArr()) { THash<TInt, TIntPrV> NodeIdValIdValPrVH; for (int ArgN = 0; ArgN < ArgVal->GetArrVals(); ArgN++) { const PJsonVal& ValJson = ArgVal->GetArrVal(ArgN); const TStr& ValNm = ValJson->GetObjStr("sensorId"); const int& Val = ValJson->GetObjInt("value"); const TIntPr& NodeIdValIdPr = JsRadio->ValNmNodeIdValIdPrH.GetDat(ValNm); const uint16 NodeId = NodeIdValIdPr.Val1; const int ValId = NodeIdValIdPr.Val2; if (!NodeIdValIdValPrVH.IsKey(NodeId)) { NodeIdValIdValPrVH.AddDat(NodeId); } TIntPrV& ValIdValPrV = NodeIdValIdValPrVH.GetDat(NodeId); ValIdValPrV.Add(TIntPr(ValId, Val)); } int KeyId = NodeIdValIdValPrVH.FFirstKeyId(); while (NodeIdValIdValPrVH.FNextKeyId(KeyId)) { const uint16 NodeId = NodeIdValIdValPrVH.GetKey(KeyId); const TIntPrV& ValIdValPrV = NodeIdValIdValPrVH[KeyId]; Success &= JsRadio->Radio.Set(NodeId, ValIdValPrV); } } else { const TStr& ValueNm = ArgVal->GetObjStr("sensorId"); const int Val = ArgVal->GetObjInt("value"); const TIntPr& NodeIdValIdPr = JsRadio->ValNmNodeIdValIdPrH.GetDat(ValueNm); const uint16 NodeId = (uint16) NodeIdValIdPr.Val1; const int ValId = NodeIdValIdPr.Val2; Success = JsRadio->Radio.Set(NodeId, ValId, Val); } Args.GetReturnValue().Set(v8::Boolean::New(Isolate, Success)); }
// parse from json configuration file TJsParam(const TStr& RootFNm, const PJsonVal& JsVal) { EAssertR(JsVal->IsObj(), "Unsupported type: " + TJsonVal::GetStrFromVal(JsVal)); // we must have at least the script name EAssert(JsVal->IsObjKey("file")); // get script name FNm = JsVal->GetObjStr("file"); // get namespace (get from script name if not available) Nm = JsVal->IsObjKey("name") ? JsVal->GetObjStr("name") : FNm.GetFMid(); // get initialization parameters (if available) InitVal = JsVal->IsObjKey("init") ? JsVal->GetObjKey("init") : TJsonVal::NewObj(); // get library include folders if (JsVal->IsObjKey("include")) { PJsonVal IncludesVal = JsVal->GetObjKey("include"); EAssertR(IncludesVal->IsArr(), "Expected array of strings, not: " + TJsonVal::GetStrFromVal(IncludesVal)); for (int IncludeN = 0; IncludeN < IncludesVal->GetArrVals(); IncludeN++) { PJsonVal IncludeVal = IncludesVal->GetArrVal(IncludeN); EAssertR(IncludeVal->IsStr(), "Expected string, not: " + TJsonVal::GetStrFromVal(IncludeVal)); IncludeFPathV.Add(IncludeVal->GetStr()); } } // handle default includes AddLocalLibFPath(); AddQMinerLibFPath(); // get folders with access permissions if (JsVal->IsObjKey("dirs")) { PJsonVal DirsVal = JsVal->GetObjKey("dirs"); EAssertR(DirsVal->IsArr(), "Expected array of strings, not: " + TJsonVal::GetStrFromVal(DirsVal)); for (int DirN = 0; DirN < DirsVal->GetArrVals(); DirN++) { PJsonVal DirVal = DirsVal->GetArrVal(DirN); EAssertR(DirVal->IsStr(), "Expected string, not: " + TJsonVal::GetStrFromVal(DirVal)); AccessFPathV.Add(DirVal->GetStr()); } } // add sandbox access AddSandboxAccessFPath(RootFNm); }
v8::Local<v8::Value> TNodeJsUtil::ParseJson(v8::Isolate* Isolate, const PJsonVal& JsonVal) { v8::EscapableHandleScope HandleScope(Isolate); if (!JsonVal->IsDef()) { return v8::Undefined(Isolate); } else if (JsonVal->IsBool()) { return v8::Boolean::New(Isolate, JsonVal->GetBool()); } else if (JsonVal->IsNull()) { return v8::Null(Isolate); } else if (JsonVal->IsNum()) { return HandleScope.Escape(v8::Number::New(Isolate, JsonVal->GetNum())); } else if (JsonVal->IsStr()) { return HandleScope.Escape(v8::String::NewFromUtf8(Isolate, JsonVal->GetStr().CStr())); } else if (JsonVal->IsArr()) { const uint Len = JsonVal->GetArrVals(); v8::Local<v8::Array> ResArr = v8::Array::New(Isolate, Len); for (uint i = 0; i < Len; i++) { ResArr->Set(i, ParseJson(Isolate, JsonVal->GetArrVal(i))); } return HandleScope.Escape(ResArr); } else if (JsonVal->IsObj()) { v8::Local<v8::Object> ResObj = v8::Object::New(Isolate); const int NKeys = JsonVal->GetObjKeys(); for (int i = 0; i < NKeys; i++) { TStr Key; PJsonVal Val; JsonVal->GetObjKeyVal(i, Key, Val); ResObj->Set(v8::String::NewFromUtf8(Isolate, Key.CStr()), ParseJson(Isolate, Val)); } return HandleScope.Escape(ResObj); } else { throw TExcept::New("Invalid JSON!", "TNodeJsUtil::ParseJson"); } }
TQmParam(const TStr& FNm) { EAssertR(TFile::Exists(FNm), "Missing configuration file " + FNm); // load configuration file PJsonVal ConfigVal = TJsonVal::GetValFromSIn(TFIn::New(FNm)); EAssertR(ConfigVal->IsObj(), "Invalid setting file - not valid JSON"); // parse out common stuff RootFPath = TStr::GetNrFPath(ConfigVal->GetObjStr("directory", TDir::GetCurDir())); LockFNm = RootFPath + "./lock"; DbFPath = ConfigVal->GetObjStr("database", "./db/"); PortN = TFlt::Round(ConfigVal->GetObjNum("port")); // parse out unicode definition file TStr UnicodeFNm = ConfigVal->GetObjStr("unicode", TQm::TEnv::QMinerFPath + "./UnicodeDef.Bin"); if (!TUnicodeDef::IsDef()) { TUnicodeDef::Load(UnicodeFNm); } // parse cache if (ConfigVal->IsObjKey("cache")) { PJsonVal CacheVal = ConfigVal->GetObjKey("cache"); // parse out index and default store cache sizes IndexCacheSize = int64(CacheVal->GetObjNum("index", 1024)) * int64(TInt::Mega); DefStoreCacheSize = int64(CacheVal->GetObjNum("store", 1024)) * int64(TInt::Mega); // prase out store specific sizes, when available if (CacheVal->IsObjKey("stores")) { PJsonVal StoreCacheVals = CacheVal->GetObjKey("stores"); for (int StoreN = 0; StoreN < StoreCacheVals->GetArrVals(); StoreN++) { PJsonVal StoreCacheVal = StoreCacheVals->GetArrVal(StoreN); TStr StoreName = StoreCacheVal->GetObjStr("name"); uint64 StoreCacheSize = int64(StoreCacheVal->GetObjNum("size")) * int64(TInt::Mega); StoreNmCacheSizeH.AddDat(StoreName, StoreCacheSize); } } } else { // default sizes are set to 1GB for index and stores IndexCacheSize = int64(1024) * int64(TInt::Mega); DefStoreCacheSize = int64(1024) * int64(TInt::Mega); } // load scripts if (ConfigVal->IsObjKey("script")) { // we have configuration file, read it PJsonVal JsVals = ConfigVal->GetObjKey("script"); if (JsVals->IsArr()) { for (int JsValN = 0; JsValN < JsVals->GetArrVals(); JsValN++) { JsParamV.Add(TJsParam(RootFPath, JsVals->GetArrVal(JsValN))); } } else { JsParamV.Add(TJsParam(RootFPath, JsVals)); } } else { // no settings for scripts, assume default setting TStr SrcFPath = TStr::GetNrAbsFPath("src", RootFPath); TFFile File(SrcFPath, ".js", false); TStr SrcFNm; while (File.Next(SrcFNm)) { JsParamV.Add(TJsParam(RootFPath, SrcFNm)); } } // load serving folders //TODO: Add to qm config ability to edit this if (ConfigVal->IsObjKey("wwwroot")) { PJsonVal WwwVals = ConfigVal->GetObjKey("wwwroot"); if (WwwVals->IsArr()) { for (int WwwValN = 0; WwwValN < WwwVals->GetArrVals(); WwwValN++) { AddWwwRoot(WwwVals->GetArrVal(WwwValN)); } } else { AddWwwRoot(WwwVals); } } // check for folder with admin GUI TStr GuiFPath = TStr::GetNrAbsFPath("gui", TQm::TEnv::QMinerFPath); if (TDir::Exists(GuiFPath)) { WwwRootV.Add(TStrPr("admin", GuiFPath)); } // check for any default wwwroot TStr DefaultWwwRootFPath = TStr::GetNrAbsFPath("www", RootFPath); if (TDir::Exists(DefaultWwwRootFPath)) { WwwRootV.Add(TStrPr("www", DefaultWwwRootFPath)); } }