Guest User

Untitled

a guest
Feb 16th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. pragma solidity >=0.4.0 <0.6.0;
  2. import {SharedStructs as lib} from "./sharedstructs.sol";
  3. pragma experimental ABIEncoderV2;
  4.  
  5. contract HarvestContract {
  6.  
  7. address peterson;
  8. modifier onlyPeterson() {
  9. require(msg.sender==peterson);
  10. _;
  11. }
  12.  
  13. mapping(uint => lib.HarvestData) loadedTrucks;
  14. lib.Field[] fields;
  15. // every truck registered by Peterson
  16. lib.Account[] trucks;
  17. uint INVALID_FIELD_ID = 0;
  18.  
  19. modifier onlyTruck() {
  20. bool isTruck = false;
  21. for(uint i = 0; i<trucks.length; i++) {
  22. if (msg.sender == trucks[i].addr)
  23. isTruck = true;
  24. }
  25. require(isTruck==true);
  26.  
  27. _;
  28. }
  29.  
  30. // only farmers should be given permission to add fields to blockchain
  31. // Field extreme positions coordinates must be converted by a scale factor of
  32. // GEO_COORD_SCALE
  33. function registerField(lib.Field memory field) public payable onlyPeterson {
  34. fields.push(field);
  35. }
  36.  
  37. function registerTruck(lib.Account memory truck) public payable onlyPeterson {
  38. trucks.push(truck);
  39. }
  40.  
  41. function deRegisterTruck(uint truckID) public payable onlyPeterson {
  42. for (uint i = 0; i<trucks.length; i++)
  43. {
  44. if (trucks[i].id == truckID) {
  45. delete trucks[i];
  46. }
  47. }
  48. }
  49.  
  50. // When truck is done loading harvest on the field must upload crop/truck data on blockchain.
  51. // Truck position coordinates must be converted by a scale factor of
  52. // GEO_COORD_SCALE
  53. // id must match address of truck, as in trucks[] array (truck program assumed safe)
  54. function storeHarvestData(lib.HarvestData memory data, uint id) public payable onlyTruck {
  55. loadedTrucks[id] = data;
  56. }
  57.  
  58. function getLoadedTruckData(uint truckID) public view returns (lib.HarvestData memory) {
  59. return loadedTrucks[truckID];
  60. }
  61.  
  62. function unloadTruck(uint truckID) public payable {
  63. loadedTrucks[truckID] = lib.HarvestData(lib.Position(0, 0), "", 0, false);
  64. }
  65.  
  66. // returns INVALID_FIELD_ID if position is not included in any registered field
  67. function positionToField(lib.Position memory pos) public view returns (uint fieldID) {
  68. for (uint i = 0; i<fields.length; i++) {
  69. // condition is met at least in Ucraine
  70. bool longitudeSatisfied = pos.longitude<fields[i].northEast.longitude &&
  71. pos.longitude>fields[i].southWest.longitude;
  72. bool latitudeSatisfied = pos.latitude<fields[i].northEast.latitude &&
  73. pos.latitude>fields[i].southWest.latitude;
  74. if (longitudeSatisfied && latitudeSatisfied) {
  75. return fields[i].id;
  76. }
  77. }
  78. return INVALID_FIELD_ID;
  79. }
  80. }
Add Comment
Please, Sign In to add comment