# RPi
RPi is a nodejs library that provides access to basic Raspberry Pi info and functionality.

## Installation
RPi is distributed via NPM:
```
npm install --save @p4d/rpi
```

## Usage
```javascript
// Include library
const RPi = require('@p4d/rpi')

// Access RPi data
console.log(RPi.Kronos.id)
console.log(RPi.Project.name)

// Exposed functionality
if (RPi.Cmd.exists('rpi-config')) {     // This is actually not required since RPi.Cmd.run already checks if the command exists.
  console.log(RPi.Cmd.run('rpi-config', ['hostname', 'PAPA4D']))
}
```
## Classes
RPi library exposes RPi data and functionality as static classes:

### Serial
+ Serial.id - gets the Serial Id for the RPi. Gets value from `/proc/cpuinfo`. Returns `0000000000000000` on non-RPi architectures.
+ Serial.exists - returns `true` if `/proc/cpuinfo` file exists.

### Kronos
+ Kronos.id - gets the Kronos Id for the RPi stored at `/etc/kronos`. Returns `null` if no id is assigned.
+ Kronos.exists - returns `true` if `/etc/kronos` file exists.
+ Kronos.update(newId) - Updates Kronos Id value to `newId`. `/etc/kronos` is updated.

### Hermes
+ Hermes.id - gets the Hermes URL for the RPi stored at `/etc/hermes`. Returns `null` if no url is assigned.
+ Hermes.exists - returns `true` if `/etc/hermes` file exists.
+ Hermes.update(newUrl) - Updates Hermes URL value to `newUrl`. `/etc/hermes` is updated.

### Cloud
+ Cloud.id - gets the Cloud URL for the RPi stored at `/etc/cloud`. Returns `null` if no url is assigned.
+ Cloud.exists - returns `true` if `/etc/cloud` file exists.
+ Cloud.update(newUrl) - Updates Cloud URL value to `newUrl`. `/etc/cloud` is updated.

### Project
+ Project.id - gets the Project Id for the RPi stored at `/etc/project`. Project Id is the snake case formated version of the Project Name. Returns `null` if no id is assigned.
+ Project.name - gets the Project Name for the RPi stored at `/etc/project`. Project name is the full string formated name for the current project. Returns `null` if no name is assigned.
+ Project.exists - returns `true` if `/etc/project` file exists.
+ Project.update(newName) - Updates Project Name value to `newName` and Project Id to `snake_cased_newName`. `/etc/project` is updated.

### Status
+ Status.ready() - Checks wether RPi is ready to send data packets to Hermes. Three conditions are needed: RPi must have assigned Project Id, Kronos Id and Hermes URL.
+ Serial.data() - Generates a data package with status information on the RPi. It does NOT check if RPi is ready to send the data to Hermes - Status.ready() needs to be checked before sending it.

The data package includes the following info:
```
serial - Serial id
kronosId - Kronos id
hermesUrl - Hermes url
cloudUrl - Cloud url
project: {
  name - Project name
  id - Project Id
}
hostname - OS Hostname
ip - IP Address
uptime - OS Uptime
disk: {
  free - Free space in bytes
  freeGB - Free space in GBytes
  freePer - Free space in percentile
  total - Total space in bytes
  totalGB - Total space in GBytes
}
ram: {
  free - Free RAM in bytes
  freeMB - Free RAM in MBytes
  freePer - Free RAM in percentile
  total - Total RAM in bytes
  totalMB - Total RAM in MBytes
}
platform: {
  os - OS Pretty Name for Rasbpian with version. For example `Raspbian GNU/Linux 8 (jessie)`
  version - OS version release identifier. For example `16.4.0` on macOS Sierra
  type - OS string identifier. For example `Linux` on Linux, `Darwin` on macOS, `Windows_NT` on Windows
}
```

### Cmd
+ Cmd.run(cmd, args) - Runs a command with arguments. Equivalent of doing `cmd args[0] args[1] ... args[n]`. `args` can be a string for single argument commands or an array with multiple arguments.
+ Cmd.exists(cmd) - Checks wether `cmd` command exists on the current environment.

### Temperature
+ Temperature.measure() - gets CPU and GPU temperature in Celsius.
+ Temperature.existsCPUTemp() - returns `true` if `/sys/class/thermal/thermal_zone0/temp` file exists.

### RAM
+ RAM.measure() - gets RAM usage statistics

### Disk
+ Disk.measure() - gets Disk usage statistics

### Network
+ Network.data() - gets Network information
