ScriptObject.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 = 0.0;//VariantsManager->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. }
  81. // 取状态量值
  82. short CScriptObject::GetYXValue(LPCTSTR varName)
  83. {
  84. short x = 0;
  85. return x;
  86. }
  87. // 设置状态值
  88. void CScriptObject::SetYXValue(LPCTSTR varName, short value)
  89. {
  90. }
  91. //////////////////////////////////////////////////////////////////////////
  92. BOOL ScriptParam::ShouldRun()
  93. {
  94. if (!m_bValid)
  95. {
  96. return FALSE;
  97. }
  98. if (m_bCondition)
  99. {
  100. m_calc.SetFormula(m_strExpression);
  101. return (BOOL)m_calc.GetResult(); //成立
  102. }
  103. else
  104. {
  105. SYSTEMTIME st;
  106. GetLocalTime(&st);
  107. CTime cur = CTime::GetCurrentTime();
  108. CTime last = CTime(m_stLastRunTime);
  109. CTimeSpan sp = cur - last;
  110. int second = (int)sp.GetTotalSeconds();
  111. if ( second * 1000 + st.wMilliseconds - m_stLastRunTime.wMilliseconds > m_curcleTime)
  112. {
  113. memcpy(&m_stLastRunTime, &st, sizeof(SYSTEMTIME));
  114. return TRUE;
  115. }
  116. }
  117. return FALSE;
  118. }
  119. ScriptParam::ScriptParam()
  120. {
  121. m_curcleTime = 0; //循环时间
  122. m_strExpression = ""; //条件表达式
  123. m_strScript = ""; //脚本
  124. m_bCircle = FALSE;
  125. m_bCondition = FALSE;
  126. m_bValid = FALSE;
  127. GetLocalTime(&m_stLastRunTime);
  128. }
  129. // 判断是有效变量名
  130. BOOL IsValidChar(char chr)
  131. {
  132. if ( chr >= '0' && chr <= '9')
  133. return TRUE;
  134. else if ((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') )
  135. return TRUE;
  136. else if (chr == '_')
  137. return TRUE;
  138. else
  139. return FALSE;
  140. }
  141. // 取下一个单词, 连续的字母和数字组合为一个单词
  142. CString GetNextWord(CString strCurLine, int& nPos)
  143. {
  144. CString strLine = strCurLine;
  145. CString strWord = "";
  146. int nLen = strLine.GetLength();
  147. BOOL bStart = FALSE;
  148. for (int i = nPos; i < nLen; i++)
  149. {
  150. char tmpChar = strLine.GetAt(i);
  151. if ( IsValidChar(tmpChar) )
  152. {
  153. if (bStart)
  154. {
  155. strWord += tmpChar;
  156. }
  157. else
  158. {
  159. bStart = TRUE;
  160. strWord = tmpChar;
  161. nPos = i;
  162. }
  163. }
  164. else if (bStart)
  165. {
  166. break;
  167. }
  168. }
  169. return strWord;
  170. }
  171. // 是否是设置
  172. BOOL IsSet(CString strCurLine, CString strWord)
  173. {
  174. CString strLine = strCurLine;
  175. strLine.TrimLeft();
  176. return (strLine.Left(strWord.GetLength()) == strWord);
  177. }
  178. // 返回新的字符串长度, nPos变为下一个寻找的起点
  179. int ReplaceVar(CString& strCurLine, int& nPos, CString strWord, CString strVar)
  180. {
  181. strCurLine.Delete(nPos, strWord.GetLength());
  182. strCurLine.Insert(nPos, strVar);
  183. nPos = nPos + strVar.GetLength();
  184. return strCurLine.GetLength();//cHn modify - strWord.GetLength() + strVar.GetLength();
  185. }
  186. // 由脚本转换为行数组
  187. int GetLinetArrayFromScript(const CString script, CStringArray& aLineCode)
  188. {
  189. aLineCode.RemoveAll();
  190. CString strScript = script +'\n';
  191. CString tempLine = "";
  192. int nStrLength = strScript.GetLength();
  193. for (int i = 0; i < nStrLength; i++)
  194. {
  195. tempLine = tempLine + strScript[i];
  196. if (strScript[i] == '\n') //换行
  197. {
  198. aLineCode.Add(tempLine);
  199. tempLine = "";
  200. }
  201. }
  202. return aLineCode.GetSize();
  203. }
  204. // 由行数组组成脚本
  205. void GetStringFromLineArray(CString& script, CStringArray& aLineCode)
  206. {
  207. script = "";
  208. int nLastSize = aLineCode.GetSize();
  209. for (int i = 0; i < nLastSize; i++)
  210. {
  211. script += aLineCode[i];
  212. }
  213. }
  214. /* 替换变量为相应的接口函数 */
  215. BOOL ProcessScript(CString& strScript)
  216. {
  217. return TRUE;
  218. }
  219. void CScriptObject::Sleeps(short mSeconds)
  220. {
  221. Sleep(mSeconds);
  222. }