123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #include "stdafx.h"
- #include "ScriptObject.h"
- #include "public.h"
- #include "kernel.h"
- //#include "log4c.h"
- // 取整
- int MyRound(double value)
- {
- int onepoint = ((int)(value*10))%10;
- if(onepoint < 5 && onepoint >= 0)
- {
- return ((int)(value*10))/10;
- }
- else if(onepoint >= 5)
- {
- return ((int)(value*10))/10 + 1;
- }
- else if(onepoint <= -5)
- {
- return ((int)(value*10))/10 -1;
- }
- else
- {
- return ((int)(value*10))/10 ;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- IMPLEMENT_DYNCREATE(CScriptObject, CCmdTarget)
- CScriptObject::CScriptObject()
- {
- EnableAutomation();
- }
- CScriptObject::~CScriptObject()
- {
- }
- void CScriptObject::OnFinalRelease()
- {
- CCmdTarget::OnFinalRelease();
- }
- BEGIN_MESSAGE_MAP(CScriptObject, CCmdTarget)
- //{{AFX_MSG_MAP(CScriptObject)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- BEGIN_DISPATCH_MAP(CScriptObject, CCmdTarget)
- //{{AFX_DISPATCH_MAP(CScriptObject)
- DISP_FUNCTION(CScriptObject, "GetYCValue", GetYCValue, VT_R8, VTS_BSTR)
- DISP_FUNCTION(CScriptObject, "SetYCValue", SetYCValue, VT_EMPTY, VTS_BSTR VTS_R8)
- DISP_FUNCTION(CScriptObject, "GetYXValue", GetYXValue, VT_I2, VTS_BSTR)
- DISP_FUNCTION(CScriptObject, "SetYXValue", SetYXValue, VT_EMPTY, VTS_BSTR VTS_I2)
- DISP_FUNCTION(CScriptObject, "ShowResult", ShowResult, VT_R8, VTS_BSTR)
- DISP_FUNCTION(CScriptObject, "Sleeps", Sleeps, VT_EMPTY, VTS_I2)
- //}}AFX_DISPATCH_MAP
- END_DISPATCH_MAP()
- // {B07CA4FB-3758-4E2C-B386-C0C493072CFB}
- static const IID IID_I_MYSCRIPTOBJECT =
- { 0xb07ca4fb, 0x3758, 0x4e2c, { 0xb3, 0x86, 0xc0, 0xc4, 0x93, 0x7, 0x2c, 0xfb } };
- BEGIN_INTERFACE_MAP(CScriptObject, CCmdTarget)
- INTERFACE_PART(CScriptObject, IID_I_MYSCRIPTOBJECT, Dispatch)
- END_INTERFACE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CScriptObject message handlers
- // 输出语法检查结果
- double CScriptObject::ShowResult(LPCTSTR prompt)
- {
- AfxMessageBox("Script Run Success!", MB_SETFOREGROUND);
- return 0.0;
- }
- // 取遥测值
- double CScriptObject::GetYCValue(LPCTSTR varName)
- {
- int nIdentifyTime = 0;
- double value = 0.0;//VariantsManager->GetAnalogValue(varName);
- //LOG4C((LOG_NOTICE, "GetYCValue varName=%s value=%d",varName,int(value)));
- return value;
- }
- // 设置遥测值
- void CScriptObject::SetYCValue(LPCTSTR varName, double value)
- {
-
- }
- // 取状态量值
- short CScriptObject::GetYXValue(LPCTSTR varName)
- {
- short x = 0;
- return x;
- }
- // 设置状态值
- void CScriptObject::SetYXValue(LPCTSTR varName, short value)
- {
- }
- //////////////////////////////////////////////////////////////////////////
- BOOL ScriptParam::ShouldRun()
- {
- if (!m_bValid)
- {
- return FALSE;
- }
-
- if (m_bCondition)
- {
- m_calc.SetFormula(m_strExpression);
- return (BOOL)m_calc.GetResult(); //成立
- }
- else
- {
- SYSTEMTIME st;
- GetLocalTime(&st);
- CTime cur = CTime::GetCurrentTime();
- CTime last = CTime(m_stLastRunTime);
- CTimeSpan sp = cur - last;
- int second = (int)sp.GetTotalSeconds();
- if ( second * 1000 + st.wMilliseconds - m_stLastRunTime.wMilliseconds > m_curcleTime)
- {
- memcpy(&m_stLastRunTime, &st, sizeof(SYSTEMTIME));
- return TRUE;
- }
- }
- return FALSE;
- }
- ScriptParam::ScriptParam()
- {
- m_curcleTime = 0; //循环时间
- m_strExpression = ""; //条件表达式
- m_strScript = ""; //脚本
- m_bCircle = FALSE;
- m_bCondition = FALSE;
- m_bValid = FALSE;
- GetLocalTime(&m_stLastRunTime);
- }
- // 判断是有效变量名
- BOOL IsValidChar(char chr)
- {
- if ( chr >= '0' && chr <= '9')
- return TRUE;
- else if ((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') )
- return TRUE;
- else if (chr == '_')
- return TRUE;
- else
- return FALSE;
- }
- // 取下一个单词, 连续的字母和数字组合为一个单词
- CString GetNextWord(CString strCurLine, int& nPos)
- {
- CString strLine = strCurLine;
- CString strWord = "";
- int nLen = strLine.GetLength();
- BOOL bStart = FALSE;
- for (int i = nPos; i < nLen; i++)
- {
- char tmpChar = strLine.GetAt(i);
- if ( IsValidChar(tmpChar) )
- {
- if (bStart)
- {
- strWord += tmpChar;
- }
- else
- {
- bStart = TRUE;
- strWord = tmpChar;
- nPos = i;
- }
- }
- else if (bStart)
- {
- break;
- }
- }
- return strWord;
- }
- // 是否是设置
- BOOL IsSet(CString strCurLine, CString strWord)
- {
- CString strLine = strCurLine;
- strLine.TrimLeft();
- return (strLine.Left(strWord.GetLength()) == strWord);
- }
- // 返回新的字符串长度, nPos变为下一个寻找的起点
- int ReplaceVar(CString& strCurLine, int& nPos, CString strWord, CString strVar)
- {
- strCurLine.Delete(nPos, strWord.GetLength());
- strCurLine.Insert(nPos, strVar);
- nPos = nPos + strVar.GetLength();
- return strCurLine.GetLength();//cHn modify - strWord.GetLength() + strVar.GetLength();
- }
- // 由脚本转换为行数组
- int GetLinetArrayFromScript(const CString script, CStringArray& aLineCode)
- {
- aLineCode.RemoveAll();
- CString strScript = script +'\n';
- CString tempLine = "";
- int nStrLength = strScript.GetLength();
- for (int i = 0; i < nStrLength; i++)
- {
- tempLine = tempLine + strScript[i];
- if (strScript[i] == '\n') //换行
- {
- aLineCode.Add(tempLine);
- tempLine = "";
- }
- }
- return aLineCode.GetSize();
- }
- // 由行数组组成脚本
- void GetStringFromLineArray(CString& script, CStringArray& aLineCode)
- {
- script = "";
- int nLastSize = aLineCode.GetSize();
- for (int i = 0; i < nLastSize; i++)
- {
- script += aLineCode[i];
- }
- }
- /* 替换变量为相应的接口函数 */
- BOOL ProcessScript(CString& strScript)
- {
-
- return TRUE;
- }
- void CScriptObject::Sleeps(short mSeconds)
- {
- Sleep(mSeconds);
- }
|