예제 #1
0
파일: utils.py 프로젝트: guanshanyue/xdata
 def __enter__(self):
     self.con = MongoClient.connect(host=self.ip,
                                    port=self.port,
                                    username=self.user,
                                    password=self.password,
                                    authSource=self.db,
                                    authMechanism='SCRAM-SHA-1')
     return self
예제 #2
0
    t=t[:-1]
    t=t.split(",")
    #print(t)
    time_t=datetime.datetime(int(t[0]), int(t[1]), int(t[2]),int(t[3]), int(t[4]),int(t[5]), int(t[6])) #, int(t[7])
    print(time_t)
    timestamp_utc = calendar.timegm(time_t.timetuple())
    #print(timestamp_utc)
    #print('Processing sample : ' + str(sample['value']))
    body= {'device' : str(sample['device']),'sample_date' : time_t.strftime("%Y-%m-%d"),'temp' : float(sample['temp']),
           'humi' : float(sample['humi']), 'gyro_x' : float(sample['gyro_x']),'gyro_y' : float(sample['gyro_x']),'time' : repr(timestamp_utc) }
    print(body)

    #in Datenbank schreiben, Collection called 'sensor2'
    result = db.bathroom_update3.insert_one(body)

    # Step 4: Print to the console the ObjectID of the new document
    print('Datensatz {0} geschrieben.'.format(result.inserted_id))

def on_log(mqttc, obj, level, string):
    print(string)
    print('Connecting to MQTT broker')
try:
    client = mqtt.Client()
    client.on_message = on_message
    client.on_connect = on_connect
    client.on_log = on_log
    client.connect(mqtt_server, mqtt_port, 60)
    client.loop_forever()
except Exception as e:
    print("oh nooo", e)
예제 #3
0
    f"mongodb+srv://{environ['MONGO_USER']}:{environ['MONGO_PASS']}@{environ['MONGO_URL']}"
)
db = client['plant_monitor']
monitor_data = db['monitor_data']


def on_connect(client, userdata, flags, rc):
    print(f"Connected with result code {rc}")

    client.subscribe("plant_monitor")


# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(f"Received message for {msg.topic} ")
    if msg.topic == 'plant_monitor':
        print(f'Received data from plant sensors, syncing with DB...')
        monitor_data.insert_many(json.loads(msg.payload))
        print('Done. Awaiting next publish')
    else:
        # Should never run
        print('Received bad topic, please check your code')


client = mqtt_client.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("localhost")

client.loop_forever()
예제 #4
0
            "co2":
            jmsg['co2'],
            "measured":
            datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "station":
            jmsg['station']
        })
    except:
        print('TODO Error Handling')


