Пример #1
0
def auto_update_charts_wattstopper_plugload(request):
    if request.method == 'POST':
        print 'inside cassandra auto update wattstopper plugload'
        _data = request.body
        _data = json.loads(_data)
        mac = _data['mac']
        if 'from_dt' in _data.keys():
            from_date = _data['from_dt']
            print from_date
        else:
            from_date = ''

        device_id = get_device_id_from_mac(mac)
        if from_date == '':
            data_points, rs = retrieve(device_id, ['time', 'status', 'power'])
        else:
            from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
            data_points, rs = retrieve(device_id, ['time', 'status', 'power'], from_date)

        rs_status = parse_resultset(data_points, 'status', rs)
        rs_power = parse_resultset(data_points, 'power', rs)

        json_result = {
            'status': rs_status,
            'power': rs_power
        }

        if request.is_ajax():
            return HttpResponse(json.dumps(json_result), mimetype='application/json')
Пример #2
0
def get_statistics_datetime_wattstopper_plugload(request):
    if request.method == 'POST':
        print 'inside cassandra get statistics for wattstopper plugload based on given from and to datetime'
        _data = request.body
        _data = json.loads(_data)
        mac = _data['mac']
        from_date = _data['from_dt']
        to_date = _data['to_dt']
        print from_date

        device_id = get_device_id_from_mac(mac)
        if not from_date and not to_date:
            data_points, rs = retrieve(device_id, ['time', 'status', 'power'])
        elif not to_date and from_date:
            from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
            data_points, rs = retrieve(device_id, ['time', 'status', 'power'], from_date)
        else:
            from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
            to_date = datetime.datetime.strptime(to_date, '%Y/%m/%d %H:%M')
            print from_date, to_date
            data_points, rs = retrieve(device_id, ['time', 'status', 'power'], from_date, to_date)

        rs_status = parse_resultset(data_points, 'status', rs)
        rs_power = parse_resultset(data_points, 'power', rs)

        json_result = {
            'status': rs_status,
            'power': rs_power
        }

        print 'Got results based on datetime'
        if request.is_ajax():
                return HttpResponse(json.dumps(json_result), mimetype='application/json')
Пример #3
0
def auto_update_charts_thermostat(request):
    if request.method == 'POST':
        print 'inside cassandra auto update thermostat'
        _data = request.body
        _data = json.loads(_data)
        mac = _data['mac']
        if 'from_dt' in _data.keys():
            from_date = _data['from_dt']
            print from_date
        else:
            from_date = ''

        device_id = get_device_id_from_mac(mac)
        if from_date == '':
            data_points, rs = retrieve(device_id, ['time', 'temperature',
                                  'heat_setpoint', 'cool_setpoint'])
        else:
            from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
            data_points, rs = retrieve(device_id, ['time', 'temperature',
                                  'heat_setpoint', 'cool_setpoint'], from_date)

        rs_temperature = parse_resultset(data_points, 'temperature', rs)
        rs_heat_setpoint = parse_resultset(data_points, 'heat_setpoint', rs)
        rs_cool_setpoint = parse_resultset(data_points, 'cool_setpoint', rs)

        json_result = {
            'temperature': rs_temperature,
            'heat_setpoint': rs_heat_setpoint,
            'cool_setpoint': rs_cool_setpoint
        }

        print 'test'
        if request.is_ajax():
                return HttpResponse(json.dumps(json_result), mimetype='application/json')
Пример #4
0
def retrieve_for_export(agentID, vars=None, startTime=None, endTime=None, tablename=None,weather_agent=False):
    if weather_agent:
        data = retrieve(agentID=agentID,vars=vars,startTime=startTime,endTime=endTime,export=True,tablename=tablename,weather_agent=settings.weather_agent)
        if data is not None:
            a,b=data
        else:
            a, b = retrieve(agentID=agentID, vars=vars, startTime=startTime, endTime=endTime, export=True,
                            tablename=tablename, weather_agent=None)
    else:
        a, b = retrieve(agentID=agentID, vars=vars, startTime=startTime, endTime=endTime, export=True,
                        tablename=tablename, weather_agent=None)
    return a,b
