def mutate(self, info, sn):
        tk = options.tracker.get(sn)

        if tk:
            cp = Composer(tk.lon, tk.lat)
            msg = cp.compose_u8(tk)
            options.socket_param.send(msg)
            code, message = RESPONSE.fmt_response(RESPONSE.OK)
        else:
            code, message = RESPONSE.fmt_response(RESPONSE.TRACKER_NOT_EXISTS)
        return Result(code=code, message=message)
    def mutate(self, info, sn, wireds):
        tk = options.tracker.get(sn)

        if tk:
            cp = Composer(tk.lon, tk.lat)
            info = ''
            for d in wireds:
                if info:
                    info += '|'
                info += d['gpiox'] + '@' + str(d['offset']) + '=' + str(
                    d['value'])
            msg = cp.compose_u6(tk, info)
            options.socket_param.send(msg)
            code, message = RESPONSE.fmt_response(RESPONSE.OK)
        else:
            code, message = RESPONSE.fmt_response(RESPONSE.TRACKER_NOT_EXISTS)

        return Result(code=code, message=message)
    def mutate(self,
               info,
               sn,
               type,
               bat=None,
               ts=int(time.time()),
               temp=None,
               gsm=None,
               acc=None,
               gps=None,
               course=None,
               speed=None,
               alt=None,
               lon=None,
               lat=None):
        tk = options.tracker.get(sn)

        def _set_conf(args):
            for k in args:
                if args[k] is not None:
                    tk[k] = args[k]

        if tk:
            options.thread_lock.acquire()
            list(
                map(_set_conf, [{
                    'type': type
                }, {
                    'bat': bat
                }, {
                    'temp': temp
                }, {
                    'ts': ts
                }, {
                    'gsm': gsm
                }, {
                    'gps': gps
                }, {
                    'course': course
                }, {
                    'speed': speed
                }, {
                    'alt': alt
                }, {
                    'lon': lon
                }, {
                    'lat': lat
                }, {
                    'acc': acc
                }]))
            options.thread_lock.release()
            u4 = Composer(tk.lon, tk.lat).compose_u4(tk)
            options.socket_param.send(u4)
            code, message = RESPONSE.fmt_response(RESPONSE.OK)
        else:
            code, message = RESPONSE.fmt_response(RESPONSE.TRACKER_NOT_EXISTS)
        return Result(code=code, message=message)
    def mutate(self, info, sn, lonlats, speed=None, last_stop=False):
        tk = options.tracker.get(sn)
        ts = int(time.time())

        if tk:
            error_lons = []
            error_lats = []
            for i, t in enumerate(lonlats):
                if t[0] > 180 or t[0] < -180:
                    error_lons.append(t[0])
                if t[1] > 90 or t[1] < -90:
                    error_lats.append(t[1])
                lonlats[i][0] *= 100000
                lonlats[i][1] *= 100000
                if len(lonlats[i]) == 2:
                    lonlats[i].append(ts)
                    ts += 10

            if error_lats or error_lons:
                return Result(code=RESPONSE.ILLEGAL_FORMAT,
                              message='经度或纬度超出范围, '
                              '经度: %s; 纬度: %s' % (error_lons, error_lats))
            else:
                options.thread_lock.acquire()
                tk['lon'] = lonlats[-1][0] / 100000
                tk['lat'] = lonlats[-1][1] / 100000
                if speed is not None:
                    tk['speed'] = speed
                options.thread_lock.release()
            u5 = Composer(tk.lon, tk.lat).compose_move(tk, lonlats)
            options.socket_param.send(u5)
            if last_stop:
                ts = lonlats[-1][2] + 2
                threading.Timer(ts,
                                options.socket_param.send,
                                args=(Composer(tk.lon,
                                               tk.lat).compose_stop(tk,
                                                                    ts=ts)))
                # u5 = Composer(tk.lon, tk.lat).compose_stop(tk)
                # options.socket_param.send(u5)
            code, message = RESPONSE.fmt_response(RESPONSE.OK)
        else:
            code, message = RESPONSE.fmt_response(RESPONSE.TRACKER_NOT_EXISTS)
        return Result(code=code, message=message)
    def mutate(self, info, sn, ts=None):
        tk = options.tracker.get(sn)

        if tk:
            u5 = Composer(tk.lon, tk.lat).compose_stop(tk, ts=ts)
            options.socket_param.send(u5)
            code, message = RESPONSE.fmt_response(RESPONSE.OK)
        else:
            code, message = RESPONSE.fmt_response(RESPONSE.TRACKER_NOT_EXISTS)
        return Result(code=code, message=message)
