This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
leaf2mqtt.dart
100 lines (83 loc) · 3.39 KB
/
leaf2mqtt.dart
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
96
97
98
99
100
import 'package:dartnissanconnect/dartnissanconnect.dart';
import 'package:intl/intl.dart';
import 'package:args/args.dart';
import 'dart:convert';
import 'dart:io';
ArgResults argResults;
void main(List<String> arguments) {
var parser = ArgParser();
parser.addOption('username');
parser.addOption('password');
parser.addOption('mqtthost');
parser.addOption('mqtttopic');
parser.addOption('mqttuser');
parser.addOption('mqttpass');
var results = parser.parse(arguments);
NissanConnectSession session = new NissanConnectSession(debug: false);
var host = results['mqtthost'];
var topic = results['mqtttopic'];
var mosuser = results['mqttuser'];
var mospass = results['mqttpass'];
var mosopt = '-r -h ${host}';
var retdict = {};
var batdict = {};
var clmdict = {};
var locdict = {};
var sttdict = {};
if (mosuser != null) {
mosopt = mosopt + ' -u "${mosuser}"';
}
if (mospass != null) {
mosopt = mosopt + ' -P "${mospass}"';
}
session
.login(username: results['username'], password: results['password'])
.then((vehicle) {
vehicle.requestBatteryStatusRefresh();
vehicle.requestClimateControlStatusRefresh();
vehicle.requestLocationRefresh();
var vin = vehicle.vin;
print('#!/bin/bash');
print('echo "${vin}" > vin.txt');
vehicle.requestMonthlyStatistics(month: DateTime.now()).then((stats) {
sttdict['tripsNumber'] = stats.tripsNumber;
sttdict['milesperkWh'] = stats.milesPerKWh;
sttdict['kWhconsumed'] = stats.kWhUsed;
sttdict['kWhregen'] = stats.kWhGained;
sttdict['distance'] = stats.travelDistanceMiles;
sttdict['avspeed'] = stats.travelSpeedAverageMph;
sttdict['traveltime'] = stats.travelTime.toString();
sttdict.forEach((k, v) => print('mosquitto_pub ${mosopt} -t "${topic}/${vin}/${k}" -m "${v}"'));
})
.catchError((e) {
String ts = DateTime.now().toIso8601String();
String en = e.toString().split(":")[0];
print('mosquitto_pub ${mosopt} -t "${topic}/${vin}/errortime" -m "${ts}"');
print('mosquitto_pub ${mosopt} -t "${topic}/${vin}/error" -m "${en}"');
});
vehicle.requestBatteryStatus().then((battery) {
batdict['battpct'] = battery.batteryPercentage;
batdict['connected'] = battery.isConnected;
batdict['charging'] = battery.isCharging;
batdict['GOM'] = battery.cruisingRangeAcOnMiles;
batdict['time3kW'] = battery.timeToFullNormal;
batdict['time6kW'] = battery.timeToFullFast;
batdict.forEach((k, v) => print('mosquitto_pub ${mosopt} -t "${topic}/${vin}/${k}" -m "${v}"'));
});
vehicle.requestClimateControlStatus().then((climate) {
clmdict['cabintemp'] = climate.cabinTemperature;
clmdict['hvacrunning'] = climate.isRunning;
clmdict.forEach((k, v) => print('mosquitto_pub ${mosopt} -t "${topic}/${vin}/${k}" -m "${v}"'));
});
vehicle.requestLocation().then((location) {
locdict['latitude'] = location.latitude;
locdict['longitude'] = location.longitude;
locdict.forEach((k, v) => print('mosquitto_pub ${mosopt} -t "${topic}/${vin}/${k}" -m "${v}"'));
});
retdict['nickname'] = vehicle.nickname;
retdict['modelname'] = vehicle.modelName;
retdict['vin'] = vehicle.vin;
retdict['updated'] = DateTime.now().toUtc().toIso8601String();
retdict.forEach((k, v) => print('mosquitto_pub ${mosopt} -t "${topic}/${vin}/${k}" -m "${v}"'));
});
}