# define connect and message method for handling mqtt messages
client = mqtt.Client(transport="websockets")
client.on_connect = on_connect
client.on_message = on_message
client.connect(socket.gethostbyname(socket.gethostname()), 1884, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.

#client.loop_forever()


def is_json(myjson):
    try:
        json_object = json.loads(myjson)
    except ValueError, e:
        return False
    return True
예제 #5
0
## VAR
###

tabla="informe"
basededatos="informespre"

###
## Codigo
###

# Me conecto al mongo
db = MongoClient().betacompany

# Me conecto al MySQL
dbmysql = MySQ
db.connect(host="localhost", user="******", passwd="sm2madmin", db=basededatos)
cur = dbmysql.cursor()

# Limpio el MySQL
query = """truncate table %s""" % tabla
cur.execute(query)

def doQuery(query):
    # Para el mysql
    try:
        cur.execute(query)
        dbmysql.commit()
    except:
        dbmysql.rollback()
        print "rollback"
예제 #6
0
    print(message)
    try:
        iot_messages = db.iot_messages
        print(iot_messages.insert_one(message).inserted_id)
    except:
        print "duplicate:"


if __name__ == "__main__":

    print "chargement de la configuration:"
    config = ConfigParser.RawConfigParser()
    config.read('conf/app.conf')

    mongoHost = config.get('mongo', 'host')
    mongoDB = config.get('mongo', 'db')

    client = MongoClient(mongoHost)
    db = client[mongoDB]

    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect("mosquitto", 1883, 60)

    # Blocking call that processes network traffic, dispatches callbacks and
    # handles reconnecting.
    # Other loop*() functions are available that give a threaded interface and a
    # manual interface.
    client.loop_forever()
예제 #7
0
		beaconId = rsSplit[0]
		rssi = rsSplit[1]
		rssiVal = int(rssi,16)
		rssiVal = abs(twos_comp(rssiVal,8))
		if(rssiVal==0):
			return
        if rssiVal>100:
            return
		currentObject['relayNo'] = relayNumber
		currentObject['beaconId'] = beaconId
		currentObject['rssiVal'] = rssiVal
		currentObject['timeStamp'] = str(datetime.now())
		result = col.insert(currentObject)
		#print("Inserted : "+str(beaconId))


def on_message(client, userdata, message):
		currentDict = {}
		raw = str(message.payload.decode("utf-8"))
		d = "}"
		s =  [e+d for e in raw.split(d) if e]
		for messages in s:
			enterMongo(messages)

print('Connecting to MQTT..')
broker_address="188.166.247.94" 
client = mqtt.Client("FingerprintingTask") #create new instance
client.connect(broker_address) #connect to broker
client.subscribe("sigma")
client.on_message=on_message
client.loop_forever()
예제 #8
0
    fft_result = [(r + i * 1j) for r, i in zip(
        updated_json['complex']['real'], updated_json['complex']['imag'])]
    S = np.absolute(fft_result)
    S = 20 * np.log10(S / np.max(S))

    if np.mean(S) < -100:
        # From previous testing. Normal fft slices have an average around -50ish
        # Failed sensor data usually gets around -120ish
        # I just ignore all failed data points
        print('Faulty data point, passing')

    else:
        if updated_json['time'] - previous_times[
                updated_json['loc']] > 600:  # This works out to 10 minutes
            previous_times[updated_json['loc']] = updated_json['time']
            result_coarse = audio_coarse.insert_one(updated_json).inserted_id
            print(f'Coarse result: {result_coarse} from {updated_json["loc"]}')

        result = audio_fine.insert_one(updated_json).inserted_id
        print(f'Fine result: {result} from {updated_json["loc"]}')


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

# I do some testing with delays of around 10 minutes so I want some leeway
client.connect('localhost', keepalive=700)
client.loop_forever()
예제 #9
0
        data = json.loads(msg.payload.decode('utf-8'))
        data["date"] = datetime.datetime.utcnow()
        # print("UPS_Monitor ", data)
        _id = UPS_Monitor.insert_one(data).inserted_id
        # print(_id)
    if topic == "air-conditioner-vent":
        data = json.loads(msg.payload.decode('utf-8'))
        data["date"] = datetime.datetime.utcnow()
        # print("air-conditioner-vent ", data)
        _id = air_conditioner_vent.insert_one(data).inserted_id
        # print(_id)
    if topic == "cabinet_A":
        data = json.loads(msg.payload.decode('utf-8'))
        data["date"] = datetime.datetime.utcnow()
        # print("cabinet_A ", data)
        _id = cabinet_A.insert_one(data).inserted_id
        # print(_id)
    if topic == "cabinet_B":
        data = json.loads(msg.payload.decode('utf-8'))
        data["date"] = datetime.datetime.utcnow()
        # print("cabinet_B ", data)
        _id = cabinet_B.insert_one(data).inserted_id
        # print(_id)


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(CLIENT_MQTT_SERVER, CLIENT_MQTT_PORT)
client.loop_forever()
예제 #10
0
print(post_id)
"""
cliente = {
    "nombre": "",
    "direccion": {
        "calle": "",
        "altura": 0,
        "pisoDepto": "",
        "cp": 0,
        "localidad": "",
        "zona": 0
    },
    "telefono":"",
    "mail":"",
    "facebook":"",
    "instagram":""
}
"""
"""
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://acozzi:<password>@farmersmarket-tu1em.gcp.mongodb.net/<dbname>?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });



client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});
"""
        })

    return data


# Actual "business logic" handled here. Get all bluetooth data from stored macs
def get_miflora_data():
    print('Getting data...')
    #    global bluetooth_thread
    global monitors

    mon = monitors.find({})
    data = get_data(mon)

    return data


