Example #1
0
class ServerCom:
  def __init__(self):
    self.username = "******"
    self.password = "******"
    print "Connection to server"
    self.moogle = Moogle()
    self.moogle.set_logging_on(True)
    self.user = self.moogle.connect(self.username,self.password,"<server address>")
    if self.user is None:
      print "login error"
      return
    
    print('connected to mooogle with user: {} with id: {}'.format(self.user.email, self.user.id))
    
    
  def uploadData(self, animaldata):
    #animals = []
    farm = self.makeFarm()
    herd = self.makeHerd(farm)
    category = self.makeCatagory()
    algorithm = self.makeAlgorithm(category)
    print(str(farm))
    for a in animaldata:
      animal = self.moogle.create_animal(farm, a.eid)
      print(str(animal))
      #result = self.moogle.set_animal_herd(animal,herd)
      for m in a.measurements:
        measurement = self.moogle.create_measurement(animal, algorithm, self.user, m.timestamp.strftime("%c"), m.values[0], m.values[1], m.values[2])
     # animals.push(animal)
    
    
  def makeFarm(self):
    farm = self.moogle.create_farm("Demo Farm")    
    print('returnd farm: {} with id: {}'.format(farm.name, farm.id))
    return farm
  
  def makeHerd(self,farm):
    herd = self.moogle.create_herd(farm,"Demo Herd 2")
    print('returned herd: {} with id: {}'.format(herd.name, herd.id))
    #herds = self.moogle.get_herds(farm)    
    #if len(herds) <= 1:
      #herd = herds[0]
      #print('found herd: {} with id: {}'.format(herd.name, herd.id))
    #else:
      #herd = self.moogle.create_herd(farm, "Demo Herd 2")
      #print('returned herd: {} with id: {}'.format(herd.name, herd.id))
      
    return herd
  
  def makeCatagory(self):
    category = self.moogle.create_measurement_category('Demo Category')
    print('returnd measurement category: {} with id: {}'.format(category.name, category.id))
    return category
  
  def makeAlgorithm(self,category):
    algorithm = self.moogle.create_algorithm('Demo Algorithm', category)
    print('returnd algorithm: {} with id: {}'.format(algorithm.name, algorithm.id))
    return algorithm
Example #2
0
    def __init__(self):
     
        self.api = Moogle()

        url_address = sys.argv[1]

        self.api.connect(self.TEST_EMAIL, self.TEST_PASSWORD, url_address)
Example #3
0
 def __init__(self):
   self.username = "******"
   self.password = "******"
   print "Connection to server"
   self.moogle = Moogle()
   self.moogle.set_logging_on(True)
   self.user = self.moogle.connect(self.username,self.password,"<server address>")
   if self.user is None:
     print "login error"
     return
   
   print('connected to mooogle with user: {} with id: {}'.format(self.user.email, self.user.id))
Example #4
0
    def setUp(self):
        print('TestMoogle.setUp')
        super(self.__class__, self).setUp()

        self.moogle = Moogle()

        self.moogle.set_logging_on(True)

        self.user = self.moogle.connect(self.testUser, self.testPwd, self.serverIp)

        if self.user is None:
            self.fail()

        print('connected to mooogle with user: {} with id: {}'.format(self.user.email, self.user.id))
