Beispiel #1
0
def build_ads(ads_v):
    cf = config.get_instance()
    res = {}
    for i in range(len(ads_v)):
        ad_port = "AD{0}".format(i + 1)
        ad_key, ad_rv = cf.parse_numeric_data(ads_v[i], ad_port)
        if ad_key is not None and ad_rv is not None:
            res[ad_key] = {'value': round(ad_rv, 2)}
    return res
Beispiel #2
0
def build_PI():
    dp = data_pool.get_instance()
    pi_v = dp.get_pi_value()
    pi_port_re = re.compile('^PI\d+')
    cf = config.get_instance()
    for key, content in cf.data_config.items():
        if pi_port_re.match(content['port']) is not None:
            return (key, {'value': round(pi_v * every_tip_parts, 1)})
    return (None, None)
Beispiel #3
0
def main_proc():
    p = gethostname(dns_sn, tcpc_dst_url)
    #p = tcpc_dst_url
    if len(p) == 0:
        return
    if establish_connect(config_download_sn, p, tcpc_dst_port) == 0:
        return
    print "established!"
    cf = config.get_instance()
    up_dict = {'device_id': cf.get_device_id(), 'method': 'pull_param'}
    jup_dict = json.dumps(up_dict)
    if 0 == send_data(config_download_sn, p, tcpc_dst_port, jup_dict,
                      len(jup_dict)):
        return

    print "start recieve data"
    res_total = ''
    for i in range(5):
        res = recv_data(config_download_sn, p, tcpc_dst_port)
        if res == None:
            return
        res_total = res_total + res
        if check_json_format(res_total):
            break
    res = res_total
    print "recive data", res
    try:
        jres = json.loads(res)
        print "server replay:", jres
        res = None
        if jres.has_key("device_id") \
                and jres.has_key("device_config_id") \
                and jres.has_key("method") and jres['method'] == 'push_param'\
                and jres.has_key("config")\
                and jres.has_key("control"):
            deal_systime(jres['ts'])
            deal_config(jres)
            deal_crontab(jres['control'], cf.ctrl_config)
            up_dict = {
                'device_id': cf.get_device_id(),
                'method': 'param_updated'
            }
            jup_dict = json.dumps(up_dict)
            if 0 == send_data(config_download_sn, p, tcpc_dst_port, jup_dict,
                              len(jup_dict)):
                return
    except Exception as e:
        print e
        pass
    wiz.socket_disconnect(config_download_sn)
    wiz.socket_close(config_download_sn)
Beispiel #4
0
def update_config(content):
    config.get_instance().update_config(content)
Beispiel #5
0
def get_Img_key(filepath):
    img_key = cf = config.get_instance().get_port_key('Img1') 
    if img_key == None:
        return "Img1"
    else:
        return img_key
Beispiel #6
0
def main_proc():
    dp = data_pool.get_instance()
    upload_values = dp.get_data(upload_count)
    if len(upload_values) == 0:
        return
    p = gethostname(dns_sn, tcpc_dst_url)
    if len(p) == 0:
        return
    #p = tcpc_dst_url
    print tcpc_dst_url
    if establish_connect(data_up_sn, p, tcpc_dst_port) == 0:
        print "not established"
        return
    cf = config.get_instance()
    up_dict = {
        'device_id': cf.get_device_id(),
        'device_config_id': cf.get_device_config_id(),
        'method': 'push_data',
        'package': {}
    }
    upload_values = dp.get_data(upload_count)
    for uv in upload_values:
        up_dict['package'][uv[0]] = json.loads(uv[1])
    jup_dict = json.dumps(up_dict)
    print "main proc up dict:", len(jup_dict)
    #send for the size
    up_dict_size = {
        'device_id': cf.get_device_id(),
        'device_config_id': cf.get_device_config_id(),
        'method': 'push_data_size',
        'size': len(jup_dict)
    }
    jup_dict_size = json.dumps(up_dict_size)
    if 0 == send_data(data_up_sn, p, tcpc_dst_port, jup_dict_size,
                      len(jup_dict_size)):
        print "send data size error"
        return
    res = recv_data(data_up_sn, p, tcpc_dst_port)
    print "recieve for datasize", res
    if res == None:
        print "replay is none"
        return
    try:
        jres = json.loads(res)
        print "server replay1:", jres
        if jres.has_key('method') and jres['method'] != 'push_data_ready':
            return
    except Exception as e:
        print e
        return
    tmp_jup_str = ''
    start_index = 0
    len_jup_str = len(jup_dict)
    for i in range(len_jup_str / 1024 + 1):
        tmp_jup_str = jup_dict[start_index:start_index +
                               (1024 if len_jup_str > 1024 else len_jup_str)]
        if 0 == send_data(data_up_sn, p, tcpc_dst_port, tmp_jup_str,
                          len(tmp_jup_str)):
            print "send data error"
            return
        start_index = start_index + len(tmp_jup_str)
        len_jup_str = len_jup_str - len(tmp_jup_str)

    res = recv_data(data_up_sn, p, tcpc_dst_port)
    if res == None:
        print "replay is none"
        return
    try:
        jres = json.loads(res)
        print "server replay2:", jres
        res = None
        if jres.has_key('method') and jres['method'] == 'data_uploaded':
            dp.del_data(upload_values)
            deal_systime(int(jres['ts']))
            return
    except Exception as e:
        print e
    wiz.socket_disconnect(data_up_sn)
    wiz.socket_close(data_up_sn)