Пример #5
0
def get_statistics_datetime_rtu(request):
    if request.method == 'POST':
        print 'inside cassandra get statistics for rtu based on given from and to datetime'
        _data = request.body
        _data = json.loads(_data)
        mac = _data['mac']
        from_date = _data['from_dt']
        to_date = _data['to_dt']
        print from_date

        device_id = get_device_id_from_mac(mac)
        if not from_date and not to_date:
            data_points, rs = retrieve(device_id, ['time', 'outside_temperature', 'return_temperature', 'supply_temperature',
                                      'heat_setpoint', 'cool_setpoint', 'cooling_mode', 'heating',
                                      'outside_damper_position', 'bypass_damper_position'])
        elif not to_date and from_date:
            from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
            data_points, rs = retrieve(device_id, ['time', 'outside_temperature', 'return_temperature', 'supply_temperature',
                                      'heat_setpoint', 'cool_setpoint', 'cooling_mode', 'heating',
                                      'outside_damper_position', 'bypass_damper_position'], from_date)
        else:
            from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
            to_date = datetime.datetime.strptime(to_date, '%Y/%m/%d %H:%M')
            data_points, rs = retrieve(device_id, ['time', 'outside_temperature', 'return_temperature', 'supply_temperature',
                                      'heat_setpoint', 'cool_setpoint', 'cooling_mode', 'heating',
                                      'outside_damper_position', 'bypass_damper_position'], from_date, to_date)

        rs_outside_temperature = parse_resultset(data_points, 'outside_temperature', rs)
        rs_return_temperature = parse_resultset(data_points, 'return_temperature', rs)
        rs_supply_temperature = parse_resultset(data_points, 'supply_temperature', rs)
        rs_heat_setpoint = parse_resultset(data_points, 'heat_setpoint', rs)
        rs_cool_setpoint = parse_resultset(data_points, 'cool_setpoint', rs)
        rs_cooling_mode = parse_resultset(data_points, 'cooling_mode', rs)
        rs_heating = parse_resultset(data_points, 'heating', rs)
        rs_outside_damper_position = parse_resultset(data_points, 'outside_damper_position', rs)
        rs_bypass_damper_position = parse_resultset(data_points, 'bypass_damper_position', rs)


        json_result = {'outside_temperature': rs_outside_temperature,
                       'supply_temperature': rs_supply_temperature,
                       'return_temperature': rs_return_temperature,
                       'heating': rs_heating,
                       'outside_damper_position': rs_outside_damper_position,
                       'bypass_damper_position': rs_bypass_damper_position,
                       'cooling_mode': rs_cooling_mode,
                       'heat_setpoint': rs_heat_setpoint,
                       'cool_setpoint': rs_cool_setpoint
                       }

        print 'Got results based on datetime'
        if request.is_ajax():
                return HttpResponse(json.dumps(json_result), mimetype='application/json')
Пример #6
0
def charts_plugload(request, mac):
    print "inside cassandra view method for plugload"
    context = RequestContext(request)
    if request.method == 'GET':

        device_metadata = [ob.device_control_page_info() for ob in DeviceMetadata.objects.filter(mac_address=mac)]
        device_id = device_metadata[0]['device_id']
        device_type_id = device_metadata[0]['device_model_id']
        device_type_id = device_type_id.device_model_id

        if device_type_id == '2WL':
            device_status = [ob.data_as_json() for ob in Lighting.objects.filter(lighting_id=device_id)]
            device_nickname = device_status[0]['nickname']
            zone_nickname = device_status[0]['zone']['zone_nickname']
        else:
            device_status = [ob.data_as_json() for ob in Plugload.objects.filter(plugload_id=device_id)]
            device_nickname = device_status[0]['nickname']
            zone_nickname = device_status[0]['zone']['zone_nickname']

        data_points, rs = retrieve(device_id, ['time', 'status'])
        rs_status = parse_resultset(data_points, 'status', rs)

        update_context(context)

        return render_to_response(
            'charts/charts_plugload.html',
            {'status': rs_status, 'mac': mac, 'nickname': device_nickname, 'zone_nickname': zone_nickname,
             'device_type_id': device_type_id}, context)
