#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 = pVariantsManager->GetAnalogValue(varName); //LOG4C((LOG_NOTICE, "GetYCValue varName=%s value=%d",varName,int(value))); return value; } // 设置遥测值 void CScriptObject::SetYCValue(LPCTSTR varName, double value) { LOG4C((LOG_NOTICE, "SetYCValue varName=%s value=%d",varName,int(value))); //if( pVariantsManager->IsEndIdentifyTime( varName ) ) { pVariantsManager->SetAnalogValue(varName, 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) { CHAR strFile[MAX_PATH + 1] = ""; CHAR strDirectory[MAX_PATH + 1] = ""; CHAR strValue[255 + 1] = ""; int iPosFile = 0; int iLenghtFile = 0; //获取系统路径 //#ifdef _DEBUG // GetCurrentDirectory(MAX_PATH, strDirectory); //#else GetModuleFileName(AfxGetApp()->m_hInstance, strDirectory, MAX_PATH); iLenghtFile = strlen(strDirectory); for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--) { if (strDirectory[iPosFile] == '\\') { strDirectory[iPosFile] = '\0'; break; } } //#endif char RUN[MAX_PATH] = {0}; memset(RUN, 0, sizeof(RUN)); wsprintf(strFile, "%s\\Project\\runpara.ini", strDirectory); int nStartRun = 0; if ( GetPrivateProfileString("RUNPARA", "STARTRUN","",RUN, 10, strFile) != 0) { nStartRun = (RUN[0] == '1' ? true:false); } if( nStartRun == 0 ) return TRUE; /* 将整个脚本,转化为一个分行的字符串数组 */ CStringArray m_strLineArray; int nArraySize = GetLinetArrayFromScript(strScript, m_strLineArray); // 处理字符串数组 int m_nVariantCount = 0; int nCurrntLine = 0; int nNextLine = 1; //当前行和下一行的行号 for ( int i = 0; i < nArraySize; i++) //处理nArraySize行脚本 { CString strCurLine = m_strLineArray[nCurrntLine]; int nLineLen = strCurLine.GetLength(); int nPos = 0; while (nPos < nLineLen) { CString strWord = GetNextWord(strCurLine, nPos); if (strWord.IsEmpty()) { break; } // 判断是否是遥测变量 if (pVariantsManager->FindAnalog(strWord) != NULL) { // 判断是否是赋值 BOOL bSetValue = IsSet(strCurLine, strWord); m_nVariantCount++; //变量数加1 CString tmpVarName; //转换的脚本变量 tmpVarName.Format("V%06d",m_nVariantCount); m_strLineArray.InsertAt(m_nVariantCount-1,"dim "+ tmpVarName+'\n'); nCurrntLine++; nNextLine++; //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos)); if (bSetValue) { nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName); CString strChangeLine = "SetYCValue \"" + strWord +"\""+","+ tmpVarName+'\n'; m_strLineArray.InsertAt(nCurrntLine+1, strChangeLine); } else { //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos)); CString strChangeLine = tmpVarName + " = GetYCValue(\"" + strWord + "\")"+'\n'; m_strLineArray.InsertAt(nCurrntLine, strChangeLine); nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName); nCurrntLine++; } nNextLine++; } // 判断是否是遥信变量 else if( pVariantsManager->FindAnalog(strWord) != NULL ) { // 判断是否是赋值 BOOL bSetValue = IsSet(strCurLine, strWord); m_nVariantCount++; //变量数加1 CString tmpVarName; //转换的脚本变量 tmpVarName.Format("V%06d",m_nVariantCount); m_strLineArray.InsertAt(m_nVariantCount-1,"dim "+ tmpVarName+'\n'); nCurrntLine++; nNextLine++; //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos)); if (bSetValue) { nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName); CString strChangeLine = "SetYXValue \"" + strWord +"\""+","+ tmpVarName+'\n'; m_strLineArray.InsertAt(nCurrntLine+1, strChangeLine); } else { //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos)); CString strChangeLine = tmpVarName + " = GetYXValue(\"" + strWord + "\")"+'\n'; m_strLineArray.InsertAt(nCurrntLine, strChangeLine); nLineLen = ReplaceVar(strCurLine, nPos, strWord, tmpVarName); nCurrntLine++; } nNextLine++; } else { //LOG4C((LOG_NOTICE, "strCurLine=%s strWord=%s nPos=%d",strCurLine,strWord,nPos)); nPos += strWord.GetLength(); } } /*一行处理完毕*/ m_strLineArray.SetAt(nCurrntLine, strCurLine); nCurrntLine = nNextLine; nNextLine++; } //将字符串列表拼接成脚本 GetStringFromLineArray(strScript, m_strLineArray); return TRUE; } void CScriptObject::Sleeps(short mSeconds) { Sleep(mSeconds); }