Repository navigation
Expand file tree
/
Copy pathReadingScale.java
More file actions
95 lines (76 loc) · 2.88 KB
/
Copy pathReadingScale.java
File metadata and controls
95 lines (76 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.jasci.printnode.api;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class ReadingScale {
static Logger log = LoggerFactory.getLogger(ReadingScale.class.getName());
/**
* @Description: Main method
* @param str
* @throws IOException
*/
public static void main(String args[]) throws Exception{
log.debug("Inside main method" + args.toString() );
getWeight(39686);
}
/**
* @Description: Get weight from scale
* @param str
* @return
* @return
* @throws Exception
* @throws IOException
*/
public static double getWeight(int computerId) throws Exception{
HashMap<String, BigInteger> measurement= null;
BigInteger measurementValue = null;
double weight = 0;
double nineZeros = 1000000000;
log.debug("Inside getWeight: " + computerId);
try {
//Firstly, we'll create a new Auth object.
Auth myAuth = new Auth();
//We're going to set our credentials for future use with Auth.setApiKey.
myAuth.setApiKey("EnterYourAPIKEY");
log.debug("API key " + Arrays.toString(myAuth.getCredentials()));
//We then make an APIClient, which saves our authorization to use with requests.
APIClient aClient = new APIClient(myAuth);
log.debug("APIClient " + aClient.getWhoami());
//If we're going to get weight, we need a scale. We'll get one using Computer Id.
Scale myScales = null;
try {
myScales = aClient.getScales(computerId)[0];
} catch (Exception ObjectJasciException) {
ObjectJasciException.printStackTrace();
log.error("can't find scale: " + ObjectJasciException.getMessage());
ObjectJasciException.printStackTrace();
throw ObjectJasciException;
}
log.debug("SCALES : " + myScales.toString());
//get Weight only if a scale is found
if (myScales != null){
//Original value from scale as a Hash Map (key, value)
measurement = myScales.getMeasurement();
//Get value only from hash map as Big Integer
measurementValue = (BigInteger) measurement.values().toArray()[0];
//Convert measurementValue into Double
Double value = measurementValue.doubleValue();
//Divide value into 1000000000 to get decimal
weight = value/nineZeros;
System.out.println("MEASUREMENT: " + measurement);
System.out.println("MEASUREMENT VALUE: " + measurementValue);
System.out.println("WEIGHT: " + weight);
}
} catch (Exception ObjectJasciException) {
ObjectJasciException.printStackTrace();
log.error(".getWeightFromScale() throws exception: " + ObjectJasciException.getMessage());
throw ObjectJasciException;
}
return weight;
}
}