Пример #7
0
def charts_vav(request, mac):
    """Page load definition for VAV statistics."""
    print "inside smap view method"
    context = RequestContext(request)
    if request.method == 'GET':

        device_id = get_device_id_from_mac(mac)

        device_status = [ob.as_json() for ob in VAV.objects.filter(vav_id=device_id)]
        device_nickname = device_status[0]['nickname']
        zone_nickname = device_status[0]['zone']['zone_nickname']

        data_points, rs = retrieve(device_id, ['time', 'temperature', 'supply_temperature',
                                  'heat_setpoint', 'cool_setpoint', 'flap_position'])

        rs_temperature = parse_resultset(data_points, 'temperature', rs)
        rs_supply_temperature = parse_resultset(data_points, 'supply_temperature', rs)
        rs_heat_setpoint = parse_resultset(data_points, 'heat_setpoint', rs)
        rs_cool_setpoint = parse_resultset(data_points, 'cool_setpoint', rs)
        rs_flap_position = parse_resultset(data_points, 'flap_position', rs)

        context = update_context(context)

        return render_to_response(
            'charts/charts_vav.html',
            {'temperature': rs_temperature, 'supply_temperature': rs_supply_temperature,
             'flap_position': rs_flap_position, 'heat_setpoint': rs_heat_setpoint, 'cool_setpoint': rs_cool_setpoint,
             'nickname': device_nickname, 'mac': mac,
             'zone_nickname': zone_nickname},
            context)
Пример #8
0
def auto_update_charts_rtu(request):
    """Statistics page update for RTU"""
    if request.method == 'POST':
        print 'inside cassandra auto update RTU'
        _data = request.body
        _data = json.loads(_data)
        mac = _data['mac']
        if 'from_dt' in _data.keys():
            from_date = _data['from_dt']
            print from_date
        else:
            from_date = ''

        device_id = get_device_id_from_mac(mac)
        if from_date == '':
            data_points, rs = retrieve(device_id, ['time', 'outside_temperature', 'return_temperature', 'supply_temperature',
                                      'heat_setpoint', 'cool_setpoint', 'cooling_mode', 'heating',
                                      'outside_damper_position', 'bypass_damper_position'])
        else:
            from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
            data_points, rs = retrieve(device_id, ['time', 'outside_temperature', 'return_temperature', 'supply_temperature',
                                      'heat_setpoint', 'cool_setpoint', 'cooling_mode', 'heating',
                                      'outside_damper_position', 'bypass_damper_position'], from_date)

        rs_outside_temperature = parse_resultset(data_points, 'outside_temperature', rs)
        rs_return_temperature = parse_resultset(data_points, 'return_temperature', rs)
        rs_supply_temperature = parse_resultset(data_points, 'supply_temperature', rs)
        rs_heat_setpoint = parse_resultset(data_points, 'heat_setpoint', rs)
        rs_cool_setpoint = parse_resultset(data_points, 'cool_setpoint', rs)
        rs_cooling_mode = parse_resultset(data_points, 'cooling_mode', rs)
        rs_heating = parse_resultset(data_points, 'heating', rs)
        rs_outside_damper_position = parse_resultset(data_points, 'outside_damper_position', rs)
        rs_bypass_damper_position = parse_resultset(data_points, 'bypass_damper_position', rs)

        json_result = {'outside_temperature': rs_outside_temperature,
                       'supply_temperature': rs_supply_temperature,
                        'return_temperature': rs_return_temperature,
                        'heating': rs_heating,
                        'outside_damper_position': rs_outside_damper_position,
                        'bypass_damper_position': rs_bypass_damper_position,
                        'cooling_mode': rs_cooling_mode,
                        'heat_setpoint': rs_heat_setpoint,
                        'cool_setpoint': rs_cool_setpoint}

        print 'test'
        if request.is_ajax():
                return HttpResponse(json.dumps(json_result), mimetype='application/json')
Пример #9
0
def returnChartsPage(request, context,mac,data_variables,page,get_weather=False):
    '''
    :param context: var obtained from RequestContext(request)
    :param mac: Device mac ID (table id)
    :param data_variables: Dict. Variables in the database that is to be retrieved as value, and varaibles in html as keys
    :param page: The html page to be returned to
    :param device_type: The device type class
    :return: the rendered html page
    '''
    agent_id = get_agent_id_from_mac(mac)
    try:
        if not get_weather:
            varlist, rs = retrieve(agent_id)
        else:
            data = retrieve(agent_id, weather_agent=settings.weather_agent)
            if data is not None:
                varlist, rs=data
            else:
                varlist, rs = retrieve(agent_id)
    except Exception as er:
        print er
        print 'Cassandra data reading error'
        return {}

    device_list_side_nav = get_device_list_and_count(request)

    data_dict = {}
    objects = [ob for ob in DeviceMetadata.objects.filter(mac_address=mac)]
    try:
        device_nickname = objects[0].nickname
        node_name = objects[0].node.node_name
        data_dict = {'mac': mac, 'nickname': device_nickname, 'node_name': node_name}
    except Exception as er:
        print er
        print 'get device status failed'

    for key,val in data_variables.items():
        try:
            data_dict[key] = parse_resultset(varlist,val,rs)
        except (KeyError, ValueError) as er:
            print er
            continue

    context.update(get_device_list_and_count(request))
    data_dict.update(device_list_side_nav)
    return render(request, page,data_dict)
