| | 1 | [[PageOutline]] |
| | 2 | |
| | 3 | = botlog,PrintAnyServer用データ登録Web APIの使い方 = |
| | 4 | |
| | 5 | botlog,PrintAnyserver共にWeb APIによるデータ登録が可能なので、それ用のpythonスクリプトを記述する。[[br]] |
| | 6 | このスクリプトを利用したpythonスクリプトを使用することで、外部PCからもログ登録が可能になる。 |
| | 7 | |
| | 8 | '''但し、大量にデータを送るとそのまま登録してしまうので、取り扱いには注意すること。''' |
| | 9 | |
| | 10 | また、外部で使用する場合には、python2.7の実行環境が必要なので、事前にインストールしておく必要がある。[[br]] |
| | 11 | |
| | 12 | |
| | 13 | == botlog == |
| | 14 | |
| | 15 | スクリプト自体は、{{{botlog/tools/epicsRecordMonitor}}}の{{{botlogSender.py}}}にある。[[br]] |
| | 16 | そのままでは、ログ出力がbotlogに依存しているので、多少修正する必要がある。[[br]] |
| | 17 | |
| | 18 | 修正したのが以下のスクリプト。 |
| | 19 | |
| | 20 | {{{ |
| | 21 | # -*- coding: utf-8 -*- |
| | 22 | # |
| | 23 | |
| | 24 | import sys |
| | 25 | import os |
| | 26 | |
| | 27 | import json |
| | 28 | import urllib2 |
| | 29 | import threading |
| | 30 | import socket |
| | 31 | import datetime |
| | 32 | import traceback |
| | 33 | import base64 |
| | 34 | |
| | 35 | #from epicsRecordMonitorLogging import logger, logInit |
| | 36 | |
| | 37 | |
| | 38 | class botlogSender(object): |
| | 39 | # |
| | 40 | # mode は自動登録(2) |
| | 41 | # group(section)はOperation(2) |
| | 42 | # |
| | 43 | def __init__(self, URL, group=2, mode=2): |
| | 44 | self.botlogURL = URL + "editLog" |
| | 45 | #logger.debug(self.botlogURL) |
| | 46 | self.entrySection = group |
| | 47 | self.entryMode = mode |
| | 48 | |
| | 49 | |
| | 50 | |
| | 51 | def __createJSONdata(self, msgData): |
| | 52 | rtn = {} |
| | 53 | rtn['id'] = "" |
| | 54 | rtn['msg'] = base64.urlsafe_b64encode(msgData[0]) |
| | 55 | rtn['setTime'] = msgData[1].strftime("%Y/%m/%d %H:%M:%S.%f") |
| | 56 | rtn['entsct'] = self.entrySection |
| | 57 | rtn['imgs'] = [] |
| | 58 | rtn['mode'] = self.entryMode |
| | 59 | |
| | 60 | return rtn |
| | 61 | |
| | 62 | # |
| | 63 | # msgList = |
| | 64 | # [ |
| | 65 | # [message, datetime], |
| | 66 | # : |
| | 67 | # ] |
| | 68 | # |
| | 69 | def sendMsg(self, msgList): |
| | 70 | for msg in msgList: |
| | 71 | jsonData = self.__createJSONdata(msg) |
| | 72 | #logger.debug(str(jsonData)) |
| | 73 | try: |
| | 74 | req = urllib2.Request(self.botlogURL, headers={'Content-Type':'application/json'}) |
| | 75 | jsonmsg = json.dumps(jsonData) |
| | 76 | f = urllib2.urlopen(req, jsonmsg) |
| | 77 | resp = json.loads(f.read()) |
| | 78 | f.close() |
| | 79 | except Exception as e: |
| | 80 | #logger.debug(self.botlogURL) |
| | 81 | #logger.debug(traceback.format_exc()) |
| | 82 | print traceback.format_exc() |
| | 83 | |
| | 84 | |
| | 85 | # テスト用main |
| | 86 | if __name__ == '__main__': |
| | 87 | #logger = logInit(None) |
| | 88 | |
| | 89 | msgList = [] |
| | 90 | msg = ["改行テスト\n改行テスト2\n改行テスト3", datetime.datetime.now()] |
| | 91 | msgList.append(msg) |
| | 92 | |
| | 93 | URL="http://localhost/botlog/botlog/" |
| | 94 | bs = botlogSender(URL) |
| | 95 | bs.sendMsg(msgList) |
| | 96 | |
| | 97 | }}} |
| | 98 | |
| | 99 | |
| | 100 | == !PrintAnyServer == |
| | 101 | |
| | 102 | PrintAnyServer用画像登録スクリプトは以下の様になる。[[br]] |
| | 103 | こちらは、一旦画像ファイルを作成してから登録スクリプトを実行する。 |
| | 104 | |
| | 105 | |
| | 106 | {{{ |
| | 107 | # -*- coding: utf-8 -*- |
| | 108 | import os |
| | 109 | import datetime |
| | 110 | import base64 |
| | 111 | import json |
| | 112 | import urllib2 |
| | 113 | import traceback |
| | 114 | |
| | 115 | class saveImage(object): |
| | 116 | def __init__(self, baseURL='http://localhost'): |
| | 117 | self.URL = baseURL + '/pas/api/addImage' |
| | 118 | |
| | 119 | def save(self, caption, imgFile): |
| | 120 | rtn = False |
| | 121 | req = urllib2.Request(self.URL, headers={'Content-Type':'application/json'}) |
| | 122 | try: |
| | 123 | img = base64.b64encode(open(imgFile, 'rt').read()) |
| | 124 | cap = base64.b64encode(caption) |
| | 125 | tm = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S') |
| | 126 | jsonData = {'from':[], |
| | 127 | 'pasImg':{'img': img, 'caption': cap, 'type':'png', 'time':tm, 'print':{}}} |
| | 128 | |
| | 129 | printData = jsonData['pasImg']['print'] |
| | 130 | printData['exec'] = 0 |
| | 131 | |
| | 132 | f = urllib2.urlopen(req, json.dumps(jsonData), timeout=5) |
| | 133 | resp = json.loads(f.read()) |
| | 134 | rtn = True |
| | 135 | except Exception as e: |
| | 136 | |
| | 137 | print traceback.format_exc() |
| | 138 | return rtn |
| | 139 | |
| | 140 | |
| | 141 | # テスト用main |
| | 142 | if __name__ == '__main__': |
| | 143 | s = saveImage() |
| | 144 | caption = 'TEST' |
| | 145 | fileName = 'test.png' |
| | 146 | |
| | 147 | if s.save(caption, fileName) == True: |
| | 148 | print "Copy : %s" % fileName |
| | 149 | else: |
| | 150 | print "Copy Fail: %s" % fileName |
| | 151 | }}} |