-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecv_data_write_db.py
More file actions
73 lines (62 loc) · 2.36 KB
/
recv_data_write_db.py
File metadata and controls
73 lines (62 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#Receive data from client and Write receive data to InfluxDB
import csv
import datetime
import json
import os
import socket
import sys
import time
from influxdb import InfluxDBClient
# load recv_data_write_db.json
conf_dir_path = os.path.normpath('%s/../conf_dir' %__file__)
json_conf_path = os.path.join(conf_dir_path, "config.json")
print(json_conf_path)
f = open(json_conf_path, "r", encoding="UTF-8")
conf_file = json.load(f)
# receive info
recv_host = conf_file["socket"]["host"]
recv_port = conf_file["socket"]["port"]
buffer_size = conf_file["socket"]["size"]
#InfluxDB info
db_client = InfluxDBClient(
host = conf_file["InfluxDB"]["host"],
port = conf_file["InfluxDB"]["port"],
username = conf_file["InfluxDB"]["username"],
password = conf_file["InfluxDB"]["password"],
database = conf_file["InfluxDB"]["database"]
)
def write_db():
""" def function for receive data from client and write data to InfluxDB """
try:
recv_srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
recv_srv.bind((recv_host,recv_port))
recv_srv.listen()
print("Wating for connect", datetime.datetime.now())
client, addr = recv_srv.accept()
start_datetime = datetime.datetime.now()
print("Connected", start_datetime)
start_time = time.time()
amass_data = b""
while True:
data = client.recv(buffer_size)
amass_data += data
if not data:
# decode unicode-escape & Line feed / split
decode_data = amass_data.decode("unicode-escape").splitlines()
# Write receive data to InfluxDB
for i in decode_data:
write_array = [eval(i)] # str→dict
# db_client.write_points(write_array)
fin_time = time.time() - start_time
fin_datetime = datetime.datetime.now()
print(client, "\n", datetime.datetime.now(), "-----NOT DATA-----")
break
except:
print("Error")
print("End Time : ", fin_time , "[sec]")
with open("./socket_comm/fin_time.csv", "a", newline="") as f:
writer = csv.writer(f, lineterminator="\n")
writer.writerow([start_datetime, fin_datetime, fin_time])
client.close()
sys.exit()
write_db()