Пример #10
0
def returnsCharts(request,data_variables,get_weather=False):

    _data = request.body
    _data = json.loads(_data)
    mac = _data['mac']
    if 'from_dt' in _data.keys():
        from_date = _data['from_dt']
        if from_date not in [None,'',u'']:
            if type(from_date) in [str,unicode]:
                from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
        else:
            from_date = None
        print from_date
    else:
        from_date = None


    if 'to_dt' in _data.keys():
        to_date = _data['to_dt']
        if to_date not in [None,'',u'']:
            if type(to_date) in [str,unicode]:
                to_date = datetime.datetime.strptime(to_date, '%Y/%m/%d %H:%M')
        else:
            to_date = None
    else:
        to_date = None

    if from_date is None and to_date is None:
        to_date = datetime.datetime(2017, 2, 4)
        from_date = datetime.datetime(2017, 1, 1)

    agent_id = get_agent_id_from_mac(mac)
    if get_weather:
        varlist, rs = retrieve(agent_id,startTime=from_date,endTime=to_date,weather_agent=settings.weather_agent)
    else:
        varlist, rs = retrieve(agent_id,startTime=from_date,endTime=to_date)
    #varlist, rs = retrieve(agent_id, weather_agent="weatheragent22101", )
    json_result =dict()
    for key,val in data_variables.items():
        if key in varlist:
            json_result[key] = parse_resultset(varlist,val,rs,to_date)

    if request.is_ajax():
            return HttpResponse(json.dumps(json_result))
Пример #11
0
def retrieve_for_export(agentID,
                        vars=None,
                        startTime=None,
                        endTime=None,
                        tablename=None,
                        weather_agent=False):
    device = DeviceMetadata.objects.get(agent_id=agentID)
    if not tablename:
        tablename = "B" + str(
            device.building.building_id) + "_" + device.device_model.replace(
                " ", "_").replace("-", "_")
        tablename = re.sub(r'\W', '_', tablename)
    weather_agent = 'BEMO_WUG' + device.building.zip_code + '_' + str(
        device.building.building_id)
    weather_table = "B" + str(device.building.building_id) + "bemossweather"
    if weather_agent:
        data = retrieve(agentID=agentID,
                        vars=vars,
                        startTime=startTime,
                        endTime=endTime,
                        export=True,
                        tablename=tablename,
                        weather_agent=weather_agent,
                        weather_table=weather_table)
        if data is not None:
            a, b = data
        else:
            a, b = retrieve(agentID=agentID,
                            vars=vars,
                            startTime=startTime,
                            endTime=endTime,
                            export=True,
                            tablename=tablename,
                            weather_agent=None)
    else:
        a, b = retrieve(agentID=agentID,
                        vars=vars,
                        startTime=startTime,
                        endTime=endTime,
                        export=True,
                        tablename=tablename,
                        weather_agent=None)
    return a, b
Пример #12
0
def auto_update_charts_vav(request):
    """Statistics page load for VAV"""
    if request.method == 'POST':
        print 'inside smap auto update VAV'
        _data = request.body
        _data = json.loads(_data)
        mac = _data['mac']
        print mac

        if 'from_dt' in _data.keys():
            from_date = _data['from_dt']
            print from_date
        else:
            from_date = ''

        device_id = get_device_id_from_mac(mac)
        if from_date == '':
            data_points, rs = retrieve(device_id, ['time', 'temperature', 'supply_temperature',
                                  'heat_setpoint', 'cool_setpoint', 'flap_position'])
        else:
            from_date = datetime.datetime.strptime(from_date, '%Y/%m/%d %H:%M')
            data_points, rs = retrieve(device_id, ['time', 'temperature', 'supply_temperature',
                                  'heat_setpoint', 'cool_setpoint', 'flap_position'], from_date)

        rs_temperature = parse_resultset(data_points, 'temperature', rs)
        rs_supply_temperature = parse_resultset(data_points, 'supply_temperature', rs)
        rs_heat_setpoint = parse_resultset(data_points, 'heat_setpoint', rs)
        rs_cool_setpoint = parse_resultset(data_points, 'cool_setpoint', rs)
        rs_flap_position = parse_resultset(data_points, 'flap_position', rs)

        json_result = {'temperature': rs_temperature,
                       'supply_temperature': rs_supply_temperature,
                        'flap_position': rs_flap_position,
                        'heat_setpoint': rs_heat_setpoint,
                        'cool_setpoint': rs_cool_setpoint}

        if request.is_ajax():
                return HttpResponse(json.dumps(json_result), mimetype='application/json')