Example #5
0
class Main:
  
    FARMS_ENDPOINT      = 'farms'
    CAT_ENDPOINT        = 'measurement-categories'
    PADDOCK_ENDPOINT    = 'spatial/paddock/'
    READING_ENDPOINT    = 'spatial/reading'
    WEATHER_ENDPOINT    = 'spatial/weather'
    
    
    FARMS_PARAMS        = 'name=Demo%20Farm'
    CAT_PARAMS          = 'include=algorithms'

    TEST_EMAIL          = "<admin account email>"
    TEST_PASSWORD       = "******"

    PASTURE_CAT         = "Pasture Length"      # pasture measurement category
    WEATHER_CAT         = "Weather"             # weather measurement category
    
    CATEGORY_ALGO       = "Demo Algorithm"      # algorithm for weather and pasture measurement
    
    WEATHER_STATION_LOC =  [0, 0]

    def __init__(self):
     
        self.api = Moogle()

        url_address = sys.argv[1]

        self.api.connect(self.TEST_EMAIL, self.TEST_PASSWORD, url_address)
        
    ## Creates 4 example paddocks, each containing 100 pasture readings.
    def create_samples(self):  
          
        # Create weather measurements
        self.create_weather_measurements()
        
        # Get the id from the demo farm.
        demo_farm_id = self.get_farm_id()

        # Get factories
        p_factory = PaddockFactory()
        r_factory = ReadingFactory()

        algo_id = self.get_algorithm_id(self.PASTURE_CAT, self.CATEGORY_ALGO)

        # For loop used to create sample paddocks.
        for p_id in range(0, 4):

	        # Create an upload and update time.
            create_time = datetime.datetime.now()
            update_time = datetime.datetime.now() + datetime.timedelta(hours=1)

	        # Create a sample paddock.
            paddock = p_factory.build_demo_paddock(  
	            demo_farm_id,
                p_id,
                create_time, 
	            create_time if p_id < 2 else update_time)
            
            paddock.set_id(p_id)
 
            # Call API.
            paddock_res = self.api._Moogle__api_call('post', self.PADDOCK_ENDPOINT, paddock.to_dict(), None)
            
            # :Add the returned object id to the current paddock object.
            paddock_json = json.loads(paddock_res.text)
            paddock.set_oid(paddock_json['data']['_id'])

            # For loop used to create sample readings for the current paddock.
            for r_id in range(0, 100):

                # Create an upload and update time.
                create_time = datetime.datetime.now()
                update_time = datetime.datetime.now() + datetime.timedelta(hours=1)

                # Create a sample pasture reading.
                reading = r_factory.build_demo_reading( 
                  p_id,
                  paddock.oid,
                  -1,
                  algo_id,
                  create_time,
                  create_time if r_id % 4 != 0 else update_time)

                # Call API.
                self.api._Moogle__api_call('post', self.READING_ENDPOINT, reading.to_dict(), None)

    def create_weather_measurements(self):

        demo_farm_id = self.get_farm_id()
        algo_id = self.get_algorithm_id(self.WEATHER_CAT, self.CATEGORY_ALGO)
        if algo_id == -1:
            return False
        
        w_factory = WeatherFactory()
        
        end_date = datetime.datetime.now()
        start_date = end_date - datetime.timedelta(days=1)

        for time_stamp in rrule.rrule(rrule.MINUTELY, dtstart=start_date, until=end_date):
            w = w_factory.build_demo_weather(demo_farm_id, time_stamp, self.WEATHER_STATION_LOC, algo_id)
            res = self.api._Moogle__api_call('post', self.WEATHER_ENDPOINT, w.to_dict(), None)

    # Returns the demo farm primary key.
    def get_farm_id(self):
        farms = self.api._Moogle__api_call('get', self.FARMS_ENDPOINT, None, self.FARMS_PARAMS)
        farms = json.loads(farms.text)
        demo_farm_id = -1

        for farm in farms['farms']:
            if farm['name'] == 'Demo Farm':
                demo_farm_id = farm['id']

        return demo_farm_id

    # Returns the alogrithm id used for demo data 
    def get_algorithm_id(self, category, algorithm):

        algo_id = -1

        categories = self.api._Moogle__api_call('get', self.CAT_ENDPOINT, None, self.CAT_PARAMS)
        categories = json.loads(categories.text)
        
        for c in categories['categories']:
            if c['name'] == category:
                for a in c['algorithms']:                    
                    if a['name'] == algorithm: 
                        algo_id = a['id']
            
        return algo_id
