Python and ScadaBR API

Hi,
anyone has more example about interfacing python with scadabr api than the one in scadabr’s site?
Thank you in advance!
Best regards, Andrea

Hello Andrea,

So far we have not created a tutorial for python, but you can relate to
https://sites.google.com/a/certi.org.br/certi_scadabr/home/minicursos/scadabr
for references.

Vincente in this thread is using python with the API
http://www.scadabr.org.br/?q=node/448
I guess you can ask him in english =]

I´ll try to put up something with python too…

Best Regards

Hello Andrea, 

following solution with suds:

 

#download and install suds http://pypi.python.org/pypi/suds
#Create Data source name: machine1, type: "Virtual Data Source"
#Create Data Point name(type):
#counterProduct(Numeric/No change)
#enableMachine(Binary/No change)
#operatorID(Alphanumeric/No change)
 
from suds.client import Client
import datetime
 
dateStart = datetime.datetime.now()
 
client = Client('http://127.0.0.1:8080/ScadaBR/services/API?wsdl')
print '######################################'
print 'describing the service:'
print client
print '######################################'
#types are created as follows:
browseTagsOptions = client.factory.create('ns3:BrowseTagsOptions')
#note the type 
print 'BronseTagsOptions'
print browseTagsOptions
#set BrowseTagsParams
browseTagsOptions.maxReturn = 2
itemsPath = 'counterProduct'
#calling browseTags(xs:string itemsPath, ns3:BrowseTagsOptions options, )
BrowseTagsResponse = client.service.browseTags(itemsPath,browseTagsOptions)
print 'browse tag counterProduct'
print BrowseTagsResponse
#all tags
print 'ALL TAGS'
print client.service.browseTags()
print '######################################'
 
#browseEventsParams = client.factory.create('tns6:BrowseEventsParams')
getStatusResponse = client.factory.create('tns4:GetStatusResponse')
#browseEventsParams.options.eventType = True
#browseEventsParams.options.returnEventsConfig = True
getStatusResponse = client.service.getStatus()
print 'status'
print getStatusResponse
print '######################################'
 
#calling readData (xs:string[] itemPathList, ns3:ReadDataOptions options, )'
#create and set the parameters:
itemPathList = ['counterProduct','operatorID']
readDataOptions = client.factory.create('ns3:ReadDataOptions')
readDataOptions.maxReturn = 3
readDataResponse = client.service.readData(itemPathList , readDataOptions )
print 'reading tags: counterProduct , operatorID'
print readDataResponse
print 'reading the tag: counterProduct'
print readDataResponse.itemsList[0]
print 'reading only the value of tag: counterProduct'
print readDataResponse.itemsList[0].value
print '######################################'
 
#calling  writeStringData(ns5:ItemStringValue[] itemsList, ns3:WriteDataOptions options, )
#create and set the parameters:
writeDataOptions = client.factory.create('ns3:WriteDataOptions')
writeDataOptions.returnItemValues = True
itemStringList= client.factory.create('ns5:ItemStringValue')
dataType = client.factory.create('ns2:DataType')
quality = client.factory.create('ns2:QualityCode')
itemStringList.itemName = 'counterProduct'
itemStringList.dataType = dataType.FLOAT
itemStringList.value = 23.5
itemStringList.quality =quality.GOOD
itemStringList.timestamp = datetime.datetime.now()
writeStringDataResponse=client.service.writeStringData(itemStringList,writeDataOptions )
print 'writing on the tag counterProduct'
print  writeStringDataResponse
print '######################################'
 
