<commit_before>#!/usr/bin/env python

#
# MansOS web server - the main file
#

import os, sys, platform
import threading, time, serial, select, socket, cgi, subprocess, struct, signal
import json
from page_login import *
from page_server import *
from page_account import *
from page_user import *
from page_graph import *
from settings import *
from user import *
from session import *
from mote import *
from sensor_data import *
from config import *
from daemon import *
import re as re
from uuid import getnode as get_mac
import urllib2

def isPython3():
    return sys.version_info[0] >= 3

if isPython3():
    from http.server import *
    from socketserver import *
    from urllib.parse import *
else:
    from BaseHTTPServer import *
    from SocketServer import *
    from urlparse import *

isListening = False
listenThread = None

lastUploadCode = ""
lastUploadConfig = ""
lastUploadFile = ""
lastData = ""

isInSubprocess = False
uploadResult = ""

motes = MoteCollection()

sealBlocklyPath = "seal-blockly"

# Packet
usePacketSeparator = False
packetSeparator = "==="
packet = None
sampleRe = re.compile('^\w+=\d+(\.\d+)?(,[\da-fA-F]{2})?$')

connection = None
try:
    from sqlalchemy import create_engine
    from sqlalchemy import Table, Column, MetaData
    from sqlalchemy.types import DateTime, Numeric, Integer, String

    # Database
    url = settingsInstance.getCfgValue("dbServer")
    username = settingsInstance.getCfgValue("dbUsername")
    password = settingsInstance.getCfgValue("dbPassword")
    host = settingsInstance.getCfgValue("dbHost")
    engine = create_engine(url % (username, password, host))
    metadata = MetaData()
    observations = Table('observations', metadata,
        Column('id', Integer, primary_key = True),
        Column('obs_time', DateTime),
        Column('unit_id', String),
        Column('port', String),
        Column('type', String),
        Column('value', Numeric(20,6))
    )
except ImportError:
    print "Warning: database storage dependencies are missing. "
    print "The following packages should be installed to use the database - python-sqlalchemy, python-mysqldb."

saveToDB = False
sendToOpensense = False

def saveDataToDB(packet):
    global connection
    
    mac = str(get_mac())
    for key in packet.keys():
        arr = key.split(":")
        val = packet[key]
        ins = observations.insert().values(obs_time = datetime.datetime.now(), unit_id = mac, port = arr[0], type = arr[1], value = val)
        connection.execute(ins)

def sendDataToSense(packet):
    url = 'http://api.sen.se/events/'
    header = {
      'sense_key' : settingsInstance.getCfgValue("senseApiKey"),<commit_msg>warning message<commit_after>#!/usr/bin/env python

#
# MansOS web server - the main file
#

import os, sys, platform
import threading, time, serial, select, socket, cgi, subprocess, struct, signal
import json
from page_login import *
from page_server import *
from page_account import *
from page_user import *
from page_graph import *
from settings import *
from user import *
from session import *
from mote import *
from sensor_data import *
from config import *
from daemon import *
import re as re
from uuid import getnode as get_mac
import urllib2

def isPython3():
    return sys.version_info[0] >= 3

if isPython3():
    from http.server import *
    from socketserver import *
    from urllib.parse import *
else:
    from BaseHTTPServer import *
    from SocketServer import *
    from urlparse import *

isListening = False
listenThread = None

lastUploadCode = ""
lastUploadConfig = ""
lastUploadFile = ""
lastData = ""

isInSubprocess = False
uploadResult = ""

motes = MoteCollection()

sealBlocklyPath = "seal-blockly"

# Packet
usePacketSeparator = False
packetSeparator = "==="
packet = None
sampleRe = re.compile('^\w+=\d+(\.\d+)?(,[\da-fA-F]{2})?$')

connection = None
try:
    from sqlalchemy import create_engine
    from sqlalchemy import Table, Column, MetaData
    from sqlalchemy.types import DateTime, Numeric, Integer, String

    # Database
    url = settingsInstance.getCfgValue("dbServer")
    username = settingsInstance.getCfgValue("dbUsername")
    password = <PASSWORD>("dbPassword")
    host = settingsInstance.getCfgValue("dbHost")
    engine = create_engine(url % (username, password, host))
    metadata = MetaData()
    observations = Table('observations', metadata,
        Column('id', Integer, primary_key = True),
        Column('obs_time', DateTime),
        Column('unit_id', String),
        Column('port', String),
        Column('type', String),
        Column('value', Numeric(20,6))
    )
except ImportError:
    # TODO: do not print this if "silent" mode is configured in config file!
    print("Warning: using a database for data storage is not possible, package dependencies are missing.")
    print("To store to use a database, install: python-sqlalchemy, python-mysqldb\n")

saveToDB = False
sendToOpensense = False

def saveDataToDB(packet):
    global connection
    
    mac = str(get_mac())
    for key in packet.keys():
        arr = key.split(":")
        val = packet[key]
        ins = observations.insert().values(obs_time = datetime.datetime.now(), unit_id = mac, port = arr[0], type = arr[1], value = val)
        connection.execute(ins)

def sendDataToSense(packet):
    url = 'http://api.sen.se/events/'
    header = {
      'sense_key' : settingsInstance.getCfgValue("senseApiKey"),
{'fraction_non_alphanumeric': 0.11166390270867883, 'fraction_numerical': 0.004053805048829924, 'mean_word_length': 3.7992926613616267, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4523658', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1614, 'n_words': 482}