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

相关推荐
nice_lcj5209 小时前
【从零理解计算机网络 · 02】分层模型:别背 OSI 七层,看数据怎么被“打包“和“拆包“
java·后端·tcp/ip·计算机网络·面试·osi·分层模型
SimonKing10 小时前
给Deepseek Harness装上插件市场,从此插件无忧
java·后端·程序员
kyle~11 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享11 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
利威尔xu11 小时前
Go Mutex 饥饿模式:正常模式等待者饿死,1ms 阈值自动切换崩溃
开发语言·后端·面试·golang·并发编程
一条Go11 小时前
边缘 Agent:为什么是它连出来,而不是平台连进去
后端·架构·github
huangfuyk12 小时前
前端融入/转后端避坑指南:AI 时代的安全边界、数据防线与实战细节
后端·ai编程
梯度下降者12 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
掘金者阿豪12 小时前
GitLab 服务器疑似遭入侵:从 502 故障到 Root 权限异常的完整排查复盘
后端
MSTcheng.12 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手