Пример #13
0
def charts_wattstopper_plugload(request, mac):
    context = RequestContext(request)
    if request.method == 'GET':

        device_id = get_device_id_from_mac(mac)

        device_status = [ob.data_as_json() for ob in Plugload.objects.filter(plugload_id=device_id)]
        device_nickname = device_status[0]['nickname']
        zone_nickname = device_status[0]['zone']['zone_nickname']

        data_points, rs = retrieve(device_id, ['time', 'status', 'power'])

        rs_status = parse_resultset(data_points, 'status', rs)
        rs_power = parse_resultset(data_points, 'power', rs)

        context = update_context(context)

        return render_to_response(
            'charts/charts_wtplug.html',
            {'status': rs_status, 'power': rs_power, 'nickname': device_nickname, 'zone_nickname': zone_nickname,
             'mac': mac}, context)
Пример #14
0
def charts_rtu(request, mac):
    """Page load definition for RTU statistics."""
    print "inside smap view method"
    context = RequestContext(request)
    if request.method == 'GET':

        device_id = get_device_id_from_mac(mac)

        device_status = [ob.as_json() for ob in RTU.objects.filter(rtu_id=device_id)]
        device_nickname = device_status[0]['nickname']
        zone_nickname = device_status[0]['zone']['zone_nickname']

        data_points, rs = retrieve(device_id, ['time', 'outside_temperature', 'return_temperature', 'supply_temperature',
                                  'heat_setpoint', 'cool_setpoint', 'cooling_mode', 'heating',
                                  'outside_damper_position', 'bypass_damper_position'])

        rs_outside_temperature = parse_resultset(data_points, 'outside_temperature', rs)
        rs_return_temperature = parse_resultset(data_points, 'return_temperature', rs)
        rs_supply_temperature = parse_resultset(data_points, 'supply_temperature', rs)
        rs_heat_setpoint = parse_resultset(data_points, 'heat_setpoint', rs)
        rs_cool_setpoint = parse_resultset(data_points, 'cool_setpoint', rs)
        rs_cooling_mode = []

        rs_heating = parse_resultset(data_points, 'heating', rs)
        rs_outside_damper_position = parse_resultset(data_points, 'outside_damper_position', rs)
        rs_bypass_damper_position = parse_resultset(data_points, 'bypass_damper_position', rs)


        context = update_context(context)

        return render_to_response(
            'charts/charts_rtu.html',
            {'outside_temperature': rs_outside_temperature, 'supply_temperature': rs_supply_temperature,
             'return_temperature': rs_return_temperature, 'heating': rs_heating,
             'outside_damper_position': rs_outside_damper_position,
             'bypass_damper_position': rs_bypass_damper_position, 'cooling_mode': rs_cooling_mode,
             'heat_setpoint': rs_heat_setpoint, 'cool_setpoint': rs_cool_setpoint,
             'nickname': device_nickname, 'mac': mac,
             'zone_nickname': zone_nickname},
            context)
Пример #15
0
def charts_lighting(request, mac):
    print "inside cassandra view method for lighting"
    context = RequestContext(request)
    if request.method == 'GET':

        device_id = get_device_id_from_mac(mac)

        device_status = [ob.data_as_json() for ob in Lighting.objects.filter(lighting_id=device_id)]
        device_nickname = device_status[0]['nickname']
        zone_nickname = device_status[0]['zone']['zone_nickname']

        data_points, rs = retrieve(device_id, ['time', 'status', 'brightness'])

        rs_status = parse_resultset(data_points, 'status', rs)
        rs_brightness = parse_resultset(data_points, 'brightness', rs)

        context = update_context(context)

        return render_to_response(
            'charts/charts_lighting.html',
            {'status': rs_status, 'brightness': rs_brightness,
             'nickname': device_nickname, 'zone_nickname': zone_nickname,
             'mac': mac}, context)