Example #6
0
class Main:
   
    NUM_PASTURE_READINGS = 25

    FARMS_ENDPOINT       = 'farms'
    PADDOCK_ENDPOINT     = 'spatial/paddock'
    READING_ENDPOINT     = 'spatial/reading'

    FARMS_PARAMS         = 'name=Demo%20Farm' # Replace Demo Farm if using a farm with a different name.
    PADDOCKS_PARAMS      = 'farm_id='

    TEST_EMAIL           = "<admin account name>"
    TEST_PASSWORD        = "******"

    def __init__(self):

        self.api = Moogle()
        self.api.connect(self.TEST_EMAIL, self.TEST_PASSWORD)

    def add_readings(self):

        # Get demo farm id
        demo_farm_id = self.get_farm_id()

        # Get the user id from the logged in user
        res = self.api._Moogle__api_call('get', 'auth', None, None)
        user = json.loads(res.text)
        user_id = user['user']['id']

        # Get pasture reading factory
        r_factory = ReadingFactory()

        # Get timestamp 
        timestamp = datetime.datetime.now()

        # Get each paddock that belongs to the demo farm
        paddock_params = self.PADDOCKS_PARAMS + str(demo_farm_id)
        response = self.api._Moogle__api_call('get', self.PADDOCK_ENDPOINT, None, paddock_params)
        paddocks = json.loads(response.text)
        paddocks = paddocks['paddocks']
       
        # Iterate through each paddock in the farm
        for paddock in paddocks:

            paddock_oid = paddock['_id']
            paddock_id  = paddock['id']

            # Create a pasture reading for each farm.
            for i in range(0, self.NUM_PASTURE_READINGS):

                # Create a sample pasture reading.
                reading = r_factory.build_demo_reading( 
                  paddock_id,
                  paddock_oid,
                  user_id,
                  timestamp,
                  timestamp)


                # Call API.
                self.api._Moogle__api_call('post', self.READING_ENDPOINT, reading.to_dict(), None)

    ## Returns the demo farm primary key.
    def get_farm_id(self):
        farms = self.api._Moogle__api_call('get', self.FARMS_ENDPOINT, None, self.FARMS_PARAMS)
        farms = json.loads(farms.text)
        return farms['farms'][0]['id']
Example #7
0
    def __init__(self):

        self.api = Moogle()
        self.api.connect(self.TEST_EMAIL, self.TEST_PASSWORD)
