Changeset 14218 for trunk


Ignore:
Timestamp:
06/22/12 22:11:37 (12 years ago)
Author:
tbretz
Message:
Unified the event history queue
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/smartfact.cc

    r14213 r14218  
    370370    };
    371371
     372    // ------------------------- History classes -----------------------
     373
     374    struct EventElement
     375    {
     376        Time time;
     377        string msg;
     378
     379        EventElement(const Time &t, const string &s) : time(t), msg(s) { }
     380    };
     381
     382    class EventHist : public deque<EventElement>
     383    {
     384        const boost::posix_time::time_duration deltat; //boost::posix_time::pos_infin
     385        const uint64_t max;
     386
     387    public:
     388        EventHist(const boost::posix_time::time_duration &dt=boost::posix_time::hours(12), uint64_t mx=UINT64_MAX) : deltat(dt), max(mx) { }
     389
     390        void add(const string &s, const Time &t=Time())
     391        {
     392            while (size()>0 && (front().time+deltat<t || size()>max))
     393                pop_front();
     394
     395            push_back(EventElement(t, s));
     396        }
     397
     398        string get() const
     399        {
     400            ostringstream out;
     401
     402            string last = "";
     403            for (auto it=begin(); it!=end(); it++)
     404            {
     405                const string tm = it->time.GetAsStr("%H:%M:%S ");
     406                out << (tm!=last?tm:"--:--:-- ") << it->msg << "<br/>";
     407                last = tm;
     408            }
     409
     410            return out.str();
     411        }
     412        string rget() const
     413        {
     414            ostringstream out;
     415
     416            for (auto it=rbegin(); it!=rend(); it++)
     417                out << it->time.GetAsStr("%H:%M:%S ") << it->msg << "<br/>";
     418
     419            return out.str();
     420        }
     421    };
     422
    372423    // ------------------------- Internal variables -----------------------
    373424
     
    385436    // ----------------------------- Data storage -------------------------
    386437
    387     deque<string> fControlMessageHist;
    388     int32_t       fControlScriptDepth;
    389 
    390     uint32_t fMcpConfigurationState;   // For consistency
    391      int64_t fMcpConfigurationMaxTime;
    392      int64_t fMcpConfigurationMaxEvents;
    393     string   fMcpConfigurationName;
    394     Time     fMcpConfigurationRunStart;
    395     Time     fMcpConfigurationLastTime;
    396     deque<string> fMcpConfigurationHist;
     438    EventHist fControlMessageHist;
     439    int32_t   fControlScriptDepth;
     440
     441    uint32_t  fMcpConfigurationState;   // For consistency
     442     int64_t  fMcpConfigurationMaxTime;
     443     int64_t  fMcpConfigurationMaxEvents;
     444    string    fMcpConfigurationName;
     445    Time      fMcpConfigurationRunStart;
     446    EventHist fMcpConfigurationHist;
    397447
    398448    bool fLastRunFinishedWithZeroEvents;
     
    447497    deque<float> fRateScanDataHist[41];
    448498
    449     set<string>   fErrorList;
    450     deque<string> fErrorHist;
    451     deque<string> fChatHist;
    452     Time          fChatLastTime;
     499    set<string> fErrorList;
     500    EventHist   fErrorHist;
     501    EventHist   fChatHist;
    453502
    454503    Sun   fSun;
     
    636685            return;
    637686
    638         const string time = d.GetTimeAsStr("%H:%M:%S ");
    639 
    640         string str = "         ";
    641         for (auto it=fControlMessageHist.rbegin(); it!=fControlMessageHist.rend(); it++)
    642         {
    643             str = it->substr(0, time.length());
    644             if (str!="--:--:-- ")
    645                 break;
    646         }
    647 
    648         ostringstream tst;
    649         tst << d.GetQoS();
    650 
    651         string msg;
    652         msg += str==time ? "--:--:-- " : time;
    653         msg += d.Ptr<char>();
    654 
    655         fControlMessageHist.push_back(msg);
     687        fControlMessageHist.add(d.GetText(), d.GetTime());
    656688
    657689        ostringstream out;
     
    659691        out << Header(d) << '\n';
    660692        out << HTML::kWhite << '\t';
    661 
    662         out << "<->";
    663         for (auto it=fControlMessageHist.begin(); it!=fControlMessageHist.end(); it++)
    664             out << *it << "<br/>";
    665         out << "</->";
    666 
     693        out << "<->" << fControlMessageHist.get() << "</->";
    667694        out << '\n';
    668695
     
    709736    }
    710737
    711     void HandleFscControlStateChange(const EventImp &d)
    712     {
    713         const int32_t &last  = fDimFscControl.last.second;
    714         const int32_t &state = fDimFscControl.state();
    715 
    716         if (last==DimState::kOffline || state==DimState::kOffline)
    717             return;
    718 
    719         if (last<FSC::State::kConnected && state==FSC::State::kConnected)
    720         {
    721             AddMcpConfigurationHist(d, "<B>Camera swiched on</B>");
    722             SetAudio("startup");
    723         }
    724 
    725         if (last==FSC::State::kConnected && state<FSC::State::kConnected)
    726         {
    727             AddMcpConfigurationHist(d, "<B>Camera swiched off</B>");
    728             SetAudio("shutdown");
    729         }
    730     }
    731 
    732738    void AddMcpConfigurationHist(const EventImp &d, const string &msg)
    733739    {
    734         if (d.GetTime()>fMcpConfigurationLastTime+boost::posix_time::hours(12))
    735             fMcpConfigurationHist.clear();
    736 
    737         fMcpConfigurationLastTime  = d.GetTime();
    738         fMcpConfigurationHist.push_back(d.GetTimeAsStr("%H:%M:%S ")+msg+"<br/>");
     740        fMcpConfigurationHist.add(msg, d.GetTime());
    739741
    740742        ostringstream out;
    741743        out << d.GetJavaDate() << '\n';
    742744        out << HTML::kWhite << '\t';
    743         for (auto it=fMcpConfigurationHist.rbegin(); it!=fMcpConfigurationHist.rend(); it++)
    744             out << *it;
     745        out << "<->" << fMcpConfigurationHist.rget() << "</->";
    745746        out << '\n';
    746747
    747748        ofstream(fPath+"/observations.data") << out.str();
     749    }
     750
     751    void HandleFscControlStateChange(const EventImp &d)
     752    {
     753        const int32_t &last  = fDimFscControl.last.second;
     754        const int32_t &state = fDimFscControl.state();
     755
     756        if (last==DimState::kOffline || state==DimState::kOffline)
     757            return;
     758
     759        if (last<FSC::State::kConnected && state==FSC::State::kConnected)
     760        {
     761            AddMcpConfigurationHist(d, "<B>Camera swiched on</B>");
     762            SetAudio("startup");
     763        }
     764
     765        if (last==FSC::State::kConnected && state<FSC::State::kConnected)
     766        {
     767            AddMcpConfigurationHist(d, "<B>Camera swiched off</B>");
     768            SetAudio("shutdown");
     769        }
    748770    }
    749771
     
    894916    }
    895917
     918    void HandleDriveControlStateChange(const EventImp &d)
     919    {
     920        const int32_t &last  = fDimFscControl.last.second;
     921        const int32_t &state = fDimFscControl.state();
     922
     923        if (last==DimState::kOffline || state==DimState::kOffline)
     924            return;
     925
     926        if (last<Drive::State::kArmed && state>=Drive::State::kArmed)
     927            AddMcpConfigurationHist(d, "Drive connected");
     928
     929        if (last>=Drive::State::kArmed && state<Drive::State::kArmed)
     930            AddMcpConfigurationHist(d, "Drive disconnected");
     931    }
     932
    896933    int HandleDrivePointing(const EventImp &d)
    897934    {
     
    13201357        // by the MCP. Hence, we get a warning. So we have to require
    13211358        // two consecutive low rates.
    1322         if (*crate<0.1)
     1359        if (*crate<1)
    13231360            fFtmControlTriggerRateTooLow++;
    13241361        else
     
    16791716            return GetCurrentState();
    16801717
    1681         if (d.GetTime()>fChatLastTime+boost::posix_time::hours(12))
    1682             fChatHist.clear();
    1683 
    16841718        if (Time()<d.GetTime()+boost::posix_time::minutes(1))
    16851719            SetAudio("message");
    16861720
    1687         fChatLastTime = d.GetTime();
    1688 
    1689         string msg;
    1690         msg += d.GetTimeAsStr("%H:%M:%S ");
    1691         msg += d.Ptr<char>();
    1692 
    1693         fChatHist.push_front(msg);
    1694         if (fChatHist.size()>80)
    1695             fChatHist.pop_back();
     1721        fChatHist.add(d.GetText(), d.GetTime());
    16961722
    16971723        ostringstream out;
     
    16991725        out << Header(d) << '\n';
    17001726        out << HTML::kWhite << '\t';
    1701 
    1702         out << "<->";
    1703         for (auto it=fChatHist.begin(); it!=fChatHist.end(); it++)
    1704             out << *it << "<br/>";
    1705         out << "</->";
    1706 
     1727        out << "<->" << fChatHist.rget() << "</->";
    17071728        out << '\n';
    17081729
     
    18101831
    18111832        const bool isnew = fErrorList.insert(err).second;
    1812 
    18131833        if (isnew)
    1814         {
    1815             ostringstream msg;
    1816             msg << "<pre>" << Time().GetAsStr("%m-%d %H:%M") << "</pre> <->" << err << "</->";
    1817             if (find(fErrorHist.begin(), fErrorHist.end(), msg.str())==fErrorHist.end())
    1818             {
    1819                 fErrorHist.push_front(msg.str());
    1820                 if (fErrorHist.size()>80)
    1821                     fErrorHist.pop_back();
    1822             }
    1823         }
     1834            fErrorHist.add(err);
    18241835
    18251836        return isnew;
     
    21222133
    21232134        newerr |= SetError(fFtmControlTriggerRateTooLow>2 && fDimMcp.state()==MCP::State::kTakingData,
    2124                            "Trigger rate below 100mHz during data taking");
     2135                           "Trigger rate below 1Hz during data taking");
    21252136
    21262137        newerr |= SetError(fDimTimeCheck.state()==1,
     
    21702181            out << now.JavaDate() << '\n';
    21712182            out << HTML::kWhite << '\t';
    2172             for (auto it=fErrorHist.begin(); it!=fErrorHist.end(); it++)
    2173                 out << *it << "<br/>";
     2183            out << "<->" << fErrorHist.rget() << "<->";
    21742184            out << '\n';
    21752185
     
    26012611
    26022612        fDimFscControl.SetCallback(bind(&StateMachineSmartFACT::HandleFscControlStateChange, this, placeholders::_1));
     2613        fDimDriveControl.SetCallback(bind(&StateMachineSmartFACT::HandleDriveControlStateChange, this, placeholders::_1));
    26032614        fDimControl.SetCallback(bind(&StateMachineSmartFACT::HandleControlStateChange, this, placeholders::_1));
    26042615        fDimControl.AddCallback("dotest.dim", bind(&StateMachineSmartFACT::HandleDoTest, this, placeholders::_1));
Note: See TracChangeset for help on using the changeset viewer.