def on_connect(client, userdata, flags, rc):
    print(f"Connected with result code {rc}")


# Establish mqtt client
client = mqtt_client.Client()
client.on_connect = on_connect
client.connect("10.0.1.42")

# Publish bluetooth data to server
data_to_publish = get_miflora_data()
print(data_to_publish)
client.publish('plant_monitor', json.dumps(data_to_publish, default=str))
예제 #12
0
import paho.mqtt.client as paho
from pymongo import MongoClient

print("connecting to mongo")
client = MongoClient('mongodb://mongo:27017')
print("connected to mongo")
db = client.rpi
collection = db.cpu
print("inserting one into mongo")
post_id = collection.insert_one({'meh':'meh'}).inserted_id
print("inserted one")
post_id = collection.insert_one({'meh':'meh'}).inserted_id
print("inserted one")

def on_message(client, userdata, msg):
	print(msg.topic+" "+str(msg.payload))

	cpu = {"CPU%": (msg.payload)}
	post_id = collection.insert_one(cpu).inserted_id

client = paho.Client()
client.on_message = on_message
client.connect("34.210.186.4", 1883)
client.subscribe("rpi/cpu", qos=1)
client.loop_forever() 
예제 #13
0
# clear mongodb records
collection.delete_many({})

hosts = ["localhost", "172.28.128.23", "172.28.128.24"]
clients = []
sftps = []

for cursor, host in enumerate(hosts):
    if host == "localhost":
        clients.append(None)
        sftps.append(None)
        continue
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hosts[cursor], username="******",
                   password="******")  # to remote hosts
    sftp = client.open_sftp()
    clients.append(client)
    sftps.append(sftp)

print("%s", clients)
print("%s", sftps)

start_timestamp = time.time()

# cp/scp file for fluentd parsing

dest_dir = "/home/vagrant/source/"
original_dir = "/home/vagrant/source_split/"
files = os.listdir(original_dir)
for cursor, filename in enumerate(files):
예제 #14
0
def on_message(client, data, message):
    global race_id
    global lap_count
    messageJson = json.loads(message.payload)
    allowed.append(ObjectId(messageJson["_id"]))
    key_dict = messageJson["_id"]
    value_dict = str(messageJson["date"])
    race_id = messageJson["raceId"]
    lap_count = messageJson["currentLap"]
    lap_dict[key_dict] = lap_count
    time_dict[key_dict] = datetime.strptime(value_dict, '%Y-%m-%d %H:%M:%S.%f')


client = mqtt.Client()
client.connect('192.168.137.177', 1883, 60)
client.on_message = on_message
client.loop_start()
client.subscribe('secondSector', qos=0)

# ----------------- Publishing car's time on the current sector -----------------#
PortRF = serial.Serial('/dev/ttyAMA0', 9600)
while True:
    ID = ""
    read_byte = PortRF.read()
    if read_byte == "\x02":
        for Counter in range(0, 12):
            read_byte = PortRF.read()
            ID = ID + str(read_byte)
        #----------------- if it's not a last lap -----------------#
        if ObjectId(ID) in allowed and is_last == False and ObjectId(
예제 #15
0
from pymongo import MongoClient

broker = "34.210.186.4"
port = 1883

client = MongoClient()
dbClient = MongoClient('localhost', 27017)
db = client.whaleDB

collection = db.whaleCPU


def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe("rpi/cpu")


def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))

    rpi_cpu = {"CPU": (msg.payload)}
    post_id = collection.insert_one(rpi_cpu).inserted_id


client = paho.Client("testclient", clean_session=False, protocol=paho.MQTTv31)
client.on_message = on_message
client.connect("localhost", 1883)
client.subscribe("rpi/cpu", qos=1)

client.loop_forever()
예제 #16
0
파일: hi_sub.py 프로젝트: rubiruchi/ece1508
from pymongo import MongoClient

BROKER_ADDRESS = '142.150.208.252'
BROKER_PORT = 1883
STUDENT_NUMBER = '1005167454'
cities = ['Montreal', 'Sydney', 'Toronto', 'New york',
        'Shanghai', 'Tehran', 'London', 'Seoul',
        'Jakarta', 'Tokyo']

client = MongoClient('localhost', 27017)
db = client.create
collection = db.hi