Example #8
0
class TestMoogle(unittest.TestCase):

    testUser = "******"
    testPwd = "test"
    serverIp = "https://moogle-test.elec.ac.nz/api/"


    def setUp(self):
        print('TestMoogle.setUp')
        super(self.__class__, self).setUp()

        self.moogle = Moogle()

        self.moogle.set_logging_on(True)

        self.user = self.moogle.connect(self.testUser, self.testPwd, self.serverIp)

        if self.user is None:
            self.fail()

        print('connected to mooogle with user: {} with id: {}'.format(self.user.email, self.user.id))


    def test_farms(self):

        test_farm = self.moogle.create_farm("Python Test Farm")

        if test_farm is None:
            self.fail()

        print('created farm: {} with id: {}'.format(test_farm.name, test_farm.id))

        farms = self.moogle.get_farms()

        if farms is None:
            self.fail()

        for farm in farms:
            print('found farm: {} with id: {}'.format(farm.name, farm.id))

        single_query_farm = self.moogle.get_farm(test_farm.id)

        if single_query_farm.id != test_farm.id:
            self.fail()

        farms = self.moogle.get_farms(self.user)

        if farms is None:
            self.fail()

        for farm in farms:
            print('The current user can access farm: {}'.format(farm.name))

        deleted = self.moogle.remove_farm(test_farm)

        if not deleted:
            self.fail()


    def test_roles(self):

        roles = self.moogle.get_roles()

        if roles is None:
            self.fail()

        for role in roles:
            print('Found role named {}'.format(role.name))


    def test_users(self):

        roles = self.moogle.get_roles()

        admin_role = None

        for role in roles:
            if role.name == "Viewer":
                admin_role = role
                break

        test_user = self.moogle.create_user("Mark", "Butler", "*****@*****.**", "trapezoid", admin_role)

        if test_user is None:
            self.fail()

        print('created user: {} with id: {}'.format(test_user.email, test_user.id))

        users = self.moogle.get_users()

        if users is None:
            self.fail()

        for user in users:
            print('found user: {} {} with email: {}'.format(user.first_name, user.last_name, user.email))

        deleted = self.moogle.remove_user(test_user)

        if not deleted:
            self.fail()


    def test_measurement_categories(self):

        test_category = self.moogle.create_measurement_category('Test Category')

        if test_category is None:
            self.fail()

        print('created measurement category: {} with id: {}'.format(test_category.name, test_category.id))

        single_query_category = self.moogle.get_measurement_category(test_category.id)

        if test_category.id != single_query_category.id:
            self.fail()

        categories = self.moogle.get_measurement_categories()

        if categories is None:
            self.fail()

        for category in categories:
            print('found category: {}'.format(category.name))

        deleted = self.moogle.remove_measurement_category(test_category)

        if not deleted:
            self.fail()


    def test_algorithms(self):

        test_category = self.moogle.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.moogle.create_algorithm('Test Algorithm', test_category)

        if test_algorithm is None:
            self.fail()

        print('created algorithm {} with id: {}'.format(test_algorithm.name, test_algorithm.id))

        single_query_algorithm = self.moogle.get_algorithm(test_algorithm.id)

        if test_algorithm.id != single_query_algorithm.id:
            self.fail()

        algorithms = self.moogle.get_algorithms()

        if algorithms is None:
            self.fail()

        for algorithm in algorithms:
            print('found algorithm: {}'.format(algorithm.name))

        deleted = self.moogle.remove_algorithm(test_algorithm)

        if not deleted:
            self.fail()

        self.moogle.remove_measurement_category(test_category)


    def test_animals(self):

        test_farm = self.moogle.create_farm('Animal Test Farm')
        test_herd = self.moogle.create_herd(test_farm, 'Animal Test Herd')

        test_eid = "AN-EID-FOR_TESTING"

        test_animal = self.moogle.create_animal(test_farm, test_eid)

        if test_animal is None:
            self.fail()

        print('created animal {} with id: {}'.format(test_animal.eid, test_animal.id))

        result = self.moogle.set_animal_herd(test_animal, test_herd)

        if result is None:
            self.fail()

        animals = self.moogle.get_animals(test_farm, test_herd)

        if animals is None:
            self.fail()

        for animal in animals:
            print('found animal: {}'.format(animal.eid))

        updated = self.moogle.update_animal_vid(test_animal, "My Pet Cow")

        if not updated:
            self.fail()

        expected_animal = self.moogle.get_animal_by_eid(test_farm, test_eid)

        if expected_animal.id != test_animal.id:
            self.fail()

        deleted = self.moogle.remove_animal(test_animal)

        if not deleted:
            self.fail()

        self.moogle.remove_herd(test_herd)
        self.moogle.remove_farm(test_farm)


    def test_measurements(self):

        test_farm = self.moogle.create_farm('Animal Test Farm')
        test_eid = "AN-EID-FOR_TESTING"
        test_animal = self.moogle.create_animal(test_farm, test_eid)
        test_category = self.moogle.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.moogle.create_algorithm('Test Algorithm', test_category)

        measurement = self.moogle.create_measurement(test_animal, test_algorithm, self.user, time.strftime("%c"), 0.3344)

        if measurement is None:
            self.fail()

        print('created measurement with id {}'.format(measurement.id))

        animal_measurements = self.moogle.get_measurements_for_animal(test_animal)

        if animal_measurements[0].id != measurement.id:
            self.fail()

        deleted = self.moogle.remove_measurement(measurement)

        if not deleted:
            self.fail()

        eid_measurement = self.moogle.create_measurement_for_eid(test_eid, test_farm, test_algorithm, self.user, time.strftime("%c"), 0.3344)

        if eid_measurement is None:
            self.fail()

        if eid_measurement.animal_id != test_animal.id:
            self.fail()

        self.moogle.remove_measurement(eid_measurement)

        self.moogle.remove_animal(test_animal)
        self.moogle.remove_farm(test_farm)
        self.moogle.remove_algorithm(test_algorithm)
        self.moogle.remove_measurement_category(test_category)


    def test_measurements_bulk_upload(self):

        test_farm = self.moogle.create_farm('Animal Test Farm')
        test_eid = "AN-EID-FOR_TESTING"
        test_animal = self.moogle.create_animal(test_farm, test_eid)
        test_category = self.moogle.create_measurement_category('Algorithm Test Category')
        test_algorithm = self.moogle.create_algorithm('Test Algorithm', test_category)

        measurement_list = self.moogle.create_bulk_measurement_upload_list(test_animal, test_algorithm, self.user)

        measurement_list.add_measurement(time.strftime("%c"), 0.3344)
        measurement_list.add_measurement(time.strftime("%c"), 0.4455)
        measurement_list.add_measurement(time.strftime("%c"), 0.5566)

        success = self.moogle.upload_measurement_list(measurement_list)

        if success is not True:
            self.fail()

        print('created bulk measurements')

        animal_measurements = self.moogle.get_measurements_for_animal(test_animal)

        if len(animal_measurements) != 3:
            self.fail()

        self.moogle.remove_animal(test_animal)
        self.moogle.remove_farm(test_farm)
        self.moogle.remove_algorithm(test_algorithm)
        self.moogle.remove_measurement_category(test_category)