ScriptObject.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include "stdafx.h"
  2. #include "ScriptObject.h"
  3. #include "public.h"
  4. #include "kernel.h"
  5. #include "log4c.h"
  6. // 取整
  7. int MyRound(double value)
  8. {
  9. int onepoint = ((int)(value*10))%10;
  10. if(onepoint < 5 && onepoint >= 0)
  11. {
  12. return ((int)(value*10))/10;
  13. }
  14. else if(onepoint >= 5)
  15. {
  16. return ((int)(value*10))/10 + 1;
  17. }
  18. else if(onepoint <= -5)
  19. {
  20. return ((int)(value*10))/10 -1;
  21. }
  22. else
  23. {
  24. return ((int)(value*10))/10 ;
  25. }
  26. }
  27. /////////////////////////////////////////////////////////////////////////////
  28. IMPLEMENT_DYNCREATE(CScriptObject, CCmdTarget)
  29. CScriptObject::CScriptObject()
  30. {
  31. EnableAutomation();
  32. }
  33. CScriptObject::~CScriptObject()
  34. {
  35. }
  36. void CScriptObject::OnFinalRelease()
  37. {
  38. CCmdTarget::OnFinalRelease();
  39. }
  40. BEGIN_MESSAGE_MAP(CScriptObject, CCmdTarget)
  41. //{{AFX_MSG_MAP(CScriptObject)
  42. // NOTE - the ClassWizard will add and remove mapping macros here.
  43. //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45. BEGIN_DISPATCH_MAP(CScriptObject, CCmdTarget)
  46. //{{AFX_DISPATCH_MAP(CScriptObject)
  47. DISP_FUNCTION(CScriptObject, "GetYCValue", GetYCValue, VT_R8, VTS_BSTR)
  48. DISP_FUNCTION(CScriptObject, "SetYCValue", SetYCValue, VT_EMPTY, VTS_BSTR VTS_R8)
  49. DISP_FUNCTION(CScriptObject, "GetYXValue", GetYXValue, VT_I2, VTS_BSTR)
  50. DISP_FUNCTION(CScriptObject, "SetYXValue", SetYXValue, VT_EMPTY, VTS_BSTR VTS_I2)
  51. DISP_FUNCTION(CScriptObject, "ShowResult", ShowResult, VT_R8, VTS_BSTR)
  52. DISP_FUNCTION(CScriptObject, "Sleeps", Sleeps, VT_EMPTY, VTS_I2)
  53. //}}AFX_DISPATCH_MAP
  54. END_DISPATCH_MAP()
  55. // {B07CA4FB-3758-4E2C-B386-C0C493072CFB}
  56. static const IID IID_I_MYSCRIPTOBJECT =
  57. { 0xb07ca4fb, 0x3758, 0x4e2c, { 0xb3, 0x86, 0xc0, 0xc4, 0x93, 0x7, 0x2c, 0xfb } };
  58. BEGIN_INTERFACE_MAP(CScriptObject, CCmdTarget)
  59. INTERFACE_PART(CScriptObject, IID_I_MYSCRIPTOBJECT, Dispatch)
  60. END_INTERFACE_MAP()
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CScriptObject message handlers
  63. // 输出语法检查结果
  64. double CScriptObject::ShowResult(LPCTSTR prompt)
  65. {
  66. AfxMessageBox("Script Run Success!", MB_SETFOREGROUND);
  67. return 0.0;
  68. }
  69. // 取遥测值
  70. double CScriptObject::GetYCValue(LPCTSTR varName)
  71. {
  72. int nIdentifyTime = 0;
  73. double value = pVariantsManager->GetAnalogValue(varName);
  74. //LOG4C((LOG_NOTICE, "GetYCValue varName=%s value=%d",varName,int(value)));
  75. return value;
  76. }
  77. // 设置遥测值
  78. void CScriptObject::SetYCValue(LPCTSTR varName, double value)
  79. {
  80. LOG4C((LOG_NOTICE, "SetYCValue varName=%s value=%d",varName,int(value)));
  81. //if( pVariantsManager->IsEndIdentifyTime( varName ) )
  82. {
  83. pVariantsManager->SetAnalogValue(varName, value);
  84. }
  85. }
  86. // 取状态量值
  87. short CScriptObject::GetYXValue(LPCTSTR varName)
  88. {
  89. short x = 0;
  90. return x;
  91. }
  92. // 设置状态值
  93. void CScriptObject::SetYXValue(LPCTSTR varName, short value)
  94. {
  95. }
  96. //////////////////////////////////////////////////////////////////////////
  97. BOOL ScriptParam::ShouldRun()
  98. {
  99. if (!m_bValid)
  100. {
  101. return FALSE;
  102. }
  103. if (m_bCondition)
  104. {
  105. m_calc.SetFormula(m_strExpression);
  106. return (BOOL)m_calc.GetResult(); //成立
  107. }
  108. else
  109. {
  110. SYSTEMTIME st;
  111. GetLocalTime(&st);
  112. CTime cur = CTime::GetCurrentTime();
  113. CTime last = CTime(m_stLastRunTime);
  114. CTimeSpan sp = cur - last;
  115. int second = (int)sp.GetTotalSeconds();
  116. if ( second * 1000 + st.wMilliseconds - m_stLastRunTime.wMilliseconds > m_curcleTime)
  117. {
  118. memcpy(&m_stLastRunTime, &st, sizeof(SYSTEMTIME));
  119. return TRUE;
  120. }
  121. }
  122. return FALSE;
  123. }
  124. ScriptParam::ScriptParam()
  125. {
  126. m_curcleTime = 0; //循环时间
  127. m_strExpression = ""; //条件表达式
  128. m_strScript = ""; //脚本
  129. m_bCircle = FALSE;
  130. m_bCondition = FALSE;
  131. m_bValid = FALSE;
  132. GetLocalTime(&m_stLastRunTime);
  133. }
  134. // 判断是有效变量名
  135. BOOL IsValidChar(char chr)
  136. {
  137. if ( chr >= '0' && chr <= '9')
  138. return TRUE;
  139. else if ((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') )
  140. return TRUE;
  141. else if (chr == '_')
  142. return TRUE;
  143. else
  144. return FALSE;
  145. }
  146. // 取下一个单词, 连续的字母和数字组合为一个单词
  147. CString GetNextWord(CString strCurLine, int& nPos)
  148. {
  149. CString strLine = strCurLine;
  150. CString strWord = "";
  151. int nLen = strLine.GetLength();
  152. BOOL bStart = FALSE;
  153. for (int i = nPos; i < nLen; i++)
  154. {
  155. char tmpChar = strLine.GetAt(i);
  156. if ( IsValidChar(tmpChar) )
  157. {
  158. if (bStart)
  159. {
  160. strWord += tmpChar;
  161. }
  162. else
  163. {
  164. bStart = TRUE;
  165. strWord = tmpChar;
  166. nPos = i;
  167. }
  168. }
  169. else if (bStart)
  170. {
  171. break;
  172. }
  173. }
  174. return strWord;
  175. }
  176. // 是否是设置
  177. BOOL IsSet(CString strCurLine, CString strWord)
  178. {
  179. CString strLine = strCurLine;
  180. strLine.TrimLeft();
  181. return (strLine.Left(strWord.GetLength()) == strWord);
  182. }
  183. // 返回新的字符串长度, nPos变为下一个寻找的起点
  184. int ReplaceVar(CString& strCurLine, int& nPos, CString strWord, CString strVar)
  185. {
  186. strCurLine.Delete(nPos, strWord.GetLength());
  187. strCurLine.Insert(nPos, strVar);
  188. nPos = nPos + strVar.GetLength();
  189. return strCurLine.GetLength();//cHn modify - strWord.GetLength() + strVar.GetLength();
  190. }
  191. // 由脚本转换为行数组
  192. int GetLinetArrayFromScript(const CString script, CStringArray& aLineCode)
  193. {
  194. aLineCode.RemoveAll();
  195. CString strScript = script +'\n';
  196. CString tempLine = "";
  197. int nStrLength = strScript.GetLength();
  198. for (int i = 0; i < nStrLength; i++)
  199. {
  200. tempLine = tempLine + strScript[i];
  201. if (strScript[i] == '\n') //换行
  202. {
  203. aLineCode.Add(tempLine);
  204. tempLine = "";
  205. }
  206. }
  207. return aLineCode.GetSize();
  208. }
  209. // 由行数组组成脚本
  210. void GetStringFromLineArray(CString& script, CStringArray& aLineCode)
  211. {
  212. script = "";
  213. int nLastSize = aLineCode.GetSize();
  214. for (int i = 0; i < nLastSize; i++)
  215. {
  216. script += aLineCode[i];
  217. }
  218. }
  219. /* 替换变量为相应的接口函数 */
  220. BOOL ProcessScript(CString& strScript)
  221. {
  222. CHAR strFile[MAX_PATH + 1] = "";
  223. CHAR strDirectory[MAX_PATH + 1] = "";
  224. CHAR strValue[255 + 1] = "";
  225. int iPosFile = 0;
  226. int iLenghtFile = 0;
  227. //获取系统路径
  228. //#ifdef _DEBUG
  229. // GetCurrentDirectory(MAX_PATH, strDirectory);
  230. //#else
  231. GetModuleFileName(AfxGetApp()->m_hInstance, strDirectory, MAX_PATH);
  232. iLenghtFile = strlen(strDirectory);
  233. for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--)
  234. {
  235. if (strDirectory[iPosFile] == '\\')
  236. {
  237. strDirectory[iPosFile] = '\0';
  238. break;
  239. }
  240. }
  241. //#endif
  242. char RUN[MAX_PATH] = {0};
  243. memset(RUN, 0, sizeof(RUN));
  244. wsprintf(strFile, "%s\\Project\\runpara.ini", strDirectory);
  245. int nStartRun = 0;
  246. if ( GetPrivateProfileString("RUNPARA", "STARTRUN","",RUN, 10, strFile) != 0)
  247. {
  248. nStartRun = (RUN[0] == '1' ? true:false);
  249. }
  250. if( nStartRun == 0 ) return TRUE;
  251. /* 将整个脚本,转化为一个分行的字符串数组 */
  252. CStringArray m_strLineArray;
  253. int nArraySize = GetLinetArrayFromScript(strScript, m_strLineArray);
  254. // 处理字符串数组
  255. int m_nVariantCount = 0;
  256. int nCurrntLine = 0;
  257. int nNextLine = 1; //当前行和下一行的行号
  258. for ( int i = 0; i < nArraySize; i++) //处理nArraySize行脚本
  259. {
  260. CString strCurLine = m_strLineArray[nCurrntLine];
  261. int nLineLen = strCurLine.GetLength();
  262. int nPos = 0;
  263. while (nPos < nLineLen)
  264. {
  265. CString strWord = GetNextWord(strCurLine, nPos);
  266. if (strWord.IsEmpty())
  267. {
  268. break;
  269. }
  270. // 判断是否是遥测变量
  271. if (pVariantsManager->FindAnalog(strWord) != NULL)
  272. {
  273. // 判断是否是赋值
  274. BOOL bSetValue = IsSet(strCurLine, strWord);
  275. m_nVariantCount++; //变量数加1
  276. CString tmpVarName; //转换的脚本变量
  277. tmpVarName.Format("V%06d",m_nVariantCount);
  278. m_strLineArray.InsertAt(m_nVariantCount-1,"dim "+ tmpVarName+'\n');
  279. nCurrntLine++;
  280. nNextLine++;
  281. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  282. if (bSetValue)
  283. {
  284. nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName);
  285. CString strChangeLine = "SetYCValue \"" + strWord +"\""+","+ tmpVarName+'\n';
  286. m_strLineArray.InsertAt(nCurrntLine+1, strChangeLine);
  287. }
  288. else
  289. {
  290. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  291. CString strChangeLine = tmpVarName + " = GetYCValue(\"" + strWord + "\")"+'\n';
  292. m_strLineArray.InsertAt(nCurrntLine, strChangeLine);
  293. nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName);
  294. nCurrntLine++;
  295. }
  296. nNextLine++;
  297. }
  298. // 判断是否是遥信变量
  299. else if( pVariantsManager->FindAnalog(strWord) != NULL )
  300. {
  301. // 判断是否是赋值
  302. BOOL bSetValue = IsSet(strCurLine, strWord);
  303. m_nVariantCount++; //变量数加1
  304. CString tmpVarName; //转换的脚本变量
  305. tmpVarName.Format("V%06d",m_nVariantCount);
  306. m_strLineArray.InsertAt(m_nVariantCount-1,"dim "+ tmpVarName+'\n');
  307. nCurrntLine++;
  308. nNextLine++;
  309. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  310. if (bSetValue)
  311. {
  312. nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName);
  313. CString strChangeLine = "SetYXValue \"" + strWord +"\""+","+ tmpVarName+'\n';
  314. m_strLineArray.InsertAt(nCurrntLine+1, strChangeLine);
  315. }
  316. else
  317. {
  318. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  319. CString strChangeLine = tmpVarName + " = GetYXValue(\"" + strWord + "\")"+'\n';
  320. m_strLineArray.InsertAt(nCurrntLine, strChangeLine);
  321. nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName);
  322. nCurrntLine++;
  323. }
  324. nNextLine++;
  325. }
  326. else
  327. {
  328. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  329. nPos += strWord.GetLength();
  330. }
  331. }
  332. /*一行处理完毕*/
  333. m_strLineArray.SetAt(nCurrntLine, strCurLine);
  334. nCurrntLine = nNextLine;
  335. nNextLine++;
  336. }
  337. //将字符串列表拼接成脚本
  338. GetStringFromLineArray(strScript, m_strLineArray);
  339. return TRUE;
  340. }
  341. void CScriptObject::Sleeps(short mSeconds)
  342. {
  343. Sleep(mSeconds);
  344. }