Пример #16
0
def charts_thermostat(request, mac):
    """Page load definition for thermostat statistics."""
    print "inside cassandra view method"
    context = RequestContext(request)
    if request.method == 'GET':

        device_id = get_device_id_from_mac(mac)
        data_points, rs = retrieve(device_id, ['time', 'temperature',
                                   'heat_setpoint', 'cool_setpoint'])

        rs_temperature = parse_resultset(data_points, 'temperature', rs)
        rs_heat_setpoint = parse_resultset(data_points, 'heat_setpoint', rs)
        rs_cool_setpoint = parse_resultset(data_points, 'cool_setpoint', rs)

        device_status = [ob.data_as_json() for ob in Thermostat.objects.filter(thermostat_id=device_id)]
        device_nickname = device_status[0]['nickname']
        zone_nickname = device_status[0]['zone']['zone_nickname']

        context = update_context(context)

        return render_to_response(
            'charts/charts_thermostat.html',
            {'temperature': rs_temperature, 'heat_setpoint': rs_heat_setpoint, 'cool_setpoint': rs_cool_setpoint,
             'mac': mac, 'nickname': device_nickname, 'zone_nickname': zone_nickname}, context)
Пример #17
0
    def check_profile_trigger(self):
        if not self.trained:
            self.train_model()

        vars = [
            'time', 'cool_setpoint', 'heat_setpoint', 'thermostat_mode',
            'thermostat_state', 'temperature'
        ]

        curtime = self.current_time()
        look_back_time = curtime - datetime.timedelta(hours=24)

        vars, result = cassandraDB.retrieve(
            self.data['thermostat'],
            vars,
            look_back_time,
            curtime,
            weather_agent=self.data['weather_agent'])

        slopes_and_points = self.getSlopesAndPoints(result, vars)
        anamoly = False
        current_mode = 'Normal setpoint-following'
        try:
            if slopes_and_points['heating_points']:
                current_mode = 'heating'
                if self.checkAnamoly(slopes_and_points['heating_points'],
                                     'heating_model'):
                    anamoly = True
                if self.checkDeviation(slopes_and_points['heating_points']):
                    anamoly = True

            elif slopes_and_points['cooling_points']:
                current_mode = 'cooling'
                if self.checkAnamoly(slopes_and_points['cooling_points'],
                                     'cooling_model'):
                    anamoly = True
                if self.checkDeviation(slopes_and_points['cooling_points']):
                    anamoly = True

            elif slopes_and_points['heatoff_points']:
                current_mode = 'heat-off'
                if self.checkAnamoly(slopes_and_points['heatoff_points'],
                                     'heatoff_model'):
                    anamoly = True

            elif slopes_and_points['cooloff_points']:
                current_mode = 'cool-off'
                if self.checkAnamoly(slopes_and_points['cooloff_points'],
                                     'cooloff_model'):
                    anamoly = True

        except ValueError as er:
            if str(er) == 'no-outdoor-temp':
                anamoly = False
                current_mode = "Missing outdoor temperature"
            elif str(er) == 'not-enough-data':
                anamoly = False
                current_mode = "Watching " + current_mode
            elif str(er) == 'not-enough-historical-data':
                anamoly = False
                current_mode = "Watching " + current_mode + ". Insufficient historical data"
            else:
                raise

        return anamoly, current_mode