#calling getDataHistory(xs:string itemName, ns4:GetDataHistoryOptions options, )
#create and set the parameters:
getDataHistoryOptions = client.factory.create('ns4:GetDataHistoryOptions')
print getDataHistoryOptions
itemName = 'counterProduct'
getDataHistoryOptions .maxReturn = 10
getDataHistoryOptions .initialDate = dateStart
itemStringList.value = 28.0
client.service.writeStringData(itemStringList,writeDataOptions )
itemStringList.value = 29.0
client.service.writeStringData(itemStringList,writeDataOptions )
itemStringList.value = 30.0
client.service.writeStringData(itemStringList,writeDataOptions )
itemStringList.value = 31.5
client.service.writeStringData(itemStringList,writeDataOptions )
getDataHistoryOptions.finalDate = datetime.datetime.now()
getDataHistoryResponse = client.factory.create('tns5:GetDataHistoryResponse')
print 'getDataHistoryResponse'
print getDataHistoryResponse
print 'Obtaining historical data of tag'
print client.service.getDataHistory(itemName,getDataHistoryOptions)
 
I hope to help
Vicente Orrù
 

Thats just great Vincente,

I was just trying something like this with koding and ScadaBR Server in Linode. Guess anyone can make a interface in python now!

In the ScadaBR there's also some info and examples, but it's all portuguese =/

Cheers

Hi Vincent thank you so much, your coding is working for me! I'm trying....very slowly....to develop a sort of gateway scadabr<-> modbustcp by using scadabr api and pymodbus module to collect data from scadabr with conventional software over modbustcp!! But maybe the scadabr team will be faster than me and they'll implement an OPC server in ScadaBR.... ;-)

Hello, 

This is indeed in our plans =]

Not only an OPC Server solution, but also Modbus Slave mode and IEC related protocols. These are solutions that we have been planning for some time, but no sponsor partner has put effort ($$) in this direction.

The study of a plugin-able, slave layer is ongoing. But we are directing time to functionalities and projects that maintain our financial health (this is important - did you know that devs and engineers eat too?).

But be sure that it´s is our backlog =]

Best regards

 

@Vincente:
I followed your code example and got suds 0.4 installed under python 2.7 with ScadaBR 0.9.1.
 
When I try to run your example code, I get the following error:
 
######################################
BrowseTagsOptions
(BrowseTagsOptions){
   maxReturn = None
 }
No handlers could be found for logger "suds.client"
Traceback (most recent call last):
  File "...\ScadaBR.py", line 33, in <module>
    BrowseTagsResponse = client.service.browseTags(itemsPath,browseTagsOptions)
  File "build\bdist.win-amd64\egg\suds\client.py", line 542, in __call__
  File "build\bdist.win-amd64\egg\suds\client.py", line 602, in invoke
  File "build\bdist.win-amd64\egg\suds\client.py", line 649, in send
  File "build\bdist.win-amd64\egg\suds\client.py", line 702, in failed
  File "build\bdist.win-amd64\egg\suds\bindings\binding.py", line 265, in get_fault
suds.WebFault: Server raised fault: 'No API authentication on header!'
 
I am not experienced with WSDL - just getting my head around it - so I do not know how to pass the authentication in the header. The example ScadaBR web client written in java creates a ScadaBRAPI service which contains the browseTags method. The service is created with the getAPI function taking the address, user and pass parameters.
 
How would I implement the authentication in Python? I am also planning to implement this for the R statistics language via its SOAP package.
 
Thanks for your time and Best Regards,
Michael Bach

I managed to get authentification working. I reproduced the steps in APIAccessDWR.getAPI(). For reference:

 

client = Client('http://localhost:8080/ScadaBR/services/API?wsdl')
auth = Element('authentication', ns=['api','http://auth.api.scadabr.org.br'])
auth.set("mustUnderstand", False)
user = Element('username').setText('admin')
auth.append(user)
passwd = Element('password').setText('admin')
auth.append(passwd)
client.set_options(soapheaders=auth)

I know that this a very old subject, but I'm having issues with it and I would like if someone here already had my problem.

@moinmoin, thank you for the code, but I'm having the following error here:

[paladini@starstuff ScadaBR-python-scripts]$ python test1.py
######################################
describing the service:

Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913

