Example #1
0
JNIEXPORT jboolean JNICALL
Java_org_ocera_orte_Publication_jORTEPublicationSend
  (JNIEnv *env, jobject obj, jlong pub_handle)
{
  int       flag_ok = 0;
  int8_t    b;

  //

  #ifdef TEST_STAGE
  printf(":c: jORTEPublicationSend() called.. \n");
  #endif

  do {
    /////////////////////////////////////////////////
    // call ORTE function
    b = ORTEPublicationSend((ORTEPublication *)pub_handle);
    #ifdef TEST_STAGE
    printf(":c: b = ORTEPublicationSend() = %d \n", b);
    #endif
    if (b == ORTE_BAD_HANDLE) {
     #ifdef TEST_STAGE
      printf(":!c: data not sent! [bad pub handle] \n");
     #endif
      break;
    }
    if (b == ORTE_OK) {
     #ifdef TEST_STAGE
      printf(":c: data sent succesfuly.. \n");
     #endif
    }
    // set flag
    flag_ok = 1;
  } while (0);

  if (flag_ok == 0) {
    return 0;
  }

  return 1;

}
/*
*Function: QueryResponse
*Inputs:none
*Outputs:none
*Returns:none
*/
void Modbus_driver_Instance::QueryResponse(QObject *p, const QString &c, int id, QObject*caller) // handles database responses
{
	IT_IT("Modbus_driver_Instance::QueryResponse");
	
	if(p != this) return;
	switch(id)
	{
		case tListUnits:
		{
			//
			// get the properties - need to be receipe aware 
			//
			unit_name = GetConfigureDb()->GetString("NAME");

			QString pc = 
			"select * from PROPS where SKEY='" + Name + 
			"' and (IKEY = '(default)' or IKEY='"+ GetReceipeName() + "') order by IKEY desc;"; 
			//
			// get the properties SKEY = unit name IKEY = receipe name
			// 
			GetConfigureDb()->DoExec(this,pc,tUnitProperties);

			
			if(GetReceipeName() == "(default)")
			{
				// get the properties for this receipe  - fall back to default
				QString cmd = "select * from SAMPLE where UNIT='" + GetConfigureDb()->GetString("NAME") + "' and ENABLED=1;"; 
				GetConfigureDb()->DoExec(this,cmd,tList);
			}
			else
			{
				QString cmd = 
				"select * from SAMPLE where UNIT='" + GetConfigureDb()->GetString("NAME") + 
				"' and NAME in (" + DriverInstance::FormSamplePointList() + ");"; 
				// only the enabled sample points
				GetConfigureDb()->DoExec(this,cmd,tList);
			};
		}
		break;
		case tList: // list of sample points controlled by the unit
		{
			//  
			// copy the list to local dictionary 
			// now get the properties for each of the sample points
			QString nl; // name list for getting properties
			// 
			for(int ii = 1; ii <= IecItems; ii++) // initialise
			{
				Values[ii].Name = "";
				Values[ii].clear();
			};
			//  
			// 
			int n = GetConfigureDb()->GetNumberResults();
			for(int i = 0; i < n; i++,GetConfigureDb()->FetchNext())
			{
				int idx = GetConfigureDb()->GetInt("IPINDEX");
				if (idx > 0 && idx <= IecItems)
				{
					Values[idx].Name = GetConfigureDb()->GetString("NAME"); // save the name
				};

				if(i)
				{
					nl += ",";
				};

				nl += "'" + GetConfigureDb()->GetString("NAME") + "'";
			};
			
			QString cmd = "select * from PROPS where SKEY='SAMPLEPROPS' and IKEY in ("+nl+");";
			GetConfigureDb()->DoExec(this,cmd,tSamplePointProperties);
		};
		break;
		case tSamplePointProperties://properties specific to a sample point
		{
			int n = GetConfigureDb()->GetNumberResults();
			for(int j = 0; j < n; j++,GetConfigureDb()->FetchNext()) // may get a boat load of properties back
			{  
				// look for the entry in the table
				for(int k = 1; k <= IecItems;k++)
				{
					if(Values[k].Name == GetConfigureDb()->GetString("IKEY"))
					{
//						QString s = UndoEscapeSQLText(GetConfigureDb()->GetString("DVAL"));
//						QTextIStream is(&s); // extract the values
//						QString a;  
//						is >> a; // interval
//						Values[k].SamplePeriod = QStringToInterval(a);
//						Values[k].clear(); // set up
//						is >> a; // now get the 
//						Values[k].Low  = a.toDouble(); // 4 - 20 low value 
//						is >> a;
//						Values[k].High = a.toDouble(); // 4 - 20 high value
//						is >> a;
//						Values[k].fSpotValue = a.toInt(); 
						break;                    
					};
				};
			};
		};
		break;
		case tUnitProperties:
		{
			if(GetConfigureDb()->GetNumberResults() > 0)
			{
				// 
				QString s = UndoEscapeSQLText(GetConfigureDb()->GetString("DVAL")); // the top one is either the receipe or (default)
				QTextIStream is(&s); // extract the values
				//
				is >> IecItems;	  // how many modbus items there are in the RTU or PLC
				is >> Cfg.SampleTime; // how long we sample for in milliseconds
				is >> Cfg.ServerID;
				//MODBUS TCP
				is >> Cfg.MODBUSServerIPAddress;  // MODBUS server IP address (slave)
				is >> Cfg.MODBUSServerTCPPort;  // MODBUS server TCP port
				//MODBUS RTU //If this fields are filled in, then we have a serial line
				is >> Cfg.SerialDevice;
				is >> Cfg.Baud;
				is >> Cfg.DataBits;
				is >> Cfg.StopBit;
				is >> Cfg.Parity;
				
				if(strlen((const char*)Cfg.SerialDevice) == 0)
				{
					Cfg.context = InstanceCfg::TCP;
				}
				else
				{
					Cfg.context = InstanceCfg::RTU;
				}

				Countdown = 1;

				if(Values)
				{
					delete[] Values;
					Values = NULL;
				}

				Values = new Track[IecItems+1];
				//
				if(InTest())
				{
					Cfg.SampleTime = 1000; // override sampling period
				};
				//

				//Start MODBUS client driver
				if(!Connect())
				{
					QSLogAlarm(Name,tr("Failed to start MODBUS client driver"));
				}
			}
		}
		break;
		case tGetSamplePointNamefromIOA:
		{
			QSTransaction &t = GetConfigureDb()->CurrentTransaction();

			if(GetConfigureDb()->GetNumberResults() > 0)
			{
				// 
				#ifdef DEPRECATED_MODBUS_CONFIG
				QString SamplePointName = UndoEscapeSQLText(GetConfigureDb()->GetString("IKEY"));
				#else
				QString SamplePointName = UndoEscapeSQLText(GetConfigureDb()->GetString("NAME"));
				#endif

				double v = 0.0;

				if(strlen((const char*)t.Data1) > 0)
				{
					v = atof((const char*)t.Data1);
                    PostValue(SamplePointName, "VALUE", v); //Post the value directly in memory database
				}

				printf("SamplePointName = %s, IOA = %s, value = %lf\n", (const char*)SamplePointName, (const char*)t.Data2, v);
			}
		}
		break;
		case tGetIOAfromSamplePointName:
		{
			QSTransaction &t = GetConfigureDb()->CurrentTransaction();

			if(GetConfigureDb()->GetNumberResults() > 0)
			{
				// 
				#ifdef DEPRECATED_MODBUS_MASTER_CONFIG
				QString IOACommand = UndoEscapeSQLText(GetConfigureDb()->GetString("DVAL"));
				#else
				int IOACommand = GetConfigureDb()->GetInt("IOA");
				#endif
				
				double command_value = 0.0;

				if(strlen((const char*)t.Data1) > 0)
				{
					command_value = atof((const char*)t.Data1);
				}

				printf("Command from %s, IOA = %d, value = %lf\n", (const char*)t.Data2, IOACommand, command_value);

				//Send C_SE_TC_1//////////////////////////////////////////////////////////////////////////
				struct iec_item item_to_send;
				memset(&item_to_send,0x00, sizeof(struct iec_item));
				item_to_send.iec_type = C_SE_TC_1;
				item_to_send.iec_obj.ioa = IOACommand;
				item_to_send.iec_obj.o.type63.sv = (float)command_value;

				struct cp56time2a actual_time;
				get_utc_host_time(&actual_time);
				item_to_send.iec_obj.o.type63.time = actual_time;

				item_to_send.msg_id = msg_sent_in_control_direction++;
				item_to_send.checksum = clearCrc((unsigned char *)&item_to_send, sizeof(struct iec_item));
				///////////////////////////////////////////////////////////////////////////////////////////

				////////////////////Middleware/////////////////////////////////////////////
				//prepare published data
				memset(&instanceSend,0x00, sizeof(iec_item_type));
				instanceSend.iec_type = item_to_send.iec_type;
				memcpy(&(instanceSend.iec_obj), &(item_to_send.iec_obj), sizeof(struct iec_object));
				instanceSend.msg_id = item_to_send.msg_id;
				instanceSend.checksum = item_to_send.checksum;

				ORTEPublicationSend(publisher);
				//////////////////////////Middleware/////////////////////////////////////////
			}
		}
		break;
		default:
		break;
	}
Example #3
0
void CustomDb::changePoint(   DnpAddr_t      addr,
			     DnpIndex_t     index,
		         PointType_t    pointType,
			     float          value,
			     DnpTime_t      timestamp,
				 uint8_t flag,
				 uint8_t grp, 
				 uint8_t var)
{
    assert(addr != 0);
    
	cp56time2a time;
	struct iec_item item_to_send;
	double delta = 0.0;
    	
	memset(&item_to_send,0x00, sizeof(struct iec_item));
	
	item_to_send.iec_obj.ioa = 0;
	//item_to_send.cause = cot;
	
//The elements of the arrays are labeled 0 through N - 1 where N is the number of blocks for the 
//respective data type.
//In DNP3 terminology, the element numbers are called the point indexes. Indexes are zero-based in DNP3, 
//that is, the lowest element is always identified as zero.
		
	switch(pointType)
	{
		case EventInterface::AI:
		{
			if(grp == 32 && var == 1)
			{
				printf("AI\n");
				//IT_COMMENT("AI\n");
				//IT_COMMENT1("addr = %d\n", addr);
				//IT_COMMENT1("index = %d\n", index);

				//Index on RTU starts from 0, while IOA on control center starts from 1
				item_to_send.iec_obj.ioa = index + nIOA_AI;

				printf("ioa = %d\n", item_to_send.iec_obj.ioa);
				//IT_COMMENT1("ioa = %d\n", item_to_send.iec_obj.ioa);

				item_to_send.iec_type = M_ME_TF_1;

				//DNP time (DnpTime_t) is a six byte unsigned int representing the number of milli-seconds
				// since midnight UTC Jan 1, 1970 (does not include leap seconds)
				
				epoch_to_cp56time2a(&time, timestamp);
				item_to_send.iec_obj.o.type36.mv = value;

				printf("value = %f\n", value);
				//IT_COMMENT1("value = %f\n", value);
				
				item_to_send.iec_obj.o.type36.time = time;

				if((flag & QUALITY_ONLINE) != QUALITY_ONLINE)
					item_to_send.iec_obj.o.type36.iv = 1;
			}
		}
		break;
		case EventInterface::BI:
		{
			printf("BI\n");
			//IT_COMMENT("BI\n");

			if(grp == 1 && var == 1)
			{
				//Index on RTU starts from 0, while IOA on control center starts from 1
				item_to_send.iec_obj.ioa = index + nIOA_BI;
				
				printf("ioa = %d\n", item_to_send.iec_obj.ioa);

				//IT_COMMENT1("ioa = %d\n", item_to_send.iec_obj.ioa);

				item_to_send.iec_type = M_SP_TB_1;
				epoch_to_cp56time2a(&time, timestamp);

				if(value > 0.0)
					item_to_send.iec_obj.o.type30.sp = 1;
				else
					item_to_send.iec_obj.o.type30.sp = 0;

				item_to_send.iec_obj.o.type30.time = time;

				printf("value = %f\n", value);

				//IT_COMMENT1("item_to_send.iec_obj.o.type30.sp = %d\n", item_to_send.iec_obj.o.type30.sp);
				
				if((flag & QUALITY_ONLINE) != QUALITY_ONLINE)
					item_to_send.iec_obj.o.type30.iv = 1;
			}
		}
		break;
		case EventInterface::CI:
		{
			if(grp == 20 && var == 5)
			{
				printf("CI\n");
				//IT_COMMENT("CI\n");

				//Index on RTU starts from 0, while IOA on control center starts from 1
				item_to_send.iec_obj.ioa = index + nIOA_CI;

				printf("ioa = %d\n", item_to_send.iec_obj.ioa);

				//IT_COMMENT1("ioa = %d\n", item_to_send.iec_obj.ioa);

				item_to_send.iec_type = M_IT_TB_1;
				epoch_to_cp56time2a(&time, timestamp);
				item_to_send.iec_obj.o.type37.counter = (int)value;
				item_to_send.iec_obj.o.type37.time = time;
					
				printf("value = %f\n", value);
				//IT_COMMENT1("value = %f\n", value);

				if((flag & QUALITY_ONLINE) != QUALITY_ONLINE)
					item_to_send.iec_obj.o.type37.iv = 1;
			}
		}
		break;
		case EventInterface::AO:
		{
			printf("AO\n");
			//IT_COMMENT("AO\n");

			//Index on RTU starts from 0, while IOA on control center starts from 1
			item_to_send.iec_obj.ioa = index + nIOA_AO;

			printf("ioa = %d\n", item_to_send.iec_obj.ioa);
			//IT_COMMENT1("ioa = %d\n", item_to_send.iec_obj.ioa);

			item_to_send.iec_type = M_ME_TF_1;
			epoch_to_cp56time2a(&time, timestamp);
			item_to_send.iec_obj.o.type36.mv = value;
			item_to_send.iec_obj.o.type36.time = time;

			printf("value = %f\n", value);
			//IT_COMMENT1("value = %f\n", value);

			if((flag & QUALITY_ONLINE) != QUALITY_ONLINE)
				item_to_send.iec_obj.o.type36.iv = 1;
		}
		break;
		case EventInterface::BO:
		{
			if(grp == 10 && var == 2)
			{
				printf("BO\n");
				//IT_COMMENT("BO");

				//Index on RTU starts from 0, while IOA on control center starts from 1
				item_to_send.iec_obj.ioa = index + nIOA_BO;

				printf("ioa = %d\n", item_to_send.iec_obj.ioa);
				//IT_COMMENT1("ioa = %d\n", item_to_send.iec_obj.ioa);

				item_to_send.iec_type = M_SP_TB_1;
				epoch_to_cp56time2a(&time, timestamp);

				if(value > 0.0)
					item_to_send.iec_obj.o.type30.sp = 1;
				else
					item_to_send.iec_obj.o.type30.sp = 0;
				
				item_to_send.iec_obj.o.type30.time = time;

				printf("value = %f\n", value);
				//IT_COMMENT1("value = %f\n", value);

				if((flag & QUALITY_ONLINE) != QUALITY_ONLINE)
					item_to_send.iec_obj.o.type30.iv = 1;
			}
		}
		break;
		case EventInterface::NONE:
		{
			printf("NONE\n");
		}
		break;
		case EventInterface::ST:
		{
			printf("ST\n");
		}
		break;
		case EventInterface::AP_AB_ST: // app abnormal stat
		{
			printf("AP_AB_ST\n");
		}
		break;
		case EventInterface::AP_NM_ST: // app normal stat
		{
			printf("AP_NM_ST\n");
		}
		break;
		case EventInterface::DL_AB_ST: // datalink abnormal stat
		{
			printf("DL_AB_ST\n");
		}
		break;
		case EventInterface::DL_NM_ST: // datalink normal stat
		{
			printf("DL_NM_ST\n");
		}
		break;
		case EventInterface::SA_AB_ST: // secure auth abnormal stat
		{
			printf("SA_AB_ST\n");
		}
		break;
		case EventInterface::SA_NM_ST: // secure auth normal stat
		{
			printf("SA_NM_ST\n");
		}
		break;
		case EventInterface::EP_AB_ST: // end point abnormal stat
		{
			printf("EP_AB_ST\n");
		}
		break;
		case EventInterface::EP_NM_ST: // end point normal stat
		{
			printf("EP_NM_ST\n");
		}
		break;
		case EventInterface::NUM_POINT_TYPES:
		{
			printf("NUM_POINT_TYPES\n");
		}
		break;
		default:
			printf("UNSUPPORTED TYPE\n");
		break;
	}

	//IT_COMMENT6("at time: %d_%d_%d_%d_%d_%d", time.hour, time.min, time.msec, time.mday, time.month, time.year);

	item_to_send.msg_id = n_sent_items;
	item_to_send.checksum = clearCrc((unsigned char *)&item_to_send, sizeof(struct iec_item));

	//unsigned char buf[sizeof(struct iec_item)];
	//int len = sizeof(struct iec_item);
	//memcpy(buf, &item_to_send, len);
	//	for(j = 0;j < len; j++)
	//	{
	//	  unsigned char c = *(buf + j);
		//fprintf(stderr,"tx ---> 0x%02x\n", c);
		//fflush(stderr);
		//IT_COMMENT1("tx ---> 0x%02x\n", c);
	//	}

	Sleep(10); //Without delay there is missing of messages in the loading

	//Send in monitor direction
	fprintf(stderr,"Sending message %u th\n", n_sent_items);
	fflush(stderr);

	//prepare published data
	memset(global_instanceSend,0x00, sizeof(iec_item_type));
	
	global_instanceSend->iec_type = item_to_send.iec_type;
	memcpy(&(global_instanceSend->iec_obj), &(item_to_send.iec_obj), sizeof(struct iec_object));
	global_instanceSend->cause = item_to_send.cause;
	global_instanceSend->msg_id = item_to_send.msg_id;
	global_instanceSend->ioa_control_center = item_to_send.ioa_control_center;
	global_instanceSend->casdu = item_to_send.casdu;
	global_instanceSend->is_neg = item_to_send.is_neg;
	global_instanceSend->checksum = item_to_send.checksum;

	ORTEPublicationSend(global_publisher);

	n_sent_items++;
}