Пример #18
0
    def train_model(self):
        end_time = self.current_time()
        start_time = end_time - datetime.timedelta(days=90)

        vars = [
            'time', 'cool_setpoint', 'heat_setpoint', 'thermostat_mode',
            'thermostat_state', 'temperature'
        ]
        vars, result = cassandraDB.retrieve(
            self.data['thermostat'],
            vars,
            start_time,
            end_time,
            weather_agent=self.data['weather_agent'])

        if not len(result):
            return

        slope_and_points = self.getSlopesAndPoints(result, vars)

        def train_models(slope_history):
            if len(slope_history) < 3:
                return None
            #slope_histroy = list of tuples: (avg_outdoor, slope, std_err,temperature_profile[0,0])
            sh = np.matrix(slope_history)
            lnmodel = LinearRegression()
            error_inverse = np.array(1 / sh[:, 2])[:, 0]
            lnfit = lnmodel.fit(sh[:, 0], sh[:, 1])
            #svr_rbf = SVR(kernel='linear', C=10, epsilon=0.5)
            #svrfit = svr_rbf.fit(sh[:, 0], sh[:,1])

            ln_residue = []
            for i in range(len(slope_history)):
                p = lnfit.predict(slope_history[i][0])[0][0]
                ln_residue.append((p - slope_history[i][1])**2)

            ln_std = stats.tstd(ln_residue)

            ln_mean = stats.tmean(ln_residue)
            new_sh = None
            for i in range(len(ln_residue)):
                if ln_residue[i] < ln_mean + 3 * ln_std:
                    #sh = np.delete(sh,i,axis=0)
                    if new_sh is None:
                        new_sh = sh[i, :]
                    else:
                        new_sh = np.vstack((new_sh, sh[i, :]))

            sh = new_sh
            #redo the fit
            error_inverse = np.array(1 / sh[:, 2])[:, 0]

            slope_mean = stats.tmean(sh[:, 1])
            slope_std = stats.tstd(sh[:, 1])

            lnfit = lnmodel.fit(sh[:, 0], sh[:, 1])
            ln_residue = []
            for i in range(len(sh)):
                p = lnfit.predict(sh[i, 0])[0][0]
                ln_residue.append((p - sh[i, 1])**2)

            ln_std = stats.tstd(ln_residue)
            ln_mean = stats.tmean(ln_residue)

            return {
                'ln_model': lnfit,
                'ln_residue': ln_residue,
                'ln_residue_std': ln_std,
                'ln_residue_mean': ln_mean,
                'slope_mean': slope_mean,
                'slope_std': slope_std,
                'data_matrix': sh
            }

        slope_dict = {
            'heating_model': 'heating_slopes',
            'cooling_model': 'cooling_slopes',
            'heatoff_model': 'heatoff_slopes',
            'cooloff_model': 'cooloff_slopes'
        }

        self.models = dict()
        for key, val in slope_dict.items():
            slope_history = slope_and_points[val]
            if len(slope_history):
                self.models[key] = train_models(slope_history)

        self.trained = True
Пример #19
0
    def check_profile_trigger(self):
        if not self.trained:
            self.train_model()

        vars = [
            'time', 'cool_setpoint', 'heat_setpoint', 'thermostat_mode',
            'thermostat_state', 'temperature'
        ]

        curtime = self.current_time()
        look_back_time = curtime - datetime.timedelta(hours=24)

        vars, result = cassandraDB.retrieve(
            self.data['thermostat'],
            vars,
            look_back_time,
            curtime,
            weather_agent=self.data['weather_agent'])
        last_result = result[-1]
        current_result = last_result.copy()
        current_result[0] = time.time() * 1000
        result = np.vstack([result, current_result])
        slopes_and_points = self.getSlopesAndPoints(result, vars)
        anamoly = False
        current_mode = 'Normal setpoint-following'

        #slope based Anamoly detection has been commented out to avoid false positives for now.
        try:
            if slopes_and_points['heating_points']:
                current_mode = 'heating'
                # if self.checkAnamoly(slopes_and_points['heating_points'],'heating_model'):
                #     anamoly = True
                if self.checkDeviation(slopes_and_points['heating_points'],
                                       'heating'):
                    anamoly = True
                    current_mode = 'Heating Failure'
                else:
                    current_mode = 'Normal Heating'

            elif slopes_and_points['cooling_points']:
                # if self.checkAnamoly(slopes_and_points['cooling_points'], 'cooling_model'):
                #     anamoly = True
                if self.checkDeviation(slopes_and_points['cooling_points'],
                                       'cooling'):
                    anamoly = True
                    current_mode = 'Cooling Failure'
                else:
                    current_mode = 'Normal Cooling'

            elif slopes_and_points['heatoff_points']:
                # if self.checkAnamoly(slopes_and_points['heatoff_points'], 'heatoff_model'):
                #     anamoly = True
                if self.checkDeviation(slopes_and_points['heatoff_points'],
                                       'heatoff'):
                    anamoly = True
                    current_mode = 'Too much cooling'
                else:
                    current_mode = 'Normal cooling'

            elif slopes_and_points['cooloff_points']:
                # if self.checkAnamoly(slopes_and_points['cooloff_points'], 'cooloff_model'):
                #     anamoly = True
                if self.checkDeviation(slopes_and_points['cooloff_points'],
                                       'cooloff'):
                    anamoly = True
                    current_mode = 'Too much heating'
                else:
                    current_mode = 'Normal heating'

        except ValueError as er:
            if str(er) == 'no-outdoor-temp':
                anamoly = False
                current_mode = "Missing outdoor temperature"
            elif str(er) == 'not-enough-data':
                anamoly = False
                current_mode = "Watching " + current_mode
            elif str(er) == 'not-enough-historical-data':
                anamoly = False
                current_mode = "Watching " + current_mode + ". Insufficient historical data"
            else:
                raise

        return anamoly, current_mode