Service ( API ) tns="http://scadabr.org.br/api/"
   Prefixes (6)
      ns0 = "http://ae.api.scadabr.org.br"
      ns1 = "http://config.api.scadabr.org.br"
      ns2 = "http://constants.api.scadabr.org.br"
      ns3 = "http://da.api.scadabr.org.br"
      ns4 = "http://hda.api.scadabr.org.br"
      ns5 = "http://vo.api.scadabr.org.br"
   Ports (1):
      (API)
         Methods (21):
            ackEvents(xs:int[] eventsId, ns0:AckEventsOptions options, )
            annotateEvent(xs:int eventId, ns5:EventMessage message, )
            browseDataPoints(xs:int dataSourceId, )
            browseDataSources(ns2:DataSourceType type, )
            browseEventsDefinitions(xs:string eventsPath, ns0:BrowseEventsOptions options, )
            browseFlexProjects()
            browseTags(xs:string itemsPath, ns3:BrowseTagsOptions options, )
            configureDataPoint(xs:int dataSourceId, ns2:DataSourceType type, xs:anyType dataPoint, )
            configureDataSource(ns2:DataSourceType type, xs:anyType dataSource, )
            getActiveEvents(xs:string eventsPath, ns0:ActiveEventsOptions options, )
            getDataHistory(xs:string itemName, ns4:GetDataHistoryOptions options, )
            getEventsHistory(xs:string eventsPath, ns0:EventsHistoryOptions options, )
            getFlexBuilderConfig(xs:int projectId, )
            getStatus()
            readData(xs:string[] itemPathList, ns3:ReadDataOptions options, )
            removeDataPoint(xs:int id, )
            removeDataSource(xs:int id, )
            removeFlexProject(xs:int id, )
            setFlexBuilderConfig(ns5:FlexProject project, )
            writeData(ns5:ItemValue[] itemsList, ns3:WriteDataOptions options, )
            writeStringData(ns5:ItemStringValue[] itemsList, ns3:WriteDataOptions options, )
         Types (25):
            ns5:APIError
            ns0:AckEventsOptions
            ns0:ActiveEventsOptions
            ns2:AlarmLevel
            ns0:BrowseEventsOptions
            ns3:BrowseTagsOptions
            ns2:DataSourceType
            ns2:DataType
            ns2:ErrorCode
            ns5:EventDefinition
            ns5:EventMessage
            ns5:EventNotification
            ns2:EventType
            ns0:EventsHistoryOptions
            ns5:FlexProject
            ns4:GetDataHistoryOptions
            ns5:ItemInfo
            ns5:ItemStringValue
            ns5:ItemValue
            ns2:QualityCode
            ns3:ReadDataOptions
            ns5:ReplyBase
            ns2:ServerStateCode
            ns5:ServerStatus
            ns3:WriteDataOptions


######################################
BrowseTagsOptions
(BrowseTagsOptions){
   maxReturn = None
 }
No handlers could be found for logger "suds.client"
Traceback (most recent call last):
  File "test1.py", line 40, in <module>
    BrowseTagsResponse = client.service.browseTags(itemsPath,browseTagsOptions)
  File "build/bdist.linux-x86_64/egg/suds/client.py", line 542, in __call__
  File "build/bdist.linux-x86_64/egg/suds/client.py", line 602, in invoke
  File "build/bdist.linux-x86_64/egg/suds/client.py", line 649, in send
  File "build/bdist.linux-x86_64/egg/suds/client.py", line 702, in failed
  File "build/bdist.linux-x86_64/egg/suds/bindings/binding.py", line 265, in get_fault
suds.WebFault: Server raised fault: 'Invalid Default Password!'

Here you can check my code: https://gist.github.com/paladini/08d2824f5692893157a6. Note that I removed my password for "******", but my password IS correct. Anyone know a workaround / fix for this problem?

Thanks!
Fernando Paladini.