Beispiel #7
0
def deal_config(new_config):
    config.get_instance().update_config(new_config)
Beispiel #8
0
def main_proc():
    print "start img upload"
    print dns_sn, tcpc_dst_url
    p = gethostname(dns_sn, tcpc_dst_url)
    print dns_sn, tcpc_dst_url
    if len(p) == 0:
        print "exit for main proc"
        return
    #p = tcpc_dst_url
    if establish_connect(img_up_sn, p, tcpc_dst_port) == 0:
        print "establish error, 26"
        return
    cf = config.get_instance()
    up_dict = {
        'device_id': cf.get_device_id(),
        'device_config_id': cf.get_device_config_id(),
        'method': 'push_image'
    }
    dp = data_pool.get_instance()
    imgs = dp.get_imgs(upload_count)
    for img in imgs:
        up_dict['key'] = img[1]
        up_dict['size'] = get_file_size(img[2])
        up_dict['acquisition_time'] = img[0]
        jup_dict = json.dumps(up_dict)
        print jup_dict
        if 0 == send_data(img_up_sn, p, tcpc_dst_port, jup_dict,
                          len(jup_dict)):
            print "error for send data, 39"
            return
        res = recv_data(img_up_sn, p, tcpc_dst_port)
        if res == None:
            print "recv error, 44"
            return
        jres = json.loads(res)
        print jres
        if not jres.has_key('method') or jres['method'] != "push_image_ready":
            print "error for recv data, 46"
            return
        f = open(img[2], 'rb')
        jres = None
        chunk_size = get_file_size(img[2])
        tmp_size = 0
        for i in range(chunk_size / once_send_size):
            chunk = f.read(once_send_size)
            last_len = send_data(img_up_sn, p, tcpc_dst_port, chunk,
                                 len(chunk))
            if last_len == 0:
                print "error for send data, 55"
                return
            tmp_size = tmp_size + len(chunk)

        chunk = f.read(once_send_size)
        if len(chunk) > 0:
            last_len = send_data(img_up_sn, p, tcpc_dst_port, chunk,
                                 len(chunk))
            if last_len == 0:
                print "error for send data, 61"
                return
        tmp_size = tmp_size + len(chunk)
        print "send data size:", tmp_size

        res = recv_data(img_up_sn, p, tcpc_dst_port)
        if res == None:
            print "recv error, 70"
            return
        jres = json.loads(res)
        if jres.has_key('method') and jres['method'] == 'image_uploaded':
            print "main_proc:image uploaded"
            dp.del_img([img])
            os.remove(img[2])

    up_dict = {'device_id': cf.get_device_id(), 'method': 'close_connection'}
    print up_dict
    if 0 == send_data(img_up_sn, p, tcpc_dst_port, json.dumps(up_dict),
                      len(json.dumps(up_dict))):
        print "send error, 79"
        return