ScriptObject.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. double value = pVariantsManager->GetAnalogValue(varName);
  73. //LOG4C((LOG_NOTICE, "GetYCValue varName=%s value=%d",varName,int(value)));
  74. return value;
  75. }
  76. // 设置遥测值
  77. void CScriptObject::SetYCValue(LPCTSTR varName, double value)
  78. {
  79. //LOG4C((LOG_NOTICE, "SetYCValue varName=%s value=%d",varName,int(value)));
  80. pVariantsManager->SetAnalogValue(varName, value);
  81. }
  82. // 取状态量值
  83. short CScriptObject::GetYXValue(LPCTSTR varName)
  84. {
  85. short x = 0;
  86. return x;
  87. }
  88. // 设置状态值
  89. void CScriptObject::SetYXValue(LPCTSTR varName, short value)
  90. {
  91. }
  92. //////////////////////////////////////////////////////////////////////////
  93. //
  94. BOOL ScriptParam::ShouldRun()
  95. {
  96. if (!m_bValid)
  97. {
  98. return FALSE;
  99. }
  100. if (m_bCondition)
  101. {
  102. m_calc.SetFormula(m_strExpression);
  103. return (BOOL)m_calc.GetResult(); //成立
  104. }
  105. else
  106. {
  107. SYSTEMTIME st;
  108. GetLocalTime(&st);
  109. CTime cur = CTime::GetCurrentTime();
  110. CTime last = CTime(m_stLastRunTime);
  111. CTimeSpan sp = cur - last;
  112. int second = (int)sp.GetTotalSeconds();
  113. if ( second * 1000 + st.wMilliseconds - m_stLastRunTime.wMilliseconds > m_curcleTime)
  114. {
  115. memcpy(&m_stLastRunTime, &st, sizeof(SYSTEMTIME));
  116. return TRUE;
  117. }
  118. }
  119. return FALSE;
  120. }
  121. ScriptParam::ScriptParam()
  122. {
  123. m_curcleTime = 0; //循环时间
  124. m_strExpression = ""; //条件表达式
  125. m_strScript = ""; //脚本
  126. m_bCircle = FALSE;
  127. m_bCondition = FALSE;
  128. m_bValid = FALSE;
  129. GetLocalTime(&m_stLastRunTime);
  130. }
  131. // 判断是有效变量名
  132. BOOL IsValidChar(char chr)
  133. {
  134. if ( chr >= '0' && chr <= '9')
  135. return TRUE;
  136. else if ((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') )
  137. return TRUE;
  138. else if (chr == '_')
  139. return TRUE;
  140. else
  141. return FALSE;
  142. }
  143. // 取下一个单词, 连续的字母和数字组合为一个单词
  144. CString GetNextWord(CString strCurLine, int& nPos)
  145. {
  146. CString strLine = strCurLine;
  147. CString strWord = "";
  148. int nLen = strLine.GetLength();
  149. BOOL bStart = FALSE;
  150. for (int i = nPos; i < nLen; i++)
  151. {
  152. char tmpChar = strLine.GetAt(i);
  153. if ( IsValidChar(tmpChar) )
  154. {
  155. if (bStart)
  156. {
  157. strWord += tmpChar;
  158. }
  159. else
  160. {
  161. bStart = TRUE;
  162. strWord = tmpChar;
  163. nPos = i;
  164. }
  165. }
  166. else if (bStart)
  167. {
  168. break;
  169. }
  170. }
  171. return strWord;
  172. }
  173. // 是否是设置
  174. BOOL IsSet(CString strCurLine, CString strWord)
  175. {
  176. CString strLine = strCurLine;
  177. strLine.TrimLeft();
  178. return (strLine.Left(strWord.GetLength()) == strWord);
  179. }
  180. // 返回新的字符串长度, nPos变为下一个寻找的起点
  181. int ReplaceVar(CString& strCurLine, int& nPos, CString strWord, CString strVar)
  182. {
  183. strCurLine.Delete(nPos, strWord.GetLength());
  184. strCurLine.Insert(nPos, strVar);
  185. nPos = nPos + strVar.GetLength();
  186. return strCurLine.GetLength();//cHn modify - strWord.GetLength() + strVar.GetLength();
  187. }
  188. // 由脚本转换为行数组
  189. int GetLinetArrayFromScript(const CString script, CStringArray& aLineCode)
  190. {
  191. aLineCode.RemoveAll();
  192. CString strScript = script +'\n';
  193. CString tempLine = "";
  194. int nStrLength = strScript.GetLength();
  195. for (int i = 0; i < nStrLength; i++)
  196. {
  197. tempLine = tempLine + strScript[i];
  198. if (strScript[i] == '\n') //换行
  199. {
  200. aLineCode.Add(tempLine);
  201. tempLine = "";
  202. }
  203. }
  204. return aLineCode.GetSize();
  205. }
  206. // 由行数组组成脚本
  207. void GetStringFromLineArray(CString& script, CStringArray& aLineCode)
  208. {
  209. script = "";
  210. int nLastSize = aLineCode.GetSize();
  211. for (int i = 0; i < nLastSize; i++)
  212. {
  213. script += aLineCode[i];
  214. }
  215. }
  216. /* 替换变量为相应的接口函数 */
  217. BOOL ProcessScript(CString& strScript)
  218. {
  219. CHAR strFile[MAX_PATH + 1] = "";
  220. CHAR strDirectory[MAX_PATH + 1] = "";
  221. CHAR strValue[255 + 1] = "";
  222. int iPosFile = 0;
  223. int iLenghtFile = 0;
  224. //获取系统路径
  225. #ifdef _DEBUG
  226. GetCurrentDirectory(MAX_PATH, strDirectory);
  227. #else
  228. GetModuleFileName(AfxGetApp()->m_hInstance, strDirectory, MAX_PATH);
  229. iLenghtFile = strlen(strDirectory);
  230. for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--)
  231. {
  232. if (strDirectory[iPosFile] == '\\')
  233. {
  234. strDirectory[iPosFile] = '\0';
  235. break;
  236. }
  237. }
  238. #endif
  239. char RUN[MAX_PATH] = {0};
  240. memset(RUN, 0, sizeof(RUN));
  241. wsprintf(strFile, "%s\\Project\\runpara.ini", strDirectory);
  242. int nStartRun = 0;
  243. if ( GetPrivateProfileString("RUNPARA", "STARTRUN","",RUN, 10, strFile) != 0)
  244. {
  245. nStartRun = (RUN[0] == '1' ? true:false);
  246. }
  247. if( nStartRun == 0 ) return TRUE;
  248. /* 将整个脚本,转化为一个分行的字符串数组 */
  249. CStringArray m_strLineArray;
  250. int nArraySize = GetLinetArrayFromScript(strScript, m_strLineArray);
  251. // 处理字符串数组
  252. int m_nVariantCount = 0;
  253. int nCurrntLine = 0;
  254. int nNextLine = 1; //当前行和下一行的行号
  255. for ( int i = 0; i < nArraySize; i++) //处理nArraySize行脚本
  256. {
  257. CString strCurLine = m_strLineArray[nCurrntLine];
  258. int nLineLen = strCurLine.GetLength();
  259. int nPos = 0;
  260. while (nPos < nLineLen)
  261. {
  262. CString strWord = GetNextWord(strCurLine, nPos);
  263. if (strWord.IsEmpty())
  264. {
  265. break;
  266. }
  267. // 判断是否是遥测变量
  268. if (pVariantsManager->FindAnalog(strWord) != NULL)
  269. {
  270. // 判断是否是赋值
  271. BOOL bSetValue = IsSet(strCurLine, strWord);
  272. m_nVariantCount++; //变量数加1
  273. CString tmpVarName; //转换的脚本变量
  274. tmpVarName.Format("V%06d",m_nVariantCount);
  275. m_strLineArray.InsertAt(m_nVariantCount-1,"dim "+ tmpVarName+'\n');
  276. nCurrntLine++;
  277. nNextLine++;
  278. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  279. if (bSetValue)
  280. {
  281. nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName);
  282. CString strChangeLine = "SetYCValue \"" + strWord +"\""+","+ tmpVarName+'\n';
  283. m_strLineArray.InsertAt(nCurrntLine+1, strChangeLine);
  284. }
  285. else
  286. {
  287. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  288. CString strChangeLine = tmpVarName + " = GetYCValue(\"" + strWord + "\")"+'\n';
  289. m_strLineArray.InsertAt(nCurrntLine, strChangeLine);
  290. nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName);
  291. nCurrntLine++;
  292. }
  293. nNextLine++;
  294. }
  295. // 判断是否是遥信变量
  296. else if( pVariantsManager->FindAnalog(strWord) != NULL )
  297. {
  298. // 判断是否是赋值
  299. BOOL bSetValue = IsSet(strCurLine, strWord);
  300. m_nVariantCount++; //变量数加1
  301. CString tmpVarName; //转换的脚本变量
  302. tmpVarName.Format("V%06d",m_nVariantCount);
  303. m_strLineArray.InsertAt(m_nVariantCount-1,"dim "+ tmpVarName+'\n');
  304. nCurrntLine++;
  305. nNextLine++;
  306. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  307. if (bSetValue)
  308. {
  309. nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName);
  310. CString strChangeLine = "SetYXValue \"" + strWord +"\""+","+ tmpVarName+'\n';
  311. m_strLineArray.InsertAt(nCurrntLine+1, strChangeLine);
  312. }
  313. else
  314. {
  315. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  316. CString strChangeLine = tmpVarName + " = GetYXValue(\"" + strWord + "\")"+'\n';
  317. m_strLineArray.InsertAt(nCurrntLine, strChangeLine);
  318. nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName);
  319. nCurrntLine++;
  320. }
  321. nNextLine++;
  322. }
  323. else
  324. {
  325. //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos));
  326. nPos += strWord.GetLength();
  327. }
  328. }
  329. /*一行处理完毕*/
  330. m_strLineArray.SetAt(nCurrntLine, strCurLine);
  331. nCurrntLine = nNextLine;
  332. nNextLine++;
  333. }
  334. //将字符串列表拼接成脚本
  335. GetStringFromLineArray(strScript, m_strLineArray);
  336. return TRUE;
  337. }
  338. void CScriptObject::Sleeps(short mSeconds)
  339. {
  340. Sleep(mSeconds);
  341. }