-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueMapper.lua
115 lines (100 loc) · 2.75 KB
/
ValueMapper.lua
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---@class ValueMapper
ValueMapper = {}
---@param direction number The direction
---@return string The enumified value
function ValueMapper.mapDirection(direction)
if direction == 1 then
return "FORWARD"
elseif direction == -1 then
return "BACKWARD"
else
return "STOPPED"
end
end
---@param state number The ignition state
---@return string The enumified value
function ValueMapper.mapMotorState(state)
if state == 1 then
return "OFF"
elseif state == 2 then
return "STARTING"
elseif state == 3 or state == 4 then
return "ON"
else
return string.format("<unknown(%d))>", state)
end
end
---@param load number 0..1
---@return string The load in percent as float
function ValueMapper.mapMotorLoad(load)
if load < 0 then
return "0"
else
return ValueMapper.mapPercentage(load, 2)
end
end
---@param value number 0..1
---@param decimals number How much decimals it should return, defaults to 2
---@return string The formated value as percentage
function ValueMapper.mapPercentage(value, decimals)
if not decimals or decimals < 0 then
decimals = 2
end
local percentage = value * 100
return string.format("%." .. tostring(decimals) .. "f", percentage)
end
---@param operatingTime number operation time in milliseconds
function ValueMapper.formatOperatingTime(operatingTime)
-- Convert milliseconds to minutes
local totalMinutes = operatingTime / 60000
-- Calculate full hours
local hours = math.floor(totalMinutes / 60)
-- Calculate remaining minutes as a decimal rounded to two decimals
local remainingMinutes = (totalMinutes - (hours * 60)) / 60
local remainingMinutesString = string.format("%.2f", remainingMinutes)
-- Combine hours and remaining minutes as a string
local formattedTime = string.format("%d.%s", hours, string.sub(remainingMinutesString, 3))
return formattedTime
end
---@param currentPeriod number
---@return number
function ValueMapper.mapPeriodToMonth(currentPeriod)
local adapted = (currentPeriod + 2) % 12
if adapted == 0 then
return 12
else
return adapted
end
end
---@param vehicle Vehicle
---@return number
function ValueMapper.calculateHeading(vehicle)
local dx, _, dz = localDirectionToWorld(vehicle.rootNode, 0, 0, 1)
local yRot = MathUtil.getYRotationFromDirection(dx, dz)
if yRot < 0 then
yRot = yRot + math.pi * 2
end
return 360 - math.deg(yRot)
end
---@param state number
---@return string
function ValueMapper.mapPipeState(state)
if state == 1 then
return "RETRACTED"
elseif state == 0 then
return "MOVING"
else
return "EXTENDED"
end
end
---@param state number
---@return string
function ValueMapper.mapCoverState(state)
if state == 1 then
return "OPEN"
elseif state == 0 then
return "CLOSED"
else
return "UNKNOWN"
end
end