#!/usr/bin/env python

import paho.mqtt.client as mqtt
import json
import requests
import subprocess
import pandas as pd
import sys
import time

broker_addr="127.0.0.1"

def from_string(s):
  "Convert dotted IPv4 address to integer."
  return reduce(lambda a,b: a<<8 | b, map(int, s.split(".")))

def to_string(ip):
  "Convert 32-bit integer to dotted IPv4 address."
  return ".".join(map(lambda n: str(ip>>n & 0xFF), [24,16,8,0]))

def on_message(mqttc, userdata, mesg):
          payload = json.loads(mesg.payload)
          filename = "sandbox/" + payload['file name'].strip("[]'")
          print(filename)
          subprocess.call(["./data_convertor.sh", filename, "predict_apply.csv"])
          file1 = pd.read_csv("predict_apply.csv")
          file1.head(10)
          file1.isnull().sum

          # step-1 to replace all empty/null to be empty
          update_file = file1.fillna(" ")
          update_file.isnull().sum()
          update_file.to_csv('update_'+"predict_apply.csv", index = False)

          # step-2 to put 0 as default value
          update_file = file1.fillna(0)
          update_file['tcp.flags'] = update_file['tcp.flags'].apply(lambda  x: int(str(x), 16))
          #update_file['ip.src']=update_file['ip.src'].apply(lambda x: from_string(x))
          #update_file['ip.dst']=update_file['ip.dst'].apply(lambda x: from_string(x))
          update_file.to_csv('update_'+"predict_apply.csv", index = False)
          message='{ "file name" : "update_predict_apply.csv" }'
          print ("*** Raw data cleaned ! Trigger Machine Learning Model to start...")
          time.sleep(1)
          r = requests.post("http://127.0.0.1:5080", data=message)
          print(r.status_code, r.reason)

client = mqtt.Client("monitor_trigger")
client.connect(broker_addr, 1883, 60)
client.on_message = on_message
client.subscribe("mosquitto_sub/file")
client.loop_forever()