def on_connect(client, userdata, flags, rc):
    for city in cities:
        topic = 'ece1508/%s/%s/hi' % (STUDENT_NUMBER, city.lower())
        client.subscribe(topic)

def on_message(client, userdata, message):
    data = json.loads(str(message.payload, encoding='utf-8'))
    collection.insert_one(data)
    print('Saved city %s at %s' % (data['data']['city'], str(datetime.datetime.now())))


if __name__ == '__main__':
    client = mqtt.Client()
    client.connect(BROKER_ADDRESS, BROKER_PORT)
    client.on_connect = on_connect
    client.on_message = on_message
    client.loop_forever()
예제 #17
0
def on_message(client, data, message):
    global race_id
    global lap_count
    messageJson = json.loads(message.payload)
    allowed.append(ObjectId(messageJson["_id"]))
    key_dict = messageJson["_id"]
    value_dict = str(messageJson["date"])
    race_id = messageJson["raceId"]
    lap_count = messageJson["currentLap"]
    print('lap ', lap_count)
    lap_dict[key_dict] = lap_count
    time_dict[key_dict] = datetime.strptime(value_dict, '%Y-%m-%d %H:%M:%S.%f')


client = mqtt.Client()
client.connect('192.168.137.245', 1883, 60)
client.on_message = on_message
client.loop_start()
client.subscribe('firstSector', qos=0)

#----------------- Publishing car's time on the current sector -----------------#
PortRF = serial.Serial('/dev/ttyAMA0', 9600)
while True:
    ID = ""
    read_byte = PortRF.read()
    if read_byte == "\x02":
        for Counter in range(0, 12):
            read_byte = PortRF.read()
            ID = ID + str(read_byte)
        current_time = datetime.now()
        lapTimeJson = {
예제 #18
0
from flask import Flask, request
from datetime import datetime
from pymongo import MongoClient
import json


api = Flask(__name__)

client = MongoClient()
client.connect('localhost', 27017)

db = client.solar_data
raw_data_table = db.raw_data
processed_data_table = db.processed_data


@api.route('/raw', methods=['GET'])
def raw_data():
    latitude = request.args['latitude']
    longitude = request.args['longitude']
    time_start = datetime.fromtimestamp(request.args['start'])
    time_end = datetime.fromtimestamp(request.args['end'])
    datapoints = raw_data_table.find({'latitude' : latitude , 'longitude' : longitude,
                        'datetime': {'$gte' : time_start, '$lt' : time_end}})
    return json.dumps(datapoints)


@api.route('/processed', methods=['GET'])
def processed_data():
    latitude = request.args['latitude']
    longitude = request.args['longitude']
예제 #19
0
            },
            'shell': {
                'cpu': shell_perf['cpu'],
                'mem': shell_perf['mem']
            },
            'dockerd': {
                'cpu': dockerd_perf['cpu'],
                'mem': dockerd_perf['mem']
            },
            'container': {
                'cpu': container_perf['cpu'],
                'mem': container_perf['mem']
            },
            'program': {
                'cpu': program_perf['cpu'],
                'mem': program_perf['mem']
            }
        }
    }

    result = collection.insert_one(updated_json).inserted_id
    print(result)


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect('localhost')
client.loop_forever()
#	print("Connection failed")

Connected = False  #global variable for the state of the connection

broker_address = "10.129.0.15"
#broker_address= "10.0.0.21"

port = 1883
#user = "******"
#password = "******"

client = mqttClient.Client()  #create new instance
#client.username_pw_set(user, password=password)    #set username and password
client.on_connect = on_connect  #attach function to callback
client.connect(broker_address, port=port)  #connect to broker

client.loop_start()  #start the loop

while Connected != True:  #Wait for connection
    time.sleep(0.1)

try:
    cnt = 0
    while (1):

        #if GPIO.input(29):
        #	client.publish("candado/slot0/set","OFF")

        detect_rfid = rfid_reader()
        if detect_rfid != None and cnt == 0:
예제 #21
0
def on_message(client, userdata, msg):
    topic_parts = msg.topic.split('/')
    if( (len(topic_parts) == 3) and (topic_parts[0] == "Nodes") ):
        nodeid = topic_parts[1]
        sensor = topic_parts[2]
        post = {
            "node": int(nodeid),
            sensor:float(msg.payload),
            "ts":datetime.datetime.utcnow()
        }
        #TODO update the webscoket with this post
        print(msg.topic+" "+str(msg.payload)+" to be broadcasted ")
        

config = json.load(open('config.json'))

# -------------------- Mongo Client -------------------- 
client = MongoClient(config["mongodb"]["uri"])
db = client[config["mongodb"]["db"]]
sensors = db.sensors

# -------------------- Mqtt Client -------------------- 
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect( config["mqtt"]["host"], config["mqtt"]["port"], 
                config["mqtt"]["keepalive"])
client.loop_forever()

예제 #22
0
import logging
import os
import uuid

import paho.mqtt.client as mqtt
from dotenv import load_dotenv
from pymongo import MongoClient

from collector.mqtt import on_connect, on_message
from utils.scheduler import schedule_jobs
from utils.validators import get_validators

load_dotenv()
logging.basicConfig(level=logging.INFO)

logging.info("initializing mongo client")
client = MongoClient(os.getenv('MONGO_CONN_STR'))
mongo_db = client.get_database()

logging.info("initializing event inserter job")
validators_by_schema_name = get_validators()
schedule_jobs(mongo_db=mongo_db,
              validators_by_schema_name=validators_by_schema_name)

logging.info("initializing mqtt client")
client = mqtt.Client(client_id=str(uuid.uuid4()))
client.on_connect = on_connect
client.on_message = on_message
client.connect(os.getenv('MQTT_BROKER_HOST'),
               int(os.getenv('MQTT_BROKER_PORT')))
client.loop_forever()
# clear mongodb records
collection.delete_many({})

hosts = ["localhost", "172.28.128.23", "172.28.128.24"]
clients = []
sftps = []

for cursor, host in enumerate(hosts):
    if host == "localhost":
        clients.append(None)
        sftps.append(None)
        continue
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hosts[cursor], username="******", password="******")  # to remote hosts
    sftp = client.open_sftp()
    clients.append(client)
    sftps.append(sftp)

print("%s", clients)
print("%s", sftps)

start_timestamp = time.time()

# cp/scp file for fluentd parsing

dest_dir = "/home/vagrant/source/"
original_dir = "/home/vagrant/source_split/"
files = os.listdir(original_dir)
for cursor, filename in enumerate(files):
예제 #24
0
bedroom2 = Room([])


def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))


def on_message(client, userdata, msg):
    print(msg.payload + "received from " + str(msg.topic) + "with QoS " +
          str(msg.qos))


client = mqtt.Client()
client.on_connect = on_connect
livingarray = client.message_callback_add('/pi/livingroom',
                                          livingroom.on_message)
client.message_callback_add('/pi/bedroom1', bedroom1.on_message)
client.message_callback_add('/pi/bedroom2', bedroom2.on_message)
client.on_message = on_message
client.connect(broker, 1883, 60)
client.subscribe('/pi/#')

client.loop_start()
print('Script is running, press Ctrl-C to quit...')

while True:
    button_first = int(input())
    time.sleep(0.02)
    if button_first == 0:
        print('Button pressed!')
        client.publish('/esp8266/livingroom', sendData)
예제 #25
0
파일: app.py 프로젝트: nithishr/jacobsHack
slack = Slacker(os.getenv('SLACK_KEY'))
conn_string = "mongodb://*****:*****@ds151513.mlab.com:51513/message-hub"
client = MongoClient(conn_string)
db = client.get_database()
messages_db = db['messages']
client = algoliasearch.Client("FLBYK5GQ8Z", 'e5f2da5cd1089729b8f4b5633e9fec41')
index = client.init_index('messages')

api_id = 632827
api_hash = '2bf8105b9536dbb821d61d00c621c16f'

phone = '004915234723254'
username = '******'

client = TelegramClient(username, api_id, api_hash)
client.connect()

# Ensure you're authorized
if not client.is_user_authorized():
    client.send_code_request(phone)
    try:
        client.sign_in(phone, input('Enter the code: '))
    except SessionPasswordNeededError:
        client.sign_in(password=input('Password: '******'/')
def hello_world():
    return 'Hello World!'