Ejemplo n.º 6
0
    def __init__(self, sn, iccid, socket_param, lon=104.03974, lat=30.66578):
        self.sn = sn
        self.iccid = iccid
        self.logging = logging
        self.seq = 0

        iccid_lst = re.findall(r'(.{2})', self.iccid)
        self.iccid_lst_int = []
        for item in iccid_lst:
            self.iccid_lst_int.append(int(item, 16))
        sn_lst = re.findall(r'(.{2})', self.sn)
        self.sn_lst_int = []
        for item in sn_lst:
            self.sn_lst_int.append(int(item, 16))

        self.socket = socket_param
        self.res = dict()
        self.composer = Composer(lon, lat)
        self.first_login = False
    def mutate(self,
               info,
               sn,
               chg=None,
               bat=None,
               temp=None,
               gsm=None,
               acc=None):
        tk = options.tracker.get(sn)

        def _set_conf(args):
            for k in args:
                if args[k] is not None:
                    tk[k] = args[k]

        if tk:
            if any([chg, bat, temp, gsm, acc]):
                options.thread_lock.acquire()
                list(
                    map(_set_conf, [{
                        'chg': chg
                    }, {
                        'bat': bat
                    }, {
                        'temp': temp
                    }, {
                        'acc': acc
                    }]))
                options.thread_lock.release()
                u2 = Composer(tk.lon,
                              tk.lat).compose_heartbat(fn.get_seq(sn),
                                                       fn.fmt_iccid(tk.iccid),
                                                       tk)
                options.socket_param.send(u2)

            code, message = RESPONSE.fmt_response(RESPONSE.OK)
        else:
            code, message = RESPONSE.fmt_response(RESPONSE.TRACKER_NOT_EXISTS)
        return Result(code=code, message=message)
    def mutate(self, info, sn, ruuvi, door, range):
        tk = options.tracker.get(sn)

        if tk:
            cp = Composer(tk.lon, tk.lat)
            lth = len(ruuvi) + len(range) + len(door)
            value, fmt = cp.compose_u7_header(tk, lth)
            if ruuvi:
                value, fmt = cp.compose_ruuvi(ruuvi, value, fmt)
            if door:
                value, fmt = cp.compose_wireless_door(door, value, fmt)
            if range:
                value, fmt = cp.compose_range(range, value, fmt)
            msg = struct.pack(fmt, *value)
            logging.info("u7_mg: %s", value)
            options.socket_param.send(msg)

            code, message = RESPONSE.fmt_response(RESPONSE.OK)
        else:
            code, message = RESPONSE.fmt_response(RESPONSE.TRACKER_NOT_EXISTS)

        return Result(code=code, message=message)
