Esempio n. 1
0
 def stock_check_multi(self, dev_id, pin):
     bc = AgentNotifier()
     devs = []
     _1 = dev_id.split(',')
     ops = []
     for d in _1:
         d2 = self.c_dev.get(bc.dbconn, bc.cacheconn, d)
         if d2:
             devs.append({
                     'device_id': d,
                     'opid': d2['operator_id'],
                     'pin': d2['pin'],
                 })
         if d2['operator_id'] not in ops:
             ops.append(d2['operator_id'])
     if len(devs) == 0:
         return json.dumps({'success': 0, 'reason': 'DEVICE NOT FOUND'})
     devs_sql = map(lambda x: '"{0}"'.format(x['device_id']), devs)
     c = bc.dbconn.cursor(MySQLdb.cursors.DictCursor)
     dcs = {}
     c.execute('''SELECT `device_id`, `check_stock_counter` from `devices` WHERE
                 `device_id` IN ({0})'''.format(','.join(devs_sql)))
     for r in c:
         dcs[r['device_id']] = r['check_stock_counter']
     c.execute('''SELECT `operator_id`,`stock_check_1`,`stock_check_2` from `operator`
                  WHERE `operator_id` IN ({0})'''.format(','.join(map(lambda x: str(x), ops))))
     ops = {}
     for r in c:
         ops[r['operator_id']] = (r['stock_check_1'], r['stock_check_2'])
     for r in devs:
         server_id, port = r['device_id'].split('.')
         leaf_server = '{0}@{2}/{1}'.format(config.LEAFSERVER, server_id,
                                            config.MSG_SERVER)
         sc1, sc2 = ops[r['opid']]
         parse = multipleReplace({'<pin>': r['pin'],}, sc1)
         msg = '{0},{1},{2}'.format(port, '0', parse)
         bc.sendMessage(leaf_server, 'JBDV', msg, commit=False)
         if len(sc2) > 0:
             parse = multipleReplace({'<pin>': r['pin'],}, sc2)
             msg = '{0},{1},{2}'.format(port, '0', parse)
             bc.sendMessage(leaf_server, 'JBDV', msg, commit=False)
     bc.dbconn.commit()
     to = datetime.now() + timedelta(seconds=59)
     tout = True
     while datetime.now() < to:
         time.sleep(0.5)
         c.execute('''SELECT `device_id`, `check_stock_counter` from `devices` WHERE
                     `device_id` IN ({0})'''.format(','.join(devs_sql)))
         for r in c:
             if dcs[r['device_id']] != r['check_stock_counter']:
                 devs_sql.remove('"{0}"'.format(r['device_id']))
                 dcs[r['device_id']] = r['check_stock_counter']
         if len(devs_sql) == 0:
             tout = False
             break
     if tout:
         return json.dumps({'success': 0, 'reason': 'TIMEOUT'})
     return json.dumps({'success': 1, 'reason': '', 'notifyid': dcs,})
Esempio n. 2
0
 def _getMessage(self, msg, tosub={}):
     tmp = self.c_message.sGet(msg)['parse']
     tosub = dict(zip(map(lambda x: '<{0}>'.format(x),tosub.keys()),
       map(str,tosub.values())))
     if tosub == {}:
         return tmp
     return multipleReplace(tosub, tmp)
Esempio n. 3
0
 def _sendToLeaf(self, tran_id, dev_id, parse, dest):
     pin = self.c_device.sGet(dev_id)["pin"]
     server_id, port = dev_id.split(".")
     leaf_server = "{0}@{2}/{1}".format(config.LEAFSERVER, server_id, config.MSG_SERVER)
     parse = multipleReplace({"<dest>": dest, "<pin>": pin}, parse)
     # msg = '{0},{1},{2},{3},{4}'.format(port, tran_id, parse, dest, pin)
     msg = "{0}.{1},{2},{3}".format(server_id, port, tran_id, parse)
     self.sendMessage(leaf_server, "JBDV", msg, commit=False)
