diff --git a/.gitignore b/.gitignore index c66c576..4a28035 100644 --- a/.gitignore +++ b/.gitignore @@ -129,3 +129,6 @@ dmypy.json .pyre/ /.idea +.vscode/settings.json +input +output diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..23dcb79 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,21 @@ +# sendIPの使い方 +## ファイルを実行 +`python3 /home/pi/demo_python/sendIP.py` +*** +## raspberrypi起動時にファイルが自動実行 +### /etc/rc.localにプログラムファイルの実行を追記 +``` +#!/bin/sh -e +... + +# Line Notify +su pi -c "/home/pi/demo_python/sendIP.py" & + +exit 0 +``` +* `su pi`とは、ユーザーpiでコマンドを実行する。 +* `-c`とは、コマンドを実行した後に、元のユーザーに戻ります。 +## raspberry pi3のconfigを設定する +1. `sudo raspi-config`コマンドでconfigを開く。 +2. `System Options > Network at Boot`を選択する。 +3. `sudo reboot`で再起動する。 diff --git a/sendIP.py b/sendIP.py new file mode 100755 index 0000000..207436d --- /dev/null +++ b/sendIP.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +# Copyright (c) 2022. 楊鵬. All Rights Reserved. +import os +import socket + +import requests +from dotenv import load_dotenv + +load_dotenv() +URL = "https://notify-api.line.me/api/notify" + + +def sendIP(ip: str): + headers = {"Authorization": "Bearer " + os.environ["TOKEN"]} + params = {"message": ip} + r = requests.post(URL, headers=headers, params=params) + print(r.text) + + +if __name__ == "__main__": + connect_interface = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + connect_interface.connect(("8.8.8.8", 80)) + ip_addr = connect_interface.getsockname()[0] + connect_interface.close() + message = f"IP Address: {ip_addr}" + sendIP(message) diff --git a/txtToCsv.py b/txtToCsv.py old mode 100644 new mode 100755 index 6b4599f..7bf0549 --- a/txtToCsv.py +++ b/txtToCsv.py @@ -1,11 +1,12 @@ -# Copyright (c) 2021. 楊鵬. All Rights Reserved. +#!/usr/bin/env python3 +# Copyright (c) 2023. 楊鵬. All Rights Reserved. from datetime import datetime import sys import getopt # キーワード -KEYWORD_LIST = ['huawei', 'pixcel4a'] +KEYWORD_LIST = ['huawei', 'pixel4a'] # 処理ファイルに項目名のindex KEY_FROM = 50 KEY_TO = 68 @@ -97,8 +98,9 @@ def create_data_from_file(file_name, keyword, device_code): data_json = {'TIME': []} keys = [] i = 0 + index = KEYWORD_LIST.index(keyword) - if keyword == '': + if index == -1: print('can not found keyword from file name...') sys.exit() @@ -106,12 +108,12 @@ def create_data_from_file(file_name, keyword, device_code): for row in f.readlines(): if row.find(device_code) > -1 and row.find('add device') == -1: key = str.strip(row[KEY_FROM:KEY_TO]) - # pixcel4aの特別のデータを集めないように - if keyword == 'pixcel4a': + # pixel4aの特別のデータを集めないように + if index == 1: if (key == 'ABS_MT_PRESSURE' and row[VALUE_FROM:VALUE_TO] == '00000000') or ( key == 'ABS_MT_TRACKING_ID' and row[VALUE_FROM:VALUE_TO] == 'ffffffff'): continue - data_json['TIME'].append(str.strip(row[TIMESTAMP_FROM:TIMESTAMP_TO])) + # 生データに項目名及び順番を取得 if not (key in keys): keys.append(key) @@ -140,6 +142,7 @@ def create_data_from_file(file_name, keyword, device_code): i += 1 if i == len(keys) and key == EVENT_STOP_FLAG: + data_json['TIME'].append(str.strip(row[TIMESTAMP_FROM:TIMESTAMP_TO])) i = 0 if len(keys) == 0: print('device_code:', device_code, 'is missed...') @@ -166,6 +169,8 @@ def data_convert(data_json, keys, user_id): result_data = [] # 行データ row_data = [] + rows = [] + max_len = 0 for i in range(event_count_all): event_count_in_row += 1 for col in COLUMNS: @@ -175,13 +180,20 @@ def data_convert(data_json, keys, user_id): row_data.append(data_json[col['bind_name']][i]) # イベントがUPの場合 if data_json[EVENT_NAME][i].find(EVENT_VALUE_TO) != -1: - result_data.append(','.join(row_data) + '\n') - # 行データをリセット - row_data = [] + rows.append(row_data) if event_count_in_row > max_event_count_in_row: max_event_count_in_row = event_count_in_row + max_len = len(row_data) # カウントをリセット event_count_in_row = 0 + # 行データをリセット + row_data = [] + + # result_dataの長さを統一する(0を埋め込む) + for row in rows: + if len(row) < max_len: + row.extend(['0'] * (max_len - len(row))) + result_data.append(','.join(row) + '\n') # 出力ヘッダを作成 result_header = []