ConMasGateway連携方法 - horiuchi-ng/i-reporter GitHub Wiki
- ConMasGatewayが既にインストール済みかつ、正常に動作することを確認済みであること
※未実施の場合はこちら→ https://cimtops-support.com/i-Reporter/ja/manual-jp/gateway-iot-manual-jp
- ConMasGatewayのマニュアルをある程度理解していること
JSONファイルを作成
↓
Pythonコードで実装(必要な場合のみ)
↓
ConMasDesignerでURLを記載
DBの接続情報を記載したものと、アクションファイルの2つ作成します。
<接続情報のファイル>
gateway/config/datasources/picim.json (任意のファイル名でOK)
{
"type": "DBの種類(Postgresql, mysqlなど)",
"user": "ユーザ名",
"host": "IPアドレスまたはホスト名",
"database": "DB名",
"password": "パスワード",
"port": ポート番号
}
<アクションファイル>
gateway/actions/get_brand.json
例:重合製造指図ビューからポリマーNOを取得する場合
{
"datasource": "picim",
"basequery": "select distinct(nrkm0425) from picim.vo_polodr管理 where nrkm0436 = :brand",
"params": [
{
"name": "brand",
"type": "string"
}
],
"fields": [
{
"no": 1,
"name": "ポリマーNO",
"type": "text",
"item": "nrkm0425"
}
]
}
必要な場合のみスクリプトを書きます。
例えば、
- DBから取得したデータをもとにグラフを出力したい
- DBから取得したデータをもとに複雑な計算処理をして帳票に出力したい、何かを判定したい
など、取得してきたデータをそのまま使わない場合やJSONだけでは要件を満たせないのみスクリプトを書く必要があります。
例:DBから取得したデータをもとにグラフを出力したい場合
<アクションファイル>
gateway/actions/chart1.json
{
"datasource": "script",
"script": "scripts/line.py" // 呼び出すpythonファイルのパス
}
gateway/scripts/line.py
import sys
import include
import matplotlib.pyplot as plt
import uuid
import json
import base64
import os
try:
#引数を処理
#code
# ex)
# http://localhost:3000/api/v1/getvalue/chart1?x1=A&x2=B&x3=C&x4=D&x5=E&y1=1&y2=2&y3=3&y4=4&y5=2
#
# 上記のリクエストの場合、以下のようなJSONで渡される
#
# { data: ← 固定
# [
# 'A', ← 以下、クエリー引数の値が配列で格納されている
# 'B',
# 'C',
# 'D',
# 'E',
# '1',
# '2',
# '3',
# '4',
# '2'
# ]
# }
jsonData = json.loads(sys.stdin.readline())
chartDatas = jsonData['data']
x = []
y = []
x = x + [chartDatas[0]]
x = x + [chartDatas[1]]
x = x + [chartDatas[2]]
x = x + [chartDatas[3]]
x = x + [chartDatas[4]]
y = y + [float(chartDatas[5])]
y = y + [float(chartDatas[6])]
y = y + [float(chartDatas[7])]
y = y + [float(chartDatas[8])]
y = y + [float(chartDatas[9])]
plt.plot(x, y)
#被らないファイル名で画像を一時保存
fileId = uuid.uuid4()
plt.savefig(fileId.hex + '.png')
#画像ファイルをbase64文字列にencode
file = open(fileId.hex + '.png', 'rb').read()
enc_file = base64.b64encode( file ).decode('utf-8')
os.remove(fileId.hex + '.png')
#標準出力JSONで返す(正常)
# 出力フォーマット
#
# {
# "error": "", ← 正常の場合は空文字列/エラー時はメッセージなど文字列を入れる
# "mappings": [ ← ConMasGatewayのアクションファイルの'mappings'形式で返す
# {
# "item": "temp", ← アイテム名(任意)
# "sheet": 1, ← シートNo
# "cluster": 43, ← クラスターID
# "type": "string", ← String固定(数値の場合もString)
# "value": "100" ← 数値の場合も文字列(ダブルクォーテーションで囲む)
# },
# {
# "item": "chart",
# "sheet": 1,
# "cluster": 44,
# "type": "string",
# "value": "[base64]" ← 画像はBase64文字列(画像形式はPNG、JPG、TIFFに対応)
# }
# ・
# ・
# ・
# ・
# ・
# ・
# ]
# }
mappings = {"error": "", "mappings": [{ "item": "chart1" ,"sheet": 1,"cluster": 43,"type": "string","value" : enc_file}]}
print(json.dumps(mappings))
except Exception as e:
#標準出力JSONで返す(エラー)
mappings = {"error": "Pythonでエラー:" + str(e)}
print(json.dumps(mappings))
結果

例:重合作業ノートの場合

①ゲートウェイ連携する場合種別はアクションorマスター選択を使います(マスター選択を使うのがほとんどになると思います)
②マスター選択の場合Gateway連携を選択
③URLを記載
# http~listまでは固定文
# get-order_numberはjsonファイルの名前
http://localhost:3000/api/v1/list/get_order_number
④トークンを記載 ※defaultljsonのtokenの値を書く
⑤対象の項目に表示させたいフィールドを選択

⑥渡したいパラメータを設定

※SQLのwhere句で指定するものを設定する
"basequery": "select distinct(nrkm0425) from picim.vo_polodr管理 where nrkm0436 = :brand",
"params": [
{
"name": "brand",
"type": "string"
}
]
i-reporterのアプリで正常に動くことを確認する