-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
142 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
var ( | ||
SQL_TX_FRAME_INSERT = "INSERT INTO `TX_FRAME` (`TIMESTAMP`, `CHANNEL`, `DIAL`, `FREQ`, `OFFSET`, `MODE`, `SPEED`, `SELECTED`, `TONES`) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" | ||
) | ||
|
||
type TxFrameObj struct { | ||
Id int64 | ||
Timestamp time.Time | ||
Channel uint16 | ||
Dial uint32 | ||
Freq uint32 | ||
Offset uint16 | ||
Mode string | ||
Speed string | ||
Selected string | ||
Tones string | ||
} | ||
|
||
func CreateTxFrameObj(event *Js8callEvent) (*TxFrameObj, error) { | ||
if event.Type != EVENT_TYPE_TX_FRAME { | ||
return nil, errors.New("wrong event type, cannot parse params") | ||
} | ||
|
||
o := new(TxFrameObj) | ||
o.Timestamp = time.Now().UTC() | ||
return o, nil | ||
} | ||
|
||
func (obj *TxFrameObj) ApplyRigStatus(rig *RigStatusWsEvent) { | ||
obj.Channel = rig.Channel | ||
obj.Offset = rig.Offset | ||
obj.Dial = rig.Dial | ||
obj.Freq = rig.Freq | ||
obj.Speed = rig.Speed | ||
obj.Selected = rig.Selected | ||
} | ||
|
||
func (obj *TxFrameObj) Insert(db *sql.DB) error { | ||
stmt, err := db.Prepare(SQL_TX_FRAME_INSERT) | ||
if err != nil { | ||
return fmt.Errorf("error preparing SQL query fo inserting new TxFrame record, caused by %w", err) | ||
} | ||
defer stmt.Close() | ||
|
||
marshalledTones, err := json.Marshal(obj.Tones) | ||
if err != nil { | ||
return fmt.Errorf("unable to marshall tones %w", err) | ||
} | ||
|
||
res, err := stmt.Exec( | ||
toSqlTime(obj.Timestamp), | ||
&obj.Channel, | ||
&obj.Dial, | ||
&obj.Freq, | ||
&obj.Offset, | ||
&obj.Mode, | ||
&obj.Speed, | ||
&obj.Selected, | ||
&marshalledTones, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("error executing SQL query inserting new TxFrame record, becouse of %w", err) | ||
} | ||
|
||
obj.Id, _ = res.LastInsertId() | ||
return nil | ||
} | ||
|
||
func (obj *TxFrameObj) Save(db *sql.DB) error { | ||
return obj.Insert(db) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/PiotrTopa/js8web/model" | ||
) | ||
|
||
func txFrameNotifier(event *model.Js8callEvent, websocketEvents chan<- model.WebsocketEvent, databaseObjects chan<- model.DbObj) error { | ||
obj, err := model.CreateTxFrameObj(event) | ||
if err != nil { | ||
return errors.New("can not convert TxFrame event to db object") | ||
} | ||
obj.ApplyRigStatus(&rigStatusCache) | ||
databaseObjects <- obj | ||
return nil | ||
} |