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

相关推荐
龙潜月七16 分钟前
Selenium 自动化测试中跳过机器人验证的完整指南:能用
python·selenium·机器人
ChinaRainbowSea42 分钟前
9-2 MySQL 分析查询语句:EXPLAIN(详细说明)
java·数据库·后端·sql·mysql
风象南1 小时前
SpringBoot基于Java Agent的无侵入式监控实现
java·spring boot·后端
崎岖Qiu1 小时前
【Spring篇08】:理解自动装配,从spring.factories到.imports剖析
java·spring boot·后端·spring·面试·java-ee
香饽饽~、2 小时前
【第十一篇】SpringBoot缓存技术
java·开发语言·spring boot·后端·缓存·intellij-idea
蓝婷儿2 小时前
Python 机器学习核心入门与实战进阶 Day 1 - 分类 vs 回归
python·机器学习·分类
Devil枫3 小时前
Kotlin扩展函数与属性
开发语言·python·kotlin
程序员爱钓鱼3 小时前
Go语言实战指南 —— Go中的反射机制:reflect 包使用
后端·google·go
.30-06Springfield3 小时前
利用人名语言分类案例演示RNN、LSTM和GRU的区别(基于PyTorch)
人工智能·pytorch·python·rnn·分类·gru·lstm
ℳ₯㎕ddzོꦿ࿐3 小时前
Spring Boot 集成 MinIO 实现分布式文件存储与管理
spring boot·分布式·后端