lockserver/zwavejs/messages-incoming.go
Finn 054008eb1f Initial commit
Connects to zwave-js, syncs all locks and codeslots with database, and records an event log. No support for updating code slots.
2024-04-08 21:25:36 -07:00

267 lines
9.6 KiB
Go

package zwavejs
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"github.com/sirupsen/logrus"
)
type IncomingMessage struct {
Type string `json:"type"`
MessageID string `json:"messageId"`
Success bool `json:"success"`
// Values for type = version
DriverVersion string `json:"driverVersion"`
ServerVersion string `json:"serverVersion"`
HomeID int `json:"homeId"`
Event *Event
Result *Result
}
type Event struct {
Source string `json:"source"`
Event string `json:"event"`
NodeID int `json:"nodeId"`
Args NodeEventArgs `json:"args"`
NotificationLabel string `json:"notificationLabel"`
Parameters EventParameters `json:"parameters"`
}
type NodeEventArgs struct {
CommandClassName string `json:"commandClassName"`
CommandClass int `json:"commandClass"`
Property string `json:"property"`
Endpoint int `json:"endpoint"`
NewValue NodePropertyValue `json:"newValue"`
PrevValue NodePropertyValue `json:"prevValue"`
PropertyName string `json:"propertyName"`
}
type EventParameters struct {
UserID int `json:"userId"`
}
type Result struct {
State struct {
Controller Controller
Driver Driver
Nodes []Node
}
}
type Controller struct {
Type int `json:"type"`
HomeID int64 `json:"homeId"`
OwnNodeID int `json:"ownNodeId"`
IsUsingHomeIDFromOtherNetwork bool `json:"isUsingHomeIdFromOtherNetwork"`
IsSISPresent bool `json:"isSISPresent"`
WasRealPrimary bool `json:"wasRealPrimary"`
ManufacturerID int `json:"manufacturerId"`
ProductType int `json:"productType"`
ProductID int `json:"productId"`
SupportedFunctionTypes []int `json:"supportedFunctionTypes"`
SucNodeID int `json:"sucNodeId"`
SupportsTimers bool `json:"supportsTimers"`
Statistics ControllerStatistics `json:"statistics"`
}
type ControllerStatistics struct {
MessagesTX int `json:"messagesTX"`
MessagesRX int `json:"messagesRX"`
MessagesDroppedRX int `json:"messagesDroppedRX"`
Nak int `json:"NAK"`
Can int `json:"CAN"`
TimeoutACK int `json:"timeoutACK"`
TimeoutResponse int `json:"timeoutResponse"`
TimeoutCallback int `json:"timeoutCallback"`
MessagesDroppedTX int `json:"messagesDroppedTX"`
InclusionState int `json:"inclusionState"`
IsSecondary bool `json:"isSecondary"`
IsStaticUpdateController bool `json:"isStaticUpdateController"`
IsSlave bool `json:"isSlave"`
IsHealNetworkActive bool `json:"isHealNetworkActive"`
LibraryVersion string `json:"libraryVersion"`
SerialAPIVersion string `json:"serialApiVersion"`
}
type Driver struct {
LogConfig DriverLogConfig
StatisticsEnabled bool
}
type DriverLogConfig struct {
Enabled bool `json:"enabled"`
Level int `json:"level"`
LogToFile bool `json:"logToFile"`
MaxFiles int `json:"maxFiles"`
Filename string `json:"filename"`
ForceConsole bool `json:"forceConsole"`
}
type Node struct {
NodeID int `json:"nodeId"`
Index int `json:"index"`
Status int `json:"status"`
Ready bool `json:"ready"`
IsListening bool `json:"isListening"`
IsRouting bool `json:"isRouting"`
ManufacturerID int `json:"manufacturerId"`
ProductID int `json:"productId"`
ProductType int `json:"productType"`
FirmwareVersion string `json:"firmwareVersion"`
DeviceConfig NodeDeviceConfig `json:"deviceConfig"`
Label string `json:"label"`
InterviewAttempts int `json:"interviewAttempts"`
Endpoints []NodeEndpoint `json:"endpoints"`
Values []NodeValues `json:"values"`
InterviewStage int `json:"interviewStage"`
IsFrequentListening any `json:"isFrequentListening"`
MaxBaudRate int `json:"maxBaudRate"`
Version int `json:"version"`
IsBeaming bool `json:"isBeaming"`
DeviceClass NodeDeviceClass `json:"deviceClass"`
InstallerIcon int `json:"installerIcon,omitempty"`
UserIcon int `json:"userIcon,omitempty"`
IsSecure bool `json:"isSecure,omitempty"`
ZwavePlusVersion int `json:"zwavePlusVersion,omitempty"`
NodeType int `json:"nodeType,omitempty"`
RoleType int `json:"roleType,omitempty"`
EndpointCountIsDynamic bool `json:"endpointCountIsDynamic,omitempty"`
EndpointsHaveIdenticalCapabilities bool `json:"endpointsHaveIdenticalCapabilities,omitempty"`
IndividualEndpointCount int `json:"individualEndpointCount,omitempty"`
AggregatedEndpointCount int `json:"aggregatedEndpointCount,omitempty"`
}
type NodeDeviceConfig struct {
Filename string `json:"filename"`
IsEmbedded bool `json:"isEmbedded"`
Manufacturer string `json:"manufacturer"`
ManufacturerID int `json:"manufacturerId"`
Label string `json:"label"`
Description string `json:"description"`
Devices []NodeDeviceConfigDevice `json:"devices"`
FirmwareVersion NodeDeviceFirmwareVersion `json:"firmwareVersion"`
Preferred bool `json:"preferred"`
Metadata NodeDeviceMetadata `json:"metadata"`
}
type NodeDeviceConfigDevice struct {
ProductType int `json:"productType"`
ProductID int `json:"productId"`
}
type NodeDeviceFirmwareVersion struct {
Min string `json:"min"`
Max string `json:"max"`
}
type NodeDeviceMetadata struct {
Comments []NodeDeviceMetadataComments `json:"comments"`
Wakeup string `json:"wakeup"`
Inclusion string `json:"inclusion"`
Exclusion string `json:"exclusion"`
Reset string `json:"reset"`
Manual string `json:"manual"`
}
type NodeDeviceMetadataComments struct {
Level string `json:"level"`
Text string `json:"text"`
}
type NodeEndpoint struct {
NodeID int `json:"nodeId"`
Index int `json:"index"`
}
type NodeValues struct {
Endpoint int `json:"endpoint"`
CommandClass int `json:"commandClass"`
CommandClassName string `json:"commandClassName"`
PropertyName string `json:"propertyName"`
PropertyKey StringOrInt `json:"propertyKey"`
CcVersion int `json:"ccVersion"`
Metadata NodeValuesMetadata `json:"metadata"`
Value NodePropertyValue `json:"value"`
}
type NodeValuesMetadata struct {
Type string `json:"type"`
Readable bool `json:"readable"`
Writeable bool `json:"writeable"`
Label string `json:"label"`
Min int `json:"min"`
Max int `json:"max"`
}
type NodeDeviceClass struct {
Basic string `json:"basic"`
Generic string `json:"generic"`
Specific string `json:"specific"`
MandatorySupportedCCs []string `json:"mandatorySupportedCCs"`
MandatoryControlCCs []string `json:"mandatoryControlCCs"`
}
type StringOrInt string
func (s *StringOrInt) UnmarshalJSON(data []byte) error {
var str string
if bytes.HasPrefix(data, []byte("\"")) {
if err := json.Unmarshal(data, &str); err != nil {
return err
}
} else if bytes.Equal(data, []byte("true")) || bytes.Equal(data, []byte("false")) {
str = string(data)
} else {
var i int
if err := json.Unmarshal(data, &i); err != nil {
return err
}
str = fmt.Sprintf("%d", i)
}
*s = StringOrInt(str)
return nil
}
func (s StringOrInt) String() string {
return string(s)
}
func (s StringOrInt) Int() (int, error) {
return strconv.Atoi(string(s))
}
type NodePropertyValue struct {
String string
Int int
Bool bool
Values []NodePropertyValue
}
func (n *NodePropertyValue) UnmarshalJSON(data []byte) error {
if bytes.HasPrefix(data, []byte("\"")) {
return json.Unmarshal(data, &n.String)
}
if bytes.Equal(data, []byte("true")) || bytes.Equal(data, []byte("false")) {
return json.Unmarshal(data, &n.Bool)
}
if bytes.HasPrefix(data, []byte("[")) {
return json.Unmarshal(data, &n.Values)
}
if err := json.Unmarshal(data, &n.Int); err != nil {
logrus.WithField("value", string(data)).Debug("error while parsing node property value of ambiguous type")
return err
}
return nil
}