西门子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。

相关推荐
我叫黑大帅1 天前
如何排查 MySQL 慢查询
后端·sql·面试
techdashen1 天前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
消失的旧时光-19431 天前
Spring Boot 实战(五):接口工程化升级(统一返回 + 异常处理 + 错误码体系 + 异常流转机制)
java·spring boot·后端·解耦
Rust研习社1 天前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
2301_813599551 天前
HTML图片怎么用UnoCSS对齐_UnoCSS原子化CSS图片对齐实战
jvm·数据库·python
m0_377618231 天前
c++怎么在不加载整个大文件的情况下获取其SHA256校验值【进阶】
jvm·数据库·python
LN花开富贵1 天前
【ROS】鱼香ROS2学习笔记二
linux·笔记·python·学习·嵌入式
qq_189807031 天前
CSS如何实现纯CSS树状目录结构_利用-checked与递归思维构建交互节点
jvm·数据库·python
Micr0671 天前
利用Werkzeug-Debug实现本地权限提升
python·web安全·网络安全
夕颜1111 天前
Skill 机器人 vs Hermes Agent:两种「AI 越用越聪明」的路径
后端