西门子WinCC Unified PC的GraphQL使用手册

TIA V20版本:添加用户

添加角色,并充分授权,尤其是GraphQL的读写权限。

通过SIMATIC Runtime Manager启动wincc unifi工程。

打开浏览器,访问本地的https://localhost/graphql/,运行正常如图:

连接外网,打开https://studio.apollographql.com/sandbox/explorer工具,在线调试GraphQL接口:

打开查询指令窗口

第一步,请求login获取token:

bash 复制代码
mutation{
login(username:"wincc",password:"Wincc12345"){
token
user{
fullName
id
}
error{
code
description
}
}
}

获取token,后面需要添加到headers里:Authorization Bearer e68814f546ed1360cb533ed7ecf77ae0。

如图所示:

第二步:查询获取变量值Tag_2是变量名称。

bash 复制代码
query Query{
tagValues(names:["Tag_2"]){
name
value{
value
timestamp
quality{
quality
subStatus
}
}
error{
code
description
}
}
}

第三步,写入一个值0:

bash 复制代码
mutation exampleTagValueWrite {
writeTagValues(input:[
{
name:"Tag_2",
value: "0"
}
], quality:{quality: GOOD_NON_CASCADE}){
name
error{
code
description
}
}
}

第四步,订阅一个变量:

bash 复制代码
subscription subscription{
tagValues(names:["Tag_2"]){
value{
value
timestamp
quality{
quality
limit
subStatus
}
}
error{
description
}
}
}
}

GraphQL通过python客户端读取和写入变量:

python 复制代码
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
 
url = 'http://localhost:4000/graphql'
USERNAME = "wincc"
PASSWORD = "Wincc12345"
TAG_NAME = "Tag_2" 
transport = RequestsHTTPTransport(url=url,verify=False, retries=3)
client = Client(transport=transport, fetch_schema_from_transport=True)
 
query = '''
mutation{
login(username:"%s",password:"%s"){
token
user{
fullName
id
}
error{
code
description
}
}
}
'''
 
variable_values = {'Authorization': 'Bearer 2d263aa90155e66bb24f7a4604153ee7'}  # 如果有变量的话
result = client.execute(gql(query % (USERNAME, PASSWORD)), variable_values=variable_values)
token = result['login']['token']
headers={'Authorization': 'Bearer ' + token}
transport = RequestsHTTPTransport(url=url,headers=headers,verify=False, retries=3)
client = Client(transport=transport, fetch_schema_from_transport=True)

query = '''
mutation exampleTagValueWrite {
writeTagValues(input:[
{
name:"%s",
value: "0"
}
], quality:{quality: GOOD_NON_CASCADE}){
name
error{
code
description
}
}
}
'''
result = client.execute(gql(query))
print(result)
query = '''
query Query{
tagValues(names:["%s"]){
name
value{
value
timestamp
quality{
quality
subStatus
}
}
error{
code
description
}
}
}
'''
result = client.execute(gql(query % (TAG_NAME)))
print(result)

GraphQL通过python客户端订阅变量:

python 复制代码
import asyncio
from gql import Client, gql
from gql.transport.websockets import WebsocketsTransport
from websockets.exceptions import ConnectionClosed

GRAPHQL_WS_URL = "wss://localhost:4000/graphql"
TAG_NAME = "Tag_2" 
AUTH_TOKEN = "1a64a7289995f7da6a9baa0cd5eb93db"

headers = {'Authorization': 'Bearer ' + AUTH_TOKEN}

subscription = gql("""
  subscription {
    tagValues(names: ["%s"]) {
      name
      value {
        value
      }
      notificationReason
    }
  }
""" % TAG_NAME)

async def main():
    transport = WebsocketsTransport(GRAPHQL_WS_URL, init_payload=headers)
    
    counter = 0

    try:

      # Using `async with` on the client will start a connection on the transport
      # and provide a `session` variable to execute queries on this connection
      async with Client(
          transport=transport,
          fetch_schema_from_transport=True,
      ) as session:
          async for result in session.subscribe(subscription):
              print(result)
              
              counter += 1
              if counter == 1000:
                print ("Closing connection from the client")          
                break
                  
    except ConnectionClosed:
      print ("Connection closed by the server")

asyncio.run(main())

注意:python依赖库安装pip install gql。

相关推荐
程序员的世界你不懂3 小时前
Appium+python自动化(八)- 认识Appium- 下章
python·appium·自动化
恸流失4 小时前
DJango项目
后端·python·django
Julyyyyyyyyyyy5 小时前
【软件测试】web自动化:Pycharm+Selenium+Firefox(一)
python·selenium·pycharm·自动化
蓝婷儿6 小时前
6个月Python学习计划 Day 15 - 函数式编程、高阶函数、生成器/迭代器
开发语言·python·学习
love530love6 小时前
【笔记】在 MSYS2(MINGW64)中正确安装 Rust
运维·开发语言·人工智能·windows·笔记·python·rust
Mr Aokey6 小时前
Spring MVC参数绑定终极手册:单&多参/对象/集合/JSON/文件上传精讲
java·后端·spring
水银嘻嘻6 小时前
05 APP 自动化- Appium 单点触控& 多点触控
python·appium·自动化
狐凄7 小时前
Python实例题:Python计算二元二次方程组
开发语言·python
地藏Kelvin7 小时前
Spring Ai 从Demo到搭建套壳项目(二)实现deepseek+MCP client让高德生成昆明游玩4天攻略
人工智能·spring boot·后端
烛阴8 小时前
Python枚举类Enum超详细入门与进阶全攻略
前端·python