Ejemplo n.º 9
0
class SimulatorTracker(object):
    # 3.3
    def __init__(self, sn, iccid, socket_param, lon=104.03974, lat=30.66578):
        self.sn = sn
        self.iccid = iccid
        self.logging = logging
        self.seq = 0

        iccid_lst = re.findall(r'(.{2})', self.iccid)
        self.iccid_lst_int = []
        for item in iccid_lst:
            self.iccid_lst_int.append(int(item, 16))
        sn_lst = re.findall(r'(.{2})', self.sn)
        self.sn_lst_int = []
        for item in sn_lst:
            self.sn_lst_int.append(int(item, 16))

        self.socket = socket_param
        self.res = dict()
        self.composer = Composer(lon, lat)
        self.first_login = False

    def send_login(self):
        T1 = self.composer.compose_login(fn.get_seq(self.sn), self.sn_lst_int,
                                         self.iccid_lst_int)
        self.socket.send(T1)

    def send_heartbat(self):
        while True:
            if options.tracker.get(self.sn):
                U2 = self.composer.compose_heartbat(fn.get_seq(self.sn),
                                                    self.iccid_lst_int,
                                                    options.tracker[self.sn])
                self.socket.send(U2)
            else:
                break
            time.sleep(1.8e3)

    def send_config(self):

        U3 = self.composer.compose_config(fn.get_seq(self.sn),
                                          self.iccid_lst_int,
                                          int(self.res['sessionID']))
        self.socket.send(U3)

    def send_realtime(self, t_time, sessionID):
        U4 = self.composer.compose_realtime(self.get_seq(), self.iccid_lst_int,
                                            sessionID, t_time)
        self.socket.send(U4)

    def send_multi_pvts(self, t_time, sessionID):
        U5 = self.composer.compose_multi_pvts(self.get_seq(),
                                              self.iccid_lst_int, sessionID,
                                              t_time)
        self.socket.send(U5)

    def send_illegalShake(self, t_time, sessionID):
        U4 = self.composer.compose_illegalShake(self.get_seq(),
                                                self.iccid_lst_int, sessionID,
                                                t_time)
        self.socket.send(U4)

    def send_powerLow(self, t_time, sessionID):
        U4 = self.composer.compose_powerLow(self.get_seq(), self.iccid_lst_int,
                                            sessionID, t_time)
        self.socket.send(U4)

    def send_fullEnerge(self, t_time, sessionID):
        U4 = self.composer.compose_fullEnerge(self.get_seq(),
                                              self.iccid_lst_int, sessionID,
                                              t_time)
        self.socket.send(U4)

    def send_emergency(self, t_time, sessionID):
        U4 = self.composer.compose_emergency(self.get_seq(),
                                             self.iccid_lst_int, sessionID,
                                             t_time)
        self.socket.send(U4)

    def send_status(self, t_time, sessionID):
        U4 = self.composer.compose_status(self.get_seq(), self.iccid_lst_int,
                                          sessionID, t_time)
        self.socket.send(U4)

    def send_move(self, t_time, sessionID):
        U5 = self.composer.compose_move(self.get_seq(), self.iccid_lst_int,
                                        sessionID, t_time)
        self.socket.send(U5)

    def send_stopcharging(self, t_time, sessionID):
        U5 = self.composer.compose_stopcharging(self.get_seq(),
                                                self.iccid_lst_int, sessionID,
                                                t_time)
        self.socket.send(U5)

    def send_charging(self, t_time, sessionID):
        U5 = self.composer.compose_charging(self.get_seq(), self.iccid_lst_int,
                                            sessionID, t_time)
        self.socket.send(U5)

    def send_overspeed(self, t_time, sessionID):
        U5 = self.composer.compose_overspeed(self.get_seq(),
                                             self.iccid_lst_int, sessionID,
                                             t_time)
        self.socket.send(U5)

    def send_enterPOI(self, t_time, sessionID):
        U5 = self.composer.compose_enterPOI(self.get_seq(), self.iccid_lst_int,
                                            sessionID, t_time)
        self.socket.send(U5)

    def send_leavePOI(self, t_time, sessionID):
        U5 = self.composer.compose_leavePOI(self.get_seq(), self.iccid_lst_int,
                                            sessionID, t_time)
        self.socket.send(U5)

    def send_enterRegion(self, t_time, sessionID):
        U5 = self.composer.compose_enterRegion(self.get_seq(),
                                               self.iccid_lst_int, sessionID,
                                               t_time)
        self.socket.send(U5)

    def send_leaveRegion(self, t_time, sessionID):
        U5 = self.composer.compose_leaveRegion(self.get_seq(),
                                               self.iccid_lst_int, sessionID,
                                               t_time)
        self.socket.send(U5)

    def send_stop(self, t_time, sessionID):
        U5 = self.composer.compose_stop(self.get_seq(), self.iccid_lst_int,
                                        sessionID, t_time)
        self.socket.send(U5)

    def send_u6(self, sessionID, t_time):
        U6 = self.composer.compose_u6(self.get_seq(), self.iccid_lst_int,
                                      sessionID, t_time)
        self.socket.send(U6)

    def send_check(self, sessionID):
        U8 = self.composer.compose_u8(self.get_seq(), self.iccid_lst_int,
                                      sessionID, int(time.time()))
        self.socket.send(U8)

    def multi_u6(self, iccid):
        while True:
            self.send_u6(int(self.res['sessionID']), int(time.time()))
            time.sleep(30)

    def login(self, iccid, isFirst=True):
        self.send_login()
        self.first_login = isFirst

    def simulator_multi_pvts(self, iccid):
        while True:
            for i in range(30):
                self.send_multi_pvts(int(time.time()),
                                     int(self.res['sessionID']))
                self.logging.info("%s, multi_pvt send success!", iccid)
                time.sleep(10)
            self.seq += 1
            self.send_stop(int(self.res['sessionID']), int(time.time()))
            self.logging.info("%s, stop send success!", iccid)
            time.sleep(15)

    def tcpClient(self, iccid):
        try:
            self.login(iccid)
            parser = Parser()
            while True:
                dat = self.socket.recv(1024)
                if dat:
                    bp = parser.parse_base(dat)
                    self.handle_recv(bp, iccid, dat)
        except Exception as e:
            self.logging.error("What's wrong, reconnect it.")

    def handle_recv(self, bp, iccid, dat):
        parser = Parser()
        p_type = bp.header.cmd_type + bp.header.cmd_num
        success = bp.header['success']
        if int(success) == 0:
            if p_type == "D1":
                lp = parser.parse_login(dat)
                self.login_status = lp.data['login_status']
                if int(self.login_status) == 0:
                    self.res['sessionID'] = int(lp.data['sessionID'])
                    options.thread_lock.acquire()
                    options.tracker[
                        self.sn]['sessionID'] = self.res['sessionID']
                    options.thread_lock.release()
                    self.logging.info("%s, login send success!", iccid)
                    self.send_config()
                    if self.first_login:
                        threading.Thread(target=self.send_heartbat).start()

            elif p_type == 'D3':
                lp = parser.parse_config(dat)
                print(lp.data)
        else:
            if p_type != "D1":
                self.logging.info("%s, status is offline!", iccid)
                self.login(iccid, False)
            else:
                self.logging.info("%s, login failed!", iccid)