Esempio n. 4
0
 def buildMessage(self, msg, tosub={}):
     '''Build templated messages from table `message` replacing <param>
        in template with words in "tosub" dictionary
     '''
     tmp = self._c_msg.get(self.dbconn, self.cacheconn, msg)
     if not tmp:
         raise RegexNotFound
     tosub = dict(zip(map(lambda x: '<{0}>'.format(x),tosub.keys()),
                  map(str,tosub.values())))
     if isinstance(tosub, dict) and len(tosub) > 0:
         param = multipleReplace(tosub, tmp['parse'])
     else:
         param = tmp['parse']
     return param
Esempio n. 5
0
 def stock_check(self, dev_id):
     dev = self.c_dev.get(self.dbconn, self.cacheconn, dev_id)
     if not dev:
         return False
     cursor = self.dbconn.cursor(MySQLdb.cursors.DictCursor)
     cursor.execute('''SELECT `check_stock_counter` from `devices` WHERE
         `device_id`=%s LIMIT 1''', (dev_id,))
     old_count = int(cursor.fetchone()['check_stock_counter'])
     cursor.execute('''SELECT `stock_check_1`,`stock_check_2` from `operator`
     WHERE `operator_id`=%s''', (dev['operator_id'],))
     r = cursor.fetchone()
     server_id, port = dev_id.split('.')
     leaf_server = '{0}@{2}/{1}'.format(config.LEAFSERVER, server_id,
         config.MSG_SERVER)
     parse = multipleReplace({'<pin>': dev['pin'],}, r['stock_check_1'])
     msg = '{0},{1},{2}'.format(port, '0', parse)
     self.sendMessage(leaf_server, 'JBDV', msg, commit=False)
     if len(r['stock_check_2']) > 0:
         parse = multipleReplace({'<pin>': dev['pin'],}, r['stock_check_2'])
         msg = '{0},{1},{2}'.format(port, '0', parse)
         self.sendMessage(leaf_server, 'JBDV', msg, commit=False)
     self.dbconn.commit()
     cursor.close()
     return True
Esempio n. 6
0
 def stock_check(self, dev_id, pin):
     bc = AgentNotifier()
     dev = self.c_dev.get(bc.dbconn, bc.cacheconn, dev_id)
     if not dev:
         return json.dumps({'success': 0, 'reason': 'DEVICE NOT FOUND'})
     c = bc.dbconn.cursor(MySQLdb.cursors.DictCursor)
     c.execute('''SELECT `check_stock_counter` from `devices` WHERE
                 `device_id`=%s LIMIT 1''', (dev_id,))
     old_count = int(c.fetchone()['check_stock_counter'])
     c.execute('''SELECT `stock_check_1`,`stock_check_2` from `operator`
                  WHERE `operator_id`=%s''', (dev['operator_id'],))
     r = c.fetchone()
     server_id, port = dev_id.split('.')
     leaf_server = '{0}@{2}/{1}'.format(config.LEAFSERVER, server_id,
                                        config.MSG_SERVER)
     parse = multipleReplace({'<pin>': dev['pin'],}, r['stock_check_1'])
     msg = '{0},{1},{2}'.format(port, '0', parse)
     bc.sendMessage(leaf_server, 'JBDV', msg, commit=False)
     if len(r['stock_check_2']) > 0:
         parse = multipleReplace({'<pin>': dev['pin'],}, r['stock_check_2'])
         msg = '{0}.{1},{2},{3}'.format(server_id, port, '0', parse)
         bc.sendMessage(leaf_server, 'JBDV', msg, commit=False)
     bc.dbconn.commit()
     to = datetime.now() + timedelta(seconds=59)
     tout = True
     while datetime.now() < to:
         time.sleep(0.5)
         c.execute('''SELECT `check_stock_counter` from `devices` WHERE
                      `device_id`=%s LIMIT 1''', (dev_id,))
         new_count = int(c.fetchone()['check_stock_counter'])
         if old_count < new_count:
             tout = False
             break
     if tout:
         return json.dumps({'success': 0, 'reason': 'TIMEOUT'})
     return json.dumps({'success': 1, 'reason': '', 'notifyid': new_count,})