ProviewR Programmer's Reference Manual  V6.1.4
ra_advappl.cpp
Go to the documentation of this file.
1 
15 #include "pwr.h"
16 
18 
26 #include "pwr.h"
27 #include "rt_gdh.h"
28 #include "rt_errh.h"
29 #include "rt_aproc.h"
30 #include "rt_pwr_msg.h"
31 #include "rt_qcom_msg.h"
32 #include "rt_ini_event.h"
33 #include "co_error.h"
34 
35 class MyAppl {
36 public:
37  MyAppl()
38  {
39  }
40  void init(qcom_sQid* qid);
41  void open();
42  void close();
43  void scan();
44  float scantime()
45  {
46  return 1.0;
47  }
48 };
49 
50 void MyAppl::init(qcom_sQid* qid)
51 {
52  qcom_sQid qini;
53  qcom_sQattr qAttr;
54  pwr_tStatus sts;
55 
56  // Init error and status logger with a unic application index per node.
57  errh_Init("rs_appl", errh_eAnix_appl1);
58  errh_SetStatus(PWR__APPLSTARTUP);
59 
60  // Init database
61  sts = gdh_Init("rs_appl");
62  if (EVEN(sts)) {
63  errh_Fatal("gdh_Init, %m", sts);
64  errh_SetStatus(PWR__APPLTERM);
65  exit(sts);
66  }
67 
68  // Create a queue to receive stop and restart events
69  if (!qcom_Init(&sts, 0, "rs_appl")) {
70  errh_Fatal("qcom_Init, %m", sts);
71  errh_SetStatus(PWR__APPLTERM);
72  exit(sts);
73  }
74 
75  qAttr.type = qcom_eQtype_private;
76  qAttr.quota = 100;
77  if (!qcom_CreateQ(&sts, qid, &qAttr, "events")) {
78  errh_Fatal("qcom_CreateQ, %m", sts);
79  errh_SetStatus(PWR__APPLTERM);
80  exit(sts);
81  }
82 
83  qini = qcom_cQini;
84  if (!qcom_Bind(&sts, qid, &qini)) {
85  errh_Fatal("qcom_Bind(Qini), %m", sts);
86  errh_SetStatus(PWR__APPLTERM);
87  exit(-1);
88  }
89 }
90 
91 void MyAppl::open()
92 {
93  pwr_tOid oid;
94  pwr_tStatus sts;
95 
96  // Get configuration object
97  sts = gdh_NameToObjid("Noder-Node-MyAppl", &oid);
98  if (EVEN(sts))
99  throw co_error(sts);
100 
102 
103  // Link to database objects
104 }
105 
106 void MyAppl::close()
107 {
108  // Unlink to database objects
109 }
110 
111 void MyAppl::scan()
112 {
113  aproc_TimeStamp(scantime(), 1);
114 
115  // Do something
116 }
117 
118 int main()
119 {
120  pwr_tStatus sts;
121  MyAppl appl;
122  int tmo;
123  char mp[2000];
124  qcom_sQid qid = qcom_cNQid;
125  qcom_sGet get;
126  int swap = 0;
127  bool first_scan = true;
128 
129  appl.init(&qid);
130 
131  try {
132  appl.open();
133  } catch (co_error& e) {
134  errh_Error((char*)e.what().c_str());
135  errh_Fatal("rs_appl aborting");
136  errh_SetStatus(PWR__APPLTERM);
137  exit(0);
138  }
139 
140  aproc_TimeStamp(appl.scantime(), 1);
141  errh_SetStatus(PWR__ARUN);
142 
143  first_scan = true;
144  for (;;) {
145  if (first_scan) {
146  tmo = (int)(appl.scantime() * 1000 - 1);
147  }
148 
149  get.maxSize = sizeof(mp);
150  get.data = mp;
151  qcom_Get(&sts, &qid, &get, tmo);
152  if (sts == QCOM__TMO || sts == QCOM__QEMPTY) {
153  if (!swap)
154  appl.scan();
155  } else {
156  ini_mEvent new_event;
157  qcom_sEvent* ep = (qcom_sEvent*)get.data;
158 
159  new_event.m = ep->mask;
160  if (new_event.b.oldPlcStop && !swap) {
161  errh_SetStatus(PWR__APPLRESTART);
162  swap = 1;
163  appl.close();
164  } else if (new_event.b.swapDone && swap) {
165  swap = 0;
166  appl.open();
167  errh_SetStatus(PWR__ARUN);
168  } else if (new_event.b.terminate) {
169  exit(0);
170  }
171  }
172  first_scan = false;
173  }
174 }
175 
Advanced c++ application example.
Definition: ra_advappl.cpp:35
pwr_tStatus aproc_TimeStamp(float scantime, float maxdelay)
Set application timestamp. The timestamp is supervised by the event monitor and if the timestamp is d...
Definition: rt_aproc.c:90
pwr_tStatus aproc_RegisterObject(pwr_tOid)
Register Application object. The Application object is stored in the $Node object and be opened from ...
Definition: rt_aproc.c:57
void errh_Error(const char *msg,...)
Log an error message. The function has a variable argument list similar to sprintf.
Definition: rt_errh.c:343
pwr_tStatus errh_Init(const char *programName, errh_eAnix anix)
Initialize errh. Set up a queue to write to, set application name.
Definition: rt_errh.c:178
void errh_SetStatus(pwr_tStatus sts)
Set application status. The application status is showed in the Node graph. The application can set t...
Definition: rt_errh.c:202
void errh_Fatal(const char *msg,...)
Log a fatal message. The function has a variable argument list similar to sprintf.
Definition: rt_errh.c:364
pwr_tStatus gdh_Init(const char *name)
Initialize the process to the Proview runtime environment.
Definition: rt_gdh.c:2065
pwr_tStatus gdh_NameToObjid(const char *objectName, pwr_tObjid *objid)
Get the object identity of an object with name 'name'.
Definition: rt_gdh.c:2202
pwr_tBoolean qcom_Bind(pwr_tStatus *sts, const qcom_sQid *myQ, const qcom_sQid *toQ)
Bind one queue to another.
Definition: rt_qcom.c:171
pwr_tBoolean qcom_CreateQ(pwr_tStatus *sts, qcom_sQid *myQ, qcom_sQattr *attr, const char *qname)
Create a queue and make an implicit connect to it.
Definition: rt_qcom.c:213
void * qcom_Get(pwr_tStatus *sts, const qcom_sQid *myQ, qcom_sGet *get, int tmo_ms)
Get a new message.
Definition: rt_qcom.c:398
pwr_tBoolean qcom_Init(pwr_tStatus *sts, qcom_sAid *aid, const char *aname)
Connect to QCom.
Definition: rt_qcom.c:643
Basic type definitions. This include file contains the Proview basic type definitions.
#define EVEN(a)
Check if value is even.
Definition: pwr.h:623
int pwr_tStatus
Status type.
Definition: pwr.h:284
Include file for AProc Application process interface.
Include file for Errh System log messages.
Include file for Gdh Global Data Handler.
Object identity type.
Definition: pwr.h:263
Qcom event.
Definition: rt_qcom.h:155
Get data structure.
Definition: rt_qcom.h:185
Queue attributes.
Definition: rt_qcom.h:162
Queue identity.
Definition: rt_qcom.h:125