Пример #20
0
def returnChartsPage(request,
                     context,
                     mac,
                     data_variables,
                     page,
                     get_weather=False):
    '''
    :param context: var obtained from RequestContext(request)
    :param mac: Device mac ID (table id)
    :param data_variables: Dict. Variables in the database that is to be retrieved as value, and varaibles in html as keys
    :param page: The html page to be returned to
    :param device_type: The device type class
    :return: the rendered html page
    '''
    agent_id = get_agent_id_from_mac(mac)
    device = DeviceMetadata.objects.get(agent_id=agent_id)
    weather_agent = 'BEMO_WUG' + device.building.zip_code + '_' + str(
        device.building.building_id)
    weather_table = "B" + str(device.building.building_id) + "_bemossweather"

    tablename = getTableName(device.building.building_id, device.device_model)

    try:
        if not get_weather:
            varlist, rs = retrieve(agent_id, tablename=tablename)
        else:
            data = retrieve(agent_id,
                            weather_agent=weather_agent,
                            tablename=tablename,
                            weather_table=weather_table)
            if data is not None:
                varlist, rs = data
            else:
                varlist, rs = retrieve(agent_id, tablename=tablename)
    except Exception as er:
        print er
        print 'Cassandra data reading error'
        return HttpResponse("Database fail", status=404)

    device_list_side_nav = get_device_list_and_count(request)

    data_dict = {}
    objects = [ob for ob in DeviceMetadata.objects.filter(mac_address=mac)]
    try:
        device_nickname = objects[0].nickname
        node_name = objects[0].node.node_name
        data_dict = {
            'mac': mac,
            'nickname': device_nickname,
            'node_name': node_name
        }
    except Exception as er:
        print er
        print 'get device status failed'

    for key, val in data_variables.items():
        try:
            data_dict[key] = parse_resultset(varlist, val, rs)
        except (KeyError, ValueError) as er:
            print er
            continue

    context.update(get_device_list_and_count(request))
    data_dict.update(device_list_side_nav)
    return render(request, page, data_dict)
Пример #21
0
def returnsCharts(_data, data_variables, get_weather=False):

    if 'agent_id' not in _data:
        mac = _data['mac']
        agent_id = get_agent_id_from_mac(mac)
    else:
        agent_id = _data['agent_id']

    if 'from_dt' in _data.keys():
        from_date = _data['from_dt']
        if from_date not in [None, '', u'']:
            if type(from_date) in [str, unicode]:
                from_date = datetime.datetime.strptime(from_date,
                                                       '%Y/%m/%d %H:%M')
        else:
            from_date = None
        print from_date
    else:
        from_date = None

    if 'to_dt' in _data.keys():
        to_date = _data['to_dt']
        if to_date not in [None, '', u'']:
            if type(to_date) in [str, unicode]:
                to_date = datetime.datetime.strptime(to_date, '%Y/%m/%d %H:%M')
        else:
            to_date = None
    else:
        to_date = None

    if from_date is None and to_date is None:
        to_date = datetime.datetime.now()
        from_date = to_date - datetime.timedelta(days=1)

    device = DeviceMetadata.objects.get(agent_id=agent_id)
    weather_agent = 'BEMO_WUG' + device.building.zip_code + '_' + str(
        device.building.building_id)
    weather_table = "B" + str(device.building.building_id) + "_bemossweather"
    tablename = "B" + str(
        device.building.building_id) + "_" + device.device_model.replace(
            " ", "_").replace("-", "_")
    tablename = re.sub(r'\W', '', tablename)
    if get_weather:
        varlist, rs = retrieve(agent_id,
                               startTime=from_date,
                               endTime=to_date,
                               weather_agent=weather_agent,
                               tablename=tablename,
                               weather_table=weather_table)
    else:
        varlist, rs = retrieve(agent_id,
                               startTime=from_date,
                               endTime=to_date,
                               tablename=tablename)
    #varlist, rs = retrieve(agent_id, weather_agent="weatheragent22101", )
    json_result = dict()

    for key, val in data_variables.items():
        if key in varlist:
            json_result[key] = parse_resultset(varlist, val, rs, to_date)

    return json_result