import json
from time import sleep
from kafka import KafkaProducer
import os
import pandas as pd
import ssl

context = ssl.create_default_context()
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations("ca-cert")

# producer = KafkaProducer(bootstrap_servers=['alikafka-pre-cn-2r42ktle800a-1.alikafka.aliyuncs.com:9093',
#                        'alikafka-pre-cn-2r42ktle800a-2.alikafka.aliyuncs.com:9093',
#                        'alikafka-pre-cn-2r42ktle800a-3.alikafka.aliyuncs.com:9093'],
#                         sasl_mechanism="PLAIN",
#                         ssl_context=context,
#                         security_protocol='SASL_SSL',
#                         api_version = (2,2),
#                         retries=5,
#                         sasl_plain_username='alikafka_pre-cn-2r42ktle800a',
#                         sasl_plain_password='y0qn8KOIasc007M5xXFLOhB7MNrnIt29')

producer = KafkaProducer(bootstrap_servers=['alikafka-pre-cn-8ed2kw57901x-1.alikafka.aliyuncs.com:9093','alikafka-pre-cn-8ed2kw57901x-2.alikafka.aliyuncs.com:9093','alikafka-pre-cn-8ed2kw57901x-3.alikafka.aliyuncs.com:9093'],
                        sasl_mechanism="PLAIN",
                        ssl_context=context,
                        security_protocol='SASL_SSL',
                        api_version = (2,2),
                        retries=5,
                        sasl_plain_username='alikafka_pre-cn-8ed2kw57901x',
                        sasl_plain_password='hlQsApgUUdUxcWEr1uQoM9BeuF8t8vMF')

#producer = KafkaProducer(bootstrap_servers="192.168.0.41:9092")
topic = "s_kafka_tbox_analysis_hh_delta_ext_topic"
path = "/Users/liuzezhong/new/hzdata/hz"
sleepInterval = 0.1

headers = None


for line in open(os.path.join(path,"signal_header.csv")):
    headers = line[:-1].split(",")

headers = [h[1:-1] for h in headers]

# print(headers,len(headers))
df = pd.read_csv('s_kafka_tbox_analysis_hh_delta_ext_topic.csv')

    for index, row in readResult.iterrows():
        print("-----", dataPath, index, len(row))
        print("*********", len(row))
        dataPart = {}
        for j in range(len(row)):
            if pd.isnull(row[j]):
                continue
            dataPart[headers[j]] = row[j]
        print(dataPart)
        dataPartStr = json.dumps(dataPart)
        producer.send(topic, dataPartStr.encode("utf-8"))
        sleep(sleepInterval)

        # for line in open(dataPath):
        #     row = line[:-1].split(",")
        #     dataPart={}
        #     for j in range(len(headers)):
        #         rowj = row[j]
        #         if rowj.startswith("\"") and rowj.endswith("\""):
        #             rowj = rowj[1:-1] 
        #         dataPart[headers[j]]=rowj
        #     print(dataPart, len(dataPart))
            # dataPartStr = json.dumps(dataPart)
            # print(dataPartStr)
            # producer.send(topic, dataPartStr.encode("utf-8"))
            # sleep(sleepInterval)

# path = "./s_kafka_tbox_analysis_hh_delta_ext_202202071438.csv"

# headers = None
# data = []

# splitNo = 0


# for line in open(path):
#     headers = line
#     break

# with open("./hz/signal_header.csv","w") as f:
#     f.write(headers)

# i=0
# index = 0
# data = []
# part = 0
# for line in open(path):
#     if i == 0:
#         i+=1
#         continue
#     else:
#         data.append(line)
#         i+=1
#         index+=1
#     if index >= 10000:
#         print("writing part",part)
#         with open("./hz/signal_data_"+str(part)+".csv","w") as f:
#             for row in data:
#                 f.write(row)
#         part+=1
#         data=[]
#         index=0
